#include <math.h>
#include <pango/pangocairo.h>
#define FONT "Sans Bold 11"
void render_shape(cairo_t *cr, PangoAttrShape *attr, gboolean do_path, gpointer data) {
PangoRectangle rect = attr->ink_rect;
cairo_translate(cr, rect.x / PANGO_SCALE, rect.y / PANGO_SCALE);
cairo_rectangle(cr, 0, 0, rect.width / PANGO_SCALE, rect.height / PANGO_SCALE);
cairo_set_source_rgb(cr, 1, 0, 0);
cairo_fill(cr);
}
void draw_text (cairo_t *cr) {
PangoLayout *layout;
PangoFontDescription *desc;
layout = pango_cairo_create_layout (cr);
// Setup base text
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);
// Setup emoji
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;
// Highlight the first line to show its height
PangoAttribute *first_line_bg = pango_attr_background_new(0, 0xffff, 0xffff);
first_line_bg->start_index = 1;
first_line_bg->end_index = 4;
// Highlight the second line to show its height
PangoAttribute *second_line_bg = pango_attr_background_new(0xffff, 0xffff, 0);
second_line_bg->start_index = 5;
second_line_bg->end_index = 9;
// Setup attributes
PangoAttrList *attr_list = pango_attr_list_new();
pango_attr_list_insert(attr_list, emoji);
pango_attr_list_insert(attr_list, first_line_bg);
pango_attr_list_insert(attr_list, second_line_bg);
pango_layout_set_attributes(layout, attr_list);
pango_cairo_context_set_shape_renderer(pango_layout_get_context(layout), render_shape, NULL, NULL);
// Render
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);
// Cleanup
g_object_unref (layout);
pango_attr_list_unref(attr_list);
}
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;
}
Attachment:
a.png
Description: PNG image