comparison mupdf-source/thirdparty/harfbuzz/src/hb-ot-os2-table.hh @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
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
34 #include "hb-set.hh"
35
36 /*
37 * OS/2 and Windows Metrics
38 * https://docs.microsoft.com/en-us/typography/opentype/spec/os2
39 */
40 #define HB_OT_TAG_OS2 HB_TAG('O','S','/','2')
41
42
43 namespace OT {
44
45 struct OS2V1Tail
46 {
47 bool sanitize (hb_sanitize_context_t *c) const
48 {
49 TRACE_SANITIZE (this);
50 return_trace (c->check_struct (this));
51 }
52
53 public:
54 HBUINT32 ulCodePageRange1;
55 HBUINT32 ulCodePageRange2;
56 public:
57 DEFINE_SIZE_STATIC (8);
58 };
59
60 struct OS2V2Tail
61 {
62 bool has_data () const { return sxHeight || sCapHeight; }
63
64 const OS2V2Tail * operator -> () const { return this; }
65
66 bool sanitize (hb_sanitize_context_t *c) const
67 {
68 TRACE_SANITIZE (this);
69 return_trace (c->check_struct (this));
70 }
71
72 public:
73 HBINT16 sxHeight;
74 HBINT16 sCapHeight;
75 HBUINT16 usDefaultChar;
76 HBUINT16 usBreakChar;
77 HBUINT16 usMaxContext;
78 public:
79 DEFINE_SIZE_STATIC (10);
80 };
81
82 struct OS2V5Tail
83 {
84 inline bool get_optical_size (unsigned int *lower, unsigned int *upper) const
85 {
86 unsigned int lower_optical_size = usLowerOpticalPointSize;
87 unsigned int upper_optical_size = usUpperOpticalPointSize;
88
89 /* Per https://docs.microsoft.com/en-us/typography/opentype/spec/os2#lps */
90 if (lower_optical_size < upper_optical_size &&
91 lower_optical_size >= 1 && lower_optical_size <= 0xFFFE &&
92 upper_optical_size >= 2 && upper_optical_size <= 0xFFFF)
93 {
94 *lower = lower_optical_size;
95 *upper = upper_optical_size;
96 return true;
97 }
98 return false;
99 }
100
101 bool sanitize (hb_sanitize_context_t *c) const
102 {
103 TRACE_SANITIZE (this);
104 return_trace (c->check_struct (this));
105 }
106
107 public:
108 HBUINT16 usLowerOpticalPointSize;
109 HBUINT16 usUpperOpticalPointSize;
110 public:
111 DEFINE_SIZE_STATIC (4);
112 };
113
114 struct OS2
115 {
116 static constexpr hb_tag_t tableTag = HB_OT_TAG_OS2;
117
118 bool has_data () const { return usWeightClass || usWidthClass || usFirstCharIndex || usLastCharIndex; }
119
120 const OS2V1Tail &v1 () const { return version >= 1 ? v1X : Null (OS2V1Tail); }
121 const OS2V2Tail &v2 () const { return version >= 2 ? v2X : Null (OS2V2Tail); }
122 const OS2V5Tail &v5 () const { return version >= 5 ? v5X : Null (OS2V5Tail); }
123
124 enum selection_flag_t {
125 ITALIC = 1u<<0,
126 UNDERSCORE = 1u<<1,
127 NEGATIVE = 1u<<2,
128 OUTLINED = 1u<<3,
129 STRIKEOUT = 1u<<4,
130 BOLD = 1u<<5,
131 REGULAR = 1u<<6,
132 USE_TYPO_METRICS = 1u<<7,
133 WWS = 1u<<8,
134 OBLIQUE = 1u<<9
135 };
136
137 bool is_italic () const { return fsSelection & ITALIC; }
138 bool is_oblique () const { return fsSelection & OBLIQUE; }
139 bool use_typo_metrics () const { return fsSelection & USE_TYPO_METRICS; }
140
141 enum width_class_t {
142 FWIDTH_ULTRA_CONDENSED = 1, /* 50% */
143 FWIDTH_EXTRA_CONDENSED = 2, /* 62.5% */
144 FWIDTH_CONDENSED = 3, /* 75% */
145 FWIDTH_SEMI_CONDENSED = 4, /* 87.5% */
146 FWIDTH_NORMAL = 5, /* 100% */
147 FWIDTH_SEMI_EXPANDED = 6, /* 112.5% */
148 FWIDTH_EXPANDED = 7, /* 125% */
149 FWIDTH_EXTRA_EXPANDED = 8, /* 150% */
150 FWIDTH_ULTRA_EXPANDED = 9 /* 200% */
151 };
152
153 float get_width () const
154 {
155 switch (usWidthClass) {
156 case FWIDTH_ULTRA_CONDENSED:return 50.f;
157 case FWIDTH_EXTRA_CONDENSED:return 62.5f;
158 case FWIDTH_CONDENSED: return 75.f;
159 case FWIDTH_SEMI_CONDENSED: return 87.5f;
160 default:
161 case FWIDTH_NORMAL: return 100.f;
162 case FWIDTH_SEMI_EXPANDED: return 112.5f;
163 case FWIDTH_EXPANDED: return 125.f;
164 case FWIDTH_EXTRA_EXPANDED: return 150.f;
165 case FWIDTH_ULTRA_EXPANDED: return 200.f;
166 }
167 }
168
169 float map_wdth_to_widthclass(float width) const
170 {
171 if (width < 50) return 1.0f;
172 if (width > 200) return 9.0f;
173
174 float ratio = (width - 50) / 12.5f;
175 int a = (int) floorf (ratio);
176 int b = (int) ceilf (ratio);
177
178 /* follow this maping:
179 * https://docs.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass
180 */
181 if (b <= 6) // 50-125
182 {
183 if (a == b) return a + 1.0f;
184 }
185 else if (b == 7) // no mapping for 137.5
186 {
187 a = 6;
188 b = 8;
189 }
190 else if (b == 8)
191 {
192 if (a == b) return 8.0f; // 150
193 a = 6;
194 }
195 else
196 {
197 if (a == b && a == 12) return 9.0f; //200
198 b = 12;
199 a = 8;
200 }
201
202 float va = 50 + a * 12.5f;
203 float vb = 50 + b * 12.5f;
204
205 float ret = a + (width - va) / (vb - va);
206 if (a <= 6) ret += 1.0f;
207 return ret;
208 }
209
210 bool subset (hb_subset_context_t *c) const
211 {
212 TRACE_SUBSET (this);
213 OS2 *os2_prime = c->serializer->embed (this);
214 if (unlikely (!os2_prime)) return_trace (false);
215
216 if (c->plan->user_axes_location->has (HB_TAG ('w','g','h','t')) &&
217 !c->plan->pinned_at_default)
218 {
219 float weight_class = c->plan->user_axes_location->get (HB_TAG ('w','g','h','t'));
220 if (!c->serializer->check_assign (os2_prime->usWeightClass,
221 roundf (hb_clamp (weight_class, 1.0f, 1000.0f)),
222 HB_SERIALIZE_ERROR_INT_OVERFLOW))
223 return_trace (false);
224 }
225
226 if (c->plan->user_axes_location->has (HB_TAG ('w','d','t','h')) &&
227 !c->plan->pinned_at_default)
228 {
229 float width = c->plan->user_axes_location->get (HB_TAG ('w','d','t','h'));
230 if (!c->serializer->check_assign (os2_prime->usWidthClass,
231 roundf (map_wdth_to_widthclass (width)),
232 HB_SERIALIZE_ERROR_INT_OVERFLOW))
233 return_trace (false);
234 }
235
236 if (c->plan->flags & HB_SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES)
237 return_trace (true);
238
239 /* when --gids option is not used, no need to do collect_mapping that is
240 * iterating all codepoints in each subtable, which is not efficient */
241 uint16_t min_cp, max_cp;
242 find_min_and_max_codepoint (c->plan->unicodes, &min_cp, &max_cp);
243 os2_prime->usFirstCharIndex = min_cp;
244 os2_prime->usLastCharIndex = max_cp;
245
246 _update_unicode_ranges (c->plan->unicodes, os2_prime->ulUnicodeRange);
247
248 return_trace (true);
249 }
250
251 void _update_unicode_ranges (const hb_set_t *codepoints,
252 HBUINT32 ulUnicodeRange[4]) const
253 {
254 HBUINT32 newBits[4];
255 for (unsigned int i = 0; i < 4; i++)
256 newBits[i] = 0;
257
258 hb_codepoint_t cp = HB_SET_VALUE_INVALID;
259 while (codepoints->next (&cp)) {
260 unsigned int bit = _hb_ot_os2_get_unicode_range_bit (cp);
261 if (bit < 128)
262 {
263 unsigned int block = bit / 32;
264 unsigned int bit_in_block = bit % 32;
265 unsigned int mask = 1 << bit_in_block;
266 newBits[block] = newBits[block] | mask;
267 }
268 if (cp >= 0x10000 && cp <= 0x110000)
269 {
270 /* the spec says that bit 57 ("Non Plane 0") implies that there's
271 at least one codepoint beyond the BMP; so I also include all
272 the non-BMP codepoints here */
273 newBits[1] = newBits[1] | (1 << 25);
274 }
275 }
276
277 for (unsigned int i = 0; i < 4; i++)
278 ulUnicodeRange[i] = ulUnicodeRange[i] & newBits[i]; // set bits only if set in the original
279 }
280
281 static void find_min_and_max_codepoint (const hb_set_t *codepoints,
282 uint16_t *min_cp, /* OUT */
283 uint16_t *max_cp /* OUT */)
284 {
285 *min_cp = hb_min (0xFFFFu, codepoints->get_min ());
286 *max_cp = hb_min (0xFFFFu, codepoints->get_max ());
287 }
288
289 /* https://github.com/Microsoft/Font-Validator/blob/520aaae/OTFontFileVal/val_OS2.cs#L644-L681
290 * https://docs.microsoft.com/en-us/typography/legacy/legacy_arabic_fonts */
291 enum font_page_t
292 {
293 FONT_PAGE_NONE = 0,
294 FONT_PAGE_HEBREW = 0xB100, /* Hebrew Windows 3.1 font page */
295 FONT_PAGE_SIMP_ARABIC = 0xB200, /* Simplified Arabic Windows 3.1 font page */
296 FONT_PAGE_TRAD_ARABIC = 0xB300, /* Traditional Arabic Windows 3.1 font page */
297 FONT_PAGE_OEM_ARABIC = 0xB400, /* OEM Arabic Windows 3.1 font page */
298 FONT_PAGE_SIMP_FARSI = 0xBA00, /* Simplified Farsi Windows 3.1 font page */
299 FONT_PAGE_TRAD_FARSI = 0xBB00, /* Traditional Farsi Windows 3.1 font page */
300 FONT_PAGE_THAI = 0xDE00 /* Thai Windows 3.1 font page */
301 };
302 font_page_t get_font_page () const
303 { return (font_page_t) (version == 0 ? fsSelection & 0xFF00 : 0); }
304
305 unsigned get_size () const
306 {
307 unsigned result = min_size;
308 if (version >= 1) result += v1X.get_size ();
309 if (version >= 2) result += v2X.get_size ();
310 if (version >= 5) result += v5X.get_size ();
311 return result;
312 }
313
314 bool sanitize (hb_sanitize_context_t *c) const
315 {
316 TRACE_SANITIZE (this);
317 if (unlikely (!c->check_struct (this))) return_trace (false);
318 if (unlikely (version >= 1 && !v1X.sanitize (c))) return_trace (false);
319 if (unlikely (version >= 2 && !v2X.sanitize (c))) return_trace (false);
320 if (unlikely (version >= 5 && !v5X.sanitize (c))) return_trace (false);
321 return_trace (true);
322 }
323
324 public:
325 HBUINT16 version;
326 HBINT16 xAvgCharWidth;
327 HBUINT16 usWeightClass;
328 HBUINT16 usWidthClass;
329 HBUINT16 fsType;
330 HBINT16 ySubscriptXSize;
331 HBINT16 ySubscriptYSize;
332 HBINT16 ySubscriptXOffset;
333 HBINT16 ySubscriptYOffset;
334 HBINT16 ySuperscriptXSize;
335 HBINT16 ySuperscriptYSize;
336 HBINT16 ySuperscriptXOffset;
337 HBINT16 ySuperscriptYOffset;
338 HBINT16 yStrikeoutSize;
339 HBINT16 yStrikeoutPosition;
340 HBINT16 sFamilyClass;
341 HBUINT8 panose[10];
342 HBUINT32 ulUnicodeRange[4];
343 Tag achVendID;
344 HBUINT16 fsSelection;
345 HBUINT16 usFirstCharIndex;
346 HBUINT16 usLastCharIndex;
347 HBINT16 sTypoAscender;
348 HBINT16 sTypoDescender;
349 HBINT16 sTypoLineGap;
350 HBUINT16 usWinAscent;
351 HBUINT16 usWinDescent;
352 OS2V1Tail v1X;
353 OS2V2Tail v2X;
354 OS2V5Tail v5X;
355 public:
356 DEFINE_SIZE_MIN (78);
357 };
358
359 } /* namespace OT */
360
361
362 #endif /* HB_OT_OS2_TABLE_HH */