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
29 #define HB_SHAPER graphite2
30 #include "hb-shaper-impl-private.hh"
32 #include "hb-graphite2.h"
34 #include <graphite2/Segment.h>
37 HB_SHAPER_DATA_ENSURE_DEFINE(graphite2, face)
38 HB_SHAPER_DATA_ENSURE_DEFINE(graphite2, font)
45 typedef struct hb_graphite2_tablelist_t {
46 struct hb_graphite2_tablelist_t *next;
49 } hb_graphite2_tablelist_t;
51 struct hb_graphite2_shaper_face_data_t {
54 hb_graphite2_tablelist_t *tlist;
57 static const void *hb_graphite2_get_table (const void *data, unsigned int tag, size_t *len)
59 hb_graphite2_shaper_face_data_t *face_data = (hb_graphite2_shaper_face_data_t *) data;
60 hb_graphite2_tablelist_t *tlist = face_data->tlist;
62 hb_blob_t *blob = nullptr;
64 for (hb_graphite2_tablelist_t *p = tlist; p; p = p->next)
72 blob = face_data->face->reference_table (tag);
74 hb_graphite2_tablelist_t *p = (hb_graphite2_tablelist_t *) calloc (1, sizeof (hb_graphite2_tablelist_t));
76 hb_blob_destroy (blob);
83 hb_graphite2_tablelist_t *tlist = (hb_graphite2_tablelist_t *) hb_atomic_ptr_get (&face_data->tlist);
86 if (!hb_atomic_ptr_cmpexch (&face_data->tlist, tlist, p))
91 const char *d = hb_blob_get_data (blob, &tlen);
96 hb_graphite2_shaper_face_data_t *
97 _hb_graphite2_shaper_face_data_create (hb_face_t *face)
99 hb_blob_t *silf_blob = face->reference_table (HB_GRAPHITE2_TAG_SILF);
100 /* Umm, we just reference the table to check whether it exists.
101 * Maybe add better API for this? */
102 if (!hb_blob_get_length (silf_blob))
104 hb_blob_destroy (silf_blob);
107 hb_blob_destroy (silf_blob);
109 hb_graphite2_shaper_face_data_t *data = (hb_graphite2_shaper_face_data_t *) calloc (1, sizeof (hb_graphite2_shaper_face_data_t));
110 if (unlikely (!data))
114 data->grface = gr_make_face (data, &hb_graphite2_get_table, gr_face_preloadAll);
116 if (unlikely (!data->grface)) {
125 _hb_graphite2_shaper_face_data_destroy (hb_graphite2_shaper_face_data_t *data)
127 hb_graphite2_tablelist_t *tlist = data->tlist;
131 hb_graphite2_tablelist_t *old = tlist;
132 hb_blob_destroy (tlist->blob);
137 gr_face_destroy (data->grface);
146 hb_graphite2_face_get_gr_face (hb_face_t *face)
148 if (unlikely (!hb_graphite2_shaper_face_data_ensure (face))) return nullptr;
149 return HB_SHAPER_DATA_GET (face)->grface;
157 struct hb_graphite2_shaper_font_data_t {};
159 hb_graphite2_shaper_font_data_t *
160 _hb_graphite2_shaper_font_data_create (hb_font_t *font HB_UNUSED)
162 return (hb_graphite2_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
166 _hb_graphite2_shaper_font_data_destroy (hb_graphite2_shaper_font_data_t *data HB_UNUSED)
174 hb_graphite2_font_get_gr_font (hb_font_t *font)
181 * shaper shape_plan data
184 struct hb_graphite2_shaper_shape_plan_data_t {};
186 hb_graphite2_shaper_shape_plan_data_t *
187 _hb_graphite2_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
188 const hb_feature_t *user_features HB_UNUSED,
189 unsigned int num_user_features HB_UNUSED,
190 const int *coords HB_UNUSED,
191 unsigned int num_coords HB_UNUSED)
193 return (hb_graphite2_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
197 _hb_graphite2_shaper_shape_plan_data_destroy (hb_graphite2_shaper_shape_plan_data_t *data HB_UNUSED)
206 struct hb_graphite2_cluster_t {
207 unsigned int base_char;
208 unsigned int num_chars;
209 unsigned int base_glyph;
210 unsigned int num_glyphs;
211 unsigned int cluster;
216 _hb_graphite2_shape (hb_shape_plan_t *shape_plan,
219 const hb_feature_t *features,
220 unsigned int num_features)
222 hb_face_t *face = font->face;
223 gr_face *grface = HB_SHAPER_DATA_GET (face)->grface;
225 const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
226 const char *lang_end = lang ? strchr (lang, '-') : nullptr;
227 int lang_len = lang_end ? lang_end - lang : -1;
228 gr_feature_val *feats = gr_face_featureval_for_lang (grface, lang ? hb_tag_from_string (lang, lang_len) : 0);
230 for (unsigned int i = 0; i < num_features; i++)
232 const gr_feature_ref *fref = gr_face_find_fref (grface, features[i].tag);
234 gr_fref_set_feature_value (fref, features[i].value, feats);
237 gr_segment *seg = nullptr;
239 unsigned int ci = 0, ic = 0;
240 float curradvx = 0., curradvy = 0.;
242 unsigned int scratch_size;
243 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
245 uint32_t *chars = (uint32_t *) scratch;
247 for (unsigned int i = 0; i < buffer->len; ++i)
248 chars[i] = buffer->info[i].codepoint;
250 /* TODO ensure_native_direction. */
252 hb_tag_t script_tag[2];
253 hb_ot_tags_from_script (hb_buffer_get_script (buffer), &script_tag[0], &script_tag[1]);
255 seg = gr_make_seg (nullptr, grface,
256 script_tag[1] == HB_TAG_NONE ? script_tag[0] : script_tag[1],
258 gr_utf32, chars, buffer->len,
259 2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0));
261 if (unlikely (!seg)) {
262 if (feats) gr_featureval_destroy (feats);
266 unsigned int glyph_count = gr_seg_n_slots (seg);
267 if (unlikely (!glyph_count)) {
268 if (feats) gr_featureval_destroy (feats);
269 gr_seg_destroy (seg);
274 buffer->ensure (glyph_count);
275 scratch = buffer->get_scratch_buffer (&scratch_size);
276 while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) +
277 DIV_CEIL (sizeof (hb_codepoint_t) * glyph_count, sizeof (*scratch))) > scratch_size)
279 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
281 if (feats) gr_featureval_destroy (feats);
282 gr_seg_destroy (seg);
285 scratch = buffer->get_scratch_buffer (&scratch_size);
288 #define ALLOCATE_ARRAY(Type, name, len) \
289 Type *name = (Type *) scratch; \
291 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
292 assert (_consumed <= scratch_size); \
293 scratch += _consumed; \
294 scratch_size -= _consumed; \
297 ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len);
298 ALLOCATE_ARRAY (hb_codepoint_t, gids, glyph_count);
300 #undef ALLOCATE_ARRAY
302 memset (clusters, 0, sizeof (clusters[0]) * buffer->len);
304 hb_codepoint_t *pg = gids;
305 clusters[0].cluster = buffer->info[0].cluster;
307 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
309 curradv = gr_slot_origin_X(gr_seg_first_slot(seg));
310 clusters[0].advance = gr_seg_advance_X(seg) - curradv;
313 clusters[0].advance = 0;
314 for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++)
316 unsigned int before = gr_slot_before (is);
317 unsigned int after = gr_slot_after (is);
318 *pg = gr_slot_gid (is);
320 while (clusters[ci].base_char > before && ci)
322 clusters[ci-1].num_chars += clusters[ci].num_chars;
323 clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
324 clusters[ci-1].advance += clusters[ci].advance;
328 if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
330 hb_graphite2_cluster_t *c = clusters + ci + 1;
331 c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
332 c->cluster = buffer->info[c->base_char].cluster;
333 c->num_chars = before - c->base_char;
336 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
337 c->advance = curradv - gr_slot_origin_X(is);
341 clusters[ci].advance += gr_slot_origin_X(is) - curradv;
344 curradv = gr_slot_origin_X(is);
346 clusters[ci].num_glyphs++;
348 if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
349 clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
352 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
353 clusters[ci].advance += curradv;
355 clusters[ci].advance += gr_seg_advance_X(seg) - curradv;
358 for (unsigned int i = 0; i < ci; ++i)
360 for (unsigned int j = 0; j < clusters[i].num_glyphs; ++j)
362 hb_glyph_info_t *info = &buffer->info[clusters[i].base_glyph + j];
363 info->codepoint = gids[clusters[i].base_glyph + j];
364 info->cluster = clusters[i].cluster;
365 info->var1.i32 = clusters[i].advance; // all glyphs in the cluster get the same advance
368 buffer->len = glyph_count;
370 unsigned int upem = hb_face_get_upem (face);
371 float xscale = (float) font->x_scale / upem;
372 float yscale = (float) font->y_scale / upem;
373 yscale *= yscale / xscale;
375 unsigned int currclus = (unsigned int) -1;
376 const hb_glyph_info_t *info = buffer->info;
377 hb_glyph_position_t *pPos = hb_buffer_get_glyph_positions (buffer, nullptr);
378 if (!HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
381 for (is = gr_seg_first_slot (seg); is; pPos++, ++info, is = gr_slot_next_in_segment (is))
383 pPos->x_offset = gr_slot_origin_X (is) * xscale - curradvx;
384 pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
385 if (info->cluster != currclus) {
386 pPos->x_advance = info->var1.i32 * xscale;
387 curradvx += pPos->x_advance;
388 currclus = info->cluster;
390 pPos->x_advance = 0.;
392 pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
393 curradvy += pPos->y_advance;
398 curradvx = gr_seg_advance_X(seg) * xscale;
399 for (is = gr_seg_first_slot (seg); is; pPos++, info++, is = gr_slot_next_in_segment (is))
401 if (info->cluster != currclus)
403 pPos->x_advance = info->var1.i32 * xscale;
404 curradvx -= pPos->x_advance;
405 currclus = info->cluster;
407 pPos->x_advance = 0.;
409 pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
410 curradvy -= pPos->y_advance;
411 pPos->x_offset = (gr_slot_origin_X (is) - info->var1.i32) * xscale - curradvx + pPos->x_advance;
412 pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
414 hb_buffer_reverse_clusters (buffer);
417 if (feats) gr_featureval_destroy (feats);
418 gr_seg_destroy (seg);
420 buffer->unsafe_to_break_all ();