blob: c9c391bad5f25eb9efa595f5e060b44d9c7577d6 [file] [log] [blame]
Garret Riegerfe428622018-02-21 14:18:49 -08001/*
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 Esfahbodc77ae402018-08-25 22:36:36 -070030#include "hb-open-type.hh"
Garret Riegerfe428622018-02-21 14:18:49 -080031
Ebrahim Byagowia02c3ee2018-04-12 13:38:19 +043032/*
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 Riegerfe428622018-02-21 14:18:49 -080039namespace OT {
40
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080041
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080042struct DeviceRecord
43{
Garret Riegere8ef0e62019-05-07 17:23:02 -070044 static unsigned int get_size (unsigned count)
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033045 { return hb_ceil_to_4 (min_size + count * HBUINT8::static_size); }
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080046
Behdad Esfahbodb7101762019-05-08 15:46:51 -070047 template<typename Iterator,
48 hb_requires (hb_is_iterator (Iterator))>
Garret Riegere8ef0e62019-05-07 17:23:02 -070049 bool serialize (hb_serialize_context_t *c, unsigned pixelSize, Iterator it)
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080050 {
51 TRACE_SERIALIZE (this);
52
Garret Riegere8ef0e62019-05-07 17:23:02 -070053 unsigned length = it.len ();
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080054
Garret Riegere8ef0e62019-05-07 17:23:02 -070055 if (unlikely (!c->extend (*this, length))) return_trace (false);
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080056
Garret Riegere8ef0e62019-05-07 17:23:02 -070057 this->pixelSize = pixelSize;
58 this->maxWidth =
59 + it
60 | hb_reduce (hb_max, 0u);
61
62 + it
63 | hb_sink (widthsZ.as_array (length));
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080064
65 return_trace (true);
66 }
67
Garret Riegere8ef0e62019-05-07 17:23:02 -070068 bool sanitize (hb_sanitize_context_t *c, unsigned sizeDeviceRecord) const
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080069 {
70 TRACE_SANITIZE (this);
71 return_trace (likely (c->check_struct (this) &&
Behdad Esfahbod0e0af112018-11-11 12:54:16 -050072 c->check_range (this, sizeDeviceRecord)));
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080073 }
74
Behdad Esfahbod0e0af112018-11-11 12:54:16 -050075 HBUINT8 pixelSize; /* Pixel size for following widths (as ppem). */
76 HBUINT8 maxWidth; /* Maximum width. */
77 UnsizedArrayOf<HBUINT8> widthsZ; /* Array of widths (numGlyphs is from the 'maxp' table). */
Behdad Esfahbod84d4bb92018-02-23 10:38:35 -080078 public:
Behdad Esfahboddff2c452018-09-10 23:29:26 +020079 DEFINE_SIZE_ARRAY (2, widthsZ);
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080080};
81
82
Garret Riegerfe428622018-02-21 14:18:49 -080083struct hdmx
84{
Behdad Esfahbodef006542019-01-22 12:08:57 +010085 static constexpr hb_tag_t tableTag = HB_OT_TAG_hdmx;
Garret Riegerbd18b6a2018-02-21 17:42:58 -080086
Ebrahim Byagowie4120082018-12-17 21:31:01 +033087 unsigned int get_size () const
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033088 { return min_size + numRecords * sizeDeviceRecord; }
Garret Riegerfe428622018-02-21 14:18:49 -080089
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033090 const DeviceRecord& operator [] (unsigned int i) const
Garret Riegerfe428622018-02-21 14:18:49 -080091 {
Behdad Esfahbodcb4bf852018-11-16 02:02:24 -050092 /* XXX Null(DeviceRecord) is NOT safe as it's num-glyphs lengthed.
93 * https://github.com/harfbuzz/harfbuzz/issues/1300 */
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +033094 if (unlikely (i >= numRecords)) return Null (DeviceRecord);
Behdad Esfahbodaf727b42018-11-16 01:55:39 -050095 return StructAtOffset<DeviceRecord> (&this->firstDeviceRecord, i * sizeDeviceRecord);
Garret Riegerfe428622018-02-21 14:18:49 -080096 }
97
Behdad Esfahbodb7101762019-05-08 15:46:51 -070098 template<typename Iterator,
99 hb_requires (hb_is_iterator (Iterator))>
Garret Riegere8ef0e62019-05-07 17:23:02 -0700100 bool serialize (hb_serialize_context_t *c, unsigned version, Iterator it)
Garret Riegerab7a8f32018-02-21 15:15:22 -0800101 {
102 TRACE_SERIALIZE (this);
103
104 if (unlikely (!c->extend_min ((*this)))) return_trace (false);
105
Garret Riegere8ef0e62019-05-07 17:23:02 -0700106 this->version = version;
107 this->numRecords = it.len ();
Behdad Esfahbodb7101762019-05-08 15:46:51 -0700108 this->sizeDeviceRecord = DeviceRecord::get_size (it ? (*it).second.len () : 0);
Garret Riegerab7a8f32018-02-21 15:15:22 -0800109
Ebrahim Byagowia224f412020-03-13 08:33:34 +0330110 for (const hb_item_type<Iterator>& _ : +it)
111 c->start_embed<DeviceRecord> ()->serialize (c, _.first, _.second);
Garret Riegerab7a8f32018-02-21 15:15:22 -0800112
Garret Riegere8ef0e62019-05-07 17:23:02 -0700113 return_trace (c->successful);
Garret Riegerab7a8f32018-02-21 15:15:22 -0800114 }
115
Garret Riegerd5decf92019-05-07 15:47:38 -0700116
117 bool subset (hb_subset_context_t *c) const
Garret Rieger84b68e52018-02-21 15:43:47 -0800118 {
Garret Riegerd5decf92019-05-07 15:47:38 -0700119 TRACE_SUBSET (this);
Garret Rieger84b68e52018-02-21 15:43:47 -0800120
Garret Riegerd5decf92019-05-07 15:47:38 -0700121 hdmx *hdmx_prime = c->serializer->start_embed <hdmx> ();
122 if (unlikely (!hdmx_prime)) return_trace (false);
Garret Rieger6704cde2018-02-21 16:00:10 -0800123
Garret Riegere8ef0e62019-05-07 17:23:02 -0700124 auto it =
Behdad Esfahbod00195a22019-05-09 12:14:36 -0700125 + hb_range ((unsigned) numRecords)
Behdad Esfahbod78d35f02019-05-15 18:15:05 -0700126 | hb_map ([c, this] (unsigned _)
Behdad Esfahbodb7101762019-05-08 15:46:51 -0700127 {
Garret Riegere8ef0e62019-05-07 17:23:02 -0700128 const DeviceRecord *device_record =
129 &StructAtOffset<DeviceRecord> (&firstDeviceRecord,
130 _ * sizeDeviceRecord);
131 auto row =
Behdad Esfahbod00195a22019-05-09 12:14:36 -0700132 + hb_range (c->plan->num_output_glyphs ())
Garret Riegere8ef0e62019-05-07 17:23:02 -0700133 | hb_map (c->plan->reverse_glyph_map)
Ebrahim Byagowi4dc3db72020-02-06 12:12:41 +0330134 | hb_map ([this, c, device_record] (hb_codepoint_t _)
Behdad Esfahbodb7101762019-05-08 15:46:51 -0700135 {
Garret Riegere8ef0e62019-05-07 17:23:02 -0700136 if (c->plan->is_empty_glyph (_))
Ebrahim Byagowi4dc3db72020-02-06 12:12:41 +0330137 return Null (HBUINT8);
Garret Riegere8ef0e62019-05-07 17:23:02 -0700138 return device_record->widthsZ.as_array (get_num_glyphs ()) [_];
139 })
140 ;
141 return hb_pair ((unsigned) device_record->pixelSize, +row);
Behdad Esfahbodb7101762019-05-08 15:46:51 -0700142 })
143 ;
Garret Riegere8ef0e62019-05-07 17:23:02 -0700144
145 hdmx_prime->serialize (c->serializer, version, it);
Garret Riegerd5decf92019-05-07 15:47:38 -0700146 return_trace (true);
Garret Riegerab7a8f32018-02-21 15:15:22 -0800147 }
148
Garret Riegere8ef0e62019-05-07 17:23:02 -0700149 unsigned get_num_glyphs () const
150 {
151 return sizeDeviceRecord - DeviceRecord::min_size;
152 }
153
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330154 bool sanitize (hb_sanitize_context_t *c) const
Garret Riegerfe428622018-02-21 14:18:49 -0800155 {
156 TRACE_SANITIZE (this);
Behdad Esfahbodaf727b42018-11-16 01:55:39 -0500157 return_trace (c->check_struct (this) &&
Behdad Esfahbod0e0af112018-11-11 12:54:16 -0500158 !hb_unsigned_mul_overflows (numRecords, sizeDeviceRecord) &&
159 sizeDeviceRecord >= DeviceRecord::min_size &&
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330160 c->check_range (this, get_size ()));
Garret Riegerfe428622018-02-21 14:18:49 -0800161 }
162
Behdad Esfahbodc2e47132018-02-23 10:45:03 -0800163 protected:
Ebrahim Byagowi08428a12020-04-24 23:45:17 +0430164 HBUINT16 version; /* Table version number (0) */
165 HBUINT16 numRecords; /* Number of device records. */
166 HBUINT32 sizeDeviceRecord;
167 /* Size of a device record, 32-bit aligned. */
168 DeviceRecord firstDeviceRecord;
169 /* Array of device records. */
Behdad Esfahbodc2e47132018-02-23 10:45:03 -0800170 public:
Behdad Esfahbodaf727b42018-11-16 01:55:39 -0500171 DEFINE_SIZE_MIN (8);
Garret Riegerfe428622018-02-21 14:18:49 -0800172};
173
174} /* namespace OT */
175
176
177#endif /* HB_OT_HDMX_TABLE_HH */