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