xref: /aosp_15_r20/external/harfbuzz_ng/src/hb-subset-plan.hh (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
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 {
head_maxp_info_thead_maxp_info_t46   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 {
initcontour_point_t72   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 
transformcontour_point_t75   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 
add_deltacontour_point_t82   void add_delta (float delta_x, float delta_y)
83   {
84     x += delta_x;
85     y += delta_y;
86   }
87 
88   HB_ALWAYS_INLINE
translatecontour_point_t89   void translate (const contour_point_t &p) { x += p.x; y += p.y; }
90 
91 
92   float x;
93   float y;
94   uint8_t flag;
95   bool is_end_point;
96 };
97 
98 struct contour_point_vector_t : hb_vector_t<contour_point_t>
99 {
extendcontour_point_vector_t100   void extend (const hb_array_t<contour_point_t> &a)
101   {
102     unsigned int old_len = length;
103     if (unlikely (!resize (old_len + a.length, false)))
104       return;
105     auto arrayZ = this->arrayZ + old_len;
106     unsigned count = a.length;
107     hb_memcpy (arrayZ, a.arrayZ, count * sizeof (arrayZ[0]));
108   }
109 
add_deltascontour_point_vector_t110   bool add_deltas (const hb_vector_t<float> deltas_x,
111                    const hb_vector_t<float> deltas_y,
112                    const hb_vector_t<bool> indices)
113   {
114     if (indices.length != deltas_x.length ||
115         indices.length != deltas_y.length)
116       return false;
117 
118     for (unsigned i = 0; i < indices.length; i++)
119     {
120       if (!indices.arrayZ[i]) continue;
121       arrayZ[i].add_delta (deltas_x.arrayZ[i], deltas_y.arrayZ[i]);
122     }
123     return true;
124   }
125 };
126 
127 namespace OT {
128   struct cff1_subset_accelerator_t;
129   struct cff2_subset_accelerator_t;
130 }
131 
132 struct hb_subset_plan_t
133 {
134   HB_INTERNAL hb_subset_plan_t (hb_face_t *,
135 				const hb_subset_input_t *input);
136 
137   HB_INTERNAL ~hb_subset_plan_t();
138 
139   hb_object_header_t header;
140 
141   bool successful;
142   unsigned flags;
143   bool attach_accelerator_data = false;
144   bool force_long_loca = false;
145 
146   // The glyph subset
147   hb_map_t *codepoint_to_glyph; // Needs to be heap-allocated
148 
149   // Old -> New glyph id mapping
150   hb_map_t *glyph_map; // Needs to be heap-allocated
151   hb_map_t *reverse_glyph_map; // Needs to be heap-allocated
152 
153   // Plan is only good for a specific source/dest so keep them with it
154   hb_face_t *source;
155 #ifndef HB_NO_SUBSET_CFF
156   // These have to be immediately after source:
157   hb_face_lazy_loader_t<OT::cff1_subset_accelerator_t, 1> cff1_accel;
158   hb_face_lazy_loader_t<OT::cff2_subset_accelerator_t, 2> cff2_accel;
159 #endif
160 
161   hb_face_t *dest;
162 
163   unsigned int _num_output_glyphs;
164 
165   bool all_axes_pinned;
166   bool pinned_at_default;
167   bool has_seac;
168 
169   // whether to insert a catch-all FeatureVariationRecord
170   bool gsub_insert_catch_all_feature_variation_rec;
171   bool gpos_insert_catch_all_feature_variation_rec;
172 
173   // whether GDEF ItemVariationStore is retained
174   mutable bool has_gdef_varstore;
175 
176 #define HB_SUBSET_PLAN_MEMBER(Type, Name) Type Name;
177 #include "hb-subset-plan-member-list.hh"
178 #undef HB_SUBSET_PLAN_MEMBER
179 
180   //recalculated head/maxp table info after instancing
181   mutable head_maxp_info_t head_maxp_info;
182 
183   const hb_subset_accelerator_t* accelerator;
184   hb_subset_accelerator_t* inprogress_accelerator;
185 
186  public:
187 
188   template<typename T>
189   struct source_table_loader
190   {
operator ()hb_subset_plan_t::source_table_loader191     hb_blob_ptr_t<T> operator () (hb_subset_plan_t *plan)
192     {
193       hb_lock_t lock (plan->accelerator ? &plan->accelerator->sanitized_table_cache_lock : nullptr);
194 
195       auto *cache = plan->accelerator ? &plan->accelerator->sanitized_table_cache : &plan->sanitized_table_cache;
196       if (cache
197 	  && !cache->in_error ()
198 	  && cache->has (+T::tableTag)) {
199 	return hb_blob_reference (cache->get (+T::tableTag).get ());
200       }
201 
202       hb::unique_ptr<hb_blob_t> table_blob {hb_sanitize_context_t ().reference_table<T> (plan->source)};
203       hb_blob_t* ret = hb_blob_reference (table_blob.get ());
204 
205       if (likely (cache))
206 	cache->set (+T::tableTag, std::move (table_blob));
207 
208       return ret;
209     }
210   };
211 
212   template<typename T>
source_tablehb_subset_plan_t213   auto source_table() HB_AUTO_RETURN (source_table_loader<T> {} (this))
214 
215   bool in_error () const { return !successful; }
216 
check_successhb_subset_plan_t217   bool check_success(bool success)
218   {
219     successful = (successful && success);
220     return successful;
221   }
222 
223   /*
224    * The set of input glyph ids which will be retained in the subset.
225    * Does NOT include ids kept due to retain_gids. You probably want to use
226    * glyph_map/reverse_glyph_map.
227    */
228   inline const hb_set_t *
glyphsethb_subset_plan_t229   glyphset () const
230   {
231     return &_glyphset;
232   }
233 
234   /*
235    * The set of input glyph ids which will be retained in the subset.
236    */
237   inline const hb_set_t *
glyphset_gsubhb_subset_plan_t238   glyphset_gsub () const
239   {
240     return &_glyphset_gsub;
241   }
242 
243   /*
244    * The total number of output glyphs in the final subset.
245    */
246   inline unsigned int
num_output_glyphshb_subset_plan_t247   num_output_glyphs () const
248   {
249     return _num_output_glyphs;
250   }
251 
new_gid_for_codepointhb_subset_plan_t252   inline bool new_gid_for_codepoint (hb_codepoint_t codepoint,
253 				     hb_codepoint_t *new_gid) const
254   {
255     hb_codepoint_t old_gid = codepoint_to_glyph->get (codepoint);
256     if (old_gid == HB_MAP_VALUE_INVALID)
257       return false;
258 
259     return new_gid_for_old_gid (old_gid, new_gid);
260   }
261 
new_gid_for_old_gidhb_subset_plan_t262   inline bool new_gid_for_old_gid (hb_codepoint_t old_gid,
263 				   hb_codepoint_t *new_gid) const
264   {
265     hb_codepoint_t gid = glyph_map->get (old_gid);
266     if (gid == HB_MAP_VALUE_INVALID)
267       return false;
268 
269     *new_gid = gid;
270     return true;
271   }
272 
old_gid_for_new_gidhb_subset_plan_t273   inline bool old_gid_for_new_gid (hb_codepoint_t  new_gid,
274 				   hb_codepoint_t *old_gid) const
275   {
276     hb_codepoint_t gid = reverse_glyph_map->get (new_gid);
277     if (gid == HB_MAP_VALUE_INVALID)
278       return false;
279 
280     *old_gid = gid;
281     return true;
282   }
283 
284   inline bool
add_tablehb_subset_plan_t285   add_table (hb_tag_t tag,
286 	     hb_blob_t *contents)
287   {
288     if (HB_DEBUG_SUBSET)
289     {
290       hb_blob_t *source_blob = source->reference_table (tag);
291       DEBUG_MSG(SUBSET, nullptr, "add table %c%c%c%c, dest %u bytes, source %u bytes",
292 		HB_UNTAG(tag),
293 		hb_blob_get_length (contents),
294 		hb_blob_get_length (source_blob));
295       hb_blob_destroy (source_blob);
296     }
297     return hb_face_builder_add_table (dest, tag, contents);
298   }
299 };
300 
301 
302 #endif /* HB_SUBSET_PLAN_HH */
303