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 = NULL;
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);
82 /* TODO Not thread-safe, but fairly harmless.
83 * We can do the double-chcked pointer cmpexch thing here. */
84 p->next = face_data->tlist;
89 const char *d = hb_blob_get_data (blob, &tlen);
94 hb_graphite2_shaper_face_data_t *
95 _hb_graphite2_shaper_face_data_create (hb_face_t *face)
97 hb_blob_t *silf_blob = face->reference_table (HB_GRAPHITE2_TAG_SILF);
98 /* Umm, we just reference the table to check whether it exists.
99 * Maybe add better API for this? */
100 if (!hb_blob_get_length (silf_blob))
102 hb_blob_destroy (silf_blob);
105 hb_blob_destroy (silf_blob);
107 hb_graphite2_shaper_face_data_t *data = (hb_graphite2_shaper_face_data_t *) calloc (1, sizeof (hb_graphite2_shaper_face_data_t));
108 if (unlikely (!data))
112 data->grface = gr_make_face (data, &hb_graphite2_get_table, gr_face_preloadAll);
114 if (unlikely (!data->grface)) {
123 _hb_graphite2_shaper_face_data_destroy (hb_graphite2_shaper_face_data_t *data)
125 hb_graphite2_tablelist_t *tlist = data->tlist;
129 hb_graphite2_tablelist_t *old = tlist;
130 hb_blob_destroy (tlist->blob);
135 gr_face_destroy (data->grface);
144 hb_graphite2_face_get_gr_face (hb_face_t *face)
146 if (unlikely (!hb_graphite2_shaper_face_data_ensure (face))) return NULL;
147 return HB_SHAPER_DATA_GET (face)->grface;
155 struct hb_graphite2_shaper_font_data_t {};
157 hb_graphite2_shaper_font_data_t *
158 _hb_graphite2_shaper_font_data_create (hb_font_t *font HB_UNUSED)
160 return (hb_graphite2_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
164 _hb_graphite2_shaper_font_data_destroy (hb_graphite2_shaper_font_data_t *data HB_UNUSED)
172 hb_graphite2_font_get_gr_font (hb_font_t *font)
179 * shaper shape_plan data
182 struct hb_graphite2_shaper_shape_plan_data_t {};
184 hb_graphite2_shaper_shape_plan_data_t *
185 _hb_graphite2_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
186 const hb_feature_t *user_features HB_UNUSED,
187 unsigned int num_user_features HB_UNUSED,
188 const int *coords HB_UNUSED,
189 unsigned int num_coords HB_UNUSED)
191 return (hb_graphite2_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
195 _hb_graphite2_shaper_shape_plan_data_destroy (hb_graphite2_shaper_shape_plan_data_t *data HB_UNUSED)
204 struct hb_graphite2_cluster_t {
205 unsigned int base_char;
206 unsigned int num_chars;
207 unsigned int base_glyph;
208 unsigned int num_glyphs;
209 unsigned int cluster;
214 _hb_graphite2_shape (hb_shape_plan_t *shape_plan,
217 const hb_feature_t *features,
218 unsigned int num_features)
220 hb_face_t *face = font->face;
221 gr_face *grface = HB_SHAPER_DATA_GET (face)->grface;
223 const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
224 const char *lang_end = lang ? strchr (lang, '-') : NULL;
225 int lang_len = lang_end ? lang_end - lang : -1;
226 gr_feature_val *feats = gr_face_featureval_for_lang (grface, lang ? hb_tag_from_string (lang, lang_len) : 0);
228 for (unsigned int i = 0; i < num_features; i++)
230 const gr_feature_ref *fref = gr_face_find_fref (grface, features[i].tag);
232 gr_fref_set_feature_value (fref, features[i].value, feats);
235 gr_segment *seg = NULL;
237 unsigned int ci = 0, ic = 0;
238 float curradvx = 0., curradvy = 0.;
240 unsigned int scratch_size;
241 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
243 uint32_t *chars = (uint32_t *) scratch;
245 for (unsigned int i = 0; i < buffer->len; ++i)
246 chars[i] = buffer->info[i].codepoint;
248 /* TODO ensure_native_direction. */
250 hb_tag_t script_tag[2];
251 hb_ot_tags_from_script (hb_buffer_get_script (buffer), &script_tag[0], &script_tag[1]);
253 seg = gr_make_seg (NULL, grface,
254 script_tag[1] == HB_TAG_NONE ? script_tag[0] : script_tag[1],
256 gr_utf32, chars, buffer->len,
257 2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0));
259 if (unlikely (!seg)) {
260 if (feats) gr_featureval_destroy (feats);
264 unsigned int glyph_count = gr_seg_n_slots (seg);
265 if (unlikely (!glyph_count)) {
266 if (feats) gr_featureval_destroy (feats);
267 gr_seg_destroy (seg);
272 buffer->ensure (glyph_count);
273 scratch = buffer->get_scratch_buffer (&scratch_size);
274 while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) +
275 DIV_CEIL (sizeof (hb_codepoint_t) * glyph_count, sizeof (*scratch))) > scratch_size)
277 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
279 if (feats) gr_featureval_destroy (feats);
280 gr_seg_destroy (seg);
283 scratch = buffer->get_scratch_buffer (&scratch_size);
286 #define ALLOCATE_ARRAY(Type, name, len) \
287 Type *name = (Type *) scratch; \
289 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
290 assert (_consumed <= scratch_size); \
291 scratch += _consumed; \
292 scratch_size -= _consumed; \
295 ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len);
296 ALLOCATE_ARRAY (hb_codepoint_t, gids, glyph_count);
298 #undef ALLOCATE_ARRAY
300 memset (clusters, 0, sizeof (clusters[0]) * buffer->len);
302 hb_codepoint_t *pg = gids;
303 clusters[0].cluster = buffer->info[0].cluster;
305 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
307 curradv = gr_slot_origin_X(gr_seg_first_slot(seg));
308 clusters[0].advance = gr_seg_advance_X(seg) - curradv;
310 for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++)
312 unsigned int before = gr_slot_before (is);
313 unsigned int after = gr_slot_after (is);
314 *pg = gr_slot_gid (is);
316 while (clusters[ci].base_char > before && ci)
318 clusters[ci-1].num_chars += clusters[ci].num_chars;
319 clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
320 clusters[ci-1].advance += clusters[ci].advance;
324 if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
326 hb_graphite2_cluster_t *c = clusters + ci + 1;
327 c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
328 c->cluster = buffer->info[c->base_char].cluster;
329 c->num_chars = before - c->base_char;
332 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
333 c->advance = curradv - gr_slot_origin_X(is);
335 clusters[ci].advance = gr_slot_origin_X(is) - curradv;
337 curradv = gr_slot_origin_X(is);
339 clusters[ci].num_glyphs++;
341 if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
342 clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
345 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
346 clusters[ci].advance += curradv;
348 clusters[ci].advance = gr_seg_advance_X(seg) - curradv;
351 for (unsigned int i = 0; i < ci; ++i)
353 for (unsigned int j = 0; j < clusters[i].num_glyphs; ++j)
355 hb_glyph_info_t *info = &buffer->info[clusters[i].base_glyph + j];
356 info->codepoint = gids[clusters[i].base_glyph + j];
357 info->cluster = clusters[i].cluster;
358 info->var1.i32 = clusters[i].advance; // all glyphs in the cluster get the same advance
361 buffer->len = glyph_count;
363 unsigned int upem = hb_face_get_upem (face);
364 float xscale = (float) font->x_scale / upem;
365 float yscale = (float) font->y_scale / upem;
366 yscale *= yscale / xscale;
369 const hb_glyph_info_t *info = buffer->info;
370 hb_glyph_position_t *pPos = hb_buffer_get_glyph_positions (buffer, NULL);
371 if (!HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
374 for (is = gr_seg_first_slot (seg); is; pPos++, ++info, is = gr_slot_next_in_segment (is))
376 pPos->x_offset = gr_slot_origin_X (is) * xscale - curradvx;
377 pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
378 if (info->cluster != currclus) {
379 pPos->x_advance = info->var1.i32 * xscale;
380 curradvx += pPos->x_advance;
381 currclus = info->cluster;
383 pPos->x_advance = 0.;
385 pPos->y_advance = gr_slot_advance_Y (is, grface, NULL) * yscale;
386 curradvy += pPos->y_advance;
391 curradvx = gr_seg_advance_X(seg);
392 for (is = gr_seg_first_slot (seg); is; pPos++, info++, is = gr_slot_next_in_segment (is))
394 if (info->cluster != currclus)
396 pPos->x_advance = info->var1.i32 * xscale;
397 curradvx -= pPos->x_advance;
398 currclus = info->cluster;
400 pPos->x_advance = 0.;
402 pPos->y_advance = gr_slot_advance_Y (is, grface, NULL) * yscale;
403 curradvy -= pPos->y_advance;
404 pPos->x_offset = (gr_slot_origin_X (is) - info->var1.i32) * xscale - curradvx + pPos->x_advance;
405 pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
407 hb_buffer_reverse_clusters (buffer);
410 if (feats) gr_featureval_destroy (feats);
411 gr_seg_destroy (seg);