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-private.hh"
36 * hdmx - Horizontal Device Metric
39 #define HB_OT_TAG_hdmx HB_TAG('h','d','m','x')
45 const DeviceRecord *source_device_record;
46 hb_subset_plan_t *subset_plan;
48 inline void init(const DeviceRecord *source_device_record,
49 hb_subset_plan_t *subset_plan)
51 this->source_device_record = source_device_record;
52 this->subset_plan = subset_plan;
55 inline unsigned int len () const
57 return this->subset_plan->gids_to_retain_sorted.len;
60 inline const HBUINT8& operator [] (unsigned int i) const
62 if (unlikely (i >= len())) return Null(HBUINT8);
63 hb_codepoint_t gid = this->subset_plan->gids_to_retain_sorted [i];
64 return this->source_device_record->widths[gid];
68 static inline unsigned int get_size (unsigned int count)
70 unsigned int raw_size = min_size + count * HBUINT8::static_size;
72 /* Align to 32 bits */
73 return raw_size + (4 - (raw_size % 4));
77 inline bool serialize (hb_serialize_context_t *c, const SubsetView &subset_view)
79 TRACE_SERIALIZE (this);
81 if (unlikely (!c->allocate_size<DeviceRecord> (get_size (subset_view.len()))))
84 this->pixel_size.set (subset_view.source_device_record->pixel_size);
85 this->max_width.set (subset_view.source_device_record->max_width);
87 for (unsigned int i = 0; i < subset_view.len(); i++)
88 widths[i].set (subset_view[i]);
93 inline bool sanitize (hb_sanitize_context_t *c, unsigned int size_device_record) const
95 TRACE_SANITIZE (this);
96 return_trace (likely (c->check_struct (this) &&
97 c->check_range (this, size_device_record)));
100 HBUINT8 pixel_size; /* Pixel size for following widths (as ppem). */
101 HBUINT8 max_width; /* Maximum width. */
102 HBUINT8 widths[VAR]; /* Array of widths (numGlyphs is from the 'maxp' table). */
104 DEFINE_SIZE_ARRAY (2, widths);
110 static const hb_tag_t tableTag = HB_OT_TAG_hdmx;
112 inline unsigned int get_size (void) const
114 return min_size + num_records * size_device_record;
117 inline const DeviceRecord& operator [] (unsigned int i) const
119 if (unlikely (i >= num_records)) return Null(DeviceRecord);
120 return StructAtOffset<DeviceRecord> (this, min_size + i * size_device_record);
123 inline bool serialize (hb_serialize_context_t *c, const hdmx *source_hdmx, hb_subset_plan_t *plan)
125 TRACE_SERIALIZE (this);
127 if (unlikely (!c->extend_min ((*this)))) return_trace (false);
129 this->version.set (source_hdmx->version);
130 this->num_records.set (source_hdmx->num_records);
131 this->size_device_record.set (DeviceRecord::get_size (plan->gids_to_retain_sorted.len));
133 for (unsigned int i = 0; i < source_hdmx->num_records; i++)
135 DeviceRecord::SubsetView subset_view;
136 subset_view.init (&(*source_hdmx)[i], plan);
138 c->start_embed<DeviceRecord> ()->serialize (c, subset_view);
144 static inline size_t get_subsetted_size (hb_subset_plan_t *plan)
146 return min_size + DeviceRecord::get_size (plan->gids_to_retain_sorted.len);
149 inline bool subset (hb_subset_plan_t *plan) const
151 size_t dest_size = get_subsetted_size (plan);
152 hdmx *dest = (hdmx *) malloc (dest_size);
153 if (unlikely (!dest))
155 DEBUG_MSG(SUBSET, nullptr, "Unable to alloc %lu for hdmx subset output.", (unsigned long) dest_size);
159 hb_serialize_context_t c (dest, dest_size);
160 hdmx *hdmx_prime = c.start_serialize<hdmx> ();
161 if (!hdmx_prime || !hdmx_prime->serialize (&c, this, plan)) {
167 hb_blob_t *hdmx_prime_blob = hb_blob_create ((const char *) dest,
169 HB_MEMORY_MODE_READONLY,
172 bool result = hb_subset_plan_add_table (plan, HB_OT_TAG_hdmx, hdmx_prime_blob);
173 hb_blob_destroy (hdmx_prime_blob);
178 inline bool sanitize (hb_sanitize_context_t *c) const
180 TRACE_SANITIZE (this);
181 return_trace (c->check_struct (this) && version == 0 &&
182 !_hb_unsigned_int_mul_overflows (num_records, size_device_record) &&
183 c->check_range (this, get_size()));
187 HBUINT16 version; /* Table version number (0) */
188 HBUINT16 num_records; /* Number of device records. */
189 HBUINT32 size_device_record; /* Size of a device record, 32-bit aligned. */
190 HBUINT8 data[VAR]; /* Array of device records. */
192 DEFINE_SIZE_ARRAY (8, data);
198 #endif /* HB_OT_HDMX_TABLE_HH */