• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2018 Adobe 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  * Adobe Author(s): Michiharu Ariza
25  */
26 #ifndef HB_OT_CFF_COMMON_HH
27 #define HB_OT_CFF_COMMON_HH
28 
29 #include "hb-open-type.hh"
30 #include "hb-bimap.hh"
31 #include "hb-ot-layout-common.hh"
32 #include "hb-cff-interp-dict-common.hh"
33 #include "hb-subset-plan.hh"
34 
35 namespace CFF {
36 
37 using namespace OT;
38 
39 #define CFF_UNDEF_CODE  0xFFFFFFFF
40 
41 using objidx_t = hb_serialize_context_t::objidx_t;
42 using whence_t = hb_serialize_context_t::whence_t;
43 
44 /* utility macro */
45 template<typename Type>
StructAtOffsetOrNull(const void * P,unsigned int offset)46 static inline const Type& StructAtOffsetOrNull (const void *P, unsigned int offset)
47 { return offset ? StructAtOffset<Type> (P, offset) : Null (Type); }
48 
49 struct code_pair_t
50 {
51   unsigned code;
52   hb_codepoint_t glyph;
53 };
54 
55 
56 using str_buff_t = hb_vector_t<unsigned char>;
57 using str_buff_vec_t = hb_vector_t<str_buff_t>;
58 using glyph_to_sid_map_t = hb_vector_t<code_pair_t>;
59 
60 struct length_f_t
61 {
62   template <typename Iterable,
63 	    hb_requires (hb_is_iterable (Iterable))>
operator ()CFF::length_f_t64   unsigned operator () (const Iterable &_) const { return hb_len (hb_iter (_)); }
65 
operator ()CFF::length_f_t66   unsigned operator () (unsigned _) const { return _; }
67 }
68 HB_FUNCOBJ (length_f);
69 
70 /* CFF INDEX */
71 template <typename COUNT>
72 struct CFFIndex
73 {
offset_array_sizeCFF::CFFIndex74   unsigned int offset_array_size () const
75   { return offSize * (count + 1); }
76 
77   template <typename Iterable,
78 	    hb_requires (hb_is_iterable (Iterable))>
serializeCFF::CFFIndex79   bool serialize (hb_serialize_context_t *c,
80 		  const Iterable &iterable,
81 		  const unsigned *p_data_size = nullptr)
82   {
83     TRACE_SERIALIZE (this);
84     unsigned data_size;
85     if (p_data_size)
86       data_size = *p_data_size;
87     else
88       total_size (iterable, &data_size);
89 
90     auto it = hb_iter (iterable);
91     if (unlikely (!serialize_header (c, +it, data_size))) return_trace (false);
92     unsigned char *ret = c->allocate_size<unsigned char> (data_size, false);
93     if (unlikely (!ret)) return_trace (false);
94     for (const auto &_ : +it)
95     {
96       unsigned len = _.length;
97       if (!len)
98 	continue;
99       if (len <= 1)
100       {
101 	*ret++ = *_.arrayZ;
102 	continue;
103       }
104       hb_memcpy (ret, _.arrayZ, len);
105       ret += len;
106     }
107     return_trace (true);
108   }
109 
110   template <typename Iterator,
111 	    hb_requires (hb_is_iterator (Iterator))>
serialize_headerCFF::CFFIndex112   bool serialize_header (hb_serialize_context_t *c,
113 			 Iterator it,
114 			 unsigned data_size)
115   {
116     TRACE_SERIALIZE (this);
117 
118     unsigned off_size = (hb_bit_storage (data_size + 1) + 7) / 8;
119 
120     /* serialize CFFIndex header */
121     if (unlikely (!c->extend_min (this))) return_trace (false);
122     this->count = hb_len (it);
123     if (!this->count) return_trace (true);
124     if (unlikely (!c->extend (this->offSize))) return_trace (false);
125     this->offSize = off_size;
126     if (unlikely (!c->allocate_size<HBUINT8> (off_size * (this->count + 1), false)))
127       return_trace (false);
128 
129     /* serialize indices */
130     unsigned int offset = 1;
131     if (HB_OPTIMIZE_SIZE_VAL)
132     {
133       unsigned int i = 0;
134       for (const auto &_ : +it)
135       {
136 	set_offset_at (i++, offset);
137 	offset += length_f (_);
138       }
139       set_offset_at (i, offset);
140     }
141     else
142       switch (off_size)
143       {
144 	case 1:
145 	{
146 	  HBUINT8 *p = (HBUINT8 *) offsets;
147 	  for (const auto &_ : +it)
148 	  {
149 	    *p++ = offset;
150 	    offset += length_f (_);
151 	  }
152 	  *p = offset;
153 	}
154 	break;
155 	case 2:
156 	{
157 	  HBUINT16 *p = (HBUINT16 *) offsets;
158 	  for (const auto &_ : +it)
159 	  {
160 	    *p++ = offset;
161 	    offset += length_f (_);
162 	  }
163 	  *p = offset;
164 	}
165 	break;
166 	case 3:
167 	{
168 	  HBUINT24 *p = (HBUINT24 *) offsets;
169 	  for (const auto &_ : +it)
170 	  {
171 	    *p++ = offset;
172 	    offset += length_f (_);
173 	  }
174 	  *p = offset;
175 	}
176 	break;
177 	case 4:
178 	{
179 	  HBUINT32 *p = (HBUINT32 *) offsets;
180 	  for (const auto &_ : +it)
181 	  {
182 	    *p++ = offset;
183 	    offset += length_f (_);
184 	  }
185 	  *p = offset;
186 	}
187 	break;
188 	default:
189 	break;
190       }
191 
192     assert (offset == data_size + 1);
193     return_trace (true);
194   }
195 
196   template <typename Iterable,
197 	    hb_requires (hb_is_iterable (Iterable))>
total_sizeCFF::CFFIndex198   static unsigned total_size (const Iterable &iterable, unsigned *data_size = nullptr)
199   {
200     auto it = + hb_iter (iterable);
201     if (!it)
202     {
203       if (data_size) *data_size = 0;
204       return min_size;
205     }
206 
207     unsigned total = 0;
208     for (const auto &_ : +it)
209       total += length_f (_);
210 
211     if (data_size) *data_size = total;
212 
213     unsigned off_size = (hb_bit_storage (total + 1) + 7) / 8;
214 
215     return min_size + HBUINT8::static_size + (hb_len (it) + 1) * off_size + total;
216   }
217 
set_offset_atCFF::CFFIndex218   void set_offset_at (unsigned int index, unsigned int offset)
219   {
220     assert (index <= count);
221 
222     unsigned int size = offSize;
223     const HBUINT8 *p = offsets;
224     switch (size)
225     {
226       case 1: ((HBUINT8  *) p)[index] = offset; break;
227       case 2: ((HBUINT16 *) p)[index] = offset; break;
228       case 3: ((HBUINT24 *) p)[index] = offset; break;
229       case 4: ((HBUINT32 *) p)[index] = offset; break;
230       default: return;
231     }
232   }
233 
234   private:
offset_atCFF::CFFIndex235   unsigned int offset_at (unsigned int index) const
236   {
237     assert (index <= count);
238 
239     unsigned int size = offSize;
240     const HBUINT8 *p = offsets;
241     switch (size)
242     {
243       case 1: return ((HBUINT8  *) p)[index];
244       case 2: return ((HBUINT16 *) p)[index];
245       case 3: return ((HBUINT24 *) p)[index];
246       case 4: return ((HBUINT32 *) p)[index];
247       default: return 0;
248     }
249   }
250 
data_baseCFF::CFFIndex251   const unsigned char *data_base () const
252   { return (const unsigned char *) this + min_size + offSize.static_size - 1 + offset_array_size (); }
253   public:
254 
operator []CFF::CFFIndex255   hb_ubytes_t operator [] (unsigned int index) const
256   {
257     if (unlikely (index >= count)) return hb_ubytes_t ();
258     _hb_compiler_memory_r_barrier ();
259     unsigned offset0 = offset_at (index);
260     unsigned offset1 = offset_at (index + 1);
261     if (unlikely (offset1 < offset0 || offset1 > offset_at (count)))
262       return hb_ubytes_t ();
263     return hb_ubytes_t (data_base () + offset0, offset1 - offset0);
264   }
265 
get_sizeCFF::CFFIndex266   unsigned int get_size () const
267   {
268     if (count)
269       return min_size + offSize.static_size + offset_array_size () + (offset_at (count) - 1);
270     return min_size;  /* empty CFFIndex contains count only */
271   }
272 
sanitizeCFF::CFFIndex273   bool sanitize (hb_sanitize_context_t *c) const
274   {
275     TRACE_SANITIZE (this);
276     return_trace (likely (c->check_struct (this) &&
277 			  (count == 0 || /* empty INDEX */
278 			   (count < count + 1u &&
279 			    c->check_struct (&offSize) && offSize >= 1 && offSize <= 4 &&
280 			    c->check_array (offsets, offSize, count + 1u) &&
281 			    c->check_array ((const HBUINT8*) data_base (), 1, offset_at (count))))));
282   }
283 
284   public:
285   COUNT		count;		/* Number of object data. Note there are (count+1) offsets */
286   private:
287   HBUINT8	offSize;	/* The byte size of each offset in the offsets array. */
288   HBUINT8	offsets[HB_VAR_ARRAY];
289 				/* The array of (count + 1) offsets into objects array (1-base). */
290   /* HBUINT8 data[HB_VAR_ARRAY];	Object data */
291   public:
292   DEFINE_SIZE_MIN (COUNT::static_size);
293 };
294 
295 /* Top Dict, Font Dict, Private Dict */
296 struct Dict : UnsizedByteStr
297 {
298   template <typename DICTVAL, typename OP_SERIALIZER, typename ...Ts>
serializeCFF::Dict299   bool serialize (hb_serialize_context_t *c,
300 		  const DICTVAL &dictval,
301 		  OP_SERIALIZER& opszr,
302 		  Ts&&... ds)
303   {
304     TRACE_SERIALIZE (this);
305     for (unsigned int i = 0; i < dictval.get_count (); i++)
306       if (unlikely (!opszr.serialize (c, dictval[i], std::forward<Ts> (ds)...)))
307 	return_trace (false);
308 
309     return_trace (true);
310   }
311 
312   template <typename T, typename V>
serialize_int_opCFF::Dict313   static bool serialize_int_op (hb_serialize_context_t *c, op_code_t op, V value, op_code_t intOp)
314   {
315     if (unlikely ((!serialize_int<T, V> (c, intOp, value))))
316       return false;
317 
318     TRACE_SERIALIZE (this);
319     /* serialize the opcode */
320     HBUINT8 *p = c->allocate_size<HBUINT8> (OpCode_Size (op), false);
321     if (unlikely (!p)) return_trace (false);
322     if (Is_OpCode_ESC (op))
323     {
324       *p = OpCode_escape;
325       op = Unmake_OpCode_ESC (op);
326       p++;
327     }
328     *p = op;
329     return_trace (true);
330   }
331 
332   template <typename V>
serialize_int4_opCFF::Dict333   static bool serialize_int4_op (hb_serialize_context_t *c, op_code_t op, V value)
334   { return serialize_int_op<HBINT32> (c, op, value, OpCode_longintdict); }
335 
336   template <typename V>
serialize_int2_opCFF::Dict337   static bool serialize_int2_op (hb_serialize_context_t *c, op_code_t op, V value)
338   { return serialize_int_op<HBINT16> (c, op, value, OpCode_shortint); }
339 
340   template <typename T, int int_op>
serialize_link_opCFF::Dict341   static bool serialize_link_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence)
342   {
343     T &ofs = *(T *) (c->head + OpCode_Size (int_op));
344     if (unlikely (!serialize_int_op<T> (c, op, 0, int_op))) return false;
345     c->add_link (ofs, link, whence);
346     return true;
347   }
348 
serialize_link4_opCFF::Dict349   static bool serialize_link4_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence = whence_t::Head)
350   { return serialize_link_op<HBINT32, OpCode_longintdict> (c, op, link, whence); }
351 
serialize_link2_opCFF::Dict352   static bool serialize_link2_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence = whence_t::Head)
353   { return serialize_link_op<HBINT16, OpCode_shortint> (c, op, link, whence); }
354 };
355 
356 struct TopDict : Dict {};
357 struct FontDict : Dict {};
358 struct PrivateDict : Dict {};
359 
360 struct table_info_t
361 {
initCFF::table_info_t362   void init () { offset = size = 0; link = 0; }
363 
364   unsigned int    offset;
365   unsigned int    size;
366   objidx_t	  link;
367 };
368 
369 template <typename COUNT>
370 struct FDArray : CFFIndex<COUNT>
371 {
372   template <typename DICTVAL, typename INFO, typename Iterator, typename OP_SERIALIZER>
serializeCFF::FDArray373   bool serialize (hb_serialize_context_t *c,
374 		  Iterator it,
375 		  OP_SERIALIZER& opszr)
376   {
377     TRACE_SERIALIZE (this);
378 
379     /* serialize INDEX data */
380     hb_vector_t<unsigned> sizes;
381     if (it.is_random_access_iterator)
382       sizes.alloc (hb_len (it));
383 
384     c->push ();
385     char *data_base = c->head;
386     + it
387     | hb_map ([&] (const hb_pair_t<const DICTVAL&, const INFO&> &_)
388     {
389       FontDict *dict = c->start_embed<FontDict> ();
390 		dict->serialize (c, _.first, opszr, _.second);
391 		return c->head - (const char*)dict;
392 	      })
393     | hb_sink (sizes)
394     ;
395     unsigned data_size = c->head - data_base;
396     c->pop_pack (false);
397 
398     if (unlikely (sizes.in_error ())) return_trace (false);
399 
400     /* It just happens that the above is packed right after the header below.
401      * Such a hack. */
402 
403     /* serialize INDEX header */
404     return_trace (CFFIndex<COUNT>::serialize_header (c, hb_iter (sizes), data_size));
405   }
406 };
407 
408 /* FDSelect */
409 struct FDSelect0 {
sanitizeCFF::FDSelect0410   bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
411   {
412     TRACE_SANITIZE (this);
413     if (unlikely (!(c->check_struct (this))))
414       return_trace (false);
415     if (unlikely (!c->check_array (fds, c->get_num_glyphs ())))
416       return_trace (false);
417 
418     return_trace (true);
419   }
420 
get_fdCFF::FDSelect0421   unsigned get_fd (hb_codepoint_t glyph) const
422   { return fds[glyph]; }
423 
get_fd_rangeCFF::FDSelect0424   hb_pair_t<unsigned, hb_codepoint_t> get_fd_range (hb_codepoint_t glyph) const
425   { return {fds[glyph], glyph + 1}; }
426 
get_sizeCFF::FDSelect0427   unsigned int get_size (unsigned int num_glyphs) const
428   { return HBUINT8::static_size * num_glyphs; }
429 
430   HBUINT8     fds[HB_VAR_ARRAY];
431 
432   DEFINE_SIZE_MIN (0);
433 };
434 
435 template <typename GID_TYPE, typename FD_TYPE>
436 struct FDSelect3_4_Range
437 {
sanitizeCFF::FDSelect3_4_Range438   bool sanitize (hb_sanitize_context_t *c, const void * /*nullptr*/, unsigned int fdcount) const
439   {
440     TRACE_SANITIZE (this);
441     return_trace (first < c->get_num_glyphs () && (fd < fdcount));
442   }
443 
444   GID_TYPE    first;
445   FD_TYPE     fd;
446   public:
447   DEFINE_SIZE_STATIC (GID_TYPE::static_size + FD_TYPE::static_size);
448 };
449 
450 template <typename GID_TYPE, typename FD_TYPE>
451 struct FDSelect3_4
452 {
get_sizeCFF::FDSelect3_4453   unsigned int get_size () const
454   { return GID_TYPE::static_size * 2 + ranges.get_size (); }
455 
sanitizeCFF::FDSelect3_4456   bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
457   {
458     TRACE_SANITIZE (this);
459     if (unlikely (!c->check_struct (this) || !ranges.sanitize (c, nullptr, fdcount) ||
460 		  (nRanges () == 0) || ranges[0].first != 0))
461       return_trace (false);
462 
463     for (unsigned int i = 1; i < nRanges (); i++)
464       if (unlikely (ranges[i - 1].first >= ranges[i].first))
465 	return_trace (false);
466 
467     if (unlikely (!sentinel().sanitize (c) || (sentinel() != c->get_num_glyphs ())))
468       return_trace (false);
469 
470     return_trace (true);
471   }
472 
_cmp_rangeCFF::FDSelect3_4473   static int _cmp_range (const void *_key, const void *_item)
474   {
475     hb_codepoint_t glyph = * (hb_codepoint_t *) _key;
476     FDSelect3_4_Range<GID_TYPE, FD_TYPE> *range = (FDSelect3_4_Range<GID_TYPE, FD_TYPE> *) _item;
477 
478     if (glyph < range[0].first) return -1;
479     if (glyph < range[1].first) return 0;
480     return +1;
481   }
482 
get_fdCFF::FDSelect3_4483   unsigned get_fd (hb_codepoint_t glyph) const
484   {
485     auto *range = hb_bsearch (glyph, &ranges[0], nRanges () - 1, sizeof (ranges[0]), _cmp_range);
486     return range ? range->fd : ranges[nRanges () - 1].fd;
487   }
488 
get_fd_rangeCFF::FDSelect3_4489   hb_pair_t<unsigned, hb_codepoint_t> get_fd_range (hb_codepoint_t glyph) const
490   {
491     auto *range = hb_bsearch (glyph, &ranges[0], nRanges () - 1, sizeof (ranges[0]), _cmp_range);
492     unsigned fd = range ? range->fd : ranges[nRanges () - 1].fd;
493     hb_codepoint_t end = range ? range[1].first : ranges[nRanges () - 1].first;
494     return {fd, end};
495   }
496 
nRangesCFF::FDSelect3_4497   GID_TYPE        &nRanges ()       { return ranges.len; }
nRangesCFF::FDSelect3_4498   GID_TYPE         nRanges () const { return ranges.len; }
sentinelCFF::FDSelect3_4499   GID_TYPE       &sentinel ()       { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
sentinelCFF::FDSelect3_4500   const GID_TYPE &sentinel () const { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
501 
502   ArrayOf<FDSelect3_4_Range<GID_TYPE, FD_TYPE>, GID_TYPE> ranges;
503   /* GID_TYPE sentinel */
504 
505   DEFINE_SIZE_ARRAY (GID_TYPE::static_size, ranges);
506 };
507 
508 typedef FDSelect3_4<HBUINT16, HBUINT8> FDSelect3;
509 typedef FDSelect3_4_Range<HBUINT16, HBUINT8> FDSelect3_Range;
510 
511 struct FDSelect
512 {
serializeCFF::FDSelect513   bool serialize (hb_serialize_context_t *c, const FDSelect &src, unsigned int num_glyphs)
514   {
515     TRACE_SERIALIZE (this);
516     unsigned int size = src.get_size (num_glyphs);
517     FDSelect *dest = c->allocate_size<FDSelect> (size, false);
518     if (unlikely (!dest)) return_trace (false);
519     hb_memcpy (dest, &src, size);
520     return_trace (true);
521   }
522 
get_sizeCFF::FDSelect523   unsigned int get_size (unsigned int num_glyphs) const
524   {
525     switch (format)
526     {
527     case 0: return format.static_size + u.format0.get_size (num_glyphs);
528     case 3: return format.static_size + u.format3.get_size ();
529     default:return 0;
530     }
531   }
532 
get_fdCFF::FDSelect533   unsigned get_fd (hb_codepoint_t glyph) const
534   {
535     if (this == &Null (FDSelect)) return 0;
536 
537     switch (format)
538     {
539     case 0: return u.format0.get_fd (glyph);
540     case 3: return u.format3.get_fd (glyph);
541     default:return 0;
542     }
543   }
544   /* Returns pair of fd and one after last glyph in range. */
get_fd_rangeCFF::FDSelect545   hb_pair_t<unsigned, hb_codepoint_t> get_fd_range (hb_codepoint_t glyph) const
546   {
547     if (this == &Null (FDSelect)) return {0, 1};
548 
549     switch (format)
550     {
551     case 0: return u.format0.get_fd_range (glyph);
552     case 3: return u.format3.get_fd_range (glyph);
553     default:return {0, 1};
554     }
555   }
556 
sanitizeCFF::FDSelect557   bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
558   {
559     TRACE_SANITIZE (this);
560     if (unlikely (!c->check_struct (this)))
561       return_trace (false);
562 
563     switch (format)
564     {
565     case 0: return_trace (u.format0.sanitize (c, fdcount));
566     case 3: return_trace (u.format3.sanitize (c, fdcount));
567     default:return_trace (false);
568     }
569   }
570 
571   HBUINT8	format;
572   union {
573   FDSelect0	format0;
574   FDSelect3	format3;
575   } u;
576   public:
577   DEFINE_SIZE_MIN (1);
578 };
579 
580 template <typename COUNT>
581 struct Subrs : CFFIndex<COUNT>
582 {
583   typedef COUNT count_type;
584   typedef CFFIndex<COUNT> SUPER;
585 };
586 
587 } /* namespace CFF */
588 
589 #endif /* HB_OT_CFF_COMMON_HH */
590