blob: 0e373d30f92c2b5b5d2b5dedebfee6e0561fa8f3 [file] [log] [blame]
Behdad Esfahbod90364842014-03-24 14:26:36 -07001/*
2 * Copyright © 2011,2014 Google, Inc.
3 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
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.
11 *
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.
17 *
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.
23 *
24 * Google Author(s): Behdad Esfahbod, Roozbeh Pournader
25 */
26
27#include "hb-private.hh"
28
29#include "hb-ot.h"
30
31#include "hb-font-private.hh"
32
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -040033#include "hb-ot-cmap-table.hh"
Behdad Esfahbodb50fcfa2015-08-23 14:42:20 +010034#include "hb-ot-glyf-table.hh"
Behdad Esfahbod90364842014-03-24 14:26:36 -070035#include "hb-ot-hmtx-table.hh"
Behdad Esfahbod2a16f642017-11-01 17:31:29 -060036#include "hb-ot-kern-table.hh"
Khaled Hosny9d4d2fb2017-10-16 10:05:42 +020037#include "hb-ot-post-table.hh"
Behdad Esfahbod90364842014-03-24 14:26:36 -070038
Ebrahim Byagowi0ef6ab22018-03-04 02:47:26 +033039#include "hb-ot-color-cbdt-table.hh"
40
Behdad Esfahbod90364842014-03-24 14:26:36 -070041
Behdad Esfahbod90364842014-03-24 14:26:36 -070042struct hb_ot_font_t
43{
Behdad Esfahbod977ddff2017-11-14 20:06:19 -080044 OT::cmap::accelerator_t cmap;
Behdad Esfahbodeab4feb2017-11-14 20:16:45 -080045 OT::hmtx::accelerator_t h_metrics;
46 OT::vmtx::accelerator_t v_metrics;
Behdad Esfahbod7e2839c2017-11-14 19:52:09 -080047 OT::hb_lazy_loader_t<OT::glyf::accelerator_t> glyf;
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -080048 OT::hb_lazy_loader_t<OT::CBDT::accelerator_t> cbdt;
Behdad Esfahbod9b04b032017-11-14 19:31:50 -080049 OT::hb_lazy_loader_t<OT::post::accelerator_t> post;
Behdad Esfahbod702d86b2017-11-14 19:25:38 -080050 OT::hb_lazy_loader_t<OT::kern::accelerator_t> kern;
Behdad Esfahbod90364842014-03-24 14:26:36 -070051};
52
53
54static hb_ot_font_t *
Behdad Esfahbod3224a592015-10-07 17:33:02 -040055_hb_ot_font_create (hb_face_t *face)
Behdad Esfahbod90364842014-03-24 14:26:36 -070056{
57 hb_ot_font_t *ot_font = (hb_ot_font_t *) calloc (1, sizeof (hb_ot_font_t));
58
59 if (unlikely (!ot_font))
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +020060 return nullptr;
Behdad Esfahbod90364842014-03-24 14:26:36 -070061
Behdad Esfahbod15685292014-09-25 17:45:49 +030062 ot_font->cmap.init (face);
Behdad Esfahbodeab4feb2017-11-14 20:16:45 -080063 ot_font->h_metrics.init (face);
64 ot_font->v_metrics.init (face, ot_font->h_metrics.ascender - ot_font->h_metrics.descender); /* TODO Can we do this lazily? */
Behdad Esfahbodb50fcfa2015-08-23 14:42:20 +010065 ot_font->glyf.init (face);
Behdad Esfahbodefca7bf2016-12-02 15:11:37 -080066 ot_font->cbdt.init (face);
Khaled Hosny9d4d2fb2017-10-16 10:05:42 +020067 ot_font->post.init (face);
Behdad Esfahbod77acc112017-11-01 19:33:09 -060068 ot_font->kern.init (face);
Behdad Esfahbod3608a682014-05-12 13:46:29 -040069
Behdad Esfahbod90364842014-03-24 14:26:36 -070070 return ot_font;
71}
72
73static void
Behdad Esfahbode1b6d922017-10-11 15:51:31 +020074_hb_ot_font_destroy (void *data)
Behdad Esfahbod90364842014-03-24 14:26:36 -070075{
Behdad Esfahbode1b6d922017-10-11 15:51:31 +020076 hb_ot_font_t *ot_font = (hb_ot_font_t *) data;
77
Behdad Esfahbod15685292014-09-25 17:45:49 +030078 ot_font->cmap.fini ();
Behdad Esfahbodbe1cca22014-09-25 16:53:24 +030079 ot_font->h_metrics.fini ();
80 ot_font->v_metrics.fini ();
Behdad Esfahboded13e2c2015-10-13 10:32:56 -030081 ot_font->glyf.fini ();
Behdad Esfahbodefca7bf2016-12-02 15:11:37 -080082 ot_font->cbdt.fini ();
Khaled Hosny9d4d2fb2017-10-16 10:05:42 +020083 ot_font->post.fini ();
Behdad Esfahbod77acc112017-11-01 19:33:09 -060084 ot_font->kern.fini ();
Behdad Esfahbod90364842014-03-24 14:26:36 -070085
86 free (ot_font);
87}
88
89
90static hb_bool_t
Behdad Esfahbod8b5bc142016-02-24 19:05:23 +090091hb_ot_get_nominal_glyph (hb_font_t *font HB_UNUSED,
92 void *font_data,
93 hb_codepoint_t unicode,
94 hb_codepoint_t *glyph,
95 void *user_data HB_UNUSED)
Behdad Esfahbod90364842014-03-24 14:26:36 -070096
97{
Behdad Esfahbod3608a682014-05-12 13:46:29 -040098 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
Behdad Esfahbod8b5bc142016-02-24 19:05:23 +090099 return ot_font->cmap.get_nominal_glyph (unicode, glyph);
100}
101
102static hb_bool_t
103hb_ot_get_variation_glyph (hb_font_t *font HB_UNUSED,
104 void *font_data,
105 hb_codepoint_t unicode,
106 hb_codepoint_t variation_selector,
107 hb_codepoint_t *glyph,
108 void *user_data HB_UNUSED)
109{
110 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
111 return ot_font->cmap.get_variation_glyph (unicode, variation_selector, glyph);
Behdad Esfahbod90364842014-03-24 14:26:36 -0700112}
113
114static hb_position_t
Behdad Esfahbodbd3b11d2017-01-23 17:34:44 -0800115hb_ot_get_glyph_h_advance (hb_font_t *font,
Behdad Esfahbod90364842014-03-24 14:26:36 -0700116 void *font_data,
117 hb_codepoint_t glyph,
118 void *user_data HB_UNUSED)
119{
120 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
Behdad Esfahbodbd3b11d2017-01-23 17:34:44 -0800121 return font->em_scale_x (ot_font->h_metrics.get_advance (glyph, font));
Behdad Esfahbod90364842014-03-24 14:26:36 -0700122}
123
124static hb_position_t
Behdad Esfahbodbd3b11d2017-01-23 17:34:44 -0800125hb_ot_get_glyph_v_advance (hb_font_t *font,
Behdad Esfahbod90364842014-03-24 14:26:36 -0700126 void *font_data,
127 hb_codepoint_t glyph,
128 void *user_data HB_UNUSED)
129{
Behdad Esfahbodd41b8092014-09-25 13:04:08 +0300130 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
Behdad Esfahbodbd3b11d2017-01-23 17:34:44 -0800131 return font->em_scale_y (-(int) ot_font->v_metrics.get_advance (glyph, font));
Behdad Esfahbod90364842014-03-24 14:26:36 -0700132}
133
Behdad Esfahbod77acc112017-11-01 19:33:09 -0600134static hb_position_t
135hb_ot_get_glyph_h_kerning (hb_font_t *font,
136 void *font_data,
137 hb_codepoint_t left_glyph,
138 hb_codepoint_t right_glyph,
139 void *user_data HB_UNUSED)
140{
141 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
142 return font->em_scale_x (ot_font->kern->get_h_kerning (left_glyph, right_glyph));
143}
144
Behdad Esfahbod90364842014-03-24 14:26:36 -0700145static hb_bool_t
Behdad Esfahbod90364842014-03-24 14:26:36 -0700146hb_ot_get_glyph_extents (hb_font_t *font HB_UNUSED,
147 void *font_data,
148 hb_codepoint_t glyph,
149 hb_glyph_extents_t *extents,
150 void *user_data HB_UNUSED)
151{
Behdad Esfahbodb50fcfa2015-08-23 14:42:20 +0100152 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
Behdad Esfahbodf00ab2a2016-05-02 10:24:00 +0200153 bool ret = ot_font->glyf->get_extents (glyph, extents);
Behdad Esfahbod4b58c9e2016-12-02 19:25:54 -0800154 if (!ret)
Behdad Esfahbodefca7bf2016-12-02 15:11:37 -0800155 ret = ot_font->cbdt->get_extents (glyph, extents);
Behdad Esfahbod79e8e272017-01-23 17:55:31 -0800156 // TODO Hook up side-bearings variations.
Behdad Esfahbodb50fcfa2015-08-23 14:42:20 +0100157 extents->x_bearing = font->em_scale_x (extents->x_bearing);
158 extents->y_bearing = font->em_scale_y (extents->y_bearing);
159 extents->width = font->em_scale_x (extents->width);
160 extents->height = font->em_scale_y (extents->height);
161 return ret;
Behdad Esfahbod90364842014-03-24 14:26:36 -0700162}
163
Simon Cozens6f2e6de2015-10-26 16:23:22 +0900164static hb_bool_t
Khaled Hosny9d4d2fb2017-10-16 10:05:42 +0200165hb_ot_get_glyph_name (hb_font_t *font HB_UNUSED,
166 void *font_data,
167 hb_codepoint_t glyph,
168 char *name, unsigned int size,
169 void *user_data HB_UNUSED)
170{
171 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
172 return ot_font->post->get_glyph_name (glyph, name, size);
173}
174
175static hb_bool_t
Khaled Hosnyd9e166f2017-10-18 20:49:16 +0200176hb_ot_get_glyph_from_name (hb_font_t *font HB_UNUSED,
177 void *font_data,
178 const char *name, int len,
179 hb_codepoint_t *glyph,
180 void *user_data HB_UNUSED)
181{
182 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
183 return ot_font->post->get_glyph_from_name (name, len, glyph);
184}
185
186static hb_bool_t
Simon Cozens6f2e6de2015-10-26 16:23:22 +0900187hb_ot_get_font_h_extents (hb_font_t *font HB_UNUSED,
188 void *font_data,
189 hb_font_extents_t *metrics,
190 void *user_data HB_UNUSED)
191{
192 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
193 metrics->ascender = font->em_scale_y (ot_font->h_metrics.ascender);
194 metrics->descender = font->em_scale_y (ot_font->h_metrics.descender);
195 metrics->line_gap = font->em_scale_y (ot_font->h_metrics.line_gap);
Behdad Esfahbod79e8e272017-01-23 17:55:31 -0800196 // TODO Hook up variations.
Behdad Esfahbodb3b08162016-10-26 17:19:07 +0200197 return ot_font->h_metrics.has_font_extents;
Simon Cozens6f2e6de2015-10-26 16:23:22 +0900198}
199
200static hb_bool_t
201hb_ot_get_font_v_extents (hb_font_t *font HB_UNUSED,
202 void *font_data,
203 hb_font_extents_t *metrics,
204 void *user_data HB_UNUSED)
205{
206 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
207 metrics->ascender = font->em_scale_x (ot_font->v_metrics.ascender);
208 metrics->descender = font->em_scale_x (ot_font->v_metrics.descender);
209 metrics->line_gap = font->em_scale_x (ot_font->v_metrics.line_gap);
Behdad Esfahbod79e8e272017-01-23 17:55:31 -0800210 // TODO Hook up variations.
Behdad Esfahbodb3b08162016-10-26 17:19:07 +0200211 return ot_font->v_metrics.has_font_extents;
Simon Cozens6f2e6de2015-10-26 16:23:22 +0900212}
Behdad Esfahbod90364842014-03-24 14:26:36 -0700213
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200214static hb_font_funcs_t *static_ot_funcs = nullptr;
Behdad Esfahbod75ea2da2015-11-04 20:43:59 -0800215
216#ifdef HB_USE_ATEXIT
217static
218void free_static_ot_funcs (void)
219{
220 hb_font_funcs_destroy (static_ot_funcs);
221}
222#endif
223
Behdad Esfahbod90364842014-03-24 14:26:36 -0700224static hb_font_funcs_t *
225_hb_ot_get_font_funcs (void)
226{
Behdad Esfahbod75ea2da2015-11-04 20:43:59 -0800227retry:
228 hb_font_funcs_t *funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ot_funcs);
Behdad Esfahbod90364842014-03-24 14:26:36 -0700229
Behdad Esfahbod75ea2da2015-11-04 20:43:59 -0800230 if (unlikely (!funcs))
231 {
232 funcs = hb_font_funcs_create ();
Behdad Esfahbod90364842014-03-24 14:26:36 -0700233
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200234 hb_font_funcs_set_font_h_extents_func (funcs, hb_ot_get_font_h_extents, nullptr, nullptr);
235 hb_font_funcs_set_font_v_extents_func (funcs, hb_ot_get_font_v_extents, nullptr, nullptr);
236 hb_font_funcs_set_nominal_glyph_func (funcs, hb_ot_get_nominal_glyph, nullptr, nullptr);
237 hb_font_funcs_set_variation_glyph_func (funcs, hb_ot_get_variation_glyph, nullptr, nullptr);
238 hb_font_funcs_set_glyph_h_advance_func (funcs, hb_ot_get_glyph_h_advance, nullptr, nullptr);
239 hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ot_get_glyph_v_advance, nullptr, nullptr);
240 //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ot_get_glyph_h_origin, nullptr, nullptr);
241 //hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ot_get_glyph_v_origin, nullptr, nullptr);
Behdad Esfahbod4b3278e2017-11-01 19:41:29 -0600242 hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ot_get_glyph_h_kerning, nullptr, nullptr);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200243 //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ot_get_glyph_v_kerning, nullptr, nullptr);
244 hb_font_funcs_set_glyph_extents_func (funcs, hb_ot_get_glyph_extents, nullptr, nullptr);
Behdad Esfahbod7b408762017-11-14 20:22:05 -0800245 //hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ot_get_glyph_contour_point, nullptr, nullptr);
Khaled Hosny9d4d2fb2017-10-16 10:05:42 +0200246 hb_font_funcs_set_glyph_name_func (funcs, hb_ot_get_glyph_name, nullptr, nullptr);
Khaled Hosnyd9e166f2017-10-18 20:49:16 +0200247 hb_font_funcs_set_glyph_from_name_func (funcs, hb_ot_get_glyph_from_name, nullptr, nullptr);
Behdad Esfahbod75ea2da2015-11-04 20:43:59 -0800248
249 hb_font_funcs_make_immutable (funcs);
250
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200251 if (!hb_atomic_ptr_cmpexch (&static_ot_funcs, nullptr, funcs)) {
Behdad Esfahbod75ea2da2015-11-04 20:43:59 -0800252 hb_font_funcs_destroy (funcs);
253 goto retry;
Behdad Esfahbod90364842014-03-24 14:26:36 -0700254 }
Behdad Esfahbod75ea2da2015-11-04 20:43:59 -0800255
256#ifdef HB_USE_ATEXIT
257 atexit (free_static_ot_funcs); /* First person registers atexit() callback. */
258#endif
Behdad Esfahbod90364842014-03-24 14:26:36 -0700259 };
260
Behdad Esfahbod75ea2da2015-11-04 20:43:59 -0800261 return funcs;
Behdad Esfahbod90364842014-03-24 14:26:36 -0700262}
263
264
Sascha Brawer01c3a882015-06-01 13:22:01 +0200265/**
Behdad Esfahbod35d18582015-11-26 19:30:37 -0500266 * hb_ot_font_set_funcs:
267 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200268 * Since: 0.9.28
269 **/
Behdad Esfahbod90364842014-03-24 14:26:36 -0700270void
271hb_ot_font_set_funcs (hb_font_t *font)
272{
Behdad Esfahbod3224a592015-10-07 17:33:02 -0400273 hb_ot_font_t *ot_font = _hb_ot_font_create (font->face);
Behdad Esfahbod90364842014-03-24 14:26:36 -0700274 if (unlikely (!ot_font))
275 return;
276
277 hb_font_set_funcs (font,
278 _hb_ot_get_font_funcs (),
279 ot_font,
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200280 _hb_ot_font_destroy);
Behdad Esfahbod90364842014-03-24 14:26:36 -0700281}