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_ptr_t<hb_graphite2_tablelist_t> tlist;
72 };
73
hb_graphite2_get_table(const void * data,unsigned int tag,size_t * len)74 static const void *hb_graphite2_get_table (const void *data, unsigned int tag, size_t *len)
75 {
76 hb_graphite2_face_data_t *face_data = (hb_graphite2_face_data_t *) data;
77 hb_graphite2_tablelist_t *tlist = face_data->tlist;
78
79 hb_blob_t *blob = nullptr;
80
81 for (hb_graphite2_tablelist_t *p = tlist; p; p = p->next)
82 if (p->tag == tag) {
83 blob = p->blob;
84 break;
85 }
86
87 if (unlikely (!blob))
88 {
89 blob = face_data->face->reference_table (tag);
90
91 hb_graphite2_tablelist_t *p = (hb_graphite2_tablelist_t *) calloc (1, sizeof (hb_graphite2_tablelist_t));
92 if (unlikely (!p)) {
93 hb_blob_destroy (blob);
94 return nullptr;
95 }
96 p->blob = blob;
97 p->tag = tag;
98
99 retry:
100 hb_graphite2_tablelist_t *tlist = face_data->tlist;
101 p->next = tlist;
102
103 if (unlikely (!face_data->tlist.cmpexch (tlist, p)))
104 goto retry;
105 }
106
107 unsigned int tlen;
108 const char *d = hb_blob_get_data (blob, &tlen);
109 *len = tlen;
110 return d;
111 }
112
113 hb_graphite2_face_data_t *
_hb_graphite2_shaper_face_data_create(hb_face_t * face)114 _hb_graphite2_shaper_face_data_create (hb_face_t *face)
115 {
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))
120 {
121 hb_blob_destroy (silf_blob);
122 return nullptr;
123 }
124 hb_blob_destroy (silf_blob);
125
126 hb_graphite2_face_data_t *data = (hb_graphite2_face_data_t *) calloc (1, sizeof (hb_graphite2_face_data_t));
127 if (unlikely (!data))
128 return nullptr;
129
130 data->face = face;
131 data->grface = gr_make_face (data, &hb_graphite2_get_table, gr_face_preloadAll);
132
133 if (unlikely (!data->grface)) {
134 free (data);
135 return nullptr;
136 }
137
138 return data;
139 }
140
141 void
_hb_graphite2_shaper_face_data_destroy(hb_graphite2_face_data_t * data)142 _hb_graphite2_shaper_face_data_destroy (hb_graphite2_face_data_t *data)
143 {
144 hb_graphite2_tablelist_t *tlist = data->tlist;
145
146 while (tlist)
147 {
148 hb_graphite2_tablelist_t *old = tlist;
149 hb_blob_destroy (tlist->blob);
150 tlist = tlist->next;
151 free (old);
152 }
153
154 gr_face_destroy (data->grface);
155
156 free (data);
157 }
158
159 /**
160 * hb_graphite2_face_get_gr_face:
161 * @face: @hb_face_t to query
162 *
163 * Fetches the Graphite2 gr_face corresponding to the specified
164 * #hb_face_t face object.
165 *
166 * Return value: the gr_face found
167 *
168 * Since: 0.9.10
169 */
170 gr_face *
hb_graphite2_face_get_gr_face(hb_face_t * face)171 hb_graphite2_face_get_gr_face (hb_face_t *face)
172 {
173 const hb_graphite2_face_data_t *data = face->data.graphite2;
174 return data ? data->grface : nullptr;
175 }
176
177
178 /*
179 * shaper font data
180 */
181
182 struct hb_graphite2_font_data_t {};
183
184 hb_graphite2_font_data_t *
_hb_graphite2_shaper_font_data_create(hb_font_t * font HB_UNUSED)185 _hb_graphite2_shaper_font_data_create (hb_font_t *font HB_UNUSED)
186 {
187 return (hb_graphite2_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
188 }
189
190 void
_hb_graphite2_shaper_font_data_destroy(hb_graphite2_font_data_t * data HB_UNUSED)191 _hb_graphite2_shaper_font_data_destroy (hb_graphite2_font_data_t *data HB_UNUSED)
192 {
193 }
194
195 #ifndef HB_DISABLE_DEPRECATED
196 /**
197 * hb_graphite2_font_get_gr_font:
198 * @font: An #hb_font_t
199 *
200 * Always returns %NULL. Use hb_graphite2_face_get_gr_face() instead.
201 *
202 * Return value: (nullable): Graphite2 font associated with @font.
203 *
204 * Since: 0.9.10
205 * Deprecated: 1.4.2
206 */
207 gr_font *
hb_graphite2_font_get_gr_font(hb_font_t * font HB_UNUSED)208 hb_graphite2_font_get_gr_font (hb_font_t *font HB_UNUSED)
209 {
210 return nullptr;
211 }
212 #endif
213
214
215 /*
216 * shaper
217 */
218
219 struct hb_graphite2_cluster_t {
220 unsigned int base_char;
221 unsigned int num_chars;
222 unsigned int base_glyph;
223 unsigned int num_glyphs;
224 unsigned int cluster;
225 unsigned int advance;
226 };
227
228 hb_bool_t
_hb_graphite2_shape(hb_shape_plan_t * shape_plan HB_UNUSED,hb_font_t * font,hb_buffer_t * buffer,const hb_feature_t * features,unsigned int num_features)229 _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED,
230 hb_font_t *font,
231 hb_buffer_t *buffer,
232 const hb_feature_t *features,
233 unsigned int num_features)
234 {
235 hb_face_t *face = font->face;
236 gr_face *grface = face->data.graphite2->grface;
237
238 const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
239 const char *lang_end = lang ? strchr (lang, '-') : nullptr;
240 int lang_len = lang_end ? lang_end - lang : -1;
241 gr_feature_val *feats = gr_face_featureval_for_lang (grface, lang ? hb_tag_from_string (lang, lang_len) : 0);
242
243 for (unsigned int i = 0; i < num_features; i++)
244 {
245 const gr_feature_ref *fref = gr_face_find_fref (grface, features[i].tag);
246 if (fref)
247 gr_fref_set_feature_value (fref, features[i].value, feats);
248 }
249
250 gr_segment *seg = nullptr;
251 const gr_slot *is;
252 unsigned int ci = 0, ic = 0;
253 unsigned int curradvx = 0, curradvy = 0;
254
255 unsigned int scratch_size;
256 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
257
258 uint32_t *chars = (uint32_t *) scratch;
259
260 for (unsigned int i = 0; i < buffer->len; ++i)
261 chars[i] = buffer->info[i].codepoint;
262
263 /* TODO ensure_native_direction. */
264
265 hb_tag_t script_tag[HB_OT_MAX_TAGS_PER_SCRIPT];
266 unsigned int count = HB_OT_MAX_TAGS_PER_SCRIPT;
267 hb_ot_tags_from_script_and_language (hb_buffer_get_script (buffer),
268 HB_LANGUAGE_INVALID,
269 &count,
270 script_tag,
271 nullptr, nullptr);
272
273 seg = gr_make_seg (nullptr, grface,
274 count ? script_tag[count - 1] : HB_OT_TAG_DEFAULT_SCRIPT,
275 feats,
276 gr_utf32, chars, buffer->len,
277 2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0));
278
279 if (unlikely (!seg)) {
280 if (feats) gr_featureval_destroy (feats);
281 return false;
282 }
283
284 unsigned int glyph_count = gr_seg_n_slots (seg);
285 if (unlikely (!glyph_count)) {
286 if (feats) gr_featureval_destroy (feats);
287 gr_seg_destroy (seg);
288 buffer->len = 0;
289 return true;
290 }
291
292 (void) buffer->ensure (glyph_count);
293 scratch = buffer->get_scratch_buffer (&scratch_size);
294 while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) +
295 DIV_CEIL (sizeof (hb_codepoint_t) * glyph_count, sizeof (*scratch))) > scratch_size)
296 {
297 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
298 {
299 if (feats) gr_featureval_destroy (feats);
300 gr_seg_destroy (seg);
301 return false;
302 }
303 scratch = buffer->get_scratch_buffer (&scratch_size);
304 }
305
306 #define ALLOCATE_ARRAY(Type, name, len) \
307 Type *name = (Type *) scratch; \
308 do { \
309 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
310 assert (_consumed <= scratch_size); \
311 scratch += _consumed; \
312 scratch_size -= _consumed; \
313 } while (0)
314
315 ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len);
316 ALLOCATE_ARRAY (hb_codepoint_t, gids, glyph_count);
317
318 #undef ALLOCATE_ARRAY
319
320 memset (clusters, 0, sizeof (clusters[0]) * buffer->len);
321
322 hb_codepoint_t *pg = gids;
323 clusters[0].cluster = buffer->info[0].cluster;
324 unsigned int upem = hb_face_get_upem (face);
325 float xscale = (float) font->x_scale / upem;
326 float yscale = (float) font->y_scale / upem;
327 yscale *= yscale / xscale;
328 unsigned int curradv = 0;
329 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
330 {
331 curradv = gr_slot_origin_X(gr_seg_first_slot(seg)) * xscale;
332 clusters[0].advance = gr_seg_advance_X(seg) * xscale - curradv;
333 }
334 else
335 clusters[0].advance = 0;
336 for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++)
337 {
338 unsigned int before = gr_slot_before (is);
339 unsigned int after = gr_slot_after (is);
340 *pg = gr_slot_gid (is);
341 pg++;
342 while (clusters[ci].base_char > before && ci)
343 {
344 clusters[ci-1].num_chars += clusters[ci].num_chars;
345 clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
346 clusters[ci-1].advance += clusters[ci].advance;
347 ci--;
348 }
349
350 if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
351 {
352 hb_graphite2_cluster_t *c = clusters + ci + 1;
353 c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
354 c->cluster = buffer->info[c->base_char].cluster;
355 c->num_chars = before - c->base_char;
356 c->base_glyph = ic;
357 c->num_glyphs = 0;
358 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
359 {
360 c->advance = curradv - gr_slot_origin_X(is) * xscale;
361 curradv -= c->advance;
362 }
363 else
364 {
365 c->advance = 0;
366 clusters[ci].advance += gr_slot_origin_X(is) * xscale - curradv;
367 curradv += clusters[ci].advance;
368 }
369 ci++;
370 }
371 clusters[ci].num_glyphs++;
372
373 if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
374 clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
375 }
376
377 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
378 clusters[ci].advance += curradv;
379 else
380 clusters[ci].advance += gr_seg_advance_X(seg) * xscale - curradv;
381 ci++;
382
383 for (unsigned int i = 0; i < ci; ++i)
384 {
385 for (unsigned int j = 0; j < clusters[i].num_glyphs; ++j)
386 {
387 hb_glyph_info_t *info = &buffer->info[clusters[i].base_glyph + j];
388 info->codepoint = gids[clusters[i].base_glyph + j];
389 info->cluster = clusters[i].cluster;
390 info->var1.i32 = clusters[i].advance; // all glyphs in the cluster get the same advance
391 }
392 }
393 buffer->len = glyph_count;
394
395 /* Positioning. */
396 unsigned int currclus = UINT_MAX;
397 const hb_glyph_info_t *info = buffer->info;
398 hb_glyph_position_t *pPos = hb_buffer_get_glyph_positions (buffer, nullptr);
399 if (!HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
400 {
401 curradvx = 0;
402 for (is = gr_seg_first_slot (seg); is; pPos++, ++info, is = gr_slot_next_in_segment (is))
403 {
404 pPos->x_offset = gr_slot_origin_X (is) * xscale - curradvx;
405 pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
406 if (info->cluster != currclus) {
407 pPos->x_advance = info->var1.i32;
408 curradvx += pPos->x_advance;
409 currclus = info->cluster;
410 } else
411 pPos->x_advance = 0.;
412
413 pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
414 curradvy += pPos->y_advance;
415 }
416 }
417 else
418 {
419 curradvx = gr_seg_advance_X(seg) * xscale;
420 for (is = gr_seg_first_slot (seg); is; pPos++, info++, is = gr_slot_next_in_segment (is))
421 {
422 if (info->cluster != currclus)
423 {
424 pPos->x_advance = info->var1.i32;
425 curradvx -= pPos->x_advance;
426 currclus = info->cluster;
427 } else
428 pPos->x_advance = 0.;
429
430 pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
431 curradvy -= pPos->y_advance;
432 pPos->x_offset = gr_slot_origin_X (is) * xscale - info->var1.i32 - curradvx + pPos->x_advance;
433 pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
434 }
435 hb_buffer_reverse_clusters (buffer);
436 }
437
438 if (feats) gr_featureval_destroy (feats);
439 gr_seg_destroy (seg);
440
441 buffer->unsafe_to_break_all ();
442
443 return true;
444 }
445
446
447 #endif
448