Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * original code: http://ubuntuforums.org/archive/index.php/t-2120175.html
- */
- /* Build using
- gcc <filename> $(pkg-config --cflags --libs pangocairo) -o <output_filename>
- */
- #include <pango/pangocairo.h>
- #define WIDTH 1000
- #define HEIGHT 300
- #define TEXT_SIZE 13
- static PangoRectangle draw_text (cairo_t *cr, char *font_desc, char *text)
- {
- PangoContext *pc;
- PangoLayout *layout;
- PangoFontDescription *desc;
- int i;
- cairo_font_options_t *fo;
- PangoRectangle pr;
- pc = pango_cairo_create_context(cr);
- fo = cairo_font_options_create();
- cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_SUBPIXEL);
- cairo_font_options_set_subpixel_order(fo, CAIRO_SUBPIXEL_ORDER_RGB);
- cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_SLIGHT);
- pango_cairo_context_set_font_options(pc, fo);
- layout = pango_cairo_create_layout (cr);
- layout = pango_layout_new(pc);
- pango_layout_set_text (layout, text, -1);
- desc = pango_font_description_from_string (font_desc);
- pango_layout_set_font_description (layout, desc);
- pango_font_description_free (desc);
- pango_cairo_update_layout (cr, layout);
- pango_cairo_show_layout (cr, layout);
- pango_layout_get_extents(layout, &pr, NULL);
- pango_extents_to_pixels(&pr, NULL);
- printf("x: %i, y: %i, width: %i, height: %i\n", pr.x, pr.y, pr.width, pr.height);
- /* free the layout object */
- g_object_unref (layout);
- return pr;
- }
- int main (int argc, char *argv[])
- {
- cairo_surface_t *cr_surface;
- cairo_t *cr;
- PangoRectangle pr;
- char text[] = "DejaVu Sans Regular 10\nThe quick brown fox jumps over the lazy dog\nConfiguration";
- char output_filename[] = "image.png";
- cr_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1000, 300);
- cr = cairo_create (cr_surface);
- cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
- cairo_rectangle(cr, 0, 0, WIDTH, HEIGHT);
- cairo_fill(cr);
- cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
- unsigned int i, ypos = 0;
- char font[32] = "DejaVu Sans Regular 10";
- draw_text(cr, font, text);
- cairo_surface_write_to_png (cr_surface, output_filename);
- cairo_surface_destroy (cr_surface);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment