Imported Upstream version 8.2.2
[platform/upstream/harfbuzz.git] / src / hb-ot-post-table.hh
1 /*
2  * Copyright © 2016  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_POST_TABLE_HH
28 #define HB_OT_POST_TABLE_HH
29
30 #include "hb-open-type.hh"
31 #include "hb-ot-var-mvar-table.hh"
32
33 #define HB_STRING_ARRAY_NAME format1_names
34 #define HB_STRING_ARRAY_LIST "hb-ot-post-macroman.hh"
35 #include "hb-string-array.hh"
36 #undef HB_STRING_ARRAY_LIST
37 #undef HB_STRING_ARRAY_NAME
38
39 /*
40  * post -- PostScript
41  * https://docs.microsoft.com/en-us/typography/opentype/spec/post
42  */
43 #define HB_OT_TAG_post HB_TAG('p','o','s','t')
44
45
46 namespace OT {
47
48
49 struct postV2Tail
50 {
51   friend struct post;
52
53   bool sanitize (hb_sanitize_context_t *c) const
54   {
55     TRACE_SANITIZE (this);
56     return_trace (glyphNameIndex.sanitize (c));
57   }
58
59   template<typename Iterator>
60   bool serialize (hb_serialize_context_t *c,
61                   Iterator it,
62                   const void* _post) const;
63
64   bool subset (hb_subset_context_t *c) const;
65
66   protected:
67   Array16Of<HBUINT16>   glyphNameIndex; /* This is not an offset, but is the
68                                          * ordinal number of the glyph in 'post'
69                                          * string tables. */
70 /*UnsizedArrayOf<HBUINT8>
71                         namesX;*/       /* Glyph names with length bytes [variable]
72                                          * (a Pascal string). */
73
74   public:
75   DEFINE_SIZE_ARRAY (2, glyphNameIndex);
76 };
77
78 struct post
79 {
80   static constexpr hb_tag_t tableTag = HB_OT_TAG_post;
81
82   bool serialize (hb_serialize_context_t *c, bool glyph_names) const
83   {
84     TRACE_SERIALIZE (this);
85     post *post_prime = c->allocate_min<post> ();
86     if (unlikely (!post_prime))  return_trace (false);
87
88     hb_memcpy (post_prime, this, post::min_size);
89     if (!glyph_names)
90       return_trace (c->check_assign (post_prime->version.major, 3,
91                                      HB_SERIALIZE_ERROR_INT_OVERFLOW)); // Version 3 does not have any glyph names.
92
93     return_trace (true);
94   }
95
96   bool subset (hb_subset_context_t *c) const
97   {
98     TRACE_SUBSET (this);
99     auto *post_prime = c->serializer->start_embed<post> ();
100
101     bool glyph_names = c->plan->flags & HB_SUBSET_FLAGS_GLYPH_NAMES;
102     if (!serialize (c->serializer, glyph_names))
103       return_trace (false);
104
105 #ifndef HB_NO_VAR
106     if (c->plan->normalized_coords)
107     {
108       auto &MVAR = *c->plan->source->table.MVAR;
109       auto *table = post_prime;
110
111       HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_UNDERLINE_SIZE,   underlineThickness);
112       HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_UNDERLINE_OFFSET, underlinePosition);
113     }
114 #endif
115
116     Triple *axis_range;
117     if (c->plan->user_axes_location.has (HB_TAG ('s','l','n','t'), &axis_range))
118     {
119       float italic_angle = hb_max (-90.f, hb_min (axis_range->middle, 90.f));
120       if (post_prime->italicAngle.to_float () != italic_angle)
121         post_prime->italicAngle.set_float (italic_angle);
122     }
123
124     if (glyph_names && version.major == 2)
125       return_trace (v2X.subset (c));
126
127     return_trace (true);
128   }
129
130   struct accelerator_t
131   {
132     friend struct postV2Tail;
133
134     accelerator_t (hb_face_t *face)
135     {
136       table = hb_sanitize_context_t ().reference_table<post> (face);
137       unsigned int table_length = table.get_length ();
138
139       version = table->version.to_int ();
140       if (version != 0x00020000) return;
141
142       const postV2Tail &v2 = table->v2X;
143
144       glyphNameIndex = &v2.glyphNameIndex;
145       pool = &StructAfter<uint8_t> (v2.glyphNameIndex);
146
147       const uint8_t *end = (const uint8_t *) (const void *) table + table_length;
148       index_to_offset.alloc (hb_min (face->get_num_glyphs (), table_length / 8));
149       for (const uint8_t *data = pool;
150            index_to_offset.length < 65535 && data < end && data + *data < end;
151            data += 1 + *data)
152         index_to_offset.push (data - pool);
153     }
154     ~accelerator_t ()
155     {
156       hb_free (gids_sorted_by_name.get_acquire ());
157       table.destroy ();
158     }
159
160     bool get_glyph_name (hb_codepoint_t glyph,
161                          char *buf, unsigned int buf_len) const
162     {
163       hb_bytes_t s = find_glyph_name (glyph);
164       if (!s.length) return false;
165       if (!buf_len) return true;
166       unsigned int len = hb_min (buf_len - 1, s.length);
167       strncpy (buf, s.arrayZ, len);
168       buf[len] = '\0';
169       return true;
170     }
171
172     bool get_glyph_from_name (const char *name, int len,
173                               hb_codepoint_t *glyph) const
174     {
175       unsigned int count = get_glyph_count ();
176       if (unlikely (!count)) return false;
177
178       if (len < 0) len = strlen (name);
179
180       if (unlikely (!len)) return false;
181
182     retry:
183       uint16_t *gids = gids_sorted_by_name.get_acquire ();
184
185       if (unlikely (!gids))
186       {
187         gids = (uint16_t *) hb_malloc (count * sizeof (gids[0]));
188         if (unlikely (!gids))
189           return false; /* Anything better?! */
190
191         for (unsigned int i = 0; i < count; i++)
192           gids[i] = i;
193         hb_qsort (gids, count, sizeof (gids[0]), cmp_gids, (void *) this);
194
195         if (unlikely (!gids_sorted_by_name.cmpexch (nullptr, gids)))
196         {
197           hb_free (gids);
198           goto retry;
199         }
200       }
201
202       hb_bytes_t st (name, len);
203       auto* gid = hb_bsearch (st, gids, count, sizeof (gids[0]), cmp_key, (void *) this);
204       if (gid)
205       {
206         *glyph = *gid;
207         return true;
208       }
209
210       return false;
211     }
212
213     hb_blob_ptr_t<post> table;
214
215     protected:
216
217     unsigned int get_glyph_count () const
218     {
219       if (version == 0x00010000)
220         return format1_names_length;
221
222       if (version == 0x00020000)
223         return glyphNameIndex->len;
224
225       return 0;
226     }
227
228     static int cmp_gids (const void *pa, const void *pb, void *arg)
229     {
230       const accelerator_t *thiz = (const accelerator_t *) arg;
231       uint16_t a = * (const uint16_t *) pa;
232       uint16_t b = * (const uint16_t *) pb;
233       return thiz->find_glyph_name (b).cmp (thiz->find_glyph_name (a));
234     }
235
236     static int cmp_key (const void *pk, const void *po, void *arg)
237     {
238       const accelerator_t *thiz = (const accelerator_t *) arg;
239       const hb_bytes_t *key = (const hb_bytes_t *) pk;
240       uint16_t o = * (const uint16_t *) po;
241       return thiz->find_glyph_name (o).cmp (*key);
242     }
243
244     hb_bytes_t find_glyph_name (hb_codepoint_t glyph) const
245     {
246       if (version == 0x00010000)
247       {
248         if (glyph >= format1_names_length)
249           return hb_bytes_t ();
250
251         return format1_names (glyph);
252       }
253
254       if (version != 0x00020000 || glyph >= glyphNameIndex->len)
255         return hb_bytes_t ();
256
257       unsigned int index = glyphNameIndex->arrayZ[glyph];
258       if (index < format1_names_length)
259         return format1_names (index);
260       index -= format1_names_length;
261
262       if (index >= index_to_offset.length)
263         return hb_bytes_t ();
264       unsigned int offset = index_to_offset[index];
265
266       const uint8_t *data = pool + offset;
267       unsigned int name_length = *data;
268       data++;
269
270       return hb_bytes_t ((const char *) data, name_length);
271     }
272
273     private:
274     uint32_t version;
275     const Array16Of<HBUINT16> *glyphNameIndex = nullptr;
276     hb_vector_t<uint32_t> index_to_offset;
277     const uint8_t *pool = nullptr;
278     hb_atomic_ptr_t<uint16_t *> gids_sorted_by_name;
279   };
280
281   bool has_data () const { return version.to_int (); }
282
283   bool sanitize (hb_sanitize_context_t *c) const
284   {
285     TRACE_SANITIZE (this);
286     return_trace (c->check_struct (this) &&
287                   (version.to_int () == 0x00010000 ||
288                    (version.to_int () == 0x00020000 && v2X.sanitize (c)) ||
289                    version.to_int () == 0x00030000));
290   }
291
292   public:
293   FixedVersion<>version;                /* 0x00010000 for version 1.0
294                                          * 0x00020000 for version 2.0
295                                          * 0x00025000 for version 2.5 (deprecated)
296                                          * 0x00030000 for version 3.0 */
297   F16DOT16      italicAngle;            /* Italic angle in counter-clockwise degrees
298                                          * from the vertical. Zero for upright text,
299                                          * negative for text that leans to the right
300                                          * (forward). */
301   FWORD         underlinePosition;      /* This is the suggested distance of the top
302                                          * of the underline from the baseline
303                                          * (negative values indicate below baseline).
304                                          * The PostScript definition of this FontInfo
305                                          * dictionary key (the y coordinate of the
306                                          * center of the stroke) is not used for
307                                          * historical reasons. The value of the
308                                          * PostScript key may be calculated by
309                                          * subtracting half the underlineThickness
310                                          * from the value of this field. */
311   FWORD         underlineThickness;     /* Suggested values for the underline
312                                            thickness. */
313   HBUINT32      isFixedPitch;           /* Set to 0 if the font is proportionally
314                                          * spaced, non-zero if the font is not
315                                          * proportionally spaced (i.e. monospaced). */
316   HBUINT32      minMemType42;           /* Minimum memory usage when an OpenType font
317                                          * is downloaded. */
318   HBUINT32      maxMemType42;           /* Maximum memory usage when an OpenType font
319                                          * is downloaded. */
320   HBUINT32      minMemType1;            /* Minimum memory usage when an OpenType font
321                                          * is downloaded as a Type 1 font. */
322   HBUINT32      maxMemType1;            /* Maximum memory usage when an OpenType font
323                                          * is downloaded as a Type 1 font. */
324   postV2Tail    v2X;
325   DEFINE_SIZE_MIN (32);
326 };
327
328 struct post_accelerator_t : post::accelerator_t {
329   post_accelerator_t (hb_face_t *face) : post::accelerator_t (face) {}
330 };
331
332
333 } /* namespace OT */
334
335
336 #endif /* HB_OT_POST_TABLE_HH */