Imported Upstream version 2.4.0
[platform/upstream/harfbuzz.git] / src / hb-ot-hdmx-table.hh
1 /*
2  * Copyright © 2018  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): Garret Rieger
25  */
26
27 #ifndef HB_OT_HDMX_TABLE_HH
28 #define HB_OT_HDMX_TABLE_HH
29
30 #include "hb-open-type.hh"
31
32 /*
33  * hdmx -- Horizontal Device Metrics
34  * https://docs.microsoft.com/en-us/typography/opentype/spec/hdmx
35  */
36 #define HB_OT_TAG_hdmx HB_TAG('h','d','m','x')
37
38
39 namespace OT {
40
41
42 struct DeviceRecord
43 {
44   struct SubsetView
45   {
46     const DeviceRecord *source_device_record;
47     unsigned int sizeDeviceRecord;
48     hb_subset_plan_t *subset_plan;
49
50     void init (const DeviceRecord *source_device_record,
51                unsigned int sizeDeviceRecord,
52                hb_subset_plan_t   *subset_plan)
53     {
54       this->source_device_record = source_device_record;
55       this->sizeDeviceRecord = sizeDeviceRecord;
56       this->subset_plan = subset_plan;
57     }
58
59     unsigned int len () const
60     { return this->subset_plan->num_output_glyphs (); }
61
62     const HBUINT8* operator [] (unsigned int new_gid) const
63     {
64       if (unlikely (new_gid >= len ())) return nullptr;
65
66       hb_codepoint_t old_gid;
67       if (!this->subset_plan->old_gid_for_new_gid (new_gid, &old_gid))
68         return &Null(HBUINT8);
69
70       if (old_gid >= sizeDeviceRecord - DeviceRecord::min_size)
71         return nullptr;
72       return &(this->source_device_record->widthsZ[old_gid]);
73     }
74   };
75
76   static unsigned int get_size (unsigned int count)
77   { return hb_ceil_to_4 (min_size + count * HBUINT8::static_size); }
78
79   bool serialize (hb_serialize_context_t *c, const SubsetView &subset_view)
80   {
81     TRACE_SERIALIZE (this);
82
83     unsigned int size = get_size (subset_view.len ());
84     if (unlikely (!c->allocate_size<DeviceRecord> (size)))
85     {
86       DEBUG_MSG(SUBSET, nullptr, "Couldn't allocate enough space for DeviceRecord: %d.",
87                  size);
88       return_trace (false);
89     }
90
91     this->pixelSize.set (subset_view.source_device_record->pixelSize);
92     this->maxWidth.set (subset_view.source_device_record->maxWidth);
93
94     for (unsigned int i = 0; i < subset_view.len (); i++)
95     {
96       const HBUINT8 *width = subset_view[i];
97       if (!width)
98       {
99         DEBUG_MSG(SUBSET, nullptr, "HDMX width for new gid %d is missing.", i);
100         return_trace (false);
101       }
102       widthsZ[i].set (*width);
103     }
104
105     return_trace (true);
106   }
107
108   bool sanitize (hb_sanitize_context_t *c, unsigned int sizeDeviceRecord) const
109   {
110     TRACE_SANITIZE (this);
111     return_trace (likely (c->check_struct (this) &&
112                           c->check_range (this, sizeDeviceRecord)));
113   }
114
115   HBUINT8                       pixelSize;      /* Pixel size for following widths (as ppem). */
116   HBUINT8                       maxWidth;       /* Maximum width. */
117   UnsizedArrayOf<HBUINT8>       widthsZ;        /* Array of widths (numGlyphs is from the 'maxp' table). */
118   public:
119   DEFINE_SIZE_ARRAY (2, widthsZ);
120 };
121
122
123 struct hdmx
124 {
125   static constexpr hb_tag_t tableTag = HB_OT_TAG_hdmx;
126
127   unsigned int get_size () const
128   { return min_size + numRecords * sizeDeviceRecord; }
129
130   const DeviceRecord& operator [] (unsigned int i) const
131   {
132     /* XXX Null(DeviceRecord) is NOT safe as it's num-glyphs lengthed.
133      * https://github.com/harfbuzz/harfbuzz/issues/1300 */
134     if (unlikely (i >= numRecords)) return Null (DeviceRecord);
135     return StructAtOffset<DeviceRecord> (&this->firstDeviceRecord, i * sizeDeviceRecord);
136   }
137
138   bool serialize (hb_serialize_context_t *c, const hdmx *source_hdmx, hb_subset_plan_t *plan)
139   {
140     TRACE_SERIALIZE (this);
141
142     if (unlikely (!c->extend_min ((*this))))  return_trace (false);
143
144     this->version.set (source_hdmx->version);
145     this->numRecords.set (source_hdmx->numRecords);
146     this->sizeDeviceRecord.set (DeviceRecord::get_size (plan->num_output_glyphs ()));
147
148     for (unsigned int i = 0; i < source_hdmx->numRecords; i++)
149     {
150       DeviceRecord::SubsetView subset_view;
151       subset_view.init (&(*source_hdmx)[i], source_hdmx->sizeDeviceRecord, plan);
152
153       if (!c->start_embed<DeviceRecord> ()->serialize (c, subset_view))
154         return_trace (false);
155     }
156
157     return_trace (true);
158   }
159
160   static size_t get_subsetted_size (const hdmx *source_hdmx, hb_subset_plan_t *plan)
161   {
162     return min_size + source_hdmx->numRecords * DeviceRecord::get_size (plan->num_output_glyphs ());
163   }
164
165   bool subset (hb_subset_plan_t *plan) const
166   {
167     size_t dest_size = get_subsetted_size (this, plan);
168     hdmx *dest = (hdmx *) malloc (dest_size);
169     if (unlikely (!dest))
170     {
171       DEBUG_MSG(SUBSET, nullptr, "Unable to alloc %lu for hdmx subset output.", (unsigned long) dest_size);
172       return false;
173     }
174
175     hb_serialize_context_t c (dest, dest_size);
176     hdmx *hdmx_prime = c.start_serialize<hdmx> ();
177     if (!hdmx_prime || !hdmx_prime->serialize (&c, this, plan))
178     {
179       free (dest);
180       DEBUG_MSG(SUBSET, nullptr, "Failed to serialize write new hdmx.");
181       return false;
182     }
183     c.end_serialize ();
184
185     hb_blob_t *hdmx_prime_blob = hb_blob_create ((const char *) dest,
186                                                  dest_size,
187                                                  HB_MEMORY_MODE_READONLY,
188                                                  dest,
189                                                  free);
190     bool result = plan->add_table (HB_OT_TAG_hdmx, hdmx_prime_blob);
191     hb_blob_destroy (hdmx_prime_blob);
192
193     return result;
194   }
195
196   bool sanitize (hb_sanitize_context_t *c) const
197   {
198     TRACE_SANITIZE (this);
199     return_trace (c->check_struct (this) &&
200                   !hb_unsigned_mul_overflows (numRecords, sizeDeviceRecord) &&
201                   sizeDeviceRecord >= DeviceRecord::min_size &&
202                   c->check_range (this, get_size ()));
203   }
204
205   protected:
206   HBUINT16              version;                /* Table version number (0) */
207   HBUINT16              numRecords;             /* Number of device records. */
208   HBUINT32              sizeDeviceRecord;       /* Size of a device record, 32-bit aligned. */
209   DeviceRecord          firstDeviceRecord;      /* Array of device records. */
210   public:
211   DEFINE_SIZE_MIN (8);
212 };
213
214 } /* namespace OT */
215
216
217 #endif /* HB_OT_HDMX_TABLE_HH */