2 * Copyright © 2018 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Garret Rieger
27 #ifndef HB_OT_HDMX_TABLE_HH
28 #define HB_OT_HDMX_TABLE_HH
30 #include "hb-open-type.hh"
33 * hdmx -- Horizontal Device Metrics
34 * https://docs.microsoft.com/en-us/typography/opentype/spec/hdmx
36 #define HB_OT_TAG_hdmx HB_TAG('h','d','m','x')
46 const DeviceRecord *source_device_record;
47 unsigned int sizeDeviceRecord;
48 hb_subset_plan_t *subset_plan;
50 void init (const DeviceRecord *source_device_record,
51 unsigned int sizeDeviceRecord,
52 hb_subset_plan_t *subset_plan)
54 this->source_device_record = source_device_record;
55 this->sizeDeviceRecord = sizeDeviceRecord;
56 this->subset_plan = subset_plan;
59 unsigned int len () const
60 { return this->subset_plan->num_output_glyphs (); }
62 const HBUINT8* operator [] (unsigned int new_gid) const
64 if (unlikely (new_gid >= len ())) return nullptr;
66 hb_codepoint_t old_gid;
67 if (!this->subset_plan->old_gid_for_new_gid (new_gid, &old_gid))
68 return &Null(HBUINT8);
70 if (old_gid >= sizeDeviceRecord - DeviceRecord::min_size)
72 return &(this->source_device_record->widthsZ[old_gid]);
76 static unsigned int get_size (unsigned int count)
77 { return hb_ceil_to_4 (min_size + count * HBUINT8::static_size); }
79 bool serialize (hb_serialize_context_t *c, const SubsetView &subset_view)
81 TRACE_SERIALIZE (this);
83 unsigned int size = get_size (subset_view.len ());
84 if (unlikely (!c->allocate_size<DeviceRecord> (size)))
86 DEBUG_MSG(SUBSET, nullptr, "Couldn't allocate enough space for DeviceRecord: %d.",
91 this->pixelSize.set (subset_view.source_device_record->pixelSize);
92 this->maxWidth.set (subset_view.source_device_record->maxWidth);
94 for (unsigned int i = 0; i < subset_view.len (); i++)
96 const HBUINT8 *width = subset_view[i];
99 DEBUG_MSG(SUBSET, nullptr, "HDMX width for new gid %d is missing.", i);
100 return_trace (false);
102 widthsZ[i].set (*width);
108 bool sanitize (hb_sanitize_context_t *c, unsigned int sizeDeviceRecord) const
110 TRACE_SANITIZE (this);
111 return_trace (likely (c->check_struct (this) &&
112 c->check_range (this, sizeDeviceRecord)));
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). */
119 DEFINE_SIZE_ARRAY (2, widthsZ);
125 static constexpr hb_tag_t tableTag = HB_OT_TAG_hdmx;
127 unsigned int get_size () const
128 { return min_size + numRecords * sizeDeviceRecord; }
130 const DeviceRecord& operator [] (unsigned int i) const
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);
138 bool serialize (hb_serialize_context_t *c, const hdmx *source_hdmx, hb_subset_plan_t *plan)
140 TRACE_SERIALIZE (this);
142 if (unlikely (!c->extend_min ((*this)))) return_trace (false);
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 ()));
148 for (unsigned int i = 0; i < source_hdmx->numRecords; i++)
150 DeviceRecord::SubsetView subset_view;
151 subset_view.init (&(*source_hdmx)[i], source_hdmx->sizeDeviceRecord, plan);
153 if (!c->start_embed<DeviceRecord> ()->serialize (c, subset_view))
154 return_trace (false);
160 static size_t get_subsetted_size (const hdmx *source_hdmx, hb_subset_plan_t *plan)
162 return min_size + source_hdmx->numRecords * DeviceRecord::get_size (plan->num_output_glyphs ());
165 bool subset (hb_subset_plan_t *plan) const
167 size_t dest_size = get_subsetted_size (this, plan);
168 hdmx *dest = (hdmx *) malloc (dest_size);
169 if (unlikely (!dest))
171 DEBUG_MSG(SUBSET, nullptr, "Unable to alloc %lu for hdmx subset output.", (unsigned long) dest_size);
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))
180 DEBUG_MSG(SUBSET, nullptr, "Failed to serialize write new hdmx.");
185 hb_blob_t *hdmx_prime_blob = hb_blob_create ((const char *) dest,
187 HB_MEMORY_MODE_READONLY,
190 bool result = plan->add_table (HB_OT_TAG_hdmx, hdmx_prime_blob);
191 hb_blob_destroy (hdmx_prime_blob);
196 bool sanitize (hb_sanitize_context_t *c) const
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 ()));
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. */
217 #endif /* HB_OT_HDMX_TABLE_HH */