Skip to content

Instantly share code, notes, and snippets.

@cat-in-136
Created July 27, 2014 08:38
Show Gist options
  • Save cat-in-136/4782d9436e4a9d12b7d7 to your computer and use it in GitHub Desktop.
Save cat-in-136/4782d9436e4a9d12b7d7 to your computer and use it in GitHub Desktop.

Revisions

  1. cat-in-136 created this gist Jul 27, 2014.
    84 changes: 84 additions & 0 deletions cairo_colored_font_experiment.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    /* vim:fileencoding=utf-8 tabstop=2 expandtab shiftwidth=2 softtabstop=0:
    *
    * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
    * Version 2, December 2004
    *
    * Copyright (C) 2014 @cat_in_136
    *
    * Everyone is permitted to copy and distribute verbatim or modified
    * copies of this license document, and changing it is allowed as long
    * as the name is changed.
    *
    * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
    * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
    *
    * 0. You just DO WHAT THE FUCK YOU WANT TO.
    *
    *
    * How to compile:
    * % gcc cairo_colored_font_experiment.c -o cairo_colored_font_experiment `pkg-config --cflags --libs gtk+-3.0 freetype2`
    */
    #include <cairo.h>
    #include <gtk/gtk.h>

    #include <ft2build.h>
    #include FT_FREETYPE_H

    char *ttf_file = NULL;
    char *text = NULL;

    static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr,
    gpointer user_data)
    {
    FT_Library ft_library;
    FT_Face ft_face;
    cairo_font_face_t * cairo_face = NULL;

    FT_Init_FreeType(&ft_library);
    FT_New_Face(ft_library, ttf_file, 0, &ft_face);
    cairo_face = cairo_ft_font_face_create_for_ft_face(ft_face, FT_LOAD_COLOR);

    cairo_set_source_rgb(cr, 0, 0, 0);
    // cairo_select_font_face(cr, "Noto Color Emoji", CAIRO_FONT_SLANT_NORMAL,
    // CAIRO_FONT_WEIGHT_NORMAL);
    cairo_set_font_face(cr, cairo_face);
    cairo_set_font_size(cr, 128);

    cairo_move_to(cr, 2, 128);
    cairo_show_text(cr, text);

    return FALSE;
    }


    int main(int argc, char *argv[])
    {
    GtkWidget *window;
    GtkWidget *darea;
    GdkRGBA white = {255, 255, 255, 255};

    gtk_init(&argc, &argv);

    if (argc != 3) {
    g_fprintf(stderr, "Usage: %s ttffile.ttf text\n", argv[0]);
    return -1;
    }
    ttf_file = argv[1];
    text = argv[2];

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    darea = gtk_drawing_area_new();
    gtk_container_add(GTK_CONTAINER(window), darea);

    g_signal_connect(G_OBJECT(darea), "draw", G_CALLBACK(on_draw_event), NULL);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    gtk_window_set_default_size(GTK_WINDOW(window), 400, 192);
    gtk_widget_override_background_color(GTK_WIDGET(window), GTK_STATE_NORMAL, &white);

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
    }