Skip to content

Instantly share code, notes, and snippets.

@behdad
Created December 4, 2017 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behdad/864a45bf32dd5ac27df961fec4e625dc to your computer and use it in GitHub Desktop.
Save behdad/864a45bf32dd5ac27df961fec4e625dc to your computer and use it in GitHub Desktop.
/* Simple example to use pangocairo to render rotated text */
#include <math.h>
#include <pango/pangocairo.h>
static void
draw_text (cairo_t *cr)
{
#define RADIUS 200
#define N_WORDS 20
#define FONT_WITH_MANUAL_SIZE "Seymour One,Sans"
#define FONT_SIZE 36
#define DEVICE_DPI 72
/* The following number applies a cairo CTM. Tests for
* https://bugzilla.gnome.org/show_bug.cgi?id=700592
*/
#define TWEAKABLE_SCALE ((double) 1.)
PangoLayout *layout;
PangoFontDescription *desc;
cairo_font_options_t *font_options;
int i;
/* Center coordinates on the middle of the region we are drawing
*/
// cairo_translate (cr, RADIUS / TWEAKABLE_SCALE, RADIUS / TWEAKABLE_SCALE);
/* Create a PangoLayout, set the font and text */
layout = pango_cairo_create_layout (cr);
pango_layout_set_text (layout, "Tegxbatz\n", -1);
desc = pango_font_description_from_string (FONT_WITH_MANUAL_SIZE);
pango_font_description_set_absolute_size(desc, FONT_SIZE * PANGO_SCALE);
pango_layout_set_font_description (layout, desc);
pango_font_description_free (desc);
font_options = cairo_font_options_create ();
cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_ON);
//cairo_set_font_options (cr, font_options);
cairo_font_options_destroy (font_options);
/* Draw the layout N_WORDS times in a circle */
for (i = 0; i < N_WORDS; i++)
{
int width, height;
cairo_save (cr);
cairo_set_source_rgb (cr, 0, 0, 0);
pango_layout_get_pixel_size (layout, &width, &height);
cairo_move_to (cr, 50, height * i * .35);
cairo_rotate (cr, i / 20.);
cairo_scale (cr, .8, 1.);
pango_cairo_update_layout (cr, layout);
pango_cairo_show_layout (cr, layout);
cairo_restore (cr);
}
/* free the layout object */
g_object_unref (layout);
}
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,
3 * RADIUS, 5 * 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;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment