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-shaper-impl.hh"
30
31 #include "hb-graphite2.h"
32
33 #include <graphite2/Segment.h>
34
35 #include "hb-ot-layout.h"
36
37
38 /**
39 * SECTION:hb-graphite2
40 * @title: hb-graphite2
41 * @short_description: Graphite2 integration
42 * @include: hb-graphite2.h
43 *
44 * Functions for using HarfBuzz with the Graphite2 fonts.
45 **/
46
47
48 /*
49 * shaper face data
50 */
51
52 typedef struct hb_graphite2_tablelist_t
53 {
54 struct hb_graphite2_tablelist_t *next;
55 hb_blob_t *blob;
56 unsigned int tag;
57 } hb_graphite2_tablelist_t;
58
59 struct hb_graphite2_face_data_t
60 {
61 hb_face_t *face;
62 gr_face *grface;
63 hb_atomic_ptr_t<hb_graphite2_tablelist_t> tlist;
64 };
65
hb_graphite2_get_table(const void * data,unsigned int tag,size_t * len)66 static const void *hb_graphite2_get_table (const void *data, unsigned int tag, size_t *len)
67 {
68 hb_graphite2_face_data_t *face_data = (hb_graphite2_face_data_t *) data;
69 hb_graphite2_tablelist_t *tlist = face_data->tlist;
70
71 hb_blob_t *blob = nullptr;
72
73 for (hb_graphite2_tablelist_t *p = tlist; p; p = p->next)
74 if (p->tag == tag) {
75 blob = p->blob;
76 break;
77 }
78
79 if (unlikely (!blob))
80 {
81 blob = face_data->face->reference_table (tag);
82
83 hb_graphite2_tablelist_t *p = (hb_graphite2_tablelist_t *) calloc (1, sizeof (hb_graphite2_tablelist_t));
84 if (unlikely (!p)) {
85 hb_blob_destroy (blob);
86 return nullptr;
87 }
88 p->blob = blob;
89 p->tag = tag;
90
91 retry:
92 hb_graphite2_tablelist_t *tlist = face_data->tlist;
93 p->next = tlist;
94
95 if (unlikely (!face_data->tlist.cmpexch (tlist, p)))
96 goto retry;
97 }
98
99 unsigned int tlen;
100 const char *d = hb_blob_get_data (blob, &tlen);
101 *len = tlen;
102 return d;
103 }
104
hb_graphite2_release_table(const void * data,const void * table_buffer)105 static void hb_graphite2_release_table(const void *data, const void *table_buffer)
106 {
107 hb_graphite2_face_data_t *face_data = (hb_graphite2_face_data_t *) data;
108 hb_graphite2_tablelist_t *tlist = face_data->tlist;
109
110 hb_graphite2_tablelist_t *prev = nullptr;
111 hb_graphite2_tablelist_t *curr = tlist;
112 while (curr)
113 {
114 if (hb_blob_get_data(curr->blob, nullptr) == table_buffer)
115 {
116 if (prev == nullptr)
117 face_data->tlist.cmpexch(tlist, curr->next);
118 else
119 prev->next = curr->next;
120 hb_blob_destroy(curr->blob);
121 free(curr);
122 break;
123 }
124 prev = curr;
125 curr = curr->next;
126 }
127 }
128
129 static gr_face_ops hb_graphite2_face_ops = { sizeof(gr_face_ops), hb_graphite2_get_table, hb_graphite2_release_table };
130
131 hb_graphite2_face_data_t *
_hb_graphite2_shaper_face_data_create(hb_face_t * face)132 _hb_graphite2_shaper_face_data_create (hb_face_t *face)
133 {
134 hb_blob_t *silf_blob = face->reference_table (HB_GRAPHITE2_TAG_SILF);
135 /* Umm, we just reference the table to check whether it exists.
136 * Maybe add better API for this? */
137 if (!hb_blob_get_length (silf_blob))
138 {
139 hb_blob_destroy (silf_blob);
140 return nullptr;
141 }
142 hb_blob_destroy (silf_blob);
143
144 hb_graphite2_face_data_t *data = (hb_graphite2_face_data_t *) calloc (1, sizeof (hb_graphite2_face_data_t));
145 if (unlikely (!data))
146 return nullptr;
147
148 data->face = face;
149 data->grface = gr_make_face_with_ops (data, &hb_graphite2_face_ops, gr_face_preloadAll);
150
151 if (unlikely (!data->grface)) {
152 free (data);
153 return nullptr;
154 }
155
156 return data;
157 }
158
159 void
_hb_graphite2_shaper_face_data_destroy(hb_graphite2_face_data_t * data)160 _hb_graphite2_shaper_face_data_destroy (hb_graphite2_face_data_t *data)
161 {
162 hb_graphite2_tablelist_t *tlist = data->tlist;
163
164 while (tlist)
165 {
166 hb_graphite2_tablelist_t *old = tlist;
167 hb_blob_destroy (tlist->blob);
168 tlist = tlist->next;
169 free (old);
170 }
171
172 gr_face_destroy (data->grface);
173
174 free (data);
175 }
176
177 /*
178 * Since: 0.9.10
179 */
180 gr_face *
hb_graphite2_face_get_gr_face(hb_face_t * face)181 hb_graphite2_face_get_gr_face (hb_face_t *face)
182 {
183 const hb_graphite2_face_data_t *data = face->data.graphite2;
184 return data ? data->grface : nullptr;
185 }
186
187
188 /*
189 * shaper font data
190 */
191
192 struct hb_graphite2_font_data_t {};
193
194 hb_graphite2_font_data_t *
_hb_graphite2_shaper_font_data_create(hb_font_t * font HB_UNUSED)195 _hb_graphite2_shaper_font_data_create (hb_font_t *font HB_UNUSED)
196 {
197 return (hb_graphite2_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
198 }
199
200 void
_hb_graphite2_shaper_font_data_destroy(hb_graphite2_font_data_t * data HB_UNUSED)201 _hb_graphite2_shaper_font_data_destroy (hb_graphite2_font_data_t *data HB_UNUSED)
202 {
203 }
204
205 /**
206 * hb_graphite2_font_get_gr_font:
207 *
208 * Since: 0.9.10
209 * Deprecated: 1.4.2
210 */
211 gr_font *
hb_graphite2_font_get_gr_font(hb_font_t * font HB_UNUSED)212 hb_graphite2_font_get_gr_font (hb_font_t *font HB_UNUSED)
213 {
214 return nullptr;
215 }
216
217
218 /*
219 * shaper
220 */
221
222 struct hb_graphite2_cluster_t {
223 unsigned int base_char;
224 unsigned int num_chars;
225 unsigned int base_glyph;
226 unsigned int num_glyphs;
227 unsigned int cluster;
228 float advance;
229 };
230
231 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)232 _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED,
233 hb_font_t *font,
234 hb_buffer_t *buffer,
235 const hb_feature_t *features,
236 unsigned int num_features)
237 {
238 hb_face_t *face = font->face;
239 gr_face *grface = face->data.graphite2->grface;
240
241 const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
242 const char *lang_end = lang ? strchr (lang, '-') : nullptr;
243 int lang_len = lang_end ? lang_end - lang : -1;
244 gr_feature_val *feats = gr_face_featureval_for_lang (grface, lang ? hb_tag_from_string (lang, lang_len) : 0);
245
246 for (unsigned int i = 0; i < num_features; i++)
247 {
248 const gr_feature_ref *fref = gr_face_find_fref (grface, features[i].tag);
249 if (fref)
250 gr_fref_set_feature_value (fref, features[i].value, feats);
251 }
252
253 gr_segment *seg = nullptr;
254 const gr_slot *is;
255 unsigned int ci = 0, ic = 0;
256 float curradvx = 0., curradvy = 0.;
257
258 unsigned int scratch_size;
259 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
260
261 uint32_t *chars = (uint32_t *) scratch;
262
263 for (unsigned int i = 0; i < buffer->len; ++i)
264 chars[i] = buffer->info[i].codepoint;
265
266 /* TODO ensure_native_direction. */
267
268 hb_tag_t script_tag[HB_OT_MAX_TAGS_PER_SCRIPT];
269 unsigned int count = HB_OT_MAX_TAGS_PER_SCRIPT;
270 hb_ot_tags_from_script_and_language (hb_buffer_get_script (buffer),
271 HB_LANGUAGE_INVALID,
272 &count,
273 script_tag,
274 nullptr, nullptr);
275
276 seg = gr_make_seg (nullptr, grface,
277 count ? script_tag[count - 1] : HB_OT_TAG_DEFAULT_SCRIPT,
278 feats,
279 gr_utf32, chars, buffer->len,
280 2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0));
281
282 if (unlikely (!seg)) {
283 if (feats) gr_featureval_destroy (feats);
284 return false;
285 }
286
287 unsigned int glyph_count = gr_seg_n_slots (seg);
288 if (unlikely (!glyph_count)) {
289 if (feats) gr_featureval_destroy (feats);
290 gr_seg_destroy (seg);
291 buffer->len = 0;
292 return true;
293 }
294
295 buffer->ensure (glyph_count);
296 scratch = buffer->get_scratch_buffer (&scratch_size);
297 while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) +
298 DIV_CEIL (sizeof (hb_codepoint_t) * glyph_count, sizeof (*scratch))) > scratch_size)
299 {
300 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
301 {
302 if (feats) gr_featureval_destroy (feats);
303 gr_seg_destroy (seg);
304 return false;
305 }
306 scratch = buffer->get_scratch_buffer (&scratch_size);
307 }
308
309 #define ALLOCATE_ARRAY(Type, name, len) \
310 Type *name = (Type *) scratch; \
311 { \
312 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
313 assert (_consumed <= scratch_size); \
314 scratch += _consumed; \
315 scratch_size -= _consumed; \
316 }
317
318 ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len);
319 ALLOCATE_ARRAY (hb_codepoint_t, gids, glyph_count);
320
321 #undef ALLOCATE_ARRAY
322
323 memset (clusters, 0, sizeof (clusters[0]) * buffer->len);
324
325 hb_codepoint_t *pg = gids;
326 clusters[0].cluster = buffer->info[0].cluster;
327 float curradv = 0.;
328 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
329 {
330 curradv = gr_slot_origin_X(gr_seg_first_slot(seg));
331 clusters[0].advance = gr_seg_advance_X(seg) - curradv;
332 }
333 else
334 clusters[0].advance = 0;
335 for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++)
336 {
337 unsigned int before = gr_slot_before (is);
338 unsigned int after = gr_slot_after (is);
339 *pg = gr_slot_gid (is);
340 pg++;
341 while (clusters[ci].base_char > before && ci)
342 {
343 clusters[ci-1].num_chars += clusters[ci].num_chars;
344 clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
345 clusters[ci-1].advance += clusters[ci].advance;
346 ci--;
347 }
348
349 if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
350 {
351 hb_graphite2_cluster_t *c = clusters + ci + 1;
352 c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
353 c->cluster = buffer->info[c->base_char].cluster;
354 c->num_chars = before - c->base_char;
355 c->base_glyph = ic;
356 c->num_glyphs = 0;
357 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
358 c->advance = curradv - gr_slot_origin_X(is);
359 else
360 {
361 c->advance = 0;
362 clusters[ci].advance += gr_slot_origin_X(is) - curradv;
363 }
364 ci++;
365 curradv = gr_slot_origin_X(is);
366 }
367 clusters[ci].num_glyphs++;
368
369 if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
370 clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
371 }
372
373 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
374 clusters[ci].advance += curradv;
375 else
376 clusters[ci].advance += gr_seg_advance_X(seg) - curradv;
377 ci++;
378
379 for (unsigned int i = 0; i < ci; ++i)
380 {
381 for (unsigned int j = 0; j < clusters[i].num_glyphs; ++j)
382 {
383 hb_glyph_info_t *info = &buffer->info[clusters[i].base_glyph + j];
384 info->codepoint = gids[clusters[i].base_glyph + j];
385 info->cluster = clusters[i].cluster;
386 info->var1.i32 = clusters[i].advance; // all glyphs in the cluster get the same advance
387 }
388 }
389 buffer->len = glyph_count;
390
391 unsigned int upem = hb_face_get_upem (face);
392 float xscale = (float) font->x_scale / upem;
393 float yscale = (float) font->y_scale / upem;
394 yscale *= yscale / xscale;
395 /* Positioning. */
396 unsigned int currclus = (unsigned int) -1;
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 * xscale;
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 * xscale;
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) - info->var1.i32) * xscale - 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