Imported Upstream version 8.2.2
[platform/upstream/harfbuzz.git] / src / hb-subset-plan.hh
1 /*
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, Roderick Sheeter
25  */
26
27 #ifndef HB_SUBSET_PLAN_HH
28 #define HB_SUBSET_PLAN_HH
29
30 #include "hb.hh"
31
32 #include "hb-subset.h"
33 #include "hb-subset-input.hh"
34 #include "hb-subset-accelerator.hh"
35
36 #include "hb-map.hh"
37 #include "hb-bimap.hh"
38 #include "hb-set.hh"
39
40 namespace OT {
41 struct Feature;
42 }
43
44 struct head_maxp_info_t
45 {
46   head_maxp_info_t ()
47       :xMin (0x7FFF), xMax (-0x7FFF), yMin (0x7FFF), yMax (-0x7FFF),
48       maxPoints (0), maxContours (0),
49       maxCompositePoints (0),
50       maxCompositeContours (0),
51       maxComponentElements (0),
52       maxComponentDepth (0),
53       allXMinIsLsb (true) {}
54
55   int xMin;
56   int xMax;
57   int yMin;
58   int yMax;
59   unsigned maxPoints;
60   unsigned maxContours;
61   unsigned maxCompositePoints;
62   unsigned maxCompositeContours;
63   unsigned maxComponentElements;
64   unsigned maxComponentDepth;
65   bool allXMinIsLsb;
66 };
67
68 typedef struct head_maxp_info_t head_maxp_info_t;
69
70 struct contour_point_t
71 {
72   void init (float x_ = 0.f, float y_ = 0.f, bool is_end_point_ = false)
73   { flag = 0; x = x_; y = y_; is_end_point = is_end_point_; }
74
75   void transform (const float (&matrix)[4])
76   {
77     float x_ = x * matrix[0] + y * matrix[2];
78           y  = x * matrix[1] + y * matrix[3];
79     x  = x_;
80   }
81   HB_ALWAYS_INLINE
82   void translate (const contour_point_t &p) { x += p.x; y += p.y; }
83
84
85   float x;
86   float y;
87   uint8_t flag;
88   bool is_end_point;
89 };
90
91 struct contour_point_vector_t : hb_vector_t<contour_point_t>
92 {
93   void extend (const hb_array_t<contour_point_t> &a)
94   {
95     unsigned int old_len = length;
96     if (unlikely (!resize (old_len + a.length, false)))
97       return;
98     auto arrayZ = this->arrayZ + old_len;
99     unsigned count = a.length;
100     hb_memcpy (arrayZ, a.arrayZ, count * sizeof (arrayZ[0]));
101   }
102 };
103
104 namespace OT {
105   struct cff1_subset_accelerator_t;
106   struct cff2_subset_accelerator_t;
107 }
108
109 struct hb_subset_plan_t
110 {
111   HB_INTERNAL hb_subset_plan_t (hb_face_t *,
112                                 const hb_subset_input_t *input);
113
114   HB_INTERNAL ~hb_subset_plan_t();
115
116   hb_object_header_t header;
117
118   bool successful;
119   unsigned flags;
120   bool attach_accelerator_data = false;
121   bool force_long_loca = false;
122
123   // The glyph subset
124   hb_map_t *codepoint_to_glyph; // Needs to be heap-allocated
125
126   // Old -> New glyph id mapping
127   hb_map_t *glyph_map; // Needs to be heap-allocated
128   hb_map_t *reverse_glyph_map; // Needs to be heap-allocated
129
130   // Plan is only good for a specific source/dest so keep them with it
131   hb_face_t *source;
132 #ifndef HB_NO_SUBSET_CFF
133   // These have to be immediately after source:
134   hb_face_lazy_loader_t<OT::cff1_subset_accelerator_t, 1> cff1_accel;
135   hb_face_lazy_loader_t<OT::cff2_subset_accelerator_t, 2> cff2_accel;
136 #endif
137
138   hb_face_t *dest;
139
140   unsigned int _num_output_glyphs;
141
142   bool all_axes_pinned;
143   bool pinned_at_default;
144   bool has_seac;
145
146   // whether to insert a catch-all FeatureVariationRecord
147   bool gsub_insert_catch_all_feature_variation_rec;
148   bool gpos_insert_catch_all_feature_variation_rec;
149
150 #define HB_SUBSET_PLAN_MEMBER(Type, Name) Type Name;
151 #include "hb-subset-plan-member-list.hh"
152 #undef HB_SUBSET_PLAN_MEMBER
153
154   //recalculated head/maxp table info after instancing
155   mutable head_maxp_info_t head_maxp_info;
156
157   const hb_subset_accelerator_t* accelerator;
158   hb_subset_accelerator_t* inprogress_accelerator;
159
160  public:
161
162   template<typename T>
163   struct source_table_loader
164   {
165     hb_blob_ptr_t<T> operator () (hb_subset_plan_t *plan)
166     {
167       hb_lock_t lock (plan->accelerator ? &plan->accelerator->sanitized_table_cache_lock : nullptr);
168
169       auto *cache = plan->accelerator ? &plan->accelerator->sanitized_table_cache : &plan->sanitized_table_cache;
170       if (cache
171           && !cache->in_error ()
172           && cache->has (+T::tableTag)) {
173         return hb_blob_reference (cache->get (+T::tableTag).get ());
174       }
175
176       hb::unique_ptr<hb_blob_t> table_blob {hb_sanitize_context_t ().reference_table<T> (plan->source)};
177       hb_blob_t* ret = hb_blob_reference (table_blob.get ());
178
179       if (likely (cache))
180         cache->set (+T::tableTag, std::move (table_blob));
181
182       return ret;
183     }
184   };
185
186   template<typename T>
187   auto source_table() HB_AUTO_RETURN (source_table_loader<T> {} (this))
188
189   bool in_error () const { return !successful; }
190
191   bool check_success(bool success)
192   {
193     successful = (successful && success);
194     return successful;
195   }
196
197   /*
198    * The set of input glyph ids which will be retained in the subset.
199    * Does NOT include ids kept due to retain_gids. You probably want to use
200    * glyph_map/reverse_glyph_map.
201    */
202   inline const hb_set_t *
203   glyphset () const
204   {
205     return &_glyphset;
206   }
207
208   /*
209    * The set of input glyph ids which will be retained in the subset.
210    */
211   inline const hb_set_t *
212   glyphset_gsub () const
213   {
214     return &_glyphset_gsub;
215   }
216
217   /*
218    * The total number of output glyphs in the final subset.
219    */
220   inline unsigned int
221   num_output_glyphs () const
222   {
223     return _num_output_glyphs;
224   }
225
226   inline bool new_gid_for_codepoint (hb_codepoint_t codepoint,
227                                      hb_codepoint_t *new_gid) const
228   {
229     hb_codepoint_t old_gid = codepoint_to_glyph->get (codepoint);
230     if (old_gid == HB_MAP_VALUE_INVALID)
231       return false;
232
233     return new_gid_for_old_gid (old_gid, new_gid);
234   }
235
236   inline bool new_gid_for_old_gid (hb_codepoint_t old_gid,
237                                    hb_codepoint_t *new_gid) const
238   {
239     hb_codepoint_t gid = glyph_map->get (old_gid);
240     if (gid == HB_MAP_VALUE_INVALID)
241       return false;
242
243     *new_gid = gid;
244     return true;
245   }
246
247   inline bool old_gid_for_new_gid (hb_codepoint_t  new_gid,
248                                    hb_codepoint_t *old_gid) const
249   {
250     hb_codepoint_t gid = reverse_glyph_map->get (new_gid);
251     if (gid == HB_MAP_VALUE_INVALID)
252       return false;
253
254     *old_gid = gid;
255     return true;
256   }
257
258   inline bool
259   add_table (hb_tag_t tag,
260              hb_blob_t *contents)
261   {
262     if (HB_DEBUG_SUBSET)
263     {
264       hb_blob_t *source_blob = source->reference_table (tag);
265       DEBUG_MSG(SUBSET, nullptr, "add table %c%c%c%c, dest %u bytes, source %u bytes",
266                 HB_UNTAG(tag),
267                 hb_blob_get_length (contents),
268                 hb_blob_get_length (source_blob));
269       hb_blob_destroy (source_blob);
270     }
271     return hb_face_builder_add_table (dest, tag, contents);
272   }
273 };
274
275
276 #endif /* HB_SUBSET_PLAN_HH */