Add a Notification Settings Button to all web notifications behind the web platform...
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-ot-font.cc
blobdf6514dd31f1c91bf490ba9ecea2542f96fb6f6c
1 /*
2 * Copyright © 2011,2014 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod, Roozbeh Pournader
27 #include "hb-private.hh"
29 #include "hb-ot.h"
31 #include "hb-font-private.hh"
33 #include "hb-ot-cmap-table.hh"
34 #include "hb-ot-hhea-table.hh"
35 #include "hb-ot-hmtx-table.hh"
38 struct hb_ot_face_metrics_accelerator_t
40 unsigned int num_metrics;
41 unsigned int num_advances;
42 unsigned int default_advance;
43 const OT::_mtx *table;
44 hb_blob_t *blob;
46 inline void init (hb_face_t *face,
47 hb_tag_t _hea_tag, hb_tag_t _mtx_tag,
48 unsigned int default_advance_)
50 this->default_advance = default_advance_;
51 this->num_metrics = face->get_num_glyphs ();
53 hb_blob_t *_hea_blob = OT::Sanitizer<OT::_hea>::sanitize (face->reference_table (_hea_tag));
54 const OT::_hea *_hea = OT::Sanitizer<OT::_hea>::lock_instance (_hea_blob);
55 this->num_advances = _hea->numberOfLongMetrics;
56 hb_blob_destroy (_hea_blob);
58 this->blob = OT::Sanitizer<OT::_mtx>::sanitize (face->reference_table (_mtx_tag));
59 if (unlikely (!this->num_advances ||
60 2 * (this->num_advances + this->num_metrics) < hb_blob_get_length (this->blob)))
62 this->num_metrics = this->num_advances = 0;
63 hb_blob_destroy (this->blob);
64 this->blob = hb_blob_get_empty ();
66 this->table = OT::Sanitizer<OT::_mtx>::lock_instance (this->blob);
69 inline void fini (void)
71 hb_blob_destroy (this->blob);
74 inline unsigned int get_advance (hb_codepoint_t glyph) const
76 if (unlikely (glyph >= this->num_metrics))
78 /* If this->num_metrics is zero, it means we don't have the metrics table
79 * for this direction: return one EM. Otherwise, it means that the glyph
80 * index is out of bound: return zero. */
81 if (this->num_metrics)
82 return 0;
83 else
84 return this->default_advance;
87 if (glyph >= this->num_advances)
88 glyph = this->num_advances - 1;
90 return this->table->longMetric[glyph].advance;
94 struct hb_ot_face_cmap_accelerator_t
96 const OT::CmapSubtable *table;
97 const OT::CmapSubtable *uvs_table;
98 hb_blob_t *blob;
100 inline void init (hb_face_t *face)
102 this->blob = OT::Sanitizer<OT::cmap>::sanitize (face->reference_table (HB_OT_TAG_cmap));
103 const OT::cmap *cmap = OT::Sanitizer<OT::cmap>::lock_instance (this->blob);
104 const OT::CmapSubtable *subtable = NULL;
105 const OT::CmapSubtable *subtable_uvs = NULL;
107 /* 32-bit subtables. */
108 if (!subtable) subtable = cmap->find_subtable (3, 10);
109 if (!subtable) subtable = cmap->find_subtable (0, 6);
110 if (!subtable) subtable = cmap->find_subtable (0, 4);
111 /* 16-bit subtables. */
112 if (!subtable) subtable = cmap->find_subtable (3, 1);
113 if (!subtable) subtable = cmap->find_subtable (0, 3);
114 if (!subtable) subtable = cmap->find_subtable (0, 2);
115 if (!subtable) subtable = cmap->find_subtable (0, 1);
116 if (!subtable) subtable = cmap->find_subtable (0, 0);
117 if (!subtable) subtable = cmap->find_subtable (3, 0);
118 /* Meh. */
119 if (!subtable) subtable = &OT::Null(OT::CmapSubtable);
121 /* UVS subtable. */
122 if (!subtable_uvs) subtable_uvs = cmap->find_subtable (0, 5);
123 /* Meh. */
124 if (!subtable_uvs) subtable_uvs = &OT::Null(OT::CmapSubtable);
126 this->table = subtable;
127 this->uvs_table = subtable_uvs;
130 inline void fini (void)
132 hb_blob_destroy (this->blob);
135 inline bool get_glyph (hb_codepoint_t unicode,
136 hb_codepoint_t variation_selector,
137 hb_codepoint_t *glyph) const
139 if (unlikely (variation_selector))
141 switch (this->uvs_table->get_glyph_variant (unicode,
142 variation_selector,
143 glyph))
145 case OT::GLYPH_VARIANT_NOT_FOUND: return false;
146 case OT::GLYPH_VARIANT_FOUND: return true;
147 case OT::GLYPH_VARIANT_USE_DEFAULT: break;
151 return this->table->get_glyph (unicode, glyph);
156 struct hb_ot_font_t
158 hb_ot_face_cmap_accelerator_t cmap;
159 hb_ot_face_metrics_accelerator_t h_metrics;
160 hb_ot_face_metrics_accelerator_t v_metrics;
164 static hb_ot_font_t *
165 _hb_ot_font_create (hb_font_t *font)
167 hb_ot_font_t *ot_font = (hb_ot_font_t *) calloc (1, sizeof (hb_ot_font_t));
168 hb_face_t *face = font->face;
170 if (unlikely (!ot_font))
171 return NULL;
173 unsigned int upem = face->get_upem ();
175 ot_font->cmap.init (face);
176 ot_font->h_metrics.init (face, HB_OT_TAG_hhea, HB_OT_TAG_hmtx, upem>>1);
177 ot_font->v_metrics.init (face, HB_OT_TAG_vhea, HB_OT_TAG_vmtx, upem); /* TODO Can we do this lazily? */
179 return ot_font;
182 static void
183 _hb_ot_font_destroy (hb_ot_font_t *ot_font)
185 ot_font->cmap.fini ();
186 ot_font->h_metrics.fini ();
187 ot_font->v_metrics.fini ();
189 free (ot_font);
193 static hb_bool_t
194 hb_ot_get_glyph (hb_font_t *font HB_UNUSED,
195 void *font_data,
196 hb_codepoint_t unicode,
197 hb_codepoint_t variation_selector,
198 hb_codepoint_t *glyph,
199 void *user_data HB_UNUSED)
202 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
203 return ot_font->cmap.get_glyph (unicode, variation_selector, glyph);
206 static hb_position_t
207 hb_ot_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
208 void *font_data,
209 hb_codepoint_t glyph,
210 void *user_data HB_UNUSED)
212 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
213 return font->em_scale_x (ot_font->h_metrics.get_advance (glyph));
216 static hb_position_t
217 hb_ot_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
218 void *font_data,
219 hb_codepoint_t glyph,
220 void *user_data HB_UNUSED)
222 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
223 return font->em_scale_y (-(int) ot_font->v_metrics.get_advance (glyph));
226 static hb_bool_t
227 hb_ot_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
228 void *font_data HB_UNUSED,
229 hb_codepoint_t glyph HB_UNUSED,
230 hb_position_t *x HB_UNUSED,
231 hb_position_t *y HB_UNUSED,
232 void *user_data HB_UNUSED)
234 /* We always work in the horizontal coordinates. */
235 return true;
238 static hb_bool_t
239 hb_ot_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
240 void *font_data,
241 hb_codepoint_t glyph,
242 hb_position_t *x,
243 hb_position_t *y,
244 void *user_data HB_UNUSED)
246 /* TODO */
247 return false;
250 static hb_position_t
251 hb_ot_get_glyph_h_kerning (hb_font_t *font,
252 void *font_data,
253 hb_codepoint_t left_glyph,
254 hb_codepoint_t right_glyph,
255 void *user_data HB_UNUSED)
257 /* TODO */
258 return 0;
261 static hb_position_t
262 hb_ot_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
263 void *font_data HB_UNUSED,
264 hb_codepoint_t top_glyph HB_UNUSED,
265 hb_codepoint_t bottom_glyph HB_UNUSED,
266 void *user_data HB_UNUSED)
268 /* OpenType doesn't have vertical-kerning other than GPOS. */
269 return 0;
272 static hb_bool_t
273 hb_ot_get_glyph_extents (hb_font_t *font HB_UNUSED,
274 void *font_data,
275 hb_codepoint_t glyph,
276 hb_glyph_extents_t *extents,
277 void *user_data HB_UNUSED)
279 /* TODO */
280 return false;
283 static hb_bool_t
284 hb_ot_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
285 void *font_data,
286 hb_codepoint_t glyph,
287 unsigned int point_index,
288 hb_position_t *x,
289 hb_position_t *y,
290 void *user_data HB_UNUSED)
292 /* TODO */
293 return false;
296 static hb_bool_t
297 hb_ot_get_glyph_name (hb_font_t *font HB_UNUSED,
298 void *font_data,
299 hb_codepoint_t glyph,
300 char *name, unsigned int size,
301 void *user_data HB_UNUSED)
303 /* TODO */
304 return false;
307 static hb_bool_t
308 hb_ot_get_glyph_from_name (hb_font_t *font HB_UNUSED,
309 void *font_data,
310 const char *name, int len, /* -1 means nul-terminated */
311 hb_codepoint_t *glyph,
312 void *user_data HB_UNUSED)
314 /* TODO */
315 return false;
319 static hb_font_funcs_t *
320 _hb_ot_get_font_funcs (void)
322 static const hb_font_funcs_t ot_ffuncs = {
323 HB_OBJECT_HEADER_STATIC,
325 true, /* immutable */
328 #define HB_FONT_FUNC_IMPLEMENT(name) hb_ot_get_##name,
329 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
330 #undef HB_FONT_FUNC_IMPLEMENT
334 return const_cast<hb_font_funcs_t *> (&ot_ffuncs);
339 * Since: 0.9.28
341 void
342 hb_ot_font_set_funcs (hb_font_t *font)
344 hb_ot_font_t *ot_font = _hb_ot_font_create (font);
345 if (unlikely (!ot_font))
346 return;
348 hb_font_set_funcs (font,
349 _hb_ot_get_font_funcs (),
350 ot_font,
351 (hb_destroy_func_t) _hb_ot_font_destroy);