Skip to content

Instantly share code, notes, and snippets.

@osak

osak/library versions

Last active Aug 26, 2018
Embed
What would you like to do?
glados% pkg-config --modversion pango
1.40.14
glados% pkg-config --modversion cairo
1.15.10
#include <math.h>
#include <pango/pangocairo.h>
#define FONT "Sans Bold 11"
void draw_text (cairo_t *cr) {
PangoLayout *layout;
PangoFontDescription *desc;
/* Create a PangoLayout, set the font and text */
layout = pango_cairo_create_layout (cr);
pango_layout_set_text (layout, "Text\nText\nText", -1);
pango_layout_set_wrap(layout, PANGO_WRAP_CHAR);
pango_layout_set_width(layout, 100 * PANGO_SCALE);
desc = pango_font_description_from_string (FONT);
pango_layout_set_font_description (layout, desc);
pango_font_description_free (desc);
PangoRectangle ink_rect = {
.x = 0,
.y = 0,
.width = 11 * PANGO_SCALE,
.height = 11 * PANGO_SCALE
};
PangoRectangle logical_rect = {
.x = 0,
.y = 0,
.width = 11 * PANGO_SCALE,
.height = 11 * PANGO_SCALE
};
PangoAttribute *emoji = pango_attr_shape_new(&ink_rect, &logical_rect);
emoji->start_index = 0;
emoji->end_index = 1;
PangoAttrList *attr_list = pango_attr_list_new();
pango_attr_list_insert(attr_list, emoji);
pango_layout_set_attributes(layout, attr_list);
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_move_to(cr, 0.0, 0.0);
pango_cairo_show_layout(cr, layout);
/* free the layout object */
g_object_unref (layout);
pango_attr_list_unref(attr_list);
//pango_attribute_destroy(emoji);
}
int main (int argc, char **argv)
{
cairo_t *cr;
char *filename;
cairo_status_t status;
cairo_surface_t *surface;
if (argc != 2) {
g_printerr ("Usage: cairosimple OUTPUT_FILENAME\n");
return 1;
}
filename = argv[1];
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 100, 100);
cr = cairo_create (surface);
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
cairo_paint (cr);
draw_text (cr);
cairo_destroy (cr);
status = cairo_surface_write_to_png (surface, filename);
cairo_surface_destroy (surface);
if (status != CAIRO_STATUS_SUCCESS) {
g_printerr ("Could not save png to '%s'\n", filename);
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.