Garret Rieger | fe42862 | 2018-02-21 14:18:49 -0800 | [diff] [blame] | 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 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 30 | #include "hb-open-type.hh" |
Garret Rieger | fe42862 | 2018-02-21 14:18:49 -0800 | [diff] [blame] | 31 | |
Ebrahim Byagowi | a02c3ee | 2018-04-12 13:38:19 +0430 | [diff] [blame] | 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 | |
Garret Rieger | fe42862 | 2018-02-21 14:18:49 -0800 | [diff] [blame] | 39 | namespace OT { |
| 40 | |
Behdad Esfahbod | cf7a6e5 | 2018-02-23 10:34:26 -0800 | [diff] [blame] | 41 | |
Behdad Esfahbod | cf7a6e5 | 2018-02-23 10:34:26 -0800 | [diff] [blame] | 42 | struct DeviceRecord |
| 43 | { |
Garret Rieger | e8ef0e6 | 2019-05-07 17:23:02 -0700 | [diff] [blame] | 44 | static unsigned int get_size (unsigned count) |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 45 | { return hb_ceil_to_4 (min_size + count * HBUINT8::static_size); } |
Behdad Esfahbod | cf7a6e5 | 2018-02-23 10:34:26 -0800 | [diff] [blame] | 46 | |
Behdad Esfahbod | b710176 | 2019-05-08 15:46:51 -0700 | [diff] [blame] | 47 | template<typename Iterator, |
| 48 | hb_requires (hb_is_iterator (Iterator))> |
Behdad Esfahbod | 4d4792c | 2023-06-03 20:34:51 -0600 | [diff] [blame] | 49 | bool serialize (hb_serialize_context_t *c, |
| 50 | unsigned pixelSize, |
| 51 | Iterator it, |
| 52 | const hb_vector_t<hb_codepoint_pair_t> new_to_old_gid_list, |
| 53 | unsigned num_glyphs) |
Behdad Esfahbod | cf7a6e5 | 2018-02-23 10:34:26 -0800 | [diff] [blame] | 54 | { |
| 55 | TRACE_SERIALIZE (this); |
| 56 | |
Behdad Esfahbod | 4d4792c | 2023-06-03 20:34:51 -0600 | [diff] [blame] | 57 | if (unlikely (!c->extend (this, num_glyphs))) return_trace (false); |
Behdad Esfahbod | cf7a6e5 | 2018-02-23 10:34:26 -0800 | [diff] [blame] | 58 | |
Garret Rieger | e8ef0e6 | 2019-05-07 17:23:02 -0700 | [diff] [blame] | 59 | this->pixelSize = pixelSize; |
| 60 | this->maxWidth = |
| 61 | + it |
| 62 | | hb_reduce (hb_max, 0u); |
| 63 | |
Behdad Esfahbod | 4d4792c | 2023-06-03 20:34:51 -0600 | [diff] [blame] | 64 | for (auto &_ : new_to_old_gid_list) |
| 65 | widthsZ[_.first] = *it++; |
Behdad Esfahbod | cf7a6e5 | 2018-02-23 10:34:26 -0800 | [diff] [blame] | 66 | |
| 67 | return_trace (true); |
| 68 | } |
| 69 | |
Garret Rieger | e8ef0e6 | 2019-05-07 17:23:02 -0700 | [diff] [blame] | 70 | bool sanitize (hb_sanitize_context_t *c, unsigned sizeDeviceRecord) const |
Behdad Esfahbod | cf7a6e5 | 2018-02-23 10:34:26 -0800 | [diff] [blame] | 71 | { |
| 72 | TRACE_SANITIZE (this); |
| 73 | return_trace (likely (c->check_struct (this) && |
Behdad Esfahbod | 0e0af11 | 2018-11-11 12:54:16 -0500 | [diff] [blame] | 74 | c->check_range (this, sizeDeviceRecord))); |
Behdad Esfahbod | cf7a6e5 | 2018-02-23 10:34:26 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Behdad Esfahbod | 0e0af11 | 2018-11-11 12:54:16 -0500 | [diff] [blame] | 77 | HBUINT8 pixelSize; /* Pixel size for following widths (as ppem). */ |
| 78 | HBUINT8 maxWidth; /* Maximum width. */ |
| 79 | UnsizedArrayOf<HBUINT8> widthsZ; /* Array of widths (numGlyphs is from the 'maxp' table). */ |
Behdad Esfahbod | 84d4bb9 | 2018-02-23 10:38:35 -0800 | [diff] [blame] | 80 | public: |
Behdad Esfahbod | 3050126 | 2023-04-21 11:42:18 -0600 | [diff] [blame] | 81 | DEFINE_SIZE_UNBOUNDED (2); |
Behdad Esfahbod | cf7a6e5 | 2018-02-23 10:34:26 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | |
Garret Rieger | fe42862 | 2018-02-21 14:18:49 -0800 | [diff] [blame] | 85 | struct hdmx |
| 86 | { |
Behdad Esfahbod | ef00654 | 2019-01-22 12:08:57 +0100 | [diff] [blame] | 87 | static constexpr hb_tag_t tableTag = HB_OT_TAG_hdmx; |
Garret Rieger | bd18b6a | 2018-02-21 17:42:58 -0800 | [diff] [blame] | 88 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 89 | unsigned int get_size () const |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 90 | { return min_size + numRecords * sizeDeviceRecord; } |
Garret Rieger | fe42862 | 2018-02-21 14:18:49 -0800 | [diff] [blame] | 91 | |
Behdad Esfahbod | b710176 | 2019-05-08 15:46:51 -0700 | [diff] [blame] | 92 | template<typename Iterator, |
| 93 | hb_requires (hb_is_iterator (Iterator))> |
Behdad Esfahbod | 4d4792c | 2023-06-03 20:34:51 -0600 | [diff] [blame] | 94 | bool serialize (hb_serialize_context_t *c, |
| 95 | unsigned version, |
| 96 | Iterator it, |
Behdad Esfahbod | 2d15892 | 2023-06-07 15:47:08 -0600 | [diff] [blame] | 97 | const hb_vector_t<hb_codepoint_pair_t> &new_to_old_gid_list, |
Behdad Esfahbod | 4d4792c | 2023-06-03 20:34:51 -0600 | [diff] [blame] | 98 | unsigned num_glyphs) |
Garret Rieger | ab7a8f3 | 2018-02-21 15:15:22 -0800 | [diff] [blame] | 99 | { |
| 100 | TRACE_SERIALIZE (this); |
| 101 | |
| 102 | if (unlikely (!c->extend_min ((*this)))) return_trace (false); |
| 103 | |
Garret Rieger | e8ef0e6 | 2019-05-07 17:23:02 -0700 | [diff] [blame] | 104 | this->version = version; |
| 105 | this->numRecords = it.len (); |
Behdad Esfahbod | 4d4792c | 2023-06-03 20:34:51 -0600 | [diff] [blame] | 106 | this->sizeDeviceRecord = DeviceRecord::get_size (num_glyphs); |
Garret Rieger | ab7a8f3 | 2018-02-21 15:15:22 -0800 | [diff] [blame] | 107 | |
Ebrahim Byagowi | a224f41 | 2020-03-13 08:33:34 +0330 | [diff] [blame] | 108 | for (const hb_item_type<Iterator>& _ : +it) |
Behdad Esfahbod | 4d4792c | 2023-06-03 20:34:51 -0600 | [diff] [blame] | 109 | c->start_embed<DeviceRecord> ()->serialize (c, _.first, _.second, new_to_old_gid_list, num_glyphs); |
Garret Rieger | ab7a8f3 | 2018-02-21 15:15:22 -0800 | [diff] [blame] | 110 | |
Garret Rieger | 73ed59f | 2021-03-17 15:53:10 -0700 | [diff] [blame] | 111 | return_trace (c->successful ()); |
Garret Rieger | ab7a8f3 | 2018-02-21 15:15:22 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Garret Rieger | d5decf9 | 2019-05-07 15:47:38 -0700 | [diff] [blame] | 114 | |
| 115 | bool subset (hb_subset_context_t *c) const |
Garret Rieger | 84b68e5 | 2018-02-21 15:43:47 -0800 | [diff] [blame] | 116 | { |
Garret Rieger | d5decf9 | 2019-05-07 15:47:38 -0700 | [diff] [blame] | 117 | TRACE_SUBSET (this); |
Garret Rieger | 84b68e5 | 2018-02-21 15:43:47 -0800 | [diff] [blame] | 118 | |
Behdad Esfahbod | 8274130 | 2023-06-04 09:41:41 -0600 | [diff] [blame] | 119 | auto *hdmx_prime = c->serializer->start_embed <hdmx> (); |
Garret Rieger | 6704cde | 2018-02-21 16:00:10 -0800 | [diff] [blame] | 120 | |
Behdad Esfahbod | 5b16793 | 2023-06-03 20:45:52 -0600 | [diff] [blame] | 121 | unsigned num_input_glyphs = get_num_glyphs (); |
Garret Rieger | e8ef0e6 | 2019-05-07 17:23:02 -0700 | [diff] [blame] | 122 | auto it = |
Behdad Esfahbod | 00195a2 | 2019-05-09 12:14:36 -0700 | [diff] [blame] | 123 | + hb_range ((unsigned) numRecords) |
Behdad Esfahbod | 5b16793 | 2023-06-03 20:45:52 -0600 | [diff] [blame] | 124 | | hb_map ([c, num_input_glyphs, this] (unsigned _) |
Behdad Esfahbod | b710176 | 2019-05-08 15:46:51 -0700 | [diff] [blame] | 125 | { |
Garret Rieger | e8ef0e6 | 2019-05-07 17:23:02 -0700 | [diff] [blame] | 126 | const DeviceRecord *device_record = |
| 127 | &StructAtOffset<DeviceRecord> (&firstDeviceRecord, |
| 128 | _ * sizeDeviceRecord); |
| 129 | auto row = |
Behdad Esfahbod | 4d4792c | 2023-06-03 20:34:51 -0600 | [diff] [blame] | 130 | + hb_iter (c->plan->new_to_old_gid_list) |
Behdad Esfahbod | 5b16793 | 2023-06-03 20:45:52 -0600 | [diff] [blame] | 131 | | hb_map ([num_input_glyphs, device_record] (hb_codepoint_pair_t _) |
Behdad Esfahbod | b710176 | 2019-05-08 15:46:51 -0700 | [diff] [blame] | 132 | { |
Behdad Esfahbod | 5b16793 | 2023-06-03 20:45:52 -0600 | [diff] [blame] | 133 | return device_record->widthsZ.as_array (num_input_glyphs) [_.second]; |
Garret Rieger | e8ef0e6 | 2019-05-07 17:23:02 -0700 | [diff] [blame] | 134 | }) |
| 135 | ; |
| 136 | return hb_pair ((unsigned) device_record->pixelSize, +row); |
Behdad Esfahbod | b710176 | 2019-05-08 15:46:51 -0700 | [diff] [blame] | 137 | }) |
| 138 | ; |
Garret Rieger | e8ef0e6 | 2019-05-07 17:23:02 -0700 | [diff] [blame] | 139 | |
Behdad Esfahbod | 4d4792c | 2023-06-03 20:34:51 -0600 | [diff] [blame] | 140 | hdmx_prime->serialize (c->serializer, version, it, |
| 141 | c->plan->new_to_old_gid_list, |
| 142 | c->plan->num_output_glyphs ()); |
Garret Rieger | d5decf9 | 2019-05-07 15:47:38 -0700 | [diff] [blame] | 143 | return_trace (true); |
Garret Rieger | ab7a8f3 | 2018-02-21 15:15:22 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Garret Rieger | e8ef0e6 | 2019-05-07 17:23:02 -0700 | [diff] [blame] | 146 | unsigned get_num_glyphs () const |
| 147 | { |
| 148 | return sizeDeviceRecord - DeviceRecord::min_size; |
| 149 | } |
| 150 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 151 | bool sanitize (hb_sanitize_context_t *c) const |
Garret Rieger | fe42862 | 2018-02-21 14:18:49 -0800 | [diff] [blame] | 152 | { |
| 153 | TRACE_SANITIZE (this); |
Behdad Esfahbod | af727b4 | 2018-11-16 01:55:39 -0500 | [diff] [blame] | 154 | return_trace (c->check_struct (this) && |
Behdad Esfahbod | 0e0af11 | 2018-11-11 12:54:16 -0500 | [diff] [blame] | 155 | !hb_unsigned_mul_overflows (numRecords, sizeDeviceRecord) && |
Garret Rieger | 40342c9 | 2022-12-21 21:52:28 +0000 | [diff] [blame] | 156 | min_size + numRecords * sizeDeviceRecord > numRecords * sizeDeviceRecord && |
Behdad Esfahbod | 0e0af11 | 2018-11-11 12:54:16 -0500 | [diff] [blame] | 157 | sizeDeviceRecord >= DeviceRecord::min_size && |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 158 | c->check_range (this, get_size ())); |
Garret Rieger | fe42862 | 2018-02-21 14:18:49 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Behdad Esfahbod | c2e4713 | 2018-02-23 10:45:03 -0800 | [diff] [blame] | 161 | protected: |
Ebrahim Byagowi | 08428a1 | 2020-04-24 23:45:17 +0430 | [diff] [blame] | 162 | HBUINT16 version; /* Table version number (0) */ |
| 163 | HBUINT16 numRecords; /* Number of device records. */ |
| 164 | HBUINT32 sizeDeviceRecord; |
| 165 | /* Size of a device record, 32-bit aligned. */ |
| 166 | DeviceRecord firstDeviceRecord; |
| 167 | /* Array of device records. */ |
Behdad Esfahbod | c2e4713 | 2018-02-23 10:45:03 -0800 | [diff] [blame] | 168 | public: |
Behdad Esfahbod | af727b4 | 2018-11-16 01:55:39 -0500 | [diff] [blame] | 169 | DEFINE_SIZE_MIN (8); |
Garret Rieger | fe42862 | 2018-02-21 14:18:49 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | } /* namespace OT */ |
| 173 | |
| 174 | |
| 175 | #endif /* HB_OT_HDMX_TABLE_HH */ |