39073db8f3250427d02df6a32ccfd4556d0b698d
[platform/upstream/harfbuzz.git] / src / hb-ot-vorg-table.hh
1 /*
2  * Copyright © 2018 Adobe 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  * 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
38 namespace OT {
39
40 struct VertOriginMetric
41 {
42   int cmp (hb_codepoint_t g) const { return glyph.cmp (g); }
43
44   bool sanitize (hb_sanitize_context_t *c) const
45   {
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
58 struct VORG
59 {
60   static constexpr hb_tag_t tableTag = HB_OT_TAG_VORG;
61
62   bool has_data () const { return version.to_int (); }
63
64   int get_y_origin (hb_codepoint_t glyph) const
65   {
66     unsigned int i;
67     if (!vertYOrigins.bfind (glyph, &i))
68       return defaultVertOriginY;
69     return vertYOrigins[i].vertOriginY;
70   }
71
72   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
77   {
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
84     subset_table->version.major.set (1);
85     subset_table->version.minor.set (0);
86
87     subset_table->defaultVertOriginY.set (vorg_table->defaultVertOriginY);
88     subset_table->vertYOrigins.len.set (subset_metrics.length);
89
90     bool success = true;
91     if (subset_metrics.length > 0)
92     {
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);
97       else
98         success = false;
99     }
100     c.end_serialize ();
101
102     return success;
103   }
104
105   bool subset (hb_subset_plan_t *plan) const
106   {
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 ();
113
114
115     hb_codepoint_t old_glyph = HB_SET_VALUE_INVALID;
116     unsigned int i = 0;
117     while (i < vertYOrigins.len
118            && plan->glyphset ()->next (&old_glyph))
119     {
120       while (old_glyph > vertYOrigins[i].glyph)
121       {
122         i++;
123         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 ();
133           metrics->glyph.set (new_glyph);
134           metrics->vertOriginY.set (vertYOrigins[i].vertOriginY);
135         }
136       }
137     }
138
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))
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
170   bool sanitize (hb_sanitize_context_t *c) const
171   {
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. */
180   FWORD                 defaultVertOriginY;     /* The default vertical origin. */
181   SortedArrayOf<VertOriginMetric>
182                         vertYOrigins;           /* The array of vertical origins. */
183
184   public:
185   DEFINE_SIZE_ARRAY(8, vertYOrigins);
186 };
187 } /* namespace OT */
188
189 #endif /* HB_OT_VORG_TABLE_HH */