1/*
2 * Copyright © 2019 Ebrahim Byagowi
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#ifndef HB_OT_META_TABLE_HH
26#define HB_OT_META_TABLE_HH
27
28#include "hb-open-type.hh"
29
30/*
31 * meta -- Metadata Table
32 * https://docs.microsoft.com/en-us/typography/opentype/spec/meta
33 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6meta.html
34 */
35#define HB_OT_TAG_meta HB_TAG ('m','e','t','a')
36
37
38namespace OT {
39
40
41struct DataMap
42{
43 int cmp (hb_tag_t a) const { return tag.cmp (a); }
44
45 hb_tag_t get_tag () const { return tag; }
46
47 hb_blob_t *reference_entry (hb_blob_t *meta_blob) const
48 { return hb_blob_create_sub_blob (meta_blob, dataZ, dataLength); }
49
50 bool sanitize (hb_sanitize_context_t *c, const void *base) const
51 {
52 TRACE_SANITIZE (this);
53 return_trace (likely (c->check_struct (this) &&
54 dataZ.sanitize (c, base, dataLength)));
55 }
56
57 protected:
58 Tag tag; /* A tag indicating the type of metadata. */
59 LNNOffsetTo<UnsizedArrayOf<HBUINT8>>
60 dataZ; /* Offset in bytes from the beginning of the
61 * metadata table to the data for this tag. */
62 HBUINT32 dataLength; /* Length of the data. The data is not required to
63 * be padded to any byte boundary. */
64 public:
65 DEFINE_SIZE_STATIC (12);
66};
67
68struct meta
69{
70 static constexpr hb_tag_t tableTag = HB_OT_TAG_meta;
71
72 struct accelerator_t
73 {
74 void init (hb_face_t *face)
75 { table = hb_sanitize_context_t ().reference_table<meta> (face); }
76 void fini () { table.destroy (); }
77
78 hb_blob_t *reference_entry (hb_tag_t tag) const
79 { return table->dataMaps.lsearch (tag).reference_entry (table.get_blob ()); }
80
81 unsigned int get_entries (unsigned int start_offset,
82 unsigned int *count,
83 hb_ot_meta_tag_t *entries) const
84 {
85 if (count)
86 {
87 + table->dataMaps.sub_array (start_offset, count)
88 | hb_map (&DataMap::get_tag)
89 | hb_map ([](hb_tag_t tag) { return (hb_ot_meta_tag_t) tag; })
90 | hb_sink (hb_array (entries, *count))
91 ;
92 }
93 return table->dataMaps.len;
94 }
95
96 private:
97 hb_blob_ptr_t<meta> table;
98 };
99
100 bool sanitize (hb_sanitize_context_t *c) const
101 {
102 TRACE_SANITIZE (this);
103 return_trace (likely (c->check_struct (this) &&
104 version == 1 &&
105 dataMaps.sanitize (c, this)));
106 }
107
108 protected:
109 HBUINT32 version; /* Version number of the metadata table — set to 1. */
110 HBUINT32 flags; /* Flags — currently unused; set to 0. */
111 HBUINT32 dataOffset;
112 /* Per Apple specification:
113 * Offset from the beginning of the table to the data.
114 * Per OT specification:
115 * Reserved. Not used; should be set to 0. */
116 LArrayOf<DataMap>
117 dataMaps;/* Array of data map records. */
118 public:
119 DEFINE_SIZE_ARRAY (16, dataMaps);
120};
121
122struct meta_accelerator_t : meta::accelerator_t {};
123
124} /* namespace OT */
125
126
127#endif /* HB_OT_META_TABLE_HH */
128