1/*
2 * Copyright © 2011,2012 Google, Inc.
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 * Google Author(s): Behdad Esfahbod
25 */
26
27#ifndef HB_OT_OS2_TABLE_HH
28#define HB_OT_OS2_TABLE_HH
29
30#include "hb-open-type.hh"
31#include "hb-ot-os2-unicode-ranges.hh"
32
33namespace OT {
34
35/*
36 * OS/2 and Windows Metrics
37 * https://docs.microsoft.com/en-us/typography/opentype/spec/os2
38 */
39#define HB_OT_TAG_os2 HB_TAG('O','S','/','2')
40
41struct os2
42{
43 static const hb_tag_t tableTag = HB_OT_TAG_os2;
44
45 inline bool sanitize (hb_sanitize_context_t *c) const
46 {
47 TRACE_SANITIZE (this);
48 return_trace (c->check_struct (this));
49 }
50
51 inline bool subset (hb_subset_plan_t *plan) const
52 {
53 hb_blob_t *os2_blob = hb_sanitize_context_t().reference_table<os2> (plan->source);
54 hb_blob_t *os2_prime_blob = hb_blob_create_sub_blob (os2_blob, 0, -1);
55 // TODO(grieger): move to hb_blob_copy_writable_or_fail
56 hb_blob_destroy (os2_blob);
57
58 os2 *os2_prime = (os2 *) hb_blob_get_data_writable (os2_prime_blob, nullptr);
59 if (unlikely (!os2_prime)) {
60 hb_blob_destroy (os2_prime_blob);
61 return false;
62 }
63
64 uint16_t min_cp, max_cp;
65 find_min_and_max_codepoint (plan->unicodes, &min_cp, &max_cp);
66 os2_prime->usFirstCharIndex.set (min_cp);
67 os2_prime->usLastCharIndex.set (max_cp);
68
69 _update_unicode_ranges (plan->unicodes, os2_prime->ulUnicodeRange);
70 bool result = plan->add_table (HB_OT_TAG_os2, os2_prime_blob);
71
72 hb_blob_destroy (os2_prime_blob);
73 return result;
74 }
75
76 inline void _update_unicode_ranges (const hb_set_t *codepoints,
77 HBUINT32 ulUnicodeRange[4]) const
78 {
79 for (unsigned int i = 0; i < 4; i++)
80 ulUnicodeRange[i].set (0);
81
82 hb_codepoint_t cp = HB_SET_VALUE_INVALID;
83 while (codepoints->next (&cp)) {
84 unsigned int bit = hb_get_unicode_range_bit (cp);
85 if (bit < 128)
86 {
87 unsigned int block = bit / 32;
88 unsigned int bit_in_block = bit % 32;
89 unsigned int mask = 1 << bit_in_block;
90 ulUnicodeRange[block].set (ulUnicodeRange[block] | mask);
91 }
92 if (cp >= 0x10000 && cp <= 0x110000)
93 {
94 /* the spec says that bit 57 ("Non Plane 0") implies that there's
95 at least one codepoint beyond the BMP; so I also include all
96 the non-BMP codepoints here */
97 ulUnicodeRange[1].set (ulUnicodeRange[1] | (1 << 25));
98 }
99 }
100 }
101
102 static inline void find_min_and_max_codepoint (const hb_set_t *codepoints,
103 uint16_t *min_cp, /* OUT */
104 uint16_t *max_cp /* OUT */)
105 {
106 *min_cp = codepoints->get_min ();
107 *max_cp = codepoints->get_max ();
108 }
109
110 enum font_page_t {
111 HEBREW_FONT_PAGE = 0xB100, // Hebrew Windows 3.1 font page
112 SIMP_ARABIC_FONT_PAGE = 0xB200, // Simplified Arabic Windows 3.1 font page
113 TRAD_ARABIC_FONT_PAGE = 0xB300, // Traditional Arabic Windows 3.1 font page
114 OEM_ARABIC_FONT_PAGE = 0xB400, // OEM Arabic Windows 3.1 font page
115 SIMP_FARSI_FONT_PAGE = 0xBA00, // Simplified Farsi Windows 3.1 font page
116 TRAD_FARSI_FONT_PAGE = 0xBB00, // Traditional Farsi Windows 3.1 font page
117 THAI_FONT_PAGE = 0xDE00 // Thai Windows 3.1 font page
118 };
119
120 // https://github.com/Microsoft/Font-Validator/blob/520aaae/OTFontFileVal/val_OS2.cs#L644-L681
121 inline font_page_t get_font_page () const
122 {
123 if (version != 0)
124 return (font_page_t) 0;
125 return (font_page_t) (fsSelection & 0xFF00);
126 }
127
128 public:
129 HBUINT16 version;
130
131 /* Version 0 */
132 HBINT16 xAvgCharWidth;
133 HBUINT16 usWeightClass;
134 HBUINT16 usWidthClass;
135 HBUINT16 fsType;
136 HBINT16 ySubscriptXSize;
137 HBINT16 ySubscriptYSize;
138 HBINT16 ySubscriptXOffset;
139 HBINT16 ySubscriptYOffset;
140 HBINT16 ySuperscriptXSize;
141 HBINT16 ySuperscriptYSize;
142 HBINT16 ySuperscriptXOffset;
143 HBINT16 ySuperscriptYOffset;
144 HBINT16 yStrikeoutSize;
145 HBINT16 yStrikeoutPosition;
146 HBINT16 sFamilyClass;
147 HBUINT8 panose[10];
148 HBUINT32 ulUnicodeRange[4];
149 Tag achVendID;
150 HBUINT16 fsSelection;
151 HBUINT16 usFirstCharIndex;
152 HBUINT16 usLastCharIndex;
153 HBINT16 sTypoAscender;
154 HBINT16 sTypoDescender;
155 HBINT16 sTypoLineGap;
156 HBUINT16 usWinAscent;
157 HBUINT16 usWinDescent;
158
159 /* Version 1 */
160 //HBUINT32 ulCodePageRange1;
161 //HBUINT32 ulCodePageRange2;
162
163 /* Version 2 */
164 //HBINT16 sxHeight;
165 //HBINT16 sCapHeight;
166 //HBUINT16 usDefaultChar;
167 //HBUINT16 usBreakChar;
168 //HBUINT16 usMaxContext;
169
170 /* Version 5 */
171 //HBUINT16 usLowerOpticalPointSize;
172 //HBUINT16 usUpperOpticalPointSize;
173
174 public:
175 DEFINE_SIZE_STATIC (78);
176};
177
178} /* namespace OT */
179
180
181#endif /* HB_OT_OS2_TABLE_HH */
182