QtBase  v6.3.1
hb-ot-post-table-v2subset.hh
Go to the documentation of this file.
1 /*
2  * Copyright © 2021 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  */
25 
26 #ifndef HB_OT_POST_TABLE_V2SUBSET_HH
27 #define HB_OT_POST_TABLE_V2SUBSET_HH
28 
29 #include "hb-open-type.hh"
30 #include "hb-ot-post-table.hh"
31 
32 /*
33  * post -- PostScript
34  * https://docs.microsoft.com/en-us/typography/opentype/spec/post
35  */
36 
37 namespace OT {
38 template<typename Iterator>
40  Iterator it,
41  const void* _post) const
42 {
43  TRACE_SERIALIZE (this);
44  auto *out = c->start_embed (this);
45  if (unlikely (!c->check_success (out))) return_trace (false);
46  if (!out->glyphNameIndex.serialize (c, + it
47  | hb_map (hb_second)))
48  return_trace (false);
49 
50  hb_set_t copied_indices;
51  for (const auto& _ : + it )
52  {
53  unsigned glyph_id = _.first;
54  unsigned new_index = _.second;
55 
56  if (new_index < 258) continue;
57  if (copied_indices.has (new_index)) continue;
58  copied_indices.add (new_index);
59 
60  hb_bytes_t s = reinterpret_cast<const post::accelerator_t*> (_post)->find_glyph_name (glyph_id);
61  HBUINT8 *o = c->allocate_size<HBUINT8> (HBUINT8::static_size * (s.length + 1));
62  if (unlikely (!o)) return_trace (false);
63  if (!c->check_assign (o[0], s.length, HB_SERIALIZE_ERROR_INT_OVERFLOW)) return_trace (false);
64  memcpy (o+1, s.arrayZ, HBUINT8::static_size * s.length);
65  }
66 
67  return_trace (true);
68 }
69 
71 {
72  TRACE_SUBSET (this);
73 
74  const hb_map_t &reverse_glyph_map = *c->plan->reverse_glyph_map;
75  unsigned num_glyphs = c->plan->num_output_glyphs ();
76  hb_map_t old_new_index_map, old_gid_new_index_map;
77  unsigned i = 0;
78 
79  post::accelerator_t _post (c->plan->source);
80 
81  hb_hashmap_t<hb_bytes_t, unsigned, std::nullptr_t, unsigned, nullptr, (unsigned)-1> glyph_name_to_new_index;
82  for (hb_codepoint_t new_gid = 0; new_gid < num_glyphs; new_gid++)
83  {
84  hb_codepoint_t old_gid = reverse_glyph_map.get (new_gid);
85  unsigned old_index = glyphNameIndex[old_gid];
86 
87  unsigned new_index;
88  if (old_index <= 257) new_index = old_index;
89  else if (old_new_index_map.has (old_index)) new_index = old_new_index_map.get (old_index);
90  else
91  {
92  hb_bytes_t s = _post.find_glyph_name (old_gid);
93  new_index = glyph_name_to_new_index.get (s);
94  if (new_index == (unsigned)-1)
95  {
96  int standard_glyph_index = -1;
97  for (unsigned i = 0; i < format1_names_length; i++)
98  {
99  if (s == format1_names (i))
100  {
101  standard_glyph_index = i;
102  break;
103  }
104  }
105 
106  if (standard_glyph_index == -1)
107  {
108  new_index = 258 + i;
109  i++;
110  }
111  else
112  { new_index = standard_glyph_index; }
113  glyph_name_to_new_index.set (s, new_index);
114  }
115  old_new_index_map.set (old_index, new_index);
116  }
117  old_gid_new_index_map.set (old_gid, new_index);
118  }
119 
120  auto index_iter =
121  + hb_range (num_glyphs)
122  | hb_map (reverse_glyph_map)
123  | hb_map_retains_sorting ([&](hb_codepoint_t old_gid)
124  {
125  unsigned new_index = old_gid_new_index_map.get (old_gid);
126  return hb_pair_t<unsigned, unsigned> (old_gid, new_index);
127  })
128  ;
129 
130  return_trace (serialize (c->serializer, index_iter, &_post));
131 }
132 
133 } /* namespace OT */
134 #endif /* HB_OT_POST_TABLE_V2SUBSET_HH */
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
hb_array_t< const char > hb_bytes_t
Definition: hb-array.hh:434
#define TRACE_SERIALIZE(this)
Definition: hb-debug.hh:426
#define return_trace(RET)
Definition: hb-debug.hh:349
#define TRACE_SUBSET(this)
Definition: hb-debug.hh:438
auto it hb_map(hb_second)) template< typename Type > inline hb_array_t< Type > operator()(hb_array_t< Type > array
#define _(S, M)
@ HB_SERIALIZE_ERROR_INT_OVERFLOW
Definition: hb-serialize.hh:52
#define unlikely(expr)
Definition: hb.hh:251
#define HB_INTERNAL
Definition: hb.hh:274
const GLubyte * c
Definition: qopenglext.h:12701
GLdouble s
[6]
Definition: qopenglext.h:235
uint32_t hb_codepoint_t
Definition: hb-common.h:106
QTextStream out(stdout)
[7]
QStringList::Iterator it
hb_bytes_t find_glyph_name(hb_codepoint_t glyph) const
bool serialize(hb_serialize_context_t *c, Iterator it, const void *_post) const
bool subset(hb_subset_context_t *c) const
Array16Of< HBUINT16 > glyphNameIndex
V get(K key) const
Definition: hb-map.hh:202
bool set(K key, const V &value)
Definition: hb-map.hh:199
bool has(K k, V *vp=nullptr) const
Definition: hb-map.hh:214
bool has(hb_codepoint_t k) const
Definition: hb-set.hh:111
void add(hb_codepoint_t g)
Definition: hb-set.hh:85