2 * Copyright © 2011,2012 Google, Inc.
3 * Copyright © 2018 Ebrahim Byagowi
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Google Author(s): Behdad Esfahbod
28 #ifndef HB_OT_OS2_TABLE_HH
29 #define HB_OT_OS2_TABLE_HH
31 #include "hb-open-type.hh"
32 #include "hb-ot-os2-unicode-ranges.hh"
37 * OS/2 and Windows Metrics
38 * https://docs.microsoft.com/en-us/typography/opentype/spec/os2
40 #define HB_OT_TAG_OS2 HB_TAG('O','S','/','2')
47 bool sanitize (hb_sanitize_context_t
*c
) const
49 TRACE_SANITIZE (this);
50 return_trace (c
->check_struct (this));
54 HBUINT32 ulCodePageRange1
;
55 HBUINT32 ulCodePageRange2
;
57 DEFINE_SIZE_STATIC (8);
62 bool has_data () const { return sxHeight
|| sCapHeight
; }
64 const OS2V2Tail
* operator -> () const { return this; }
66 bool sanitize (hb_sanitize_context_t
*c
) const
68 TRACE_SANITIZE (this);
69 return_trace (c
->check_struct (this));
75 HBUINT16 usDefaultChar
;
77 HBUINT16 usMaxContext
;
79 DEFINE_SIZE_STATIC (10);
84 inline bool get_optical_size (unsigned int *lower
, unsigned int *upper
) const
86 unsigned int lower_optical_size
= usLowerOpticalPointSize
;
87 unsigned int upper_optical_size
= usUpperOpticalPointSize
;
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)
94 *lower
= lower_optical_size
;
95 *upper
= upper_optical_size
;
101 bool sanitize (hb_sanitize_context_t
*c
) const
103 TRACE_SANITIZE (this);
104 return_trace (c
->check_struct (this));
108 HBUINT16 usLowerOpticalPointSize
;
109 HBUINT16 usUpperOpticalPointSize
;
111 DEFINE_SIZE_STATIC (4);
116 static constexpr hb_tag_t tableTag
= HB_OT_TAG_OS2
;
118 bool has_data () const { return usWeightClass
|| usWidthClass
|| usFirstCharIndex
|| usLastCharIndex
; }
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
); }
124 enum selection_flag_t
{
132 USE_TYPO_METRICS
= 1u<<7,
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
; }
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% */
153 float get_width () const
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
;
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
;
169 bool subset (hb_subset_context_t
*c
) const
172 OS2
*os2_prime
= c
->serializer
->embed (this);
173 if (unlikely (!os2_prime
)) return_trace (false);
174 if (c
->plan
->flags
& HB_SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES
)
177 /* when --gids option is not used, no need to do collect_mapping that is
178 * iterating all codepoints in each subtable, which is not efficient */
179 uint16_t min_cp
, max_cp
;
180 find_min_and_max_codepoint (c
->plan
->unicodes
, &min_cp
, &max_cp
);
181 os2_prime
->usFirstCharIndex
= min_cp
;
182 os2_prime
->usLastCharIndex
= max_cp
;
184 _update_unicode_ranges (c
->plan
->unicodes
, os2_prime
->ulUnicodeRange
);
189 void _update_unicode_ranges (const hb_set_t
*codepoints
,
190 HBUINT32 ulUnicodeRange
[4]) const
193 for (unsigned int i
= 0; i
< 4; i
++)
196 hb_codepoint_t cp
= HB_SET_VALUE_INVALID
;
197 while (codepoints
->next (&cp
)) {
198 unsigned int bit
= _hb_ot_os2_get_unicode_range_bit (cp
);
201 unsigned int block
= bit
/ 32;
202 unsigned int bit_in_block
= bit
% 32;
203 unsigned int mask
= 1 << bit_in_block
;
204 newBits
[block
] = newBits
[block
] | mask
;
206 if (cp
>= 0x10000 && cp
<= 0x110000)
208 /* the spec says that bit 57 ("Non Plane 0") implies that there's
209 at least one codepoint beyond the BMP; so I also include all
210 the non-BMP codepoints here */
211 newBits
[1] = newBits
[1] | (1 << 25);
215 for (unsigned int i
= 0; i
< 4; i
++)
216 ulUnicodeRange
[i
] = ulUnicodeRange
[i
] & newBits
[i
]; // set bits only if set in the original
219 static void find_min_and_max_codepoint (const hb_set_t
*codepoints
,
220 uint16_t *min_cp
, /* OUT */
221 uint16_t *max_cp
/* OUT */)
223 *min_cp
= hb_min (0xFFFFu
, codepoints
->get_min ());
224 *max_cp
= hb_min (0xFFFFu
, codepoints
->get_max ());
227 /* https://github.com/Microsoft/Font-Validator/blob/520aaae/OTFontFileVal/val_OS2.cs#L644-L681 */
230 FONT_PAGE_HEBREW
= 0xB100, /* Hebrew Windows 3.1 font page */
231 FONT_PAGE_SIMP_ARABIC
= 0xB200, /* Simplified Arabic Windows 3.1 font page */
232 FONT_PAGE_TRAD_ARABIC
= 0xB300, /* Traditional Arabic Windows 3.1 font page */
233 FONT_PAGE_OEM_ARABIC
= 0xB400, /* OEM Arabic Windows 3.1 font page */
234 FONT_PAGE_SIMP_FARSI
= 0xBA00, /* Simplified Farsi Windows 3.1 font page */
235 FONT_PAGE_TRAD_FARSI
= 0xBB00, /* Traditional Farsi Windows 3.1 font page */
236 FONT_PAGE_THAI
= 0xDE00 /* Thai Windows 3.1 font page */
238 font_page_t
get_font_page () const
239 { return (font_page_t
) (version
== 0 ? fsSelection
& 0xFF00 : 0); }
241 unsigned get_size () const
243 unsigned result
= min_size
;
244 if (version
>= 1) result
+= v1X
.get_size ();
245 if (version
>= 2) result
+= v2X
.get_size ();
246 if (version
>= 5) result
+= v5X
.get_size ();
250 bool sanitize (hb_sanitize_context_t
*c
) const
252 TRACE_SANITIZE (this);
253 if (unlikely (!c
->check_struct (this))) return_trace (false);
254 if (unlikely (version
>= 1 && !v1X
.sanitize (c
))) return_trace (false);
255 if (unlikely (version
>= 2 && !v2X
.sanitize (c
))) return_trace (false);
256 if (unlikely (version
>= 5 && !v5X
.sanitize (c
))) return_trace (false);
262 HBINT16 xAvgCharWidth
;
263 HBUINT16 usWeightClass
;
264 HBUINT16 usWidthClass
;
266 HBINT16 ySubscriptXSize
;
267 HBINT16 ySubscriptYSize
;
268 HBINT16 ySubscriptXOffset
;
269 HBINT16 ySubscriptYOffset
;
270 HBINT16 ySuperscriptXSize
;
271 HBINT16 ySuperscriptYSize
;
272 HBINT16 ySuperscriptXOffset
;
273 HBINT16 ySuperscriptYOffset
;
274 HBINT16 yStrikeoutSize
;
275 HBINT16 yStrikeoutPosition
;
276 HBINT16 sFamilyClass
;
278 HBUINT32 ulUnicodeRange
[4];
280 HBUINT16 fsSelection
;
281 HBUINT16 usFirstCharIndex
;
282 HBUINT16 usLastCharIndex
;
283 HBINT16 sTypoAscender
;
284 HBINT16 sTypoDescender
;
285 HBINT16 sTypoLineGap
;
286 HBUINT16 usWinAscent
;
287 HBUINT16 usWinDescent
;
292 DEFINE_SIZE_MIN (78);
298 #endif /* HB_OT_OS2_TABLE_HH */