2 * Copyright © 2011 Martin Hosken
3 * Copyright © 2011 SIL International
4 * Copyright © 2011,2012 Google, Inc.
6 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
26 * Google Author(s): Behdad Esfahbod
33 #include "hb-shaper-impl.hh"
35 #include "hb-graphite2.h"
37 #include <graphite2/Segment.h>
39 #include "hb-ot-layout.h"
43 * SECTION:hb-graphite2
44 * @title: hb-graphite2
45 * @short_description: Graphite2 integration
46 * @include: hb-graphite2.h
48 * Functions for using HarfBuzz with fonts that include Graphite features.
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.
60 typedef struct hb_graphite2_tablelist_t
62 struct hb_graphite2_tablelist_t *next;
65 } hb_graphite2_tablelist_t;
67 struct hb_graphite2_face_data_t
71 hb_atomic_ptr_t<hb_graphite2_tablelist_t> tlist;
74 static const void *hb_graphite2_get_table (const void *data, unsigned int tag, size_t *len)
76 hb_graphite2_face_data_t *face_data = (hb_graphite2_face_data_t *) data;
77 hb_graphite2_tablelist_t *tlist = face_data->tlist;
79 hb_blob_t *blob = nullptr;
81 for (hb_graphite2_tablelist_t *p = tlist; p; p = p->next)
89 blob = face_data->face->reference_table (tag);
91 hb_graphite2_tablelist_t *p = (hb_graphite2_tablelist_t *) hb_calloc (1, sizeof (hb_graphite2_tablelist_t));
93 hb_blob_destroy (blob);
100 hb_graphite2_tablelist_t *tlist = face_data->tlist;
103 if (unlikely (!face_data->tlist.cmpexch (tlist, p)))
108 const char *d = hb_blob_get_data (blob, &tlen);
113 hb_graphite2_face_data_t *
114 _hb_graphite2_shaper_face_data_create (hb_face_t *face)
116 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 if (!hb_blob_get_length (silf_blob))
121 hb_blob_destroy (silf_blob);
124 hb_blob_destroy (silf_blob);
126 hb_graphite2_face_data_t *data = (hb_graphite2_face_data_t *) hb_calloc (1, sizeof (hb_graphite2_face_data_t));
127 if (unlikely (!data))
131 const gr_face_ops ops = {sizeof(gr_face_ops), &hb_graphite2_get_table, NULL};
132 data->grface = gr_make_face_with_ops (data, &ops, gr_face_preloadAll);
134 if (unlikely (!data->grface)) {
143 _hb_graphite2_shaper_face_data_destroy (hb_graphite2_face_data_t *data)
145 hb_graphite2_tablelist_t *tlist = data->tlist;
149 hb_graphite2_tablelist_t *old = tlist;
150 hb_blob_destroy (tlist->blob);
155 gr_face_destroy (data->grface);
161 * hb_graphite2_face_get_gr_face:
162 * @face: @hb_face_t to query
164 * Fetches the Graphite2 gr_face corresponding to the specified
165 * #hb_face_t face object.
167 * Return value: the gr_face found
172 hb_graphite2_face_get_gr_face (hb_face_t *face)
174 const hb_graphite2_face_data_t *data = face->data.graphite2;
175 return data ? data->grface : nullptr;
183 struct hb_graphite2_font_data_t {};
185 hb_graphite2_font_data_t *
186 _hb_graphite2_shaper_font_data_create (hb_font_t *font HB_UNUSED)
188 return (hb_graphite2_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
192 _hb_graphite2_shaper_font_data_destroy (hb_graphite2_font_data_t *data HB_UNUSED)
196 #ifndef HB_DISABLE_DEPRECATED
198 * hb_graphite2_font_get_gr_font:
199 * @font: An #hb_font_t
201 * Always returns %NULL. Use hb_graphite2_face_get_gr_face() instead.
203 * Return value: (nullable): Graphite2 font associated with @font.
209 hb_graphite2_font_get_gr_font (hb_font_t *font HB_UNUSED)
220 struct hb_graphite2_cluster_t {
221 unsigned int base_char;
222 unsigned int num_chars;
223 unsigned int base_glyph;
224 unsigned int num_glyphs;
225 unsigned int cluster;
226 unsigned int advance;
230 _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED,
233 const hb_feature_t *features,
234 unsigned int num_features)
236 hb_face_t *face = font->face;
237 gr_face *grface = face->data.graphite2->grface;
239 const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
240 const char *lang_end = lang ? strchr (lang, '-') : nullptr;
241 int lang_len = lang_end ? lang_end - lang : -1;
242 gr_feature_val *feats = gr_face_featureval_for_lang (grface, lang ? hb_tag_from_string (lang, lang_len) : 0);
244 for (unsigned int i = 0; i < num_features; i++)
246 const gr_feature_ref *fref = gr_face_find_fref (grface, features[i].tag);
248 gr_fref_set_feature_value (fref, features[i].value, feats);
251 gr_segment *seg = nullptr;
253 unsigned int ci = 0, ic = 0;
254 unsigned int curradvx = 0, curradvy = 0;
256 unsigned int scratch_size;
257 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
259 uint32_t *chars = (uint32_t *) scratch;
261 for (unsigned int i = 0; i < buffer->len; ++i)
262 chars[i] = buffer->info[i].codepoint;
264 /* TODO ensure_native_direction. */
266 hb_tag_t script_tag[HB_OT_MAX_TAGS_PER_SCRIPT];
267 unsigned int count = HB_OT_MAX_TAGS_PER_SCRIPT;
268 hb_ot_tags_from_script_and_language (hb_buffer_get_script (buffer),
274 seg = gr_make_seg (nullptr, grface,
275 count ? script_tag[count - 1] : HB_OT_TAG_DEFAULT_SCRIPT,
277 gr_utf32, chars, buffer->len,
278 2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0));
280 if (unlikely (!seg)) {
281 if (feats) gr_featureval_destroy (feats);
285 unsigned int glyph_count = gr_seg_n_slots (seg);
286 if (unlikely (!glyph_count)) {
287 if (feats) gr_featureval_destroy (feats);
288 gr_seg_destroy (seg);
293 (void) buffer->ensure (glyph_count);
294 scratch = buffer->get_scratch_buffer (&scratch_size);
295 while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) +
296 DIV_CEIL (sizeof (hb_codepoint_t) * glyph_count, sizeof (*scratch))) > scratch_size)
298 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
300 if (feats) gr_featureval_destroy (feats);
301 gr_seg_destroy (seg);
304 scratch = buffer->get_scratch_buffer (&scratch_size);
307 #define ALLOCATE_ARRAY(Type, name, len) \
308 Type *name = (Type *) scratch; \
310 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
311 assert (_consumed <= scratch_size); \
312 scratch += _consumed; \
313 scratch_size -= _consumed; \
316 ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len);
317 ALLOCATE_ARRAY (hb_codepoint_t, gids, glyph_count);
319 #undef ALLOCATE_ARRAY
321 memset (clusters, 0, sizeof (clusters[0]) * buffer->len);
323 hb_codepoint_t *pg = gids;
324 clusters[0].cluster = buffer->info[0].cluster;
325 unsigned int upem = hb_face_get_upem (face);
326 float xscale = (float) font->x_scale / upem;
327 float yscale = (float) font->y_scale / upem;
328 yscale *= yscale / xscale;
329 unsigned int curradv = 0;
330 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
332 curradv = gr_slot_origin_X(gr_seg_first_slot(seg)) * xscale;
333 clusters[0].advance = gr_seg_advance_X(seg) * xscale - curradv;
336 clusters[0].advance = 0;
337 for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++)
339 unsigned int before = gr_slot_before (is);
340 unsigned int after = gr_slot_after (is);
341 *pg = gr_slot_gid (is);
343 while (clusters[ci].base_char > before && ci)
345 clusters[ci-1].num_chars += clusters[ci].num_chars;
346 clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
347 clusters[ci-1].advance += clusters[ci].advance;
351 if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
353 hb_graphite2_cluster_t *c = clusters + ci + 1;
354 c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
355 c->cluster = buffer->info[c->base_char].cluster;
356 c->num_chars = before - c->base_char;
359 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
361 c->advance = curradv - gr_slot_origin_X(is) * xscale;
362 curradv -= c->advance;
367 clusters[ci].advance += gr_slot_origin_X(is) * xscale - curradv;
368 curradv += clusters[ci].advance;
372 clusters[ci].num_glyphs++;
374 if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
375 clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
378 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
379 clusters[ci].advance += curradv;
381 clusters[ci].advance += gr_seg_advance_X(seg) * xscale - curradv;
384 for (unsigned int i = 0; i < ci; ++i)
386 for (unsigned int j = 0; j < clusters[i].num_glyphs; ++j)
388 hb_glyph_info_t *info = &buffer->info[clusters[i].base_glyph + j];
389 info->codepoint = gids[clusters[i].base_glyph + j];
390 info->cluster = clusters[i].cluster;
391 info->var1.i32 = clusters[i].advance; // all glyphs in the cluster get the same advance
394 buffer->len = glyph_count;
397 unsigned int currclus = UINT_MAX;
398 const hb_glyph_info_t *info = buffer->info;
399 hb_glyph_position_t *pPos = hb_buffer_get_glyph_positions (buffer, nullptr);
400 if (!HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
403 for (is = gr_seg_first_slot (seg); is; pPos++, ++info, is = gr_slot_next_in_segment (is))
405 pPos->x_offset = gr_slot_origin_X (is) * xscale - curradvx;
406 pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
407 if (info->cluster != currclus) {
408 pPos->x_advance = info->var1.i32;
409 curradvx += pPos->x_advance;
410 currclus = info->cluster;
412 pPos->x_advance = 0.;
414 pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
415 curradvy += pPos->y_advance;
420 curradvx = gr_seg_advance_X(seg) * xscale;
421 for (is = gr_seg_first_slot (seg); is; pPos++, info++, is = gr_slot_next_in_segment (is))
423 if (info->cluster != currclus)
425 pPos->x_advance = info->var1.i32;
426 curradvx -= pPos->x_advance;
427 currclus = info->cluster;
429 pPos->x_advance = 0.;
431 pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
432 curradvy -= pPos->y_advance;
433 pPos->x_offset = gr_slot_origin_X (is) * xscale - info->var1.i32 - curradvx + pPos->x_advance;
434 pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
436 hb_buffer_reverse_clusters (buffer);
439 if (feats) gr_featureval_destroy (feats);
440 gr_seg_destroy (seg);
442 buffer->clear_glyph_flags ();
443 buffer->unsafe_to_break ();