blob: b2059e86e64d97667cb6f4cab7d972deea04e724 [file] [log] [blame]
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -07001/*
Michiharu Ariza0dfa5842018-11-12 08:47:07 -08002 * Copyright © 2018 Adobe Inc.
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -07003 *
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 * Adobe Author(s): Michiharu Ariza
25 */
26
27#ifndef HB_OT_VORG_TABLE_HH
28#define HB_OT_VORG_TABLE_HH
29
30#include "hb-open-type.hh"
31
32/*
33 * VORG -- Vertical Origin Table
34 * https://docs.microsoft.com/en-us/typography/opentype/spec/vorg
35 */
36#define HB_OT_TAG_VORG HB_TAG('V','O','R','G')
37
38namespace OT {
39
40struct VertOriginMetric
41{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033042 int cmp (hb_codepoint_t g) const { return glyph.cmp (g); }
Behdad Esfahbod097ecfd2018-10-23 02:09:42 -070043
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033044 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -070045 {
46 TRACE_SANITIZE (this);
47 return_trace (c->check_struct (this));
48 }
49
50 public:
51 GlyphID glyph;
52 FWORD vertOriginY;
53
54 public:
55 DEFINE_SIZE_STATIC (4);
56};
57
58struct VORG
59{
Behdad Esfahbodef006542019-01-22 12:08:57 +010060 static constexpr hb_tag_t tableTag = HB_OT_TAG_VORG;
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -070061
Ebrahim Byagowie4120082018-12-17 21:31:01 +033062 bool has_data () const { return version.to_int (); }
Behdad Esfahbod097ecfd2018-10-23 02:09:42 -070063
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033064 int get_y_origin (hb_codepoint_t glyph) const
Behdad Esfahbod097ecfd2018-10-23 02:09:42 -070065 {
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -050066 unsigned int i;
67 if (!vertYOrigins.bfind (glyph, &i))
68 return defaultVertOriginY;
69 return vertYOrigins[i].vertOriginY;
Behdad Esfahbod097ecfd2018-10-23 02:09:42 -070070 }
71
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033072 bool _subset (const hb_subset_plan_t *plan HB_UNUSED,
73 const VORG *vorg_table,
74 const hb_vector_t<VertOriginMetric> &subset_metrics,
75 unsigned int dest_sz,
76 void *dest) const
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -070077 {
78 hb_serialize_context_t c (dest, dest_sz);
79
80 VORG *subset_table = c.start_serialize<VORG> ();
81 if (unlikely (!c.extend_min (*subset_table)))
82 return false;
83
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -070084 subset_table->version.major = 1;
85 subset_table->version.minor = 0;
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -070086
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -070087 subset_table->defaultVertOriginY = vorg_table->defaultVertOriginY;
88 subset_table->vertYOrigins.len = subset_metrics.length;
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -070089
90 bool success = true;
Behdad Esfahbod474a1202018-12-21 18:46:51 -050091 if (subset_metrics.length > 0)
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -070092 {
Behdad Esfahbod474a1202018-12-21 18:46:51 -050093 unsigned int size = VertOriginMetric::static_size * subset_metrics.length;
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -070094 VertOriginMetric *metrics = c.allocate_size<VertOriginMetric> (size);
95 if (likely (metrics != nullptr))
Michiharu Arizad8c69132018-11-30 18:58:14 -080096 memcpy (metrics, &subset_metrics[0], size);
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -070097 else
98 success = false;
99 }
100 c.end_serialize ();
101
102 return success;
103 }
104
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330105 bool subset (hb_subset_plan_t *plan) const
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -0700106 {
107 hb_blob_t *vorg_blob = hb_sanitize_context_t().reference_table<VORG> (plan->source);
108 const VORG *vorg_table = vorg_blob->as<VORG> ();
109
110 /* count the number of glyphs to be included in the subset table */
111 hb_vector_t<VertOriginMetric> subset_metrics;
112 subset_metrics.init ();
Garret Rieger925be292019-01-25 18:04:41 -0800113
114
115 hb_codepoint_t old_glyph = HB_SET_VALUE_INVALID;
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -0700116 unsigned int i = 0;
Garret Rieger925be292019-01-25 18:04:41 -0800117 while (i < vertYOrigins.len
118 && plan->glyphset ()->next (&old_glyph))
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -0700119 {
Garret Rieger925be292019-01-25 18:04:41 -0800120 while (old_glyph > vertYOrigins[i].glyph)
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -0700121 {
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -0700122 i++;
Garret Rieger925be292019-01-25 18:04:41 -0800123 if (i >= vertYOrigins.len)
124 break;
125 }
126
127 if (old_glyph == vertYOrigins[i].glyph)
128 {
129 hb_codepoint_t new_glyph;
130 if (plan->new_gid_for_old_gid (old_glyph, &new_glyph))
131 {
132 VertOriginMetric *metrics = subset_metrics.push ();
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700133 metrics->glyph = new_glyph;
134 metrics->vertOriginY = vertYOrigins[i].vertOriginY;
Garret Rieger925be292019-01-25 18:04:41 -0800135 }
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -0700136 }
137 }
138
139 /* alloc the new table */
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500140 unsigned int dest_sz = VORG::min_size + VertOriginMetric::static_size * subset_metrics.length;
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -0700141 void *dest = (void *) malloc (dest_sz);
142 if (unlikely (!dest))
143 {
144 subset_metrics.fini ();
145 hb_blob_destroy (vorg_blob);
146 return false;
147 }
148
149 /* serialize the new table */
150 if (!_subset (plan, vorg_table, subset_metrics, dest_sz, dest))
151 {
152 subset_metrics.fini ();
153 free (dest);
154 hb_blob_destroy (vorg_blob);
155 return false;
156 }
157
158 hb_blob_t *result = hb_blob_create ((const char *)dest,
159 dest_sz,
160 HB_MEMORY_MODE_READONLY,
161 dest,
162 free);
163 bool success = plan->add_table (HB_OT_TAG_VORG, result);
164 hb_blob_destroy (result);
165 subset_metrics.fini ();
166 hb_blob_destroy (vorg_blob);
167 return success;
168 }
169
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330170 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -0700171 {
172 TRACE_SANITIZE (this);
173 return_trace (c->check_struct (this) &&
174 version.major == 1 &&
175 vertYOrigins.sanitize (c));
176 }
177
178 protected:
179 FixedVersion<> version; /* Version of VORG table. Set to 0x00010000u. */
Behdad Esfahbod097ecfd2018-10-23 02:09:42 -0700180 FWORD defaultVertOriginY; /* The default vertical origin. */
181 SortedArrayOf<VertOriginMetric>
182 vertYOrigins; /* The array of vertical origins. */
Behdad Esfahbod6fb24d52018-10-23 01:58:59 -0700183
184 public:
185 DEFINE_SIZE_ARRAY(8, vertYOrigins);
186};
187} /* namespace OT */
188
189#endif /* HB_OT_VORG_TABLE_HH */