1 /*
2  * Copyright © 2016  Igalia S.L.
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  * Igalia Author(s): Frédéric Wang
25  */
26 
27 #include "hb-open-type-private.hh"
28 
29 #include "hb-ot-layout-math-table.hh"
30 
HB_SHAPER_DATA_ENSURE_DECLARE(ot,face) const31 HB_SHAPER_DATA_ENSURE_DECLARE(ot, face)
32 
33 static inline const OT::MATH&
34 _get_math (hb_face_t *face)
35 {
36   if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::MATH);
37 
38   hb_ot_layout_t * layout = hb_ot_layout_from_face (face);
39 
40 retry:
41   const OT::MATH *math = (const OT::MATH *) hb_atomic_ptr_get (&layout->math);
42 
43   if (unlikely (!math))
44   {
45     hb_blob_t *blob = OT::Sanitizer<OT::MATH>::sanitize (face->reference_table (HB_OT_TAG_MATH));
46     math = OT::Sanitizer<OT::MATH>::lock_instance (blob);
47     if (!hb_atomic_ptr_cmpexch (&layout->math, NULL, math))
48     {
49       hb_blob_destroy (blob);
50       goto retry;
51     }
52     layout->math_blob = blob;
53   }
54 
55   return *math;
56 }
57 
58 /*
59  * OT::MATH
60  */
61 
62 /**
63  * hb_ot_math_has_data:
64  * @face: #hb_face_t to test
65  *
66  * This function allows to verify the presence of an OpenType MATH table on the
67  * face. If so, such a table will be loaded into memory and sanitized. You can
68  * then safely call other functions for math layout and shaping.
69  *
70  * Return value: #TRUE if face has a MATH table and #FALSE otherwise
71  *
72  * Since: 1.3.3
73  **/
74 hb_bool_t
hb_ot_math_has_data(hb_face_t * face)75 hb_ot_math_has_data (hb_face_t *face)
76 {
77   return &_get_math (face) != &OT::Null(OT::MATH);
78 }
79 
80 /**
81  * hb_ot_math_get_constant:
82  * @font: #hb_font_t from which to retrieve the value
83  * @constant: #hb_ot_math_constant_t the constant to retrieve
84  *
85  * This function returns the requested math constants as a #hb_position_t.
86  * If the request constant is HB_OT_MATH_CONSTANT_SCRIPT_PERCENT_SCALE_DOWN,
87  * HB_OT_MATH_CONSTANT_SCRIPT_SCRIPT_PERCENT_SCALE_DOWN or
88  * HB_OT_MATH_CONSTANT_SCRIPT_PERCENT_SCALE_DOWN then the return value is
89  * actually an integer between 0 and 100 representing that percentage.
90  *
91  * Return value: the requested constant or 0
92  *
93  * Since: 1.3.3
94  **/
95 hb_position_t
hb_ot_math_get_constant(hb_font_t * font,hb_ot_math_constant_t constant)96 hb_ot_math_get_constant (hb_font_t *font,
97 			 hb_ot_math_constant_t constant)
98 {
99   const OT::MATH &math = _get_math (font->face);
100   return math.get_constant(constant, font);
101 }
102 
103 /**
104  * hb_ot_math_get_glyph_italics_correction:
105  * @font: #hb_font_t from which to retrieve the value
106  * @glyph: glyph index from which to retrieve the value
107  *
108  * Return value: the italics correction of the glyph or 0
109  *
110  * Since: 1.3.3
111  **/
112 hb_position_t
hb_ot_math_get_glyph_italics_correction(hb_font_t * font,hb_codepoint_t glyph)113 hb_ot_math_get_glyph_italics_correction (hb_font_t *font,
114 					 hb_codepoint_t glyph)
115 {
116   const OT::MATH &math = _get_math (font->face);
117   return math.get_math_glyph_info().get_italics_correction (glyph, font);
118 }
119 
120 /**
121  * hb_ot_math_get_glyph_top_accent_attachment:
122  * @font: #hb_font_t from which to retrieve the value
123  * @glyph: glyph index from which to retrieve the value
124  *
125  * Return value: the top accent attachment of the glyph or 0
126  *
127  * Since: 1.3.3
128  **/
129 hb_position_t
hb_ot_math_get_glyph_top_accent_attachment(hb_font_t * font,hb_codepoint_t glyph)130 hb_ot_math_get_glyph_top_accent_attachment (hb_font_t *font,
131 					    hb_codepoint_t glyph)
132 {
133   const OT::MATH &math = _get_math (font->face);
134   return math.get_math_glyph_info().get_top_accent_attachment (glyph, font);
135 }
136 
137 /**
138  * hb_ot_math_is_glyph_extended_shape:
139  * @font: a #hb_font_t to test
140  * @glyph: a glyph index to test
141  *
142  * Return value: #TRUE if the glyph is an extended shape and #FALSE otherwise
143  *
144  * Since: 1.3.3
145  **/
146 hb_bool_t
hb_ot_math_is_glyph_extended_shape(hb_face_t * face,hb_codepoint_t glyph)147 hb_ot_math_is_glyph_extended_shape (hb_face_t *face,
148 				    hb_codepoint_t glyph)
149 {
150   const OT::MATH &math = _get_math (face);
151   return math.get_math_glyph_info().is_extended_shape (glyph);
152 }
153 
154 /**
155  * hb_ot_math_get_glyph_kerning:
156  * @font: #hb_font_t from which to retrieve the value
157  * @glyph: glyph index from which to retrieve the value
158  * @kern: the #hb_ot_math_kern_t from which to retrieve the value
159  * @correction_height: the correction height to use to determine the kerning.
160  *
161  * This function tries to retrieve the MathKern table for the specified font,
162  * glyph and #hb_ot_math_kern_t. Then it browses the list of heights from the
163  * MathKern table to find one value that is greater or equal to specified
164  * correction_height. If one is found the corresponding value from the list of
165  * kerns is returned and otherwise the last kern value is returned.
166  *
167  * Return value: requested kerning or 0
168  *
169  * Since: 1.3.3
170  **/
171 hb_position_t
hb_ot_math_get_glyph_kerning(hb_font_t * font,hb_codepoint_t glyph,hb_ot_math_kern_t kern,hb_position_t correction_height)172 hb_ot_math_get_glyph_kerning (hb_font_t *font,
173 			      hb_codepoint_t glyph,
174 			      hb_ot_math_kern_t kern,
175 			      hb_position_t correction_height)
176 {
177   const OT::MATH &math = _get_math (font->face);
178   return math.get_math_glyph_info().get_kerning (glyph, kern, correction_height, font);
179 }
180 
181 /**
182  * hb_ot_math_get_glyph_variants:
183  * @font: #hb_font_t from which to retrieve the values
184  * @glyph: index of the glyph to stretch
185  * @direction: direction of the stretching
186  * @start_offset: offset of the first variant to retrieve
187  * @variants_count: maximum number of variants to retrieve after start_offset
188  * (IN) and actual number of variants retrieved (OUT)
189  * @variants: array of size at least @variants_count to store the result
190  *
191  * This function tries to retrieve the MathGlyphConstruction for the specified
192  * font, glyph and direction. Note that only the value of
193  * #HB_DIRECTION_IS_HORIZONTAL is considered. It provides the corresponding list
194  * of size variants as an array of hb_ot_math_glyph_variant_t structs.
195  *
196  * Return value: the total number of size variants available or 0
197  *
198  * Since: 1.3.3
199  **/
200 unsigned int
hb_ot_math_get_glyph_variants(hb_font_t * font,hb_codepoint_t glyph,hb_direction_t direction,unsigned int start_offset,unsigned int * variants_count,hb_ot_math_glyph_variant_t * variants)201 hb_ot_math_get_glyph_variants (hb_font_t *font,
202 			       hb_codepoint_t glyph,
203 			       hb_direction_t direction,
204 			       unsigned int start_offset,
205 			       unsigned int *variants_count, /* IN/OUT */
206 			       hb_ot_math_glyph_variant_t *variants /* OUT */)
207 {
208   const OT::MATH &math = _get_math (font->face);
209   return math.get_math_variants().get_glyph_variants (glyph, direction, font,
210 						      start_offset,
211 						      variants_count,
212 						      variants);
213 }
214 
215 /**
216  * hb_ot_math_get_min_connector_overlap:
217  * @font: #hb_font_t from which to retrieve the value
218  * @direction: direction of the stretching
219  *
220  * This function tries to retrieve the MathVariants table for the specified
221  * font and returns the minimum overlap of connecting glyphs to draw a glyph
222  * assembly in the specified direction. Note that only the value of
223  * #HB_DIRECTION_IS_HORIZONTAL is considered.
224  *
225  * Return value: requested min connector overlap or 0
226  *
227  * Since: 1.3.3
228  **/
229 hb_position_t
hb_ot_math_get_min_connector_overlap(hb_font_t * font,hb_direction_t direction)230 hb_ot_math_get_min_connector_overlap (hb_font_t *font,
231 				      hb_direction_t direction)
232 {
233   const OT::MATH &math = _get_math (font->face);
234   return math.get_math_variants().get_min_connector_overlap (direction, font);
235 }
236 
237 /**
238  * hb_ot_math_get_glyph_assembly:
239  * @font: #hb_font_t from which to retrieve the values
240  * @glyph: index of the glyph to stretch
241  * @direction: direction of the stretching
242  * @start_offset: offset of the first glyph part to retrieve
243  * @parts_count: maximum number of glyph parts to retrieve after start_offset
244  * (IN) and actual number of parts retrieved (OUT)
245  * @parts: array of size at least @parts_count to store the result
246  * @italics_correction: italic correction of the glyph assembly
247  *
248  * This function tries to retrieve the GlyphAssembly for the specified font,
249  * glyph and direction. Note that only the value of #HB_DIRECTION_IS_HORIZONTAL
250  * is considered. It provides the information necessary to draw the glyph
251  * assembly as an array of #hb_ot_math_glyph_part_t.
252  *
253  * Return value: the total number of parts in the glyph assembly
254  *
255  * Since: 1.3.3
256  **/
257 unsigned int
hb_ot_math_get_glyph_assembly(hb_font_t * font,hb_codepoint_t glyph,hb_direction_t direction,unsigned int start_offset,unsigned int * parts_count,hb_ot_math_glyph_part_t * parts,hb_position_t * italics_correction)258 hb_ot_math_get_glyph_assembly (hb_font_t *font,
259 			       hb_codepoint_t glyph,
260 			       hb_direction_t direction,
261 			       unsigned int start_offset,
262 			       unsigned int *parts_count, /* IN/OUT */
263 			       hb_ot_math_glyph_part_t *parts, /* OUT */
264 			       hb_position_t *italics_correction /* OUT */)
265 {
266   const OT::MATH &math = _get_math (font->face);
267   return math.get_math_variants().get_glyph_parts (glyph, direction, font,
268 						   start_offset,
269 						   parts_count,
270 						   parts,
271 						   italics_correction);
272 }
273