Add git cl format presubmit warning for extension and apps.
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-graphite2.cc
blob60d68d6b57a6aa69d65ec11940315524bbafaa81
1 /*
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
18 * DAMAGE.
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 #define hb_graphite2_shaper_font_data_t gr_font
31 #include "hb-shaper-impl-private.hh"
33 #include "hb-graphite2.h"
35 #include <graphite2/Segment.h>
37 #include "hb-ot-tag.h"
40 HB_SHAPER_DATA_ENSURE_DECLARE(graphite2, face)
41 HB_SHAPER_DATA_ENSURE_DECLARE(graphite2, font)
45 * shaper face data
48 typedef struct hb_graphite2_tablelist_t {
49 struct hb_graphite2_tablelist_t *next;
50 hb_blob_t *blob;
51 unsigned int tag;
52 } hb_graphite2_tablelist_t;
54 struct hb_graphite2_shaper_face_data_t {
55 hb_face_t *face;
56 gr_face *grface;
57 hb_graphite2_tablelist_t *tlist;
60 static const void *hb_graphite2_get_table (const void *data, unsigned int tag, size_t *len)
62 hb_graphite2_shaper_face_data_t *face_data = (hb_graphite2_shaper_face_data_t *) data;
63 hb_graphite2_tablelist_t *tlist = face_data->tlist;
65 hb_blob_t *blob = NULL;
67 for (hb_graphite2_tablelist_t *p = tlist; p; p = p->next)
68 if (p->tag == tag) {
69 blob = p->blob;
70 break;
73 if (unlikely (!blob))
75 blob = face_data->face->reference_table (tag);
77 hb_graphite2_tablelist_t *p = (hb_graphite2_tablelist_t *) calloc (1, sizeof (hb_graphite2_tablelist_t));
78 if (unlikely (!p)) {
79 hb_blob_destroy (blob);
80 return NULL;
82 p->blob = blob;
83 p->tag = tag;
85 /* TODO Not thread-safe, but fairly harmless.
86 * We can do the double-chcked pointer cmpexch thing here. */
87 p->next = face_data->tlist;
88 face_data->tlist = p;
91 unsigned int tlen;
92 const char *d = hb_blob_get_data (blob, &tlen);
93 *len = tlen;
94 return d;
97 hb_graphite2_shaper_face_data_t *
98 _hb_graphite2_shaper_face_data_create (hb_face_t *face)
100 hb_blob_t *silf_blob = face->reference_table (HB_GRAPHITE2_TAG_SILF);
101 /* Umm, we just reference the table to check whether it exists.
102 * Maybe add better API for this? */
103 if (!hb_blob_get_length (silf_blob))
105 hb_blob_destroy (silf_blob);
106 return NULL;
108 hb_blob_destroy (silf_blob);
110 hb_graphite2_shaper_face_data_t *data = (hb_graphite2_shaper_face_data_t *) calloc (1, sizeof (hb_graphite2_shaper_face_data_t));
111 if (unlikely (!data))
112 hb_blob_destroy (silf_blob);
114 data->face = face;
115 data->grface = gr_make_face (data, &hb_graphite2_get_table, gr_face_preloadAll);
117 if (unlikely (!data->grface)) {
118 free (data);
119 return NULL;
122 return data;
125 void
126 _hb_graphite2_shaper_face_data_destroy (hb_graphite2_shaper_face_data_t *data)
128 hb_graphite2_tablelist_t *tlist = data->tlist;
130 while (tlist)
132 hb_graphite2_tablelist_t *old = tlist;
133 hb_blob_destroy (tlist->blob);
134 tlist = tlist->next;
135 free (old);
138 gr_face_destroy (data->grface);
140 free (data);
143 gr_face *
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;
152 * shaper font data
155 static float hb_graphite2_get_advance (const void *hb_font, unsigned short gid)
157 return ((hb_font_t *) hb_font)->get_glyph_h_advance (gid);
160 hb_graphite2_shaper_font_data_t *
161 _hb_graphite2_shaper_font_data_create (hb_font_t *font)
163 if (unlikely (!hb_graphite2_shaper_face_data_ensure (font->face))) return NULL;
165 hb_face_t *face = font->face;
166 hb_graphite2_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
168 return gr_make_font_with_advance_fn (font->x_scale, font, &hb_graphite2_get_advance, face_data->grface);
171 void
172 _hb_graphite2_shaper_font_data_destroy (hb_graphite2_shaper_font_data_t *data)
174 gr_font_destroy (data);
177 gr_font *
178 hb_graphite2_font_get_gr_font (hb_font_t *font)
180 if (unlikely (!hb_graphite2_shaper_font_data_ensure (font))) return NULL;
181 return HB_SHAPER_DATA_GET (font);
186 * shaper shape_plan data
189 struct hb_graphite2_shaper_shape_plan_data_t {};
191 hb_graphite2_shaper_shape_plan_data_t *
192 _hb_graphite2_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
193 const hb_feature_t *user_features HB_UNUSED,
194 unsigned int num_user_features HB_UNUSED)
196 return (hb_graphite2_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
199 void
200 _hb_graphite2_shaper_shape_plan_data_destroy (hb_graphite2_shaper_shape_plan_data_t *data HB_UNUSED)
206 * shaper
209 struct hb_graphite2_cluster_t {
210 unsigned int base_char;
211 unsigned int num_chars;
212 unsigned int base_glyph;
213 unsigned int num_glyphs;
216 hb_bool_t
217 _hb_graphite2_shape (hb_shape_plan_t *shape_plan,
218 hb_font_t *font,
219 hb_buffer_t *buffer,
220 const hb_feature_t *features,
221 unsigned int num_features)
223 hb_face_t *face = font->face;
224 gr_face *grface = HB_SHAPER_DATA_GET (face)->grface;
225 gr_font *grfont = HB_SHAPER_DATA_GET (font);
227 const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
228 const char *lang_end = lang ? strchr (lang, '-') : NULL;
229 int lang_len = lang_end ? lang_end - lang : -1;
230 gr_feature_val *feats = gr_face_featureval_for_lang (grface, lang ? hb_tag_from_string (lang, lang_len) : 0);
232 while (num_features--)
234 const gr_feature_ref *fref = gr_face_find_fref (grface, features->tag);
235 if (fref)
236 gr_fref_set_feature_value (fref, features->value, feats);
237 features++;
240 gr_segment *seg = NULL;
241 const gr_slot *is;
242 unsigned int ci = 0, ic = 0;
243 float curradvx = 0., curradvy = 0.;
245 unsigned int scratch_size;
246 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
248 uint32_t *chars = (uint32_t *) scratch;
250 for (unsigned int i = 0; i < buffer->len; ++i)
251 chars[i] = buffer->info[i].codepoint;
253 hb_tag_t script_tag[2];
254 hb_ot_tags_from_script (hb_buffer_get_script (buffer), &script_tag[0], &script_tag[1]);
256 seg = gr_make_seg (grfont, grface,
257 script_tag[1] == HB_TAG_NONE ? script_tag[0] : script_tag[1],
258 feats,
259 gr_utf32, chars, buffer->len,
260 2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0));
262 if (unlikely (!seg)) {
263 if (feats) gr_featureval_destroy (feats);
264 return false;
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 return false;
274 scratch = buffer->get_scratch_buffer (&scratch_size);
275 while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) +
276 DIV_CEIL (sizeof (hb_codepoint_t) * glyph_count, sizeof (*scratch))) > scratch_size)
278 buffer->ensure (buffer->allocated * 2);
279 if (unlikely (buffer->in_error)) {
280 if (feats) gr_featureval_destroy (feats);
281 gr_seg_destroy (seg);
282 return false;
284 scratch = buffer->get_scratch_buffer (&scratch_size);
287 #define ALLOCATE_ARRAY(Type, name, len) \
288 Type *name = (Type *) scratch; \
290 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
291 assert (_consumed <= scratch_size); \
292 scratch += _consumed; \
293 scratch_size -= _consumed; \
296 ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len);
297 ALLOCATE_ARRAY (hb_codepoint_t, gids, glyph_count);
299 #undef ALLOCATE_ARRAY
301 memset (clusters, 0, sizeof (clusters[0]) * buffer->len);
303 hb_codepoint_t *pg = gids;
304 for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++)
306 unsigned int before = gr_slot_before (is);
307 unsigned int after = gr_slot_after (is);
308 *pg = gr_slot_gid (is);
309 pg++;
310 while (clusters[ci].base_char > before && ci)
312 clusters[ci-1].num_chars += clusters[ci].num_chars;
313 clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
314 ci--;
317 if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
319 hb_graphite2_cluster_t *c = clusters + ci + 1;
320 c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
321 c->num_chars = before - c->base_char;
322 c->base_glyph = ic;
323 c->num_glyphs = 0;
324 ci++;
326 clusters[ci].num_glyphs++;
328 if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
329 clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
331 ci++;
333 //buffer->clear_output ();
334 for (unsigned int i = 0; i < ci; ++i)
336 for (unsigned int j = 0; j < clusters[i].num_glyphs; ++j)
338 hb_glyph_info_t *info = &buffer->info[clusters[i].base_glyph + j];
339 info->codepoint = gids[clusters[i].base_glyph + j];
340 info->cluster = gr_cinfo_base(gr_seg_cinfo(seg, clusters[i].base_char));
343 buffer->len = glyph_count;
344 //buffer->swap_buffers ();
346 if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
347 curradvx = gr_seg_advance_X(seg);
349 hb_glyph_position_t *pPos;
350 for (pPos = hb_buffer_get_glyph_positions (buffer, NULL), is = gr_seg_first_slot (seg);
351 is; pPos++, is = gr_slot_next_in_segment (is))
353 pPos->x_offset = gr_slot_origin_X (is) - curradvx;
354 pPos->y_offset = gr_slot_origin_Y (is) - curradvy;
355 pPos->x_advance = gr_slot_advance_X (is, grface, grfont);
356 pPos->y_advance = gr_slot_advance_Y (is, grface, grfont);
357 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
358 curradvx -= pPos->x_advance;
359 pPos->x_offset = gr_slot_origin_X (is) - curradvx;
360 if (!HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
361 curradvx += pPos->x_advance;
362 pPos->y_offset = gr_slot_origin_Y (is) - curradvy;
363 curradvy += pPos->y_advance;
365 if (!HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
366 pPos[-1].x_advance += gr_seg_advance_X(seg) - curradvx;
368 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
369 hb_buffer_reverse_clusters (buffer);
371 if (feats) gr_featureval_destroy (feats);
372 gr_seg_destroy (seg);
374 return true;