Coverage Report

Created: 2026-09-28 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/harfbuzz/src/hb-graphite2.cc
Line
Count
Source
1
/*
2
 * Copyright © 2011  Martin Hosken
3
 * Copyright © 2011  SIL International
4
 * Copyright © 2011,2012  Google, Inc.
5
 *
6
 *  This is part of HarfBuzz, a text shaping library.
7
 *
8
 * Permission is hereby granted, without written agreement and without
9
 * license or royalty fees, to use, copy, modify, and distribute this
10
 * software and its documentation for any purpose, provided that the
11
 * above copyright notice and the following two paragraphs appear in
12
 * all copies of this software.
13
 *
14
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18
 * DAMAGE.
19
 *
20
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25
 *
26
 * Google Author(s): Behdad Esfahbod
27
 */
28
29
#include "hb.hh"
30
31
#ifdef HAVE_GRAPHITE2
32
33
#include "hb-shaper-impl.hh"
34
35
#include "hb-graphite2.h"
36
37
#include <graphite2/Segment.h>
38
39
#include "hb-ot-layout.h"
40
41
42
/**
43
 * SECTION:hb-graphite2
44
 * @title: hb-graphite2
45
 * @short_description: Graphite2 integration
46
 * @include: hb-graphite2.h
47
 *
48
 * Functions for using HarfBuzz with fonts that include Graphite features.
49
 * 
50
 * For Graphite features to work, you must be sure that HarfBuzz was compiled
51
 * with the `graphite2` shaping engine enabled. Currently, the default is to
52
 * not enable `graphite2` shaping.
53
 **/
54
55
56
/*
57
 * shaper face data
58
 */
59
60
typedef struct hb_graphite2_tablelist_t
61
{
62
  struct hb_graphite2_tablelist_t *next;
63
  hb_blob_t *blob;
64
  unsigned int tag;
65
} hb_graphite2_tablelist_t;
66
67
struct hb_graphite2_face_data_t
68
{
69
  hb_face_t *face;
70
  gr_face   *grface;
71
  hb_atomic_t<hb_graphite2_tablelist_t *> tlist;
72
};
73
74
static const void *hb_graphite2_get_table (const void *data, unsigned int tag, size_t *len)
75
0
{
76
0
  hb_graphite2_face_data_t *face_data = (hb_graphite2_face_data_t *) data;
77
0
  hb_graphite2_tablelist_t *tlist = face_data->tlist;
78
79
0
  hb_blob_t *blob = nullptr;
80
81
0
  for (hb_graphite2_tablelist_t *p = tlist; p; p = p->next)
82
0
    if (p->tag == tag) {
83
0
      blob = p->blob;
84
0
      break;
85
0
    }
86
87
0
  if (unlikely (!blob))
88
0
  {
89
0
    blob = face_data->face->reference_table (tag);
90
91
0
    hb_graphite2_tablelist_t *p = (hb_graphite2_tablelist_t *) hb_calloc (1, sizeof (hb_graphite2_tablelist_t));
92
0
    if (unlikely (!p)) {
93
0
      hb_blob_destroy (blob);
94
0
      return nullptr;
95
0
    }
96
0
    p->blob = blob;
97
0
    p->tag = tag;
98
99
0
retry:
100
0
    hb_graphite2_tablelist_t *tlist = face_data->tlist;
101
0
    p->next = tlist;
102
103
0
    if (unlikely (!face_data->tlist.cmpexch (tlist, p)))
104
0
      goto retry;
105
0
  }
106
107
0
  unsigned int tlen;
108
0
  const char *d = hb_blob_get_data (blob, &tlen);
109
0
  *len = tlen;
110
0
  return d;
111
0
}
112
113
hb_graphite2_face_data_t *
114
_hb_graphite2_shaper_face_data_create (hb_face_t *face)
115
14.1M
{
116
14.1M
  hb_unique_ptr_t<hb_blob_t> silf_blob (face->reference_table (HB_GRAPHITE2_TAG_SILF));
117
  /* Umm, we just reference the table to check whether it exists.
118
   * Maybe add better API for this? */
119
14.1M
  if (!hb_blob_get_length (silf_blob))
120
14.1M
    return nullptr;
121
122
0
  hb_graphite2_face_data_t *data = (hb_graphite2_face_data_t *) hb_calloc (1, sizeof (hb_graphite2_face_data_t));
123
0
  if (unlikely (!data))
124
0
    return nullptr;
125
126
0
  data->face = face;
127
0
  const gr_face_ops ops = {sizeof(gr_face_ops), &hb_graphite2_get_table, NULL};
128
0
  data->grface = gr_make_face_with_ops (data, &ops, gr_face_preloadAll);
129
130
0
  if (unlikely (!data->grface)) {
131
    /* data->tlist has already been populated by the callback,
132
     * so everything needs to be destroyed */
133
0
    _hb_graphite2_shaper_face_data_destroy (data);
134
0
    return nullptr;
135
0
  }
136
137
0
  return data;
138
0
}
139
140
void
141
_hb_graphite2_shaper_face_data_destroy (hb_graphite2_face_data_t *data)
142
0
{
143
0
  hb_graphite2_tablelist_t *tlist = data->tlist;
144
145
0
  while (tlist)
146
0
  {
147
0
    hb_graphite2_tablelist_t *old = tlist;
148
0
    hb_blob_destroy (tlist->blob);
149
0
    tlist = tlist->next;
150
0
    hb_free (old);
151
0
  }
152
153
0
  gr_face_destroy (data->grface);
154
155
0
  hb_free (data);
156
0
}
157
158
/**
159
 * hb_graphite2_face_get_gr_face: (skip)
160
 * @face: @hb_face_t to query
161
 *
162
 * Fetches the Graphite2 gr_face corresponding to the specified
163
 * #hb_face_t face object.
164
 *
165
 * Return value: the gr_face found
166
 *
167
 * Since: 0.9.10
168
 */
169
gr_face *
170
hb_graphite2_face_get_gr_face (hb_face_t *face)
171
150k
{
172
150k
  const hb_graphite2_face_data_t *data = face->data.graphite2;
173
150k
  return data ? data->grface : nullptr;
174
150k
}
175
176
177
/*
178
 * shaper font data
179
 */
180
181
struct hb_graphite2_font_data_t {};
182
183
hb_graphite2_font_data_t *
184
_hb_graphite2_shaper_font_data_create (hb_font_t *font HB_UNUSED)
185
0
{
186
0
  return (hb_graphite2_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
187
0
}
188
189
void
190
_hb_graphite2_shaper_font_data_destroy (hb_graphite2_font_data_t *data HB_UNUSED)
191
0
{
192
0
}
193
194
#ifndef HB_DISABLE_DEPRECATED
195
/**
196
 * hb_graphite2_font_get_gr_font: (skip)
197
 * @font: An #hb_font_t
198
 *
199
 * Always returns `NULL`. Use hb_graphite2_face_get_gr_face() instead.
200
 *
201
 * Return value: (nullable): Graphite2 font associated with @font.
202
 *
203
 * Since: 0.9.10
204
 * Deprecated: 1.4.2
205
 */
206
gr_font *
207
hb_graphite2_font_get_gr_font (hb_font_t *font HB_UNUSED)
208
0
{
209
0
  return nullptr;
210
0
}
211
#endif
212
213
214
/*
215
 * shaper
216
 */
217
218
struct hb_graphite2_cluster_t {
219
  unsigned int base_char;
220
  unsigned int num_chars;
221
  unsigned int base_glyph;
222
  unsigned int num_glyphs;
223
  unsigned int cluster;
224
  hb_position_t advance;
225
};
226
227
struct hb_graphite2_glyph_t {
228
  hb_codepoint_t codepoint;
229
  unsigned int cluster;
230
};
231
232
hb_bool_t
233
_hb_graphite2_shape (hb_shape_plan_t    *shape_plan HB_UNUSED,
234
         hb_font_t          *font,
235
         hb_buffer_t        *buffer,
236
         const hb_feature_t *features,
237
         unsigned int        num_features)
238
0
{
239
0
  hb_face_t *face = font->face;
240
0
  gr_face *grface = face->data.graphite2->grface;
241
242
0
  const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
243
0
  const char *lang_end = lang ? strchr (lang, '-') : nullptr;
244
0
  int lang_len = lang_end ? lang_end - lang : -1;
245
0
  gr_feature_val *feats = gr_face_featureval_for_lang (grface, lang ? hb_tag_from_string (lang, lang_len) : 0);
246
247
0
  for (unsigned int i = 0; i < num_features; i++)
248
0
  {
249
0
    const gr_feature_ref *fref = gr_face_find_fref (grface, features[i].tag);
250
0
    if (fref)
251
0
      gr_fref_set_feature_value (fref, features[i].value, feats);
252
0
  }
253
254
0
  hb_direction_t direction = buffer->props.direction;
255
0
  hb_direction_t horiz_dir = hb_script_get_horizontal_direction (buffer->props.script);
256
  /* TODO vertical:
257
   * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
258
   * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
259
   * first. */
260
0
  if ((HB_DIRECTION_IS_HORIZONTAL (direction) &&
261
0
       direction != horiz_dir && horiz_dir != HB_DIRECTION_INVALID) ||
262
0
      (HB_DIRECTION_IS_VERTICAL   (direction) &&
263
0
       direction != HB_DIRECTION_TTB))
264
0
  {
265
0
    hb_buffer_reverse_clusters (buffer);
266
0
    direction = HB_DIRECTION_REVERSE (direction);
267
0
  }
268
269
0
  gr_segment *seg = nullptr;
270
0
  const gr_slot *is;
271
0
  unsigned int ci = 0, ic = 0;
272
0
  hb_position_t curradvx = 0, curradvy = 0;
273
274
0
  unsigned int scratch_size;
275
0
  hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
276
277
0
  uint32_t *chars = (uint32_t *) scratch;
278
279
0
  for (unsigned int i = 0; i < buffer->len; ++i)
280
0
    chars[i] = buffer->info[i].codepoint;
281
282
0
  seg = gr_make_seg (nullptr, grface,
283
0
         HB_TAG_NONE, // https://github.com/harfbuzz/harfbuzz/issues/3439#issuecomment-1442650148
284
0
         feats,
285
0
         gr_utf32, chars, buffer->len,
286
0
         2 | (direction == HB_DIRECTION_RTL ? 1 : 0));
287
288
0
  if (unlikely (!seg)) {
289
0
    if (feats) gr_featureval_destroy (feats);
290
0
    return false;
291
0
  }
292
293
0
  unsigned int glyph_count = gr_seg_n_slots (seg);
294
0
  if (unlikely (!glyph_count)) {
295
0
    if (feats) gr_featureval_destroy (feats);
296
0
    gr_seg_destroy (seg);
297
0
    buffer->len = 0;
298
0
    return true;
299
0
  }
300
301
0
  (void) buffer->ensure (glyph_count);
302
0
  scratch = buffer->get_scratch_buffer (&scratch_size);
303
0
  while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) +
304
0
    DIV_CEIL (sizeof (hb_graphite2_glyph_t) * glyph_count, sizeof (*scratch))) > scratch_size)
305
0
  {
306
0
    if (unlikely (!buffer->ensure (buffer->allocated * 2)))
307
0
    {
308
0
      if (feats) gr_featureval_destroy (feats);
309
0
      gr_seg_destroy (seg);
310
0
      return false;
311
0
    }
312
0
    scratch = buffer->get_scratch_buffer (&scratch_size);
313
0
  }
314
315
0
#define ALLOCATE_ARRAY(Type, name, len) \
316
0
  Type *name = (Type *) scratch; \
317
0
  do { \
318
0
    unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
319
0
    assert (_consumed <= scratch_size); \
320
0
    scratch += _consumed; \
321
0
    scratch_size -= _consumed; \
322
0
  } while (0)
323
324
0
  ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len);
325
0
  ALLOCATE_ARRAY (hb_graphite2_glyph_t, glyphs, glyph_count);
326
327
0
#undef ALLOCATE_ARRAY
328
329
0
  hb_memset (clusters, 0, sizeof (clusters[0]) * buffer->len);
330
331
0
  hb_graphite2_glyph_t *pg = glyphs;
332
0
  clusters[0].cluster = buffer->info[0].cluster;
333
0
  unsigned int upem = hb_face_get_upem (face);
334
0
  float xscale = (float) font->x_scale / upem;
335
0
  float yscale = (float) font->y_scale / upem;
336
0
  yscale *= yscale / xscale;
337
0
  hb_position_t curradv = 0;
338
0
  if (HB_DIRECTION_IS_BACKWARD (direction))
339
0
  {
340
0
    curradv = hb_clamp_to<hb_position_t> (gr_slot_origin_X(gr_seg_first_slot(seg)) * xscale);
341
0
    clusters[0].advance = hb_clamp_to<hb_position_t> ((double) (gr_seg_advance_X(seg) * xscale) - curradv);
342
0
  }
343
0
  else
344
0
    clusters[0].advance = 0;
345
0
  for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++)
346
0
  {
347
0
    int slot_before = gr_slot_before (is);
348
0
    int slot_after = gr_slot_after (is);
349
0
    if (unlikely (slot_before < 0 ||
350
0
      slot_after < slot_before ||
351
0
      (unsigned int) slot_after >= buffer->len))
352
0
    {
353
0
      if (feats) gr_featureval_destroy (feats);
354
0
      gr_seg_destroy (seg);
355
0
      return false;
356
0
    }
357
0
    unsigned int before = (unsigned int) slot_before;
358
0
    unsigned int after = (unsigned int) slot_after;
359
0
    pg->codepoint = gr_slot_gid (is);
360
0
    pg->cluster = hb_min (buffer->info[before].cluster,
361
0
        buffer->info[after].cluster);
362
0
    pg++;
363
0
    while (clusters[ci].base_char > before && ci)
364
0
    {
365
0
      clusters[ci-1].num_chars += clusters[ci].num_chars;
366
0
      clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
367
0
      clusters[ci-1].advance = hb_saturate_add (clusters[ci-1].advance, clusters[ci].advance);
368
0
      ci--;
369
0
    }
370
371
0
    if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
372
0
    {
373
0
      hb_graphite2_cluster_t *c = clusters + ci + 1;
374
0
      c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
375
0
      c->cluster = buffer->info[c->base_char].cluster;
376
0
      c->num_chars = before - c->base_char;
377
0
      c->base_glyph = ic;
378
0
      c->num_glyphs = 0;
379
0
      if (HB_DIRECTION_IS_BACKWARD (direction))
380
0
      {
381
0
  c->advance = hb_clamp_to<hb_position_t> ((double) curradv -
382
0
               (double) (gr_slot_origin_X(is) * xscale));
383
0
  curradv = hb_saturate_sub (curradv, c->advance);
384
0
      }
385
0
      else
386
0
      {
387
0
  hb_position_t origin_X = hb_clamp_to<hb_position_t> (gr_slot_origin_X (is) * xscale);
388
0
  c->advance = 0;
389
0
  clusters[ci].advance = hb_saturate_add (clusters[ci].advance,
390
0
                  hb_saturate_sub (origin_X, curradv));
391
0
  curradv = origin_X;
392
0
      }
393
0
      ci++;
394
0
    }
395
0
    clusters[ci].num_glyphs++;
396
397
0
    if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
398
0
  clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
399
0
  }
400
401
0
  if (HB_DIRECTION_IS_BACKWARD (direction))
402
0
    clusters[ci].advance = hb_saturate_add (clusters[ci].advance, curradv);
403
0
  else
404
0
    clusters[ci].advance = hb_clamp_to<hb_position_t> ((double) clusters[ci].advance +
405
0
                     (double) (gr_seg_advance_X(seg) * xscale) - curradv);
406
0
  ci++;
407
408
0
  for (unsigned int i = 0; i < ci; ++i)
409
0
  {
410
0
    for (unsigned int j = 0; j < clusters[i].num_glyphs; ++j)
411
0
    {
412
0
      hb_glyph_info_t *info = &buffer->info[clusters[i].base_glyph + j];
413
0
      info->codepoint = glyphs[clusters[i].base_glyph + j].codepoint;
414
0
      info->cluster = HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (buffer->cluster_level) ?
415
0
          glyphs[clusters[i].base_glyph + j].cluster :
416
0
          clusters[i].cluster;
417
0
      info->var1.i32 = clusters[i].advance;     // all glyphs in the cluster get the same advance
418
0
      info->var2.u32 = i;                       // Graphite cluster index
419
0
    }
420
0
  }
421
0
  buffer->len = glyph_count;
422
423
0
  for (unsigned int i = 0; i < ci; ++i)
424
0
    buffer->merge_clusters (clusters[i].base_glyph,
425
0
          clusters[i].base_glyph + clusters[i].num_glyphs);
426
427
  /* Positioning. */
428
0
  unsigned int curr_gr_cluster = UINT_MAX;
429
0
  const hb_glyph_info_t *info = buffer->info;
430
0
  hb_glyph_position_t *pPos = hb_buffer_get_glyph_positions (buffer, nullptr);
431
0
  if (!HB_DIRECTION_IS_BACKWARD (direction))
432
0
  {
433
0
    curradvx = 0;
434
0
    for (is = gr_seg_first_slot (seg); is; pPos++, ++info, is = gr_slot_next_in_segment (is))
435
0
    {
436
0
      pPos->x_offset = hb_clamp_to<hb_position_t> ((double) (gr_slot_origin_X (is) * xscale) - curradvx);
437
0
      pPos->y_offset = hb_clamp_to<hb_position_t> ((double) (gr_slot_origin_Y (is) * yscale) - curradvy);
438
0
      if (info->var2.u32 != curr_gr_cluster) {
439
0
  pPos->x_advance = info->var1.i32;
440
0
  curradvx = hb_saturate_add (curradvx, pPos->x_advance);
441
0
  curr_gr_cluster = info->var2.u32;
442
0
      } else
443
0
  pPos->x_advance = 0.;
444
445
0
      pPos->y_advance = hb_clamp_to<hb_position_t> (gr_slot_advance_Y (is, grface, nullptr) * yscale);
446
0
      curradvy = hb_saturate_add (curradvy, pPos->y_advance);
447
0
    }
448
0
  }
449
0
  else
450
0
  {
451
0
    curradvx = hb_clamp_to<hb_position_t> (gr_seg_advance_X(seg) * xscale);
452
0
    for (is = gr_seg_first_slot (seg); is; pPos++, info++, is = gr_slot_next_in_segment (is))
453
0
    {
454
0
      if (info->var2.u32 != curr_gr_cluster)
455
0
      {
456
0
  pPos->x_advance = info->var1.i32;
457
0
  curradvx = hb_saturate_sub (curradvx, pPos->x_advance);
458
0
  curr_gr_cluster = info->var2.u32;
459
0
      } else
460
0
  pPos->x_advance = 0.;
461
462
0
      pPos->y_advance = hb_clamp_to<hb_position_t> (gr_slot_advance_Y (is, grface, nullptr) * yscale);
463
0
      curradvy = hb_saturate_sub (curradvy, pPos->y_advance);
464
0
      pPos->x_offset = hb_clamp_to<hb_position_t> ((double) (gr_slot_origin_X (is) * xscale) -
465
0
                info->var1.i32 - curradvx + pPos->x_advance);
466
0
      pPos->y_offset = hb_clamp_to<hb_position_t> ((double) (gr_slot_origin_Y (is) * yscale) - curradvy);
467
0
    }
468
0
    hb_buffer_reverse_clusters (buffer);
469
0
  }
470
471
0
  if (feats) gr_featureval_destroy (feats);
472
0
  gr_seg_destroy (seg);
473
474
0
  buffer->clear_glyph_flags ();
475
0
  buffer->unsafe_to_break ();
476
477
0
  return true;
478
0
}
479
480
481
#endif