blob: cbcf6f5f22f22ef17f00e8106f3cce7c675a0baa [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))>
Behdad Esfahbod4d4792c2023-06-03 20:34:51 -060049 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 Esfahbodcf7a6e52018-02-23 10:34:26 -080054 {
55 TRACE_SERIALIZE (this);
56
Behdad Esfahbod4d4792c2023-06-03 20:34:51 -060057 if (unlikely (!c->extend (this, num_glyphs))) return_trace (false);
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080058
Garret Riegere8ef0e62019-05-07 17:23:02 -070059 this->pixelSize = pixelSize;
60 this->maxWidth =
61 + it
62 | hb_reduce (hb_max, 0u);
63
Behdad Esfahbod4d4792c2023-06-03 20:34:51 -060064 for (auto &_ : new_to_old_gid_list)
65 widthsZ[_.first] = *it++;
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080066
67 return_trace (true);
68 }
69
Garret Riegere8ef0e62019-05-07 17:23:02 -070070 bool sanitize (hb_sanitize_context_t *c, unsigned sizeDeviceRecord) const
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080071 {
72 TRACE_SANITIZE (this);
73 return_trace (likely (c->check_struct (this) &&
Behdad Esfahbod0e0af112018-11-11 12:54:16 -050074 c->check_range (this, sizeDeviceRecord)));
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080075 }
76
Behdad Esfahbod0e0af112018-11-11 12:54:16 -050077 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 Esfahbod84d4bb92018-02-23 10:38:35 -080080 public:
Behdad Esfahbod30501262023-04-21 11:42:18 -060081 DEFINE_SIZE_UNBOUNDED (2);
Behdad Esfahbodcf7a6e52018-02-23 10:34:26 -080082};
83
84
Garret Riegerfe428622018-02-21 14:18:49 -080085struct hdmx
86{
Behdad Esfahbodef006542019-01-22 12:08:57 +010087 static constexpr hb_tag_t tableTag = HB_OT_TAG_hdmx;
Garret Riegerbd18b6a2018-02-21 17:42:58 -080088
Ebrahim Byagowie4120082018-12-17 21:31:01 +033089 unsigned int get_size () const
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033090 { return min_size + numRecords * sizeDeviceRecord; }
Garret Riegerfe428622018-02-21 14:18:49 -080091
Behdad Esfahbodb7101762019-05-08 15:46:51 -070092 template<typename Iterator,
93 hb_requires (hb_is_iterator (Iterator))>
Behdad Esfahbod4d4792c2023-06-03 20:34:51 -060094 bool serialize (hb_serialize_context_t *c,
95 unsigned version,
96 Iterator it,
Behdad Esfahbod2d158922023-06-07 15:47:08 -060097 const hb_vector_t<hb_codepoint_pair_t> &new_to_old_gid_list,
Behdad Esfahbod4d4792c2023-06-03 20:34:51 -060098 unsigned num_glyphs)
Garret Riegerab7a8f32018-02-21 15:15:22 -080099 {
100 TRACE_SERIALIZE (this);
101
102 if (unlikely (!c->extend_min ((*this)))) return_trace (false);
103
Garret Riegere8ef0e62019-05-07 17:23:02 -0700104 this->version = version;
105 this->numRecords = it.len ();
Behdad Esfahbod4d4792c2023-06-03 20:34:51 -0600106 this->sizeDeviceRecord = DeviceRecord::get_size (num_glyphs);
Garret Riegerab7a8f32018-02-21 15:15:22 -0800107
Ebrahim Byagowia224f412020-03-13 08:33:34 +0330108 for (const hb_item_type<Iterator>& _ : +it)
Behdad Esfahbod4d4792c2023-06-03 20:34:51 -0600109 c->start_embed<DeviceRecord> ()->serialize (c, _.first, _.second, new_to_old_gid_list, num_glyphs);
Garret Riegerab7a8f32018-02-21 15:15:22 -0800110
Garret Rieger73ed59f2021-03-17 15:53:10 -0700111 return_trace (c->successful ());
Garret Riegerab7a8f32018-02-21 15:15:22 -0800112 }
113
Garret Riegerd5decf92019-05-07 15:47:38 -0700114
115 bool subset (hb_subset_context_t *c) const
Garret Rieger84b68e52018-02-21 15:43:47 -0800116 {
Garret Riegerd5decf92019-05-07 15:47:38 -0700117 TRACE_SUBSET (this);
Garret Rieger84b68e52018-02-21 15:43:47 -0800118
Behdad Esfahbod82741302023-06-04 09:41:41 -0600119 auto *hdmx_prime = c->serializer->start_embed <hdmx> ();
Garret Rieger6704cde2018-02-21 16:00:10 -0800120
Behdad Esfahbod5b167932023-06-03 20:45:52 -0600121 unsigned num_input_glyphs = get_num_glyphs ();
Garret Riegere8ef0e62019-05-07 17:23:02 -0700122 auto it =
Behdad Esfahbod00195a22019-05-09 12:14:36 -0700123 + hb_range ((unsigned) numRecords)
Behdad Esfahbod5b167932023-06-03 20:45:52 -0600124 | hb_map ([c, num_input_glyphs, this] (unsigned _)
Behdad Esfahbodb7101762019-05-08 15:46:51 -0700125 {
Garret Riegere8ef0e62019-05-07 17:23:02 -0700126 const DeviceRecord *device_record =
127 &StructAtOffset<DeviceRecord> (&firstDeviceRecord,
128 _ * sizeDeviceRecord);
129 auto row =
Behdad Esfahbod4d4792c2023-06-03 20:34:51 -0600130 + hb_iter (c->plan->new_to_old_gid_list)
Behdad Esfahbod5b167932023-06-03 20:45:52 -0600131 | hb_map ([num_input_glyphs, device_record] (hb_codepoint_pair_t _)
Behdad Esfahbodb7101762019-05-08 15:46:51 -0700132 {
Behdad Esfahbod5b167932023-06-03 20:45:52 -0600133 return device_record->widthsZ.as_array (num_input_glyphs) [_.second];
Garret Riegere8ef0e62019-05-07 17:23:02 -0700134 })
135 ;
136 return hb_pair ((unsigned) device_record->pixelSize, +row);
Behdad Esfahbodb7101762019-05-08 15:46:51 -0700137 })
138 ;
Garret Riegere8ef0e62019-05-07 17:23:02 -0700139
Behdad Esfahbod4d4792c2023-06-03 20:34:51 -0600140 hdmx_prime->serialize (c->serializer, version, it,
141 c->plan->new_to_old_gid_list,
142 c->plan->num_output_glyphs ());
Garret Riegerd5decf92019-05-07 15:47:38 -0700143 return_trace (true);
Garret Riegerab7a8f32018-02-21 15:15:22 -0800144 }
145
Garret Riegere8ef0e62019-05-07 17:23:02 -0700146 unsigned get_num_glyphs () const
147 {
148 return sizeDeviceRecord - DeviceRecord::min_size;
149 }
150
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330151 bool sanitize (hb_sanitize_context_t *c) const
Garret Riegerfe428622018-02-21 14:18:49 -0800152 {
153 TRACE_SANITIZE (this);
Behdad Esfahbodaf727b42018-11-16 01:55:39 -0500154 return_trace (c->check_struct (this) &&
Behdad Esfahbod0e0af112018-11-11 12:54:16 -0500155 !hb_unsigned_mul_overflows (numRecords, sizeDeviceRecord) &&
Garret Rieger40342c92022-12-21 21:52:28 +0000156 min_size + numRecords * sizeDeviceRecord > numRecords * sizeDeviceRecord &&
Behdad Esfahbod0e0af112018-11-11 12:54:16 -0500157 sizeDeviceRecord >= DeviceRecord::min_size &&
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330158 c->check_range (this, get_size ()));
Garret Riegerfe428622018-02-21 14:18:49 -0800159 }
160
Behdad Esfahbodc2e47132018-02-23 10:45:03 -0800161 protected:
Ebrahim Byagowi08428a12020-04-24 23:45:17 +0430162 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 Esfahbodc2e47132018-02-23 10:45:03 -0800168 public:
Behdad Esfahbodaf727b42018-11-16 01:55:39 -0500169 DEFINE_SIZE_MIN (8);
Garret Riegerfe428622018-02-21 14:18:49 -0800170};
171
172} /* namespace OT */
173
174
175#endif /* HB_OT_HDMX_TABLE_HH */