Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / src / hb-ot-os2-table.hh
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-private.hh"
31 #include "hb-ot-os2-unicode-ranges.hh"
32
33 namespace OT {
34
35 /*
36  * OS/2 and Windows Metrics
37  * http://www.microsoft.com/typography/otspec/os2.htm
38  */
39
40 #define HB_OT_TAG_os2 HB_TAG('O','S','/','2')
41
42 struct os2
43 {
44   static const hb_tag_t tableTag = HB_OT_TAG_os2;
45
46   inline bool sanitize (hb_sanitize_context_t *c) const
47   {
48     TRACE_SANITIZE (this);
49     return_trace (c->check_struct (this));
50   }
51
52   inline bool subset (hb_subset_plan_t *plan) const
53   {
54     hb_blob_t *os2_blob = OT::Sanitizer<OT::os2>().sanitize (hb_face_reference_table (plan->source, HB_OT_TAG_os2));
55     hb_blob_t *os2_prime_blob = hb_blob_create_sub_blob (os2_blob, 0, -1);
56     // TODO(grieger): move to hb_blob_copy_writable_or_fail
57     hb_blob_destroy (os2_blob);
58
59     OT::os2 *os2_prime = (OT::os2 *) hb_blob_get_data_writable (os2_prime_blob, nullptr);
60     if (unlikely (!os2_prime)) {
61       hb_blob_destroy (os2_prime_blob);
62       return false;
63     }
64
65     uint16_t min_cp, max_cp;
66     find_min_and_max_codepoint (plan->codepoints, &min_cp, &max_cp);
67     os2_prime->usFirstCharIndex.set (min_cp);
68     os2_prime->usLastCharIndex.set (max_cp);
69
70     _update_unicode_ranges (plan->codepoints, os2_prime->ulUnicodeRange);
71     bool result = hb_subset_plan_add_table(plan, HB_OT_TAG_os2, os2_prime_blob);
72
73     hb_blob_destroy (os2_prime_blob);
74     return result;
75   }
76
77   inline void _update_unicode_ranges (const hb_prealloced_array_t<hb_codepoint_t> &codepoints,
78                                       HBUINT32 ulUnicodeRange[4]) const
79   {
80     for (unsigned int i = 0; i < 4; i++)
81       ulUnicodeRange[i].set (0);
82
83     for (unsigned int i = 0; i < codepoints.len; i++)
84     {
85       hb_codepoint_t cp = codepoints[i];
86       unsigned int bit = hb_get_unicode_range_bit (cp);
87       if (bit < 128)
88       {
89         unsigned int block = bit / 32;
90         unsigned int bit_in_block = bit % 32;
91         unsigned int mask = 1 << bit_in_block;
92         ulUnicodeRange[block].set (ulUnicodeRange[block] | mask);
93       }
94       if (cp >= 0x10000 && cp <= 0x110000)
95       {
96         /* the spec says that bit 57 ("Non Plane 0") implies that there's
97            at least one codepoint beyond the BMP; so I also include all
98            the non-BMP codepoints here */
99         ulUnicodeRange[1].set (ulUnicodeRange[1] | (1 << 25));
100       }
101     }
102   }
103
104   static inline void find_min_and_max_codepoint (const hb_prealloced_array_t<hb_codepoint_t> &codepoints,
105                                                  uint16_t *min_cp, /* OUT */
106                                                  uint16_t *max_cp  /* OUT */)
107   {
108     hb_codepoint_t min = -1, max = 0;
109
110     for (unsigned int i = 0; i < codepoints.len; i++)
111     {
112       hb_codepoint_t cp = codepoints[i];
113       if (cp < min)
114         min = cp;
115       if (cp > max)
116         max = cp;
117     }
118
119     if (min > 0xFFFF)
120       min = 0xFFFF;
121     if (max > 0xFFFF)
122       max = 0xFFFF;
123
124     *min_cp = min;
125     *max_cp = max;
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 */