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_SBIX_TABLE_HH
26#define HB_OT_COLOR_SBIX_TABLE_HH
27
28#include "hb-open-type.hh"
29
30/*
31 * sbix -- Standard Bitmap Graphics
32 * https://docs.microsoft.com/en-us/typography/opentype/spec/sbix
33 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6sbix.html
34 */
35#define HB_OT_TAG_sbix HB_TAG('s','b','i','x')
36
37
38namespace OT {
39
40
41struct SBIXGlyph
42{
43 HBINT16 xOffset; /* The horizontal (x-axis) offset from the left
44 * edge of the graphic to the glyph’s origin.
45 * That is, the x-coordinate of the point on the
46 * baseline at the left edge of the glyph. */
47 HBINT16 yOffset; /* The vertical (y-axis) offset from the bottom
48 * edge of the graphic to the glyph’s origin.
49 * That is, the y-coordinate of the point on the
50 * baseline at the left edge of the glyph. */
51 Tag graphicType; /* Indicates the format of the embedded graphic
52 * data: one of 'jpg ', 'png ' or 'tiff', or the
53 * special format 'dupe'. */
54 UnsizedArrayOf<HBUINT8>
55 data; /* The actual embedded graphic data. The total
56 * length is inferred from sequential entries in
57 * the glyphDataOffsets array and the fixed size
58 * (8 bytes) of the preceding fields. */
59 public:
60 DEFINE_SIZE_ARRAY (8, data);
61};
62
63struct SBIXStrike
64{
65 friend struct sbix;
66
67 inline bool sanitize (hb_sanitize_context_t *c) const
68 {
69 TRACE_SANITIZE (this);
70 return_trace (c->check_struct (this) &&
71 imageOffsetsZ.sanitize_shallow (c, c->get_num_glyphs () + 1));
72 }
73
74 protected:
75 HBUINT16 ppem; /* The PPEM size for which this strike was designed. */
76 HBUINT16 resolution; /* The device pixel density (in PPI) for which this
77 * strike was designed. (E.g., 96 PPI, 192 PPI.) */
78 UnsizedArrayOf<LOffsetTo<SBIXGlyph> >
79 imageOffsetsZ; /* Offset from the beginning of the strike data header
80 * to bitmap data for an individual glyph ID. */
81 public:
82 DEFINE_SIZE_STATIC (8);
83};
84
85struct sbix
86{
87 static const hb_tag_t tableTag = HB_OT_TAG_sbix;
88
89 inline bool sanitize (hb_sanitize_context_t *c) const
90 {
91 TRACE_SANITIZE (this);
92 return_trace (likely (c->check_struct (this) && strikes.sanitize (c, this)));
93 }
94
95 struct accelerator_t
96 {
97 inline void init (hb_face_t *face)
98 {
99 sbix_blob = hb_sanitize_context_t().reference_table<sbix> (face);
100 sbix_len = hb_blob_get_length (sbix_blob);
101 sbix_table = sbix_blob->as<sbix> ();
102 }
103
104 inline void fini (void)
105 {
106 hb_blob_destroy (sbix_blob);
107 }
108
109 inline void dump (void (*callback) (const uint8_t* data, unsigned int length,
110 unsigned int group, unsigned int gid)) const
111 {
112 for (unsigned group = 0; group < sbix_table->strikes.len; ++group)
113 {
114 const SBIXStrike &strike = sbix_table->strikes[group](sbix_table);
115 for (unsigned int glyph = 0; glyph < num_glyphs; ++glyph)
116 if (strike.imageOffsetsZ[glyph + 1] - strike.imageOffsetsZ[glyph] > 0)
117 {
118 const SBIXGlyph &sbixGlyph = strike.imageOffsetsZ[glyph]((const void *) &strike);
119 callback ((const uint8_t*) &sbixGlyph.data,
120 strike.imageOffsetsZ[glyph + 1] - strike.imageOffsetsZ[glyph] - 8,
121 group, glyph);
122 }
123 }
124 }
125
126 private:
127 hb_blob_t *sbix_blob;
128 const sbix *sbix_table;
129
130 unsigned int sbix_len;
131 unsigned int num_glyphs;
132 };
133
134 protected:
135 HBUINT16 version; /* Table version number — set to 1 */
136 HBUINT16 flags; /* Bit 0: Set to 1. Bit 1: Draw outlines.
137 * Bits 2 to 15: reserved (set to 0). */
138 LArrayOf<LOffsetTo<SBIXStrike> >
139 strikes; /* Offsets from the beginning of the 'sbix'
140 * table to data for each individual bitmap strike. */
141 public:
142 DEFINE_SIZE_ARRAY (8, strikes);
143};
144
145} /* namespace OT */
146
147#endif /* HB_OT_COLOR_SBIX_TABLE_HH */
148