Guest User

Untitled

a guest
Jul 28th, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.11 KB | None | 0 0
  1. /**
  2.  * original code: http://ubuntuforums.org/archive/index.php/t-2120175.html
  3.  */
  4.  
  5.  
  6. /* Build using
  7. gcc <filename> $(pkg-config --cflags --libs pangocairo) -o <output_filename>
  8. */
  9.  
  10. #include <pango/pangocairo.h>
  11.  
  12. #define WIDTH 1000
  13. #define HEIGHT 300
  14. #define TEXT_SIZE 13
  15.  
  16. static PangoRectangle draw_text (cairo_t *cr, char *font_desc, char *text)
  17. {
  18.     PangoContext *pc;
  19.     PangoLayout *layout;
  20.     PangoFontDescription *desc;
  21.     int i;
  22.     cairo_font_options_t *fo;
  23.     PangoRectangle pr;
  24.  
  25.     pc = pango_cairo_create_context(cr);
  26.     fo = cairo_font_options_create();
  27.  
  28.     cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_SUBPIXEL);
  29.     cairo_font_options_set_subpixel_order(fo, CAIRO_SUBPIXEL_ORDER_RGB);
  30.     cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_SLIGHT);
  31.  
  32.     pango_cairo_context_set_font_options(pc, fo);
  33.  
  34.     layout = pango_cairo_create_layout (cr);
  35.  
  36.     layout = pango_layout_new(pc);
  37.     pango_layout_set_text (layout, text, -1);
  38.     desc = pango_font_description_from_string (font_desc);
  39.     pango_layout_set_font_description (layout, desc);
  40.     pango_font_description_free (desc);
  41.  
  42.     pango_cairo_update_layout (cr, layout);
  43.     pango_cairo_show_layout (cr, layout);
  44.  
  45.     pango_layout_get_extents(layout, &pr, NULL);
  46.     pango_extents_to_pixels(&pr, NULL);
  47.     printf("x: %i, y: %i, width: %i, height: %i\n", pr.x, pr.y, pr.width, pr.height);
  48.  
  49.     /* free the layout object */
  50.     g_object_unref (layout);
  51.  
  52.     return pr;
  53. }
  54.  
  55. int main (int argc, char *argv[])
  56. {
  57.     cairo_surface_t *cr_surface;
  58.     cairo_t *cr;
  59.     PangoRectangle pr;
  60.  
  61.     char text[] = "DejaVu Sans Regular 10\nThe quick brown fox jumps over the lazy dog\nConfiguration";
  62.     char output_filename[] = "image.png";
  63.  
  64.     cr_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1000, 300);
  65.     cr = cairo_create (cr_surface);
  66.  
  67.     cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
  68.     cairo_rectangle(cr, 0, 0, WIDTH, HEIGHT);
  69.     cairo_fill(cr);
  70.  
  71.     cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
  72.  
  73.     unsigned int i, ypos = 0;
  74.     char font[32] = "DejaVu Sans Regular 10";
  75.     draw_text(cr, font, text);
  76.  
  77.     cairo_surface_write_to_png (cr_surface, output_filename);
  78.     cairo_surface_destroy (cr_surface);
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment