2016-05-16 17:34:04 +03:00
|
|
|
/* Taken from
|
|
|
|
* https://developer.gnome.org/pango/stable/pango-Cairo-Rendering.html */
|
2013-08-06 13:33:59 +04:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <pango/pangocairo.h>
|
2016-05-16 17:34:04 +03:00
|
|
|
static void draw_text(cairo_t* cr)
|
2013-08-06 13:33:59 +04:00
|
|
|
{
|
|
|
|
#define RADIUS 150
|
|
|
|
#define N_WORDS 10
|
|
|
|
#define FONT "Sans Bold 27"
|
2016-05-16 17:34:04 +03:00
|
|
|
PangoLayout* layout;
|
|
|
|
PangoFontDescription* desc;
|
2013-08-06 13:33:59 +04:00
|
|
|
int i;
|
|
|
|
/* Center coordinates on the middle of the region we are drawing
|
|
|
|
*/
|
2016-05-16 17:34:04 +03:00
|
|
|
cairo_translate(cr, RADIUS, RADIUS);
|
2013-08-06 13:33:59 +04:00
|
|
|
/* Create a PangoLayout, set the font and text */
|
2016-05-16 17:34:04 +03:00
|
|
|
layout = pango_cairo_create_layout(cr);
|
|
|
|
pango_layout_set_text(layout, "Text", -1);
|
|
|
|
desc = pango_font_description_from_string(FONT);
|
|
|
|
pango_layout_set_font_description(layout, desc);
|
|
|
|
pango_font_description_free(desc);
|
2013-08-06 13:33:59 +04:00
|
|
|
/* Draw the layout N_WORDS times in a circle */
|
2016-05-16 17:34:04 +03:00
|
|
|
for (i = 0; i < N_WORDS; i++) {
|
|
|
|
int width, height;
|
|
|
|
double angle = (360. * i) / N_WORDS;
|
|
|
|
double red;
|
|
|
|
cairo_save(cr);
|
|
|
|
/* Gradient from red at angle == 60 to blue at angle == 240 */
|
|
|
|
red = (1 + cos((angle - 60) * G_PI / 180.)) / 2;
|
|
|
|
cairo_set_source_rgb(cr, red, 0, 1.0 - red);
|
|
|
|
cairo_rotate(cr, angle * G_PI / 180.);
|
|
|
|
/* Inform Pango to re-layout the text with the new transformation */
|
|
|
|
pango_cairo_update_layout(cr, layout);
|
|
|
|
pango_layout_get_size(layout, &width, &height);
|
|
|
|
cairo_move_to(cr, -((double)width / PANGO_SCALE) / 2, -RADIUS);
|
|
|
|
pango_cairo_show_layout(cr, layout);
|
|
|
|
cairo_restore(cr);
|
|
|
|
}
|
2013-08-06 13:33:59 +04:00
|
|
|
/* free the layout object */
|
2016-05-16 17:34:04 +03:00
|
|
|
g_object_unref(layout);
|
2013-08-06 13:33:59 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
int main(int argc, char** argv)
|
2013-08-06 13:33:59 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
cairo_t* cr;
|
|
|
|
char* filename;
|
2013-08-06 13:33:59 +04:00
|
|
|
cairo_status_t status;
|
2016-05-16 17:34:04 +03:00
|
|
|
cairo_surface_t* surface;
|
|
|
|
if (argc != 2) {
|
|
|
|
g_printerr("Usage: cairosimple OUTPUT_FILENAME\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2013-08-06 13:33:59 +04:00
|
|
|
filename = argv[1];
|
2016-05-16 17:34:04 +03:00
|
|
|
surface =
|
|
|
|
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 2 * RADIUS, 2 * RADIUS);
|
|
|
|
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;
|
|
|
|
}
|
2013-08-06 13:33:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|