summaryrefslogtreecommitdiff
path: root/examples/userfont.c
blob: 930ad2cf9f6fefa4c689b2b2f778b3c92b203525 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* Example code to show how to use user fonts with Pango2
 *
 * Written by Matthias Clasen, 2022
 *
 * Permission to use, copy, modify, distribute, and sell this example
 * for any purpose is hereby granted without fee.
 * It is provided "as is" without express or implied warranty.
 *
 * Font data taken from the cairo user-font test, written by
 *   Kristian Høgsberg <krh@redhat.com>
 *   Behdad Esfahbod <behdad@behdad.org>
 */

#include <stdlib.h>
#include <stdio.h>

#include <pango2/pangocairo.h>

static Pango2FontMap *fontmap;

#define END_GLYPH 0
#define STROKE 126
#define CLOSE 127

/* Simple glyph definition: 1 - 15 means lineto (or moveto for first
 * point) for one of the points on this grid:
 *
 *      1  2  3
 *      4  5  6
 *      7  8  9
 * ----10 11 12----(baseline)
 *     13 14 15
 */
typedef struct
{
  gunichar ucs4;
  int width;
  char data[16];
} test_scaled_font_glyph_t;

/* Simple glyph definition: 1 - 15 means lineto (or moveto for first
 * point) for one of the points on this grid:
 *
 *      1  2  3
 *      4  5  6
 *      7  8  9
 * ----10 11 12----(baseline)
 *     13 14 15
 */
static const test_scaled_font_glyph_t glyphs [] = {
  { 'a',  3, { 4, 6, 12, 10, 7, 9, STROKE, END_GLYPH } },
  { 'c',  3, { 6, 4, 10, 12, STROKE, END_GLYPH } },
  { 'e',  3, { 12, 10, 4, 6, 9, 7, STROKE, END_GLYPH } },
  { 'f',  3, { 3, 2, 11, STROKE, 4, 6, STROKE, END_GLYPH } },
  { 'g',  3, { 12, 10, 4, 6, 15, 13, STROKE, END_GLYPH } },
  { 'h',  3, { 1, 10, STROKE, 7, 5, 6, 12, STROKE, END_GLYPH } },
  { 'i',  1, { 1, 1, STROKE, 4, 10, STROKE, END_GLYPH } },
  { 'l',  1, { 1, 10, STROKE, END_GLYPH } },
  { 'n',  3, { 10, 4, STROKE, 7, 5, 6, 12, STROKE, END_GLYPH } },
  { 'o',  3, { 4, 10, 12, 6, CLOSE, END_GLYPH } },
  { 'p',  3, { 4, 10, 12, 6, CLOSE, 4, 13, STROKE, END_GLYPH } },
  { 'r',  3, { 4, 10, STROKE, 7, 5, 6, STROKE, END_GLYPH } },
  { 's',  3, { 6, 4, 7, 9, 12, 10, STROKE, END_GLYPH } },
  { 't',  3, { 2, 11, 12, STROKE, 4, 6, STROKE, END_GLYPH } },
  { 'u',  3, { 4, 10, 12, 6, STROKE, END_GLYPH } },
  { 'y',  3, { 4, 10, 12, 6, STROKE, 12, 15, 13, STROKE, END_GLYPH } },
  { 'z',  3, { 4, 6, 10, 12, STROKE, END_GLYPH } },
  { ' ',  1, { END_GLYPH } },
  { '-',  2, { 7, 8, STROKE, END_GLYPH } },
  { '.',  1, { 10, 10, STROKE, END_GLYPH } },
  { 0xe000, 3, { 3, 2, 11, STROKE, 4, 6, STROKE, 3, 3, STROKE, 6, 12, STROKE, END_GLYPH } }, /* fi */
  { -1,  0, { END_GLYPH } },
};

const char text[] = "finally... pango user-font";

static Pango2Layout *
get_layout (void)
{
  Pango2Context *context;
  Pango2Layout *layout;
  Pango2FontDescription *desc;

  /* Create a Pango2Layout, set the font and text */
  context = pango2_context_new_with_font_map (fontmap);
  layout = pango2_layout_new (context);
  g_object_unref (context);

  pango2_layout_set_text (layout, text, -1);

  desc = pango2_font_description_from_string ("Userfont 20");
  pango2_layout_set_font_description (layout, desc);
  pango2_font_description_free (desc);

  pango2_layout_write_to_file (layout, "out.layout");

  return layout;
}

static gboolean
glyph_cb (Pango2UserFace  *face,
          hb_codepoint_t  unicode,
          hb_codepoint_t *glyph,
          gpointer        user_data)
{
  test_scaled_font_glyph_t *glyphs = user_data;

  for (int i = 0; glyphs[i].ucs4 != (gunichar) -1; i++)
    {
      if (glyphs[i].ucs4 == unicode)
        {
          *glyph = i;
          return TRUE;
        }
    }

  return FALSE;
}

static gboolean
glyph_info_cb (Pango2UserFace      *face,
               int                 size,
               hb_codepoint_t      glyph,
               hb_glyph_extents_t *extents,
               hb_position_t      *h_advance,
               hb_position_t      *v_advance,
               gboolean           *is_color,
               gpointer            user_data)
{
  test_scaled_font_glyph_t *glyphs = user_data;

  extents->x_bearing = 0;
  extents->y_bearing = 0.75 * size;
  extents->width = glyphs[glyph].width / 4.0 * size;
  extents->height = - size;

  *h_advance = *v_advance = glyphs[glyph].width / 4.0 * size;

  *is_color = FALSE;

  return TRUE;
}

static gboolean
shape_cb (Pango2UserFace       *face,
          int                  size,
          const char          *text,
          int                  length,
          const Pango2Analysis *analysis,
          Pango2GlyphString    *glyphs,
          Pango2ShapeFlags      flags,
          gpointer             user_data)
{
  int n_chars;
  const char *p;
  int cluster = 0;
  int i, j;
  int last_cluster;
  gboolean is_color;
  hb_glyph_extents_t ext;
  hb_position_t dummy;

  n_chars = g_utf8_strlen (text, length);

  pango2_glyph_string_set_size (glyphs, n_chars);

  last_cluster = -1;

  p = text;
  j = 0;
  for (i = 0; i < n_chars; i++)
    {
      gunichar wc;
      Pango2Glyph glyph = 0;
      Pango2Rectangle logical_rect;

      wc = g_utf8_get_char (p);

      if (g_unichar_type (wc) != G_UNICODE_NON_SPACING_MARK)
        cluster = p - text;

      /* Handle the fi ligature */
      if (p[0] == 'f' && p[1] == 'i')
        {
          p = g_utf8_next_char (p);
          i++;
          glyph_cb (face, 0xe000, &glyph, user_data);
        }
      else if (pango2_is_zero_width (wc))
        glyph = PANGO2_GLYPH_EMPTY;
      else if (!glyph_cb (face, wc, &glyph, user_data))
        glyph = PANGO2_GET_UNKNOWN_GLYPH (wc);

      glyph_info_cb (face, size, glyph, &ext, &dummy, &dummy, &is_color, user_data);
      pango2_font_get_glyph_extents (pango2_analysis_get_font (analysis), glyph, NULL, &logical_rect);

      glyphs->glyphs[j].glyph = glyph;

      glyphs->glyphs[j].attr.is_cluster_start = cluster != last_cluster;
      glyphs->glyphs[j].attr.is_color = is_color;

      glyphs->glyphs[j].geometry.x_offset = 0;
      glyphs->glyphs[j].geometry.y_offset = 0;
      glyphs->glyphs[j].geometry.width = logical_rect.width;

      glyphs->log_clusters[j] = cluster;

      j++;

      last_cluster = cluster;

      p = g_utf8_next_char (p);
    }

  glyphs->num_glyphs = j;

#if 0
  /* FIXME export this */
  if (analysis->level & 1)
    pango2_glyph_string_reverse_range (glyphs, 0, glyphs->num_glyphs);
#endif

  return TRUE;
}

static gboolean
font_info_cb (Pango2UserFace     *face,
              int                size,
              hb_font_extents_t *extents,
              gpointer           user_data)
{
  extents->ascender = 0.75 * size;
  extents->descender = - 0.25 * size;
  extents->line_gap = 0;

  return TRUE;
}

static gboolean
render_cb (Pango2UserFace  *face,
           int             size,
           hb_codepoint_t  glyph,
           gpointer        user_data,
           const char     *backend_id,
           gpointer        backend_data)
{
  test_scaled_font_glyph_t *glyphs = user_data;
  cairo_t *cr = backend_data;
  const char *data;
  div_t d;
  double x, y;

  if (strcmp (backend_id, "cairo") != 0)
    return FALSE;

  cairo_set_line_width (cr, 0.1);
  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);

  data = glyphs[glyph].data;
  for (int i = 0; data[i] != END_GLYPH; i++)
    {
      switch (data[i])
        {
        case STROKE:
          cairo_new_sub_path (cr);
          break;

        case CLOSE:
          cairo_close_path (cr);
          break;

        default:
          d = div (data[i] - 1, 3);
          x = d.rem / 4.0 + 0.125;
          y = d.quot / 5.0 + 0.4 - 1.0;
          cairo_line_to (cr, x, y);
        }
    }

  cairo_stroke (cr);

  return TRUE;
}

static void
setup_fontmap (Pango2FontMap *fontmap)
{
  Pango2FontDescription *desc;
  Pango2UserFace *face;

  desc = pango2_font_description_new ();
  pango2_font_description_set_family (desc, "Userfont");
  face = pango2_user_face_new (font_info_cb,
                              glyph_cb,
                              glyph_info_cb,
                              shape_cb,
                              render_cb,
                              (gpointer) glyphs, NULL,
                              "Black", desc);
  pango2_font_map_add_face (fontmap, PANGO2_FONT_FACE (face));
  pango2_font_description_free (desc);
}

int
main (int argc, char **argv)
{
  cairo_t *cr;
  char *filename;
  cairo_status_t status;
  cairo_surface_t *surface;
  Pango2Layout *layout;
  Pango2Rectangle ext;

  if (argc != 2)
    {
      g_printerr ("Usage: userfont OUTPUT_FILENAME\n");
      return 1;
    }

  filename = argv[1];

  fontmap = PANGO2_FONT_MAP (pango2_font_map_new_default ());
  setup_fontmap (PANGO2_FONT_MAP (fontmap));

  layout = get_layout ();

  pango2_lines_get_extents (pango2_layout_get_lines (layout), NULL, &ext);
  pango2_extents_to_pixels (&ext, NULL);

  /* Now create the final surface and draw to it. */
  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, ext.width + 20, ext.height + 20);
  cr = cairo_create (surface);

  cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
  cairo_paint (cr);
  cairo_set_source_rgb (cr, 0.0, 0.0, 0.5);

  cairo_move_to (cr, 10, 10);
  pango2_cairo_show_layout (cr, layout);

  cairo_destroy (cr);

  g_object_unref (layout);

  /* Write out the surface as PNG */
  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': %s\n", filename, cairo_status_to_string (status));
      return 1;
    }

  return 0;
}