1/*
2 * Copyright © 2018 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_COLOR_SVG_TABLE_HH
26#define HB_OT_COLOR_SVG_TABLE_HH
27
28#include "hb-open-type.hh"
29
30/*
31 * SVG -- SVG (Scalable Vector Graphics)
32 * https://docs.microsoft.com/en-us/typography/opentype/spec/svg
33 */
34
35#define HB_OT_TAG_SVG HB_TAG('S','V','G',' ')
36
37
38namespace OT {
39
40
41struct SVGDocumentIndexEntry
42{
43 int cmp (hb_codepoint_t g) const
44 { return g < startGlyphID ? -1 : g > endGlyphID ? 1 : 0; }
45
46 hb_blob_t *reference_blob (hb_blob_t *svg_blob, unsigned int index_offset) const
47 {
48 return hb_blob_create_sub_blob (svg_blob,
49 index_offset + (unsigned int) svgDoc,
50 svgDocLength);
51 }
52
53 bool sanitize (hb_sanitize_context_t *c, const void *base) const
54 {
55 TRACE_SANITIZE (this);
56 return_trace (c->check_struct (this) &&
57 svgDoc.sanitize (c, base, svgDocLength));
58 }
59
60 protected:
61 HBUINT16 startGlyphID; /* The first glyph ID in the range described by
62 * this index entry. */
63 HBUINT16 endGlyphID; /* The last glyph ID in the range described by
64 * this index entry. Must be >= startGlyphID. */
65 LNNOffsetTo<UnsizedArrayOf<HBUINT8>>
66 svgDoc; /* Offset from the beginning of the SVG Document Index
67 * to an SVG document. Must be non-zero. */
68 HBUINT32 svgDocLength; /* Length of the SVG document.
69 * Must be non-zero. */
70 public:
71 DEFINE_SIZE_STATIC (12);
72};
73
74struct SVG
75{
76 static constexpr hb_tag_t tableTag = HB_OT_TAG_SVG;
77
78 bool has_data () const { return svgDocEntries; }
79
80 struct accelerator_t
81 {
82 void init (hb_face_t *face)
83 { table = hb_sanitize_context_t().reference_table<SVG> (face); }
84 void fini () { table.destroy (); }
85
86 hb_blob_t *reference_blob_for_glyph (hb_codepoint_t glyph_id) const
87 {
88 return table->get_glyph_entry (glyph_id).reference_blob (table.get_blob (),
89 table->svgDocEntries);
90 }
91
92 bool has_data () const { return table->has_data (); }
93
94 private:
95 hb_blob_ptr_t<SVG> table;
96 };
97
98 const SVGDocumentIndexEntry &get_glyph_entry (hb_codepoint_t glyph_id) const
99 { return (this+svgDocEntries).bsearch (glyph_id); }
100
101 bool sanitize (hb_sanitize_context_t *c) const
102 {
103 TRACE_SANITIZE (this);
104 return_trace (likely (c->check_struct (this) &&
105 (this+svgDocEntries).sanitize_shallow (c)));
106 }
107
108 protected:
109 HBUINT16 version; /* Table version (starting at 0). */
110 LOffsetTo<SortedArrayOf<SVGDocumentIndexEntry>>
111 svgDocEntries; /* Offset (relative to the start of the SVG table) to the
112 * SVG Documents Index. Must be non-zero. */
113 /* Array of SVG Document Index Entries. */
114 HBUINT32 reserved; /* Set to 0. */
115 public:
116 DEFINE_SIZE_STATIC (10);
117};
118
119struct SVG_accelerator_t : SVG::accelerator_t {};
120
121} /* namespace OT */
122
123
124#endif /* HB_OT_COLOR_SVG_TABLE_HH */
125