main.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Taken from
  2. * https://developer.gnome.org/pango/stable/pango-Cairo-Rendering.html */
  3. #include <math.h>
  4. #include <pango/pangocairo.h>
  5. static void draw_text(cairo_t* cr)
  6. {
  7. #define RADIUS 150
  8. #define N_WORDS 10
  9. #define FONT "Sans Bold 27"
  10. PangoLayout* layout;
  11. PangoFontDescription* desc;
  12. int i;
  13. /* Center coordinates on the middle of the region we are drawing
  14. */
  15. cairo_translate(cr, RADIUS, RADIUS);
  16. /* Create a PangoLayout, set the font and text */
  17. layout = pango_cairo_create_layout(cr);
  18. pango_layout_set_text(layout, "Text", -1);
  19. desc = pango_font_description_from_string(FONT);
  20. pango_layout_set_font_description(layout, desc);
  21. pango_font_description_free(desc);
  22. /* Draw the layout N_WORDS times in a circle */
  23. for (i = 0; i < N_WORDS; i++) {
  24. int width, height;
  25. double angle = (360. * i) / N_WORDS;
  26. double red;
  27. cairo_save(cr);
  28. /* Gradient from red at angle == 60 to blue at angle == 240 */
  29. red = (1 + cos((angle - 60) * G_PI / 180.)) / 2;
  30. cairo_set_source_rgb(cr, red, 0, 1.0 - red);
  31. cairo_rotate(cr, angle * G_PI / 180.);
  32. /* Inform Pango to re-layout the text with the new transformation */
  33. pango_cairo_update_layout(cr, layout);
  34. pango_layout_get_size(layout, &width, &height);
  35. cairo_move_to(cr, -((double)width / PANGO_SCALE) / 2, -RADIUS);
  36. pango_cairo_show_layout(cr, layout);
  37. cairo_restore(cr);
  38. }
  39. /* free the layout object */
  40. g_object_unref(layout);
  41. }
  42. int main(int argc, char** argv)
  43. {
  44. cairo_t* cr;
  45. char* filename;
  46. cairo_status_t status;
  47. cairo_surface_t* surface;
  48. if (argc != 2) {
  49. g_printerr("Usage: cairosimple OUTPUT_FILENAME\n");
  50. return 1;
  51. }
  52. filename = argv[1];
  53. surface =
  54. cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 2 * RADIUS, 2 * RADIUS);
  55. cr = cairo_create(surface);
  56. cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
  57. cairo_paint(cr);
  58. draw_text(cr);
  59. cairo_destroy(cr);
  60. status = cairo_surface_write_to_png(surface, filename);
  61. cairo_surface_destroy(surface);
  62. if (status != CAIRO_STATUS_SUCCESS) {
  63. g_printerr("Could not save png to '%s'\n", filename);
  64. return 1;
  65. }
  66. return 0;
  67. }