2 * Copyright © 2018 Google, 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 * Google Author(s): Garret Rieger, Roderick Sheeter
27 #ifndef HB_SUBSET_PLAN_HH
28 #define HB_SUBSET_PLAN_HH
32 #include "hb-subset.h"
33 #include "hb-subset-input.hh"
34 #include "hb-subset-accelerator.hh"
37 #include "hb-bimap.hh"
44 struct 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) {}
61 unsigned maxCompositePoints;
62 unsigned maxCompositeContours;
63 unsigned maxComponentElements;
64 unsigned maxComponentDepth;
68 typedef struct head_maxp_info_t head_maxp_info_t;
70 struct contour_point_t
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_; }
75 void transform (const float (&matrix)[4])
77 float x_ = x * matrix[0] + y * matrix[2];
78 y = x * matrix[1] + y * matrix[3];
82 void translate (const contour_point_t &p) { x += p.x; y += p.y; }
91 struct contour_point_vector_t : hb_vector_t<contour_point_t>
93 void extend (const hb_array_t<contour_point_t> &a)
95 unsigned int old_len = length;
96 if (unlikely (!resize (old_len + a.length, false)))
98 auto arrayZ = this->arrayZ + old_len;
99 unsigned count = a.length;
100 hb_memcpy (arrayZ, a.arrayZ, count * sizeof (arrayZ[0]));
105 struct cff1_subset_accelerator_t;
106 struct cff2_subset_accelerator_t;
109 struct hb_subset_plan_t
111 HB_INTERNAL hb_subset_plan_t (hb_face_t *,
112 const hb_subset_input_t *input);
114 HB_INTERNAL ~hb_subset_plan_t();
116 hb_object_header_t header;
120 bool attach_accelerator_data = false;
121 bool force_long_loca = false;
124 hb_map_t *codepoint_to_glyph; // Needs to be heap-allocated
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
130 // Plan is only good for a specific source/dest so keep them with it
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;
140 unsigned int _num_output_glyphs;
142 bool all_axes_pinned;
143 bool pinned_at_default;
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;
150 #define HB_SUBSET_PLAN_MEMBER(Type, Name) Type Name;
151 #include "hb-subset-plan-member-list.hh"
152 #undef HB_SUBSET_PLAN_MEMBER
154 //recalculated head/maxp table info after instancing
155 mutable head_maxp_info_t head_maxp_info;
157 const hb_subset_accelerator_t* accelerator;
158 hb_subset_accelerator_t* inprogress_accelerator;
163 struct source_table_loader
165 hb_blob_ptr_t<T> operator () (hb_subset_plan_t *plan)
167 hb_lock_t lock (plan->accelerator ? &plan->accelerator->sanitized_table_cache_lock : nullptr);
169 auto *cache = plan->accelerator ? &plan->accelerator->sanitized_table_cache : &plan->sanitized_table_cache;
171 && !cache->in_error ()
172 && cache->has (+T::tableTag)) {
173 return hb_blob_reference (cache->get (+T::tableTag).get ());
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 ());
180 cache->set (+T::tableTag, std::move (table_blob));
187 auto source_table() HB_AUTO_RETURN (source_table_loader<T> {} (this))
189 bool in_error () const { return !successful; }
191 bool check_success(bool success)
193 successful = (successful && success);
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.
202 inline const hb_set_t *
209 * The set of input glyph ids which will be retained in the subset.
211 inline const hb_set_t *
212 glyphset_gsub () const
214 return &_glyphset_gsub;
218 * The total number of output glyphs in the final subset.
221 num_output_glyphs () const
223 return _num_output_glyphs;
226 inline bool new_gid_for_codepoint (hb_codepoint_t codepoint,
227 hb_codepoint_t *new_gid) const
229 hb_codepoint_t old_gid = codepoint_to_glyph->get (codepoint);
230 if (old_gid == HB_MAP_VALUE_INVALID)
233 return new_gid_for_old_gid (old_gid, new_gid);
236 inline bool new_gid_for_old_gid (hb_codepoint_t old_gid,
237 hb_codepoint_t *new_gid) const
239 hb_codepoint_t gid = glyph_map->get (old_gid);
240 if (gid == HB_MAP_VALUE_INVALID)
247 inline bool old_gid_for_new_gid (hb_codepoint_t new_gid,
248 hb_codepoint_t *old_gid) const
250 hb_codepoint_t gid = reverse_glyph_map->get (new_gid);
251 if (gid == HB_MAP_VALUE_INVALID)
259 add_table (hb_tag_t tag,
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",
267 hb_blob_get_length (contents),
268 hb_blob_get_length (source_blob));
269 hb_blob_destroy (source_blob);
271 return hb_face_builder_add_table (dest, tag, contents);
276 #endif /* HB_SUBSET_PLAN_HH */