Skip to content

Instantly share code, notes, and snippets.

@luchr
Created September 2, 2015 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luchr/fdd435b7e2c9acf4b650 to your computer and use it in GitHub Desktop.
Save luchr/fdd435b7e2c9acf4b650 to your computer and use it in GitHub Desktop.
/*
* Example: In some fonts (e.g. Ubuntu Mono) if you render a combining
* code point (e.g. U+0301) without a preceding code point
* then U+25CC is used as preceding code point.
*
* Compile and Link with
* gcc $( pkg-config --cflags --libs pangocairo ) -o test1 test1.c
*/
#include <cairo.h>
#include <pango/pangocairo.h>
#include <assert.h>
/* Unicode code points to UTF-8 code-unit(s) */
#define U002D "\x2D"
#define U0061 "\x61"
#define U0301 "\xCC\x81"
static char const * const text1 = U0061 U0301 U002D; /* "á-" in UTF-8 encoding */
static char const * const text2 = U0301 U002D; /* " ́-" in UTF-8 encoding */
int main (void) {
cairo_surface_t *s = cairo_image_surface_create( CAIRO_FORMAT_RGB24, 200, 200);
assert(NULL!=s);
cairo_t *cr = cairo_create(s); assert(NULL!=s);
cairo_set_source_rgb(cr,1.0,1.0,1.0); cairo_paint(cr); /* background */
cairo_set_source_rgb(cr,0.0,0.0,0.0); /* font color */
PangoFontDescription *fd = pango_font_description_new(); assert(NULL!=fd);
pango_font_description_set_family(fd,"Ubuntu Mono");
pango_font_description_set_absolute_size(fd, 30*PANGO_SCALE);
PangoLayout *layout = pango_cairo_create_layout(cr); assert(NULL!=layout);
pango_layout_set_font_description(layout,fd);
pango_layout_set_text(layout,text1,-1); cairo_move_to(cr,10,50);
pango_cairo_show_layout_line(cr,pango_layout_get_line(layout,0));
pango_layout_set_text(layout,text2,-1); cairo_move_to(cr,10,100);
pango_cairo_show_layout_line(cr,pango_layout_get_line(layout,0));
g_object_unref(layout); pango_font_description_free(fd); cairo_destroy(cr);
cairo_surface_write_to_png(s,"test1.png");
cairo_surface_destroy(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment