1 | /* |
2 | * Copyright © 2011,2012 Google, Inc. |
3 | * Copyright © 2018 Ebrahim Byagowi |
4 | * |
5 | * This is part of HarfBuzz, a text shaping library. |
6 | * |
7 | * Permission is hereby granted, without written agreement and without |
8 | * license or royalty fees, to use, copy, modify, and distribute this |
9 | * software and its documentation for any purpose, provided that the |
10 | * above copyright notice and the following two paragraphs appear in |
11 | * all copies of this software. |
12 | * |
13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
14 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
15 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
16 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
17 | * DAMAGE. |
18 | * |
19 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
20 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
21 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
22 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
23 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
24 | * |
25 | * Google Author(s): Behdad Esfahbod |
26 | */ |
27 | |
28 | #ifndef HB_OT_OS2_TABLE_HH |
29 | #define HB_OT_OS2_TABLE_HH |
30 | |
31 | #include "hb-open-type.hh" |
32 | #include "hb-ot-os2-unicode-ranges.hh" |
33 | #include "hb-ot-var-mvar-table.hh" |
34 | |
35 | #include "hb-set.hh" |
36 | |
37 | /* |
38 | * OS/2 and Windows Metrics |
39 | * https://docs.microsoft.com/en-us/typography/opentype/spec/os2 |
40 | */ |
41 | #define HB_OT_TAG_OS2 HB_TAG('O','S','/','2') |
42 | |
43 | |
44 | namespace OT { |
45 | |
46 | struct OS2V1Tail |
47 | { |
48 | bool sanitize (hb_sanitize_context_t *c) const |
49 | { |
50 | TRACE_SANITIZE (this); |
51 | return_trace (c->check_struct (this)); |
52 | } |
53 | |
54 | public: |
55 | HBUINT32 ; |
56 | HBUINT32 ; |
57 | public: |
58 | DEFINE_SIZE_STATIC (8); |
59 | }; |
60 | |
61 | struct OS2V2Tail |
62 | { |
63 | bool has_data () const { return sxHeight || sCapHeight; } |
64 | |
65 | const OS2V2Tail * operator -> () const { return this; } |
66 | OS2V2Tail * operator -> () { return this; } |
67 | |
68 | bool sanitize (hb_sanitize_context_t *c) const |
69 | { |
70 | TRACE_SANITIZE (this); |
71 | return_trace (c->check_struct (this)); |
72 | } |
73 | |
74 | public: |
75 | HBINT16 sxHeight; |
76 | HBINT16 sCapHeight; |
77 | HBUINT16 usDefaultChar; |
78 | HBUINT16 usBreakChar; |
79 | HBUINT16 usMaxContext; |
80 | public: |
81 | DEFINE_SIZE_STATIC (10); |
82 | }; |
83 | |
84 | struct OS2V5Tail |
85 | { |
86 | inline bool get_optical_size (unsigned int *lower, unsigned int *upper) const |
87 | { |
88 | unsigned int lower_optical_size = usLowerOpticalPointSize; |
89 | unsigned int upper_optical_size = usUpperOpticalPointSize; |
90 | |
91 | /* Per https://docs.microsoft.com/en-us/typography/opentype/spec/os2#lps */ |
92 | if (lower_optical_size < upper_optical_size && |
93 | lower_optical_size >= 1 && lower_optical_size <= 0xFFFE && |
94 | upper_optical_size >= 2 && upper_optical_size <= 0xFFFF) |
95 | { |
96 | *lower = lower_optical_size; |
97 | *upper = upper_optical_size; |
98 | return true; |
99 | } |
100 | return false; |
101 | } |
102 | |
103 | bool sanitize (hb_sanitize_context_t *c) const |
104 | { |
105 | TRACE_SANITIZE (this); |
106 | return_trace (c->check_struct (this)); |
107 | } |
108 | |
109 | public: |
110 | HBUINT16 usLowerOpticalPointSize; |
111 | HBUINT16 usUpperOpticalPointSize; |
112 | public: |
113 | DEFINE_SIZE_STATIC (4); |
114 | }; |
115 | |
116 | struct OS2 |
117 | { |
118 | static constexpr hb_tag_t tableTag = HB_OT_TAG_OS2; |
119 | |
120 | bool has_data () const { return usWeightClass || usWidthClass || usFirstCharIndex || usLastCharIndex; } |
121 | |
122 | const OS2V1Tail &v1 () const { return version >= 1 ? v1X : Null (OS2V1Tail); } |
123 | const OS2V2Tail &v2 () const { return version >= 2 ? v2X : Null (OS2V2Tail); } |
124 | const OS2V5Tail &v5 () const { return version >= 5 ? v5X : Null (OS2V5Tail); } |
125 | |
126 | enum selection_flag_t { |
127 | ITALIC = 1u<<0, |
128 | UNDERSCORE = 1u<<1, |
129 | NEGATIVE = 1u<<2, |
130 | OUTLINED = 1u<<3, |
131 | STRIKEOUT = 1u<<4, |
132 | BOLD = 1u<<5, |
133 | REGULAR = 1u<<6, |
134 | USE_TYPO_METRICS = 1u<<7, |
135 | WWS = 1u<<8, |
136 | OBLIQUE = 1u<<9 |
137 | }; |
138 | |
139 | bool is_italic () const { return fsSelection & ITALIC; } |
140 | bool is_oblique () const { return fsSelection & OBLIQUE; } |
141 | bool use_typo_metrics () const { return fsSelection & USE_TYPO_METRICS; } |
142 | |
143 | enum width_class_t { |
144 | FWIDTH_ULTRA_CONDENSED = 1, /* 50% */ |
145 | = 2, /* 62.5% */ |
146 | FWIDTH_CONDENSED = 3, /* 75% */ |
147 | FWIDTH_SEMI_CONDENSED = 4, /* 87.5% */ |
148 | FWIDTH_NORMAL = 5, /* 100% */ |
149 | FWIDTH_SEMI_EXPANDED = 6, /* 112.5% */ |
150 | FWIDTH_EXPANDED = 7, /* 125% */ |
151 | FWIDTH_EXTRA_EXPANDED = 8, /* 150% */ |
152 | FWIDTH_ULTRA_EXPANDED = 9 /* 200% */ |
153 | }; |
154 | |
155 | float get_width () const |
156 | { |
157 | switch (usWidthClass) { |
158 | case FWIDTH_ULTRA_CONDENSED:return 50.f; |
159 | case FWIDTH_EXTRA_CONDENSED:return 62.5f; |
160 | case FWIDTH_CONDENSED: return 75.f; |
161 | case FWIDTH_SEMI_CONDENSED: return 87.5f; |
162 | default: |
163 | case FWIDTH_NORMAL: return 100.f; |
164 | case FWIDTH_SEMI_EXPANDED: return 112.5f; |
165 | case FWIDTH_EXPANDED: return 125.f; |
166 | case FWIDTH_EXTRA_EXPANDED: return 150.f; |
167 | case FWIDTH_ULTRA_EXPANDED: return 200.f; |
168 | } |
169 | } |
170 | |
171 | float map_wdth_to_widthclass(float width) const |
172 | { |
173 | if (width < 50) return 1.0f; |
174 | if (width > 200) return 9.0f; |
175 | |
176 | float ratio = (width - 50) / 12.5f; |
177 | int a = (int) floorf (ratio); |
178 | int b = (int) ceilf (ratio); |
179 | |
180 | /* follow this maping: |
181 | * https://docs.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass |
182 | */ |
183 | if (b <= 6) // 50-125 |
184 | { |
185 | if (a == b) return a + 1.0f; |
186 | } |
187 | else if (b == 7) // no mapping for 137.5 |
188 | { |
189 | a = 6; |
190 | b = 8; |
191 | } |
192 | else if (b == 8) |
193 | { |
194 | if (a == b) return 8.0f; // 150 |
195 | a = 6; |
196 | } |
197 | else |
198 | { |
199 | if (a == b && a == 12) return 9.0f; //200 |
200 | b = 12; |
201 | a = 8; |
202 | } |
203 | |
204 | float va = 50 + a * 12.5f; |
205 | float vb = 50 + b * 12.5f; |
206 | |
207 | float ret = a + (width - va) / (vb - va); |
208 | if (a <= 6) ret += 1.0f; |
209 | return ret; |
210 | } |
211 | |
212 | bool subset (hb_subset_context_t *c) const |
213 | { |
214 | TRACE_SUBSET (this); |
215 | OS2 *os2_prime = c->serializer->embed (this); |
216 | if (unlikely (!os2_prime)) return_trace (false); |
217 | |
218 | #ifndef HB_NO_VAR |
219 | if (c->plan->normalized_coords) |
220 | { |
221 | auto &MVAR = *c->plan->source->table.MVAR; |
222 | auto *table = os2_prime; |
223 | |
224 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER, sTypoAscender); |
225 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER, sTypoDescender); |
226 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP, sTypoLineGap); |
227 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_HORIZONTAL_CLIPPING_ASCENT, usWinAscent); |
228 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_HORIZONTAL_CLIPPING_DESCENT, usWinDescent); |
229 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_SUBSCRIPT_EM_X_SIZE, ySubscriptXSize); |
230 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_SUBSCRIPT_EM_Y_SIZE, ySubscriptYSize); |
231 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_SUBSCRIPT_EM_X_OFFSET, ySubscriptXOffset); |
232 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_SUBSCRIPT_EM_Y_OFFSET, ySubscriptYOffset); |
233 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_SUPERSCRIPT_EM_X_SIZE, ySuperscriptXSize); |
234 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_SUPERSCRIPT_EM_Y_SIZE, ySuperscriptYSize); |
235 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_SUPERSCRIPT_EM_X_OFFSET, ySuperscriptXOffset); |
236 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_SUPERSCRIPT_EM_Y_OFFSET, ySuperscriptYOffset); |
237 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_STRIKEOUT_SIZE, yStrikeoutSize); |
238 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_STRIKEOUT_OFFSET, yStrikeoutPosition); |
239 | |
240 | if (os2_prime->version >= 2) |
241 | { |
242 | auto *table = & const_cast<OS2V2Tail &> (os2_prime->v2 ()); |
243 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_X_HEIGHT, sxHeight); |
244 | HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_CAP_HEIGHT, sCapHeight); |
245 | } |
246 | } |
247 | #endif |
248 | |
249 | if (c->plan->user_axes_location.has (HB_TAG ('w','g','h','t')) && |
250 | !c->plan->pinned_at_default) |
251 | { |
252 | float weight_class = c->plan->user_axes_location.get (HB_TAG ('w','g','h','t')).middle; |
253 | if (!c->serializer->check_assign (os2_prime->usWeightClass, |
254 | roundf (hb_clamp (weight_class, 1.0f, 1000.0f)), |
255 | HB_SERIALIZE_ERROR_INT_OVERFLOW)) |
256 | return_trace (false); |
257 | } |
258 | |
259 | if (c->plan->user_axes_location.has (HB_TAG ('w','d','t','h')) && |
260 | !c->plan->pinned_at_default) |
261 | { |
262 | float width = c->plan->user_axes_location.get (HB_TAG ('w','d','t','h')).middle; |
263 | if (!c->serializer->check_assign (os2_prime->usWidthClass, |
264 | roundf (map_wdth_to_widthclass (width)), |
265 | HB_SERIALIZE_ERROR_INT_OVERFLOW)) |
266 | return_trace (false); |
267 | } |
268 | |
269 | if (c->plan->flags & HB_SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES) |
270 | return_trace (true); |
271 | |
272 | os2_prime->usFirstCharIndex = hb_min (0xFFFFu, c->plan->unicodes.get_min ()); |
273 | os2_prime->usLastCharIndex = hb_min (0xFFFFu, c->plan->unicodes.get_max ()); |
274 | |
275 | _update_unicode_ranges (&c->plan->unicodes, os2_prime->ulUnicodeRange); |
276 | |
277 | return_trace (true); |
278 | } |
279 | |
280 | void _update_unicode_ranges (const hb_set_t *codepoints, |
281 | HBUINT32 ulUnicodeRange[4]) const |
282 | { |
283 | HBUINT32 newBits[4]; |
284 | for (unsigned int i = 0; i < 4; i++) |
285 | newBits[i] = 0; |
286 | |
287 | /* This block doesn't show up in profiles. If it ever did, |
288 | * we can rewrite it to iterate over OS/2 ranges and use |
289 | * set iteration to check if the range matches. */ |
290 | for (auto cp : *codepoints) |
291 | { |
292 | unsigned int bit = _hb_ot_os2_get_unicode_range_bit (cp); |
293 | if (bit < 128) |
294 | { |
295 | unsigned int block = bit / 32; |
296 | unsigned int bit_in_block = bit % 32; |
297 | unsigned int mask = 1 << bit_in_block; |
298 | newBits[block] = newBits[block] | mask; |
299 | } |
300 | if (cp >= 0x10000 && cp <= 0x110000) |
301 | { |
302 | /* the spec says that bit 57 ("Non Plane 0") implies that there's |
303 | at least one codepoint beyond the BMP; so I also include all |
304 | the non-BMP codepoints here */ |
305 | newBits[1] = newBits[1] | (1 << 25); |
306 | } |
307 | } |
308 | |
309 | for (unsigned int i = 0; i < 4; i++) |
310 | ulUnicodeRange[i] = ulUnicodeRange[i] & newBits[i]; // set bits only if set in the original |
311 | } |
312 | |
313 | /* https://github.com/Microsoft/Font-Validator/blob/520aaae/OTFontFileVal/val_OS2.cs#L644-L681 |
314 | * https://docs.microsoft.com/en-us/typography/legacy/legacy_arabic_fonts */ |
315 | enum font_page_t |
316 | { |
317 | FONT_PAGE_NONE = 0, |
318 | FONT_PAGE_HEBREW = 0xB100, /* Hebrew Windows 3.1 font page */ |
319 | FONT_PAGE_SIMP_ARABIC = 0xB200, /* Simplified Arabic Windows 3.1 font page */ |
320 | FONT_PAGE_TRAD_ARABIC = 0xB300, /* Traditional Arabic Windows 3.1 font page */ |
321 | FONT_PAGE_OEM_ARABIC = 0xB400, /* OEM Arabic Windows 3.1 font page */ |
322 | FONT_PAGE_SIMP_FARSI = 0xBA00, /* Simplified Farsi Windows 3.1 font page */ |
323 | FONT_PAGE_TRAD_FARSI = 0xBB00, /* Traditional Farsi Windows 3.1 font page */ |
324 | FONT_PAGE_THAI = 0xDE00 /* Thai Windows 3.1 font page */ |
325 | }; |
326 | font_page_t get_font_page () const |
327 | { return (font_page_t) (version == 0 ? fsSelection & 0xFF00 : 0); } |
328 | |
329 | unsigned get_size () const |
330 | { |
331 | unsigned result = min_size; |
332 | if (version >= 1) result += v1X.get_size (); |
333 | if (version >= 2) result += v2X.get_size (); |
334 | if (version >= 5) result += v5X.get_size (); |
335 | return result; |
336 | } |
337 | |
338 | bool sanitize (hb_sanitize_context_t *c) const |
339 | { |
340 | TRACE_SANITIZE (this); |
341 | if (unlikely (!c->check_struct (this))) return_trace (false); |
342 | if (unlikely (version >= 1 && !v1X.sanitize (c))) return_trace (false); |
343 | if (unlikely (version >= 2 && !v2X.sanitize (c))) return_trace (false); |
344 | if (unlikely (version >= 5 && !v5X.sanitize (c))) return_trace (false); |
345 | return_trace (true); |
346 | } |
347 | |
348 | public: |
349 | HBUINT16 version; |
350 | HBINT16 xAvgCharWidth; |
351 | HBUINT16 usWeightClass; |
352 | HBUINT16 usWidthClass; |
353 | HBUINT16 fsType; |
354 | HBINT16 ySubscriptXSize; |
355 | HBINT16 ySubscriptYSize; |
356 | HBINT16 ySubscriptXOffset; |
357 | HBINT16 ySubscriptYOffset; |
358 | HBINT16 ySuperscriptXSize; |
359 | HBINT16 ySuperscriptYSize; |
360 | HBINT16 ySuperscriptXOffset; |
361 | HBINT16 ySuperscriptYOffset; |
362 | HBINT16 yStrikeoutSize; |
363 | HBINT16 yStrikeoutPosition; |
364 | HBINT16 sFamilyClass; |
365 | HBUINT8 panose[10]; |
366 | HBUINT32 ulUnicodeRange[4]; |
367 | Tag achVendID; |
368 | HBUINT16 fsSelection; |
369 | HBUINT16 usFirstCharIndex; |
370 | HBUINT16 usLastCharIndex; |
371 | HBINT16 sTypoAscender; |
372 | HBINT16 sTypoDescender; |
373 | HBINT16 sTypoLineGap; |
374 | HBUINT16 usWinAscent; |
375 | HBUINT16 usWinDescent; |
376 | OS2V1Tail v1X; |
377 | OS2V2Tail v2X; |
378 | OS2V5Tail v5X; |
379 | public: |
380 | DEFINE_SIZE_MIN (78); |
381 | }; |
382 | |
383 | } /* namespace OT */ |
384 | |
385 | |
386 | #endif /* HB_OT_OS2_TABLE_HH */ |
387 | |