1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright © 2018  Google, Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci *  This is part of HarfBuzz, a text shaping library.
5cb93a386Sopenharmony_ci *
6cb93a386Sopenharmony_ci * Permission is hereby granted, without written agreement and without
7cb93a386Sopenharmony_ci * license or royalty fees, to use, copy, modify, and distribute this
8cb93a386Sopenharmony_ci * software and its documentation for any purpose, provided that the
9cb93a386Sopenharmony_ci * above copyright notice and the following two paragraphs appear in
10cb93a386Sopenharmony_ci * all copies of this software.
11cb93a386Sopenharmony_ci *
12cb93a386Sopenharmony_ci * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13cb93a386Sopenharmony_ci * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14cb93a386Sopenharmony_ci * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15cb93a386Sopenharmony_ci * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16cb93a386Sopenharmony_ci * DAMAGE.
17cb93a386Sopenharmony_ci *
18cb93a386Sopenharmony_ci * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19cb93a386Sopenharmony_ci * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20cb93a386Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21cb93a386Sopenharmony_ci * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22cb93a386Sopenharmony_ci * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23cb93a386Sopenharmony_ci *
24cb93a386Sopenharmony_ci * Google Author(s): Garret Rieger, Roderick Sheeter
25cb93a386Sopenharmony_ci */
26cb93a386Sopenharmony_ci
27cb93a386Sopenharmony_ci#ifndef HB_SUBSET_PLAN_HH
28cb93a386Sopenharmony_ci#define HB_SUBSET_PLAN_HH
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_ci#include "hb.hh"
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_ci#include "hb-subset.h"
33cb93a386Sopenharmony_ci#include "hb-subset-input.hh"
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_ci#include "hb-map.hh"
36cb93a386Sopenharmony_ci#include "hb-set.hh"
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_cistruct hb_subset_plan_t
39cb93a386Sopenharmony_ci{
40cb93a386Sopenharmony_ci  hb_object_header_t header;
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci  bool successful;
43cb93a386Sopenharmony_ci  unsigned flags;
44cb93a386Sopenharmony_ci
45cb93a386Sopenharmony_ci  // For each cp that we'd like to retain maps to the corresponding gid.
46cb93a386Sopenharmony_ci  hb_set_t *unicodes;
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci  // name_ids we would like to retain
49cb93a386Sopenharmony_ci  hb_set_t *name_ids;
50cb93a386Sopenharmony_ci
51cb93a386Sopenharmony_ci  // name_languages we would like to retain
52cb93a386Sopenharmony_ci  hb_set_t *name_languages;
53cb93a386Sopenharmony_ci
54cb93a386Sopenharmony_ci  //layout features which will be preserved
55cb93a386Sopenharmony_ci  hb_set_t *layout_features;
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci  //glyph ids requested to retain
58cb93a386Sopenharmony_ci  hb_set_t *glyphs_requested;
59cb93a386Sopenharmony_ci
60cb93a386Sopenharmony_ci  // Tables which should not be processed, just pass them through.
61cb93a386Sopenharmony_ci  hb_set_t *no_subset_tables;
62cb93a386Sopenharmony_ci
63cb93a386Sopenharmony_ci  // Tables which should be dropped.
64cb93a386Sopenharmony_ci  hb_set_t *drop_tables;
65cb93a386Sopenharmony_ci
66cb93a386Sopenharmony_ci  // The glyph subset
67cb93a386Sopenharmony_ci  hb_map_t *codepoint_to_glyph;
68cb93a386Sopenharmony_ci
69cb93a386Sopenharmony_ci  // Old -> New glyph id mapping
70cb93a386Sopenharmony_ci  hb_map_t *glyph_map;
71cb93a386Sopenharmony_ci  hb_map_t *reverse_glyph_map;
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_ci  // Plan is only good for a specific source/dest so keep them with it
74cb93a386Sopenharmony_ci  hb_face_t *source;
75cb93a386Sopenharmony_ci  hb_face_t *dest;
76cb93a386Sopenharmony_ci
77cb93a386Sopenharmony_ci  unsigned int _num_output_glyphs;
78cb93a386Sopenharmony_ci  hb_set_t *_glyphset;
79cb93a386Sopenharmony_ci  hb_set_t *_glyphset_gsub;
80cb93a386Sopenharmony_ci  hb_set_t *_glyphset_mathed;
81cb93a386Sopenharmony_ci
82cb93a386Sopenharmony_ci  //active lookups we'd like to retain
83cb93a386Sopenharmony_ci  hb_map_t *gsub_lookups;
84cb93a386Sopenharmony_ci  hb_map_t *gpos_lookups;
85cb93a386Sopenharmony_ci
86cb93a386Sopenharmony_ci  //active langsys we'd like to retain
87cb93a386Sopenharmony_ci  hb_hashmap_t<unsigned, hb_set_t *> *gsub_langsys;
88cb93a386Sopenharmony_ci  hb_hashmap_t<unsigned, hb_set_t *> *gpos_langsys;
89cb93a386Sopenharmony_ci
90cb93a386Sopenharmony_ci  //active features after removing redundant langsys and prune_features
91cb93a386Sopenharmony_ci  hb_map_t *gsub_features;
92cb93a386Sopenharmony_ci  hb_map_t *gpos_features;
93cb93a386Sopenharmony_ci
94cb93a386Sopenharmony_ci  //active layers/palettes we'd like to retain
95cb93a386Sopenharmony_ci  hb_map_t *colrv1_layers;
96cb93a386Sopenharmony_ci  hb_map_t *colr_palettes;
97cb93a386Sopenharmony_ci
98cb93a386Sopenharmony_ci  //The set of layout item variation store delta set indices to be retained
99cb93a386Sopenharmony_ci  hb_set_t *layout_variation_indices;
100cb93a386Sopenharmony_ci  //Old -> New layout item variation store delta set index mapping
101cb93a386Sopenharmony_ci  hb_map_t *layout_variation_idx_map;
102cb93a386Sopenharmony_ci
103cb93a386Sopenharmony_ci public:
104cb93a386Sopenharmony_ci
105cb93a386Sopenharmony_ci  bool in_error () const { return !successful; }
106cb93a386Sopenharmony_ci
107cb93a386Sopenharmony_ci  bool check_success(bool success)
108cb93a386Sopenharmony_ci  {
109cb93a386Sopenharmony_ci    successful = (successful && success);
110cb93a386Sopenharmony_ci    return successful;
111cb93a386Sopenharmony_ci  }
112cb93a386Sopenharmony_ci
113cb93a386Sopenharmony_ci  /*
114cb93a386Sopenharmony_ci   * The set of input glyph ids which will be retained in the subset.
115cb93a386Sopenharmony_ci   * Does NOT include ids kept due to retain_gids. You probably want to use
116cb93a386Sopenharmony_ci   * glyph_map/reverse_glyph_map.
117cb93a386Sopenharmony_ci   */
118cb93a386Sopenharmony_ci  inline const hb_set_t *
119cb93a386Sopenharmony_ci  glyphset () const
120cb93a386Sopenharmony_ci  {
121cb93a386Sopenharmony_ci    return _glyphset;
122cb93a386Sopenharmony_ci  }
123cb93a386Sopenharmony_ci
124cb93a386Sopenharmony_ci  /*
125cb93a386Sopenharmony_ci   * The set of input glyph ids which will be retained in the subset.
126cb93a386Sopenharmony_ci   */
127cb93a386Sopenharmony_ci  inline const hb_set_t *
128cb93a386Sopenharmony_ci  glyphset_gsub () const
129cb93a386Sopenharmony_ci  {
130cb93a386Sopenharmony_ci    return _glyphset_gsub;
131cb93a386Sopenharmony_ci  }
132cb93a386Sopenharmony_ci
133cb93a386Sopenharmony_ci  /*
134cb93a386Sopenharmony_ci   * The total number of output glyphs in the final subset.
135cb93a386Sopenharmony_ci   */
136cb93a386Sopenharmony_ci  inline unsigned int
137cb93a386Sopenharmony_ci  num_output_glyphs () const
138cb93a386Sopenharmony_ci  {
139cb93a386Sopenharmony_ci    return _num_output_glyphs;
140cb93a386Sopenharmony_ci  }
141cb93a386Sopenharmony_ci
142cb93a386Sopenharmony_ci  /*
143cb93a386Sopenharmony_ci   * Given an output gid , returns true if that glyph id is an empty
144cb93a386Sopenharmony_ci   * glyph (ie. it's a gid that we are dropping all data for).
145cb93a386Sopenharmony_ci   */
146cb93a386Sopenharmony_ci  inline bool is_empty_glyph (hb_codepoint_t gid) const
147cb93a386Sopenharmony_ci  {
148cb93a386Sopenharmony_ci    return !_glyphset->has (gid);
149cb93a386Sopenharmony_ci  }
150cb93a386Sopenharmony_ci
151cb93a386Sopenharmony_ci  inline bool new_gid_for_codepoint (hb_codepoint_t codepoint,
152cb93a386Sopenharmony_ci				     hb_codepoint_t *new_gid) const
153cb93a386Sopenharmony_ci  {
154cb93a386Sopenharmony_ci    hb_codepoint_t old_gid = codepoint_to_glyph->get (codepoint);
155cb93a386Sopenharmony_ci    if (old_gid == HB_MAP_VALUE_INVALID)
156cb93a386Sopenharmony_ci      return false;
157cb93a386Sopenharmony_ci
158cb93a386Sopenharmony_ci    return new_gid_for_old_gid (old_gid, new_gid);
159cb93a386Sopenharmony_ci  }
160cb93a386Sopenharmony_ci
161cb93a386Sopenharmony_ci  inline bool new_gid_for_old_gid (hb_codepoint_t old_gid,
162cb93a386Sopenharmony_ci				   hb_codepoint_t *new_gid) const
163cb93a386Sopenharmony_ci  {
164cb93a386Sopenharmony_ci    hb_codepoint_t gid = glyph_map->get (old_gid);
165cb93a386Sopenharmony_ci    if (gid == HB_MAP_VALUE_INVALID)
166cb93a386Sopenharmony_ci      return false;
167cb93a386Sopenharmony_ci
168cb93a386Sopenharmony_ci    *new_gid = gid;
169cb93a386Sopenharmony_ci    return true;
170cb93a386Sopenharmony_ci  }
171cb93a386Sopenharmony_ci
172cb93a386Sopenharmony_ci  inline bool old_gid_for_new_gid (hb_codepoint_t  new_gid,
173cb93a386Sopenharmony_ci				   hb_codepoint_t *old_gid) const
174cb93a386Sopenharmony_ci  {
175cb93a386Sopenharmony_ci    hb_codepoint_t gid = reverse_glyph_map->get (new_gid);
176cb93a386Sopenharmony_ci    if (gid == HB_MAP_VALUE_INVALID)
177cb93a386Sopenharmony_ci      return false;
178cb93a386Sopenharmony_ci
179cb93a386Sopenharmony_ci    *old_gid = gid;
180cb93a386Sopenharmony_ci    return true;
181cb93a386Sopenharmony_ci  }
182cb93a386Sopenharmony_ci
183cb93a386Sopenharmony_ci  inline bool
184cb93a386Sopenharmony_ci  add_table (hb_tag_t tag,
185cb93a386Sopenharmony_ci	     hb_blob_t *contents)
186cb93a386Sopenharmony_ci  {
187cb93a386Sopenharmony_ci    if (HB_DEBUG_SUBSET)
188cb93a386Sopenharmony_ci    {
189cb93a386Sopenharmony_ci      hb_blob_t *source_blob = source->reference_table (tag);
190cb93a386Sopenharmony_ci      DEBUG_MSG(SUBSET, nullptr, "add table %c%c%c%c, dest %d bytes, source %d bytes",
191cb93a386Sopenharmony_ci		HB_UNTAG(tag),
192cb93a386Sopenharmony_ci		hb_blob_get_length (contents),
193cb93a386Sopenharmony_ci		hb_blob_get_length (source_blob));
194cb93a386Sopenharmony_ci      hb_blob_destroy (source_blob);
195cb93a386Sopenharmony_ci    }
196cb93a386Sopenharmony_ci    return hb_face_builder_add_table (dest, tag, contents);
197cb93a386Sopenharmony_ci  }
198cb93a386Sopenharmony_ci};
199cb93a386Sopenharmony_ci
200cb93a386Sopenharmony_citypedef struct hb_subset_plan_t hb_subset_plan_t;
201cb93a386Sopenharmony_ci
202cb93a386Sopenharmony_ciHB_INTERNAL hb_subset_plan_t *
203cb93a386Sopenharmony_cihb_subset_plan_create (hb_face_t           *face,
204cb93a386Sopenharmony_ci		       const hb_subset_input_t   *input);
205cb93a386Sopenharmony_ci
206cb93a386Sopenharmony_ciHB_INTERNAL void
207cb93a386Sopenharmony_cihb_subset_plan_destroy (hb_subset_plan_t *plan);
208cb93a386Sopenharmony_ci
209cb93a386Sopenharmony_ci#endif /* HB_SUBSET_PLAN_HH */
210