2 * Copyright © 2018 Adobe 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 * Adobe Author(s): Michiharu Ariza
27 #ifndef HB_OT_VORG_TABLE_HH
28 #define HB_OT_VORG_TABLE_HH
30 #include "hb-open-type.hh"
33 * VORG -- Vertical Origin Table
34 * https://docs.microsoft.com/en-us/typography/opentype/spec/vorg
36 #define HB_OT_TAG_VORG HB_TAG('V','O','R','G')
40 struct VertOriginMetric
42 int cmp (hb_codepoint_t g) const { return glyph.cmp (g); }
44 bool sanitize (hb_sanitize_context_t *c) const
46 TRACE_SANITIZE (this);
47 return_trace (c->check_struct (this));
55 DEFINE_SIZE_STATIC (4);
60 static constexpr hb_tag_t tableTag = HB_OT_TAG_VORG;
62 bool has_data () const { return version.to_int (); }
64 int get_y_origin (hb_codepoint_t glyph) const
67 if (!vertYOrigins.bfind (glyph, &i))
68 return defaultVertOriginY;
69 return vertYOrigins[i].vertOriginY;
72 bool _subset (const hb_subset_plan_t *plan HB_UNUSED,
73 const VORG *vorg_table,
74 const hb_vector_t<VertOriginMetric> &subset_metrics,
78 hb_serialize_context_t c (dest, dest_sz);
80 VORG *subset_table = c.start_serialize<VORG> ();
81 if (unlikely (!c.extend_min (*subset_table)))
84 subset_table->version.major.set (1);
85 subset_table->version.minor.set (0);
87 subset_table->defaultVertOriginY.set (vorg_table->defaultVertOriginY);
88 subset_table->vertYOrigins.len.set (subset_metrics.length);
91 if (subset_metrics.length > 0)
93 unsigned int size = VertOriginMetric::static_size * subset_metrics.length;
94 VertOriginMetric *metrics = c.allocate_size<VertOriginMetric> (size);
95 if (likely (metrics != nullptr))
96 memcpy (metrics, &subset_metrics[0], size);
105 bool subset (hb_subset_plan_t *plan) const
107 hb_blob_t *vorg_blob = hb_sanitize_context_t().reference_table<VORG> (plan->source);
108 const VORG *vorg_table = vorg_blob->as<VORG> ();
110 /* count the number of glyphs to be included in the subset table */
111 hb_vector_t<VertOriginMetric> subset_metrics;
112 subset_metrics.init ();
115 hb_codepoint_t old_glyph = HB_SET_VALUE_INVALID;
117 while (i < vertYOrigins.len
118 && plan->glyphset ()->next (&old_glyph))
120 while (old_glyph > vertYOrigins[i].glyph)
123 if (i >= vertYOrigins.len)
127 if (old_glyph == vertYOrigins[i].glyph)
129 hb_codepoint_t new_glyph;
130 if (plan->new_gid_for_old_gid (old_glyph, &new_glyph))
132 VertOriginMetric *metrics = subset_metrics.push ();
133 metrics->glyph.set (new_glyph);
134 metrics->vertOriginY.set (vertYOrigins[i].vertOriginY);
139 /* alloc the new table */
140 unsigned int dest_sz = VORG::min_size + VertOriginMetric::static_size * subset_metrics.length;
141 void *dest = (void *) malloc (dest_sz);
142 if (unlikely (!dest))
144 subset_metrics.fini ();
145 hb_blob_destroy (vorg_blob);
149 /* serialize the new table */
150 if (!_subset (plan, vorg_table, subset_metrics, dest_sz, dest))
152 subset_metrics.fini ();
154 hb_blob_destroy (vorg_blob);
158 hb_blob_t *result = hb_blob_create ((const char *)dest,
160 HB_MEMORY_MODE_READONLY,
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);
170 bool sanitize (hb_sanitize_context_t *c) const
172 TRACE_SANITIZE (this);
173 return_trace (c->check_struct (this) &&
174 version.major == 1 &&
175 vertYOrigins.sanitize (c));
179 FixedVersion<> version; /* Version of VORG table. Set to 0x00010000u. */
180 FWORD defaultVertOriginY; /* The default vertical origin. */
181 SortedArrayOf<VertOriginMetric>
182 vertYOrigins; /* The array of vertical origins. */
185 DEFINE_SIZE_ARRAY(8, vertYOrigins);
189 #endif /* HB_OT_VORG_TABLE_HH */