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
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"
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"
41 unsigned int num_glyphs;
42 unsigned int num_hmetrics;
46 const OT::CmapSubtable *cmap;
47 const OT::CmapSubtable *cmap_uvs;
53 _hb_ot_font_create (hb_font_t *font)
55 hb_ot_font_t *ot_font = (hb_ot_font_t *) calloc (1, sizeof (hb_ot_font_t));
57 if (unlikely (!ot_font))
60 ot_font->num_glyphs = font->face->get_num_glyphs ();
63 hb_blob_t *hhea_blob = OT::Sanitizer<OT::hhea>::sanitize (font->face->reference_table (HB_OT_TAG_hhea));
64 const OT::hhea *hhea = OT::Sanitizer<OT::hhea>::lock_instance (hhea_blob);
65 ot_font->num_hmetrics = hhea->numberOfHMetrics;
66 hb_blob_destroy (hhea_blob);
68 ot_font->hmtx_blob = OT::Sanitizer<OT::hmtx>::sanitize (font->face->reference_table (HB_OT_TAG_hmtx));
69 if (unlikely (!ot_font->num_hmetrics ||
70 2 * (ot_font->num_hmetrics + ot_font->num_glyphs) < hb_blob_get_length (ot_font->hmtx_blob)))
72 hb_blob_destroy (ot_font->hmtx_blob);
76 ot_font->hmtx = OT::Sanitizer<OT::hmtx>::lock_instance (ot_font->hmtx_blob);
78 ot_font->cmap_blob = OT::Sanitizer<OT::cmap>::sanitize (font->face->reference_table (HB_OT_TAG_cmap));
79 const OT::cmap *cmap = OT::Sanitizer<OT::cmap>::lock_instance (ot_font->cmap_blob);
80 const OT::CmapSubtable *subtable = NULL;
81 const OT::CmapSubtable *subtable_uvs = NULL;
83 /* 32-bit subtables. */
84 if (!subtable) subtable = cmap->find_subtable (0, 6);
85 if (!subtable) subtable = cmap->find_subtable (0, 4);
86 if (!subtable) subtable = cmap->find_subtable (3, 10);
87 /* 16-bit subtables. */
88 if (!subtable) subtable = cmap->find_subtable (0, 3);
89 if (!subtable) subtable = cmap->find_subtable (3, 1);
91 if (!subtable) subtable = &OT::Null(OT::CmapSubtable);
94 if (!subtable_uvs) subtable_uvs = cmap->find_subtable (0, 5);
96 if (!subtable_uvs) subtable_uvs = &OT::Null(OT::CmapSubtable);
98 ot_font->cmap = subtable;
99 ot_font->cmap_uvs = subtable_uvs;
105 _hb_ot_font_destroy (hb_ot_font_t *ot_font)
107 hb_blob_destroy (ot_font->cmap_blob);
108 hb_blob_destroy (ot_font->hmtx_blob);
115 hb_ot_get_glyph (hb_font_t *font HB_UNUSED,
117 hb_codepoint_t unicode,
118 hb_codepoint_t variation_selector,
119 hb_codepoint_t *glyph,
120 void *user_data HB_UNUSED)
123 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
125 if (unlikely (variation_selector))
127 switch (ot_font->cmap_uvs->get_glyph_variant (unicode,
131 case OT::GLYPH_VARIANT_NOT_FOUND: return false;
132 case OT::GLYPH_VARIANT_FOUND: return true;
133 case OT::GLYPH_VARIANT_USE_DEFAULT: break;
137 return ot_font->cmap->get_glyph (unicode, glyph);
141 hb_ot_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
143 hb_codepoint_t glyph,
144 void *user_data HB_UNUSED)
146 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
148 if (unlikely (glyph >= ot_font->num_glyphs))
149 return 0; /* Maybe better to return notdef's advance instead? */
151 if (glyph >= ot_font->num_hmetrics)
152 glyph = ot_font->num_hmetrics - 1;
154 return font->em_scale_x (ot_font->hmtx->longHorMetric[glyph].advanceWidth);
158 hb_ot_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
160 hb_codepoint_t glyph,
161 void *user_data HB_UNUSED)
168 hb_ot_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
169 void *font_data HB_UNUSED,
170 hb_codepoint_t glyph HB_UNUSED,
171 hb_position_t *x HB_UNUSED,
172 hb_position_t *y HB_UNUSED,
173 void *user_data HB_UNUSED)
175 /* We always work in the horizontal coordinates. */
180 hb_ot_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
182 hb_codepoint_t glyph,
185 void *user_data HB_UNUSED)
192 hb_ot_get_glyph_h_kerning (hb_font_t *font,
194 hb_codepoint_t left_glyph,
195 hb_codepoint_t right_glyph,
196 void *user_data HB_UNUSED)
203 hb_ot_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
204 void *font_data HB_UNUSED,
205 hb_codepoint_t top_glyph HB_UNUSED,
206 hb_codepoint_t bottom_glyph HB_UNUSED,
207 void *user_data HB_UNUSED)
213 hb_ot_get_glyph_extents (hb_font_t *font HB_UNUSED,
215 hb_codepoint_t glyph,
216 hb_glyph_extents_t *extents,
217 void *user_data HB_UNUSED)
224 hb_ot_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
226 hb_codepoint_t glyph,
227 unsigned int point_index,
230 void *user_data HB_UNUSED)
237 hb_ot_get_glyph_name (hb_font_t *font HB_UNUSED,
239 hb_codepoint_t glyph,
240 char *name, unsigned int size,
241 void *user_data HB_UNUSED)
248 hb_ot_get_glyph_from_name (hb_font_t *font HB_UNUSED,
250 const char *name, int len, /* -1 means nul-terminated */
251 hb_codepoint_t *glyph,
252 void *user_data HB_UNUSED)
259 static hb_font_funcs_t *
260 _hb_ot_get_font_funcs (void)
262 static const hb_font_funcs_t ot_ffuncs = {
263 HB_OBJECT_HEADER_STATIC,
265 true, /* immutable */
268 #define HB_FONT_FUNC_IMPLEMENT(name) hb_ot_get_##name,
269 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
270 #undef HB_FONT_FUNC_IMPLEMENT
274 return const_cast<hb_font_funcs_t *> (&ot_ffuncs);
279 hb_ot_font_set_funcs (hb_font_t *font)
281 hb_ot_font_t *ot_font = _hb_ot_font_create (font);
282 if (unlikely (!ot_font))
285 hb_font_set_funcs (font,
286 _hb_ot_get_font_funcs (),
288 (hb_destroy_func_t) _hb_ot_font_destroy);