Skip to content

Instantly share code, notes, and snippets.

@obyknovenius
Created May 19, 2023 13:30
Show Gist options
  • Save obyknovenius/bdd234332a5237a08840c49efd4032e7 to your computer and use it in GitHub Desktop.
Save obyknovenius/bdd234332a5237a08840c49efd4032e7 to your computer and use it in GitHub Desktop.
Cairo Paint Glyphs
#include <cairo/cairo.h>
#include <stddef.h>
#include <stdio.h>
#define FONT_FACE "Liberation Serif"
#define FONT_SIZE 16.0
void paint(cairo_t *cr) {
cairo_select_font_face(cr, FONT_FACE, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, FONT_SIZE);
cairo_font_options_t *options = cairo_font_options_create();
cairo_font_options_set_hint_metrics(options, CAIRO_HINT_METRICS_OFF);
cairo_font_options_set_hint_style(options, CAIRO_HINT_STYLE_NONE);
cairo_set_font_options(cr, options);
const char *text = "Hello World";
cairo_glyph_t *glyphs = NULL;
int num_glyphs;
cairo_scaled_font_t *font = cairo_get_scaled_font(cr);
cairo_scaled_font_text_to_glyphs(font, 0.0, FONT_SIZE, text, -1, &glyphs, &num_glyphs, NULL, NULL, NULL);
for (int i = 0; i < num_glyphs; i++) {
fprintf(stderr, "%lu:\t%f\t%f\n", glyphs[i].index, glyphs[i].x, glyphs[i].y);
}
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_show_glyphs(cr, glyphs, num_glyphs);
}
int main() {
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 100, 100);
cairo_t *cr = cairo_create(surface);
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
cairo_paint(cr);
paint(cr);
cairo_surface_write_to_png(surface, "result.png");
}
main: main.c
gcc -g -O0 -o $@ $^ `pkg-config --cflags --libs cairo`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment