• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
sanitizeOT::OS2V1Tail48   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	ulCodePageRange1;
56   HBUINT32	ulCodePageRange2;
57   public:
58   DEFINE_SIZE_STATIC (8);
59 };
60 
61 struct OS2V2Tail
62 {
has_dataOT::OS2V2Tail63   bool has_data () const { return sxHeight || sCapHeight; }
64 
operator ->OT::OS2V2Tail65   const OS2V2Tail * operator -> () const { return this; }
operator ->OT::OS2V2Tail66   OS2V2Tail * operator -> () { return this; }
67 
sanitizeOT::OS2V2Tail68   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 {
get_optical_sizeOT::OS2V5Tail86   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 
sanitizeOT::OS2V5Tail103   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 
has_dataOT::OS2120   bool has_data () const { return usWeightClass || usWidthClass || usFirstCharIndex || usLastCharIndex; }
121 
v1OT::OS2122   const OS2V1Tail &v1 () const { return version >= 1 ? v1X : Null (OS2V1Tail); }
v2OT::OS2123   const OS2V2Tail &v2 () const { return version >= 2 ? v2X : Null (OS2V2Tail); }
v5OT::OS2124   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 
is_italicOT::OS2139   bool        is_italic () const { return fsSelection & ITALIC; }
is_obliqueOT::OS2140   bool       is_oblique () const { return fsSelection & OBLIQUE; }
use_typo_metricsOT::OS2141   bool use_typo_metrics () const { return fsSelection & USE_TYPO_METRICS; }
142 
143   enum width_class_t {
144     FWIDTH_ULTRA_CONDENSED	= 1, /* 50% */
145     FWIDTH_EXTRA_CONDENSED	= 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 
get_widthOT::OS2155   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 
map_wdth_to_widthclassOT::OS2171   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 
subsetOT::OS2212   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     Triple *axis_range;
250     if (c->plan->user_axes_location.has (HB_TAG ('w','g','h','t'), &axis_range))
251     {
252       unsigned weight_class = static_cast<unsigned> (roundf (hb_clamp (axis_range->middle, 1.0f, 1000.0f)));
253       if (os2_prime->usWeightClass != weight_class)
254         os2_prime->usWeightClass = weight_class;
255     }
256 
257     if (c->plan->user_axes_location.has (HB_TAG ('w','d','t','h'), &axis_range))
258     {
259       unsigned width_class = static_cast<unsigned> (roundf (map_wdth_to_widthclass (axis_range->middle)));
260       if (os2_prime->usWidthClass != width_class)
261         os2_prime->usWidthClass = width_class;
262     }
263 
264     if (c->plan->flags & HB_SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES)
265       return_trace (true);
266 
267     os2_prime->usFirstCharIndex = hb_min (0xFFFFu, c->plan->unicodes.get_min ());
268     os2_prime->usLastCharIndex  = hb_min (0xFFFFu, c->plan->unicodes.get_max ());
269 
270     _update_unicode_ranges (&c->plan->unicodes, os2_prime->ulUnicodeRange);
271 
272     return_trace (true);
273   }
274 
_update_unicode_rangesOT::OS2275   void _update_unicode_ranges (const hb_set_t *codepoints,
276 			       HBUINT32 ulUnicodeRange[4]) const
277   {
278     HBUINT32 newBits[4];
279     for (unsigned int i = 0; i < 4; i++)
280       newBits[i] = 0;
281 
282     /* This block doesn't show up in profiles. If it ever did,
283      * we can rewrite it to iterate over OS/2 ranges and use
284      * set iteration to check if the range matches. */
285     for (auto cp : *codepoints)
286     {
287       unsigned int bit = _hb_ot_os2_get_unicode_range_bit (cp);
288       if (bit < 128)
289       {
290 	unsigned int block = bit / 32;
291 	unsigned int bit_in_block = bit % 32;
292 	unsigned int mask = 1 << bit_in_block;
293 	newBits[block] = newBits[block] | mask;
294       }
295       if (cp >= 0x10000 && cp <= 0x110000)
296       {
297 	/* the spec says that bit 57 ("Non Plane 0") implies that there's
298 	   at least one codepoint beyond the BMP; so I also include all
299 	   the non-BMP codepoints here */
300 	newBits[1] = newBits[1] | (1 << 25);
301       }
302     }
303 
304     for (unsigned int i = 0; i < 4; i++)
305       ulUnicodeRange[i] = ulUnicodeRange[i] & newBits[i]; // set bits only if set in the original
306   }
307 
308   /* https://github.com/Microsoft/Font-Validator/blob/520aaae/OTFontFileVal/val_OS2.cs#L644-L681
309    * https://docs.microsoft.com/en-us/typography/legacy/legacy_arabic_fonts */
310   enum font_page_t
311   {
312     FONT_PAGE_NONE		= 0,
313     FONT_PAGE_HEBREW		= 0xB100, /* Hebrew Windows 3.1 font page */
314     FONT_PAGE_SIMP_ARABIC	= 0xB200, /* Simplified Arabic Windows 3.1 font page */
315     FONT_PAGE_TRAD_ARABIC	= 0xB300, /* Traditional Arabic Windows 3.1 font page */
316     FONT_PAGE_OEM_ARABIC	= 0xB400, /* OEM Arabic Windows 3.1 font page */
317     FONT_PAGE_SIMP_FARSI	= 0xBA00, /* Simplified Farsi Windows 3.1 font page */
318     FONT_PAGE_TRAD_FARSI	= 0xBB00, /* Traditional Farsi Windows 3.1 font page */
319     FONT_PAGE_THAI		= 0xDE00  /* Thai Windows 3.1 font page */
320   };
get_font_pageOT::OS2321   font_page_t get_font_page () const
322   { return (font_page_t) (version == 0 ? fsSelection & 0xFF00 : 0); }
323 
get_sizeOT::OS2324   unsigned get_size () const
325   {
326     unsigned result = min_size;
327     if (version >= 1) result += v1X.get_size ();
328     if (version >= 2) result += v2X.get_size ();
329     if (version >= 5) result += v5X.get_size ();
330     return result;
331   }
332 
sanitizeOT::OS2333   bool sanitize (hb_sanitize_context_t *c) const
334   {
335     TRACE_SANITIZE (this);
336     if (unlikely (!c->check_struct (this))) return_trace (false);
337     if (unlikely (version >= 1 && !v1X.sanitize (c))) return_trace (false);
338     if (unlikely (version >= 2 && !v2X.sanitize (c))) return_trace (false);
339     if (unlikely (version >= 5 && !v5X.sanitize (c))) return_trace (false);
340     return_trace (true);
341   }
342 
343   public:
344   HBUINT16	version;
345   HBINT16	xAvgCharWidth;
346   HBUINT16	usWeightClass;
347   HBUINT16	usWidthClass;
348   HBUINT16	fsType;
349   HBINT16	ySubscriptXSize;
350   HBINT16	ySubscriptYSize;
351   HBINT16	ySubscriptXOffset;
352   HBINT16	ySubscriptYOffset;
353   HBINT16	ySuperscriptXSize;
354   HBINT16	ySuperscriptYSize;
355   HBINT16	ySuperscriptXOffset;
356   HBINT16	ySuperscriptYOffset;
357   HBINT16	yStrikeoutSize;
358   HBINT16	yStrikeoutPosition;
359   HBINT16	sFamilyClass;
360   HBUINT8	panose[10];
361   HBUINT32	ulUnicodeRange[4];
362   Tag		achVendID;
363   HBUINT16	fsSelection;
364   HBUINT16	usFirstCharIndex;
365   HBUINT16	usLastCharIndex;
366   HBINT16	sTypoAscender;
367   HBINT16	sTypoDescender;
368   HBINT16	sTypoLineGap;
369   HBUINT16	usWinAscent;
370   HBUINT16	usWinDescent;
371   OS2V1Tail	v1X;
372   OS2V2Tail	v2X;
373   OS2V5Tail	v5X;
374   public:
375   DEFINE_SIZE_MIN (78);
376 };
377 
378 } /* namespace OT */
379 
380 
381 #endif /* HB_OT_OS2_TABLE_HH */
382