blob: dc75f5db57b395274f8bed5aad1661eda6ba9da2 [file] [log] [blame]
Ebrahim Byagowi60d9f002019-08-11 16:15:19 +04301/*
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_AAT_LAYOUT_OPBD_TABLE_HH
26#define HB_AAT_LAYOUT_OPBD_TABLE_HH
27
28#include "hb-aat-layout-common.hh"
29#include "hb-open-type.hh"
30
31/*
32 * opbd -- Optical Bounds
33 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6opbd.html
34 */
35#define HB_AAT_TAG_opbd HB_TAG('o','p','b','d')
36
37
38namespace AAT {
39
40struct OpticalBounds
41{
42 bool sanitize (hb_sanitize_context_t *c) const
43 {
44 TRACE_SANITIZE (this);
Behdad Esfahbod7c4e9082022-07-11 14:01:52 -060045 return_trace (c->check_struct (this));
Ebrahim Byagowi60d9f002019-08-11 16:15:19 +043046 }
47
Ebrahim Byagowid6206db2019-08-14 11:24:06 +043048 FWORD leftSide;
49 FWORD topSide;
50 FWORD rightSide;
51 FWORD bottomSide;
Ebrahim Byagowi60d9f002019-08-11 16:15:19 +043052 public:
53 DEFINE_SIZE_STATIC (8);
54};
55
Ebrahim Byagowibfffe852019-08-14 13:55:49 +043056struct opbdFormat0
57{
58 bool get_bounds (hb_font_t *font, hb_codepoint_t glyph_id,
59 hb_glyph_extents_t *extents, const void *base) const
60 {
Behdad Esfahbodad28f972021-03-31 12:49:14 -060061 const Offset16To<OpticalBounds> *bounds_offset = lookupTable.get_value (glyph_id, font->face->get_num_glyphs ());
Ebrahim Byagowibfffe852019-08-14 13:55:49 +043062 if (!bounds_offset) return false;
63 const OpticalBounds &bounds = base+*bounds_offset;
64
65 if (extents)
66 *extents = {
67 font->em_scale_x (bounds.leftSide),
68 font->em_scale_y (bounds.topSide),
69 font->em_scale_x (bounds.rightSide),
70 font->em_scale_y (bounds.bottomSide)
71 };
72 return true;
73 }
74
75 bool sanitize (hb_sanitize_context_t *c, const void *base) const
76 {
77 TRACE_SANITIZE (this);
78 return_trace (likely (c->check_struct (this) && lookupTable.sanitize (c, base)));
79 }
80
81 protected:
Behdad Esfahbodad28f972021-03-31 12:49:14 -060082 Lookup<Offset16To<OpticalBounds>>
Ebrahim Byagowibfffe852019-08-14 13:55:49 +043083 lookupTable; /* Lookup table associating glyphs with the four
84 * int16 values for the left-side, top-side,
85 * right-side, and bottom-side optical bounds. */
86 public:
87 DEFINE_SIZE_MIN (2);
88};
89
90struct opbdFormat1
91{
92 bool get_bounds (hb_font_t *font, hb_codepoint_t glyph_id,
93 hb_glyph_extents_t *extents, const void *base) const
94 {
Behdad Esfahbodad28f972021-03-31 12:49:14 -060095 const Offset16To<OpticalBounds> *bounds_offset = lookupTable.get_value (glyph_id, font->face->get_num_glyphs ());
Ebrahim Byagowibfffe852019-08-14 13:55:49 +043096 if (!bounds_offset) return false;
97 const OpticalBounds &bounds = base+*bounds_offset;
98
99 hb_position_t left = 0, top = 0, right = 0, bottom = 0, ignore;
100 if (font->get_glyph_contour_point (glyph_id, bounds.leftSide, &left, &ignore) ||
101 font->get_glyph_contour_point (glyph_id, bounds.topSide, &ignore, &top) ||
102 font->get_glyph_contour_point (glyph_id, bounds.rightSide, &right, &ignore) ||
103 font->get_glyph_contour_point (glyph_id, bounds.bottomSide, &ignore, &bottom))
104 {
105 if (extents)
106 *extents = {left, top, right, bottom};
107 return true;
108 }
109 return false;
110 }
111
112 bool sanitize (hb_sanitize_context_t *c, const void *base) const
113 {
114 TRACE_SANITIZE (this);
115 return_trace (likely (c->check_struct (this) && lookupTable.sanitize (c, base)));
116 }
117
118 protected:
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600119 Lookup<Offset16To<OpticalBounds>>
Ebrahim Byagowibfffe852019-08-14 13:55:49 +0430120 lookupTable; /* Lookup table associating glyphs with the four
121 * int16 values for the left-side, top-side,
122 * right-side, and bottom-side optical bounds. */
123 public:
124 DEFINE_SIZE_MIN (2);
125};
126
Ebrahim Byagowi60d9f002019-08-11 16:15:19 +0430127struct opbd
128{
129 static constexpr hb_tag_t tableTag = HB_AAT_TAG_opbd;
130
Ebrahim Byagowibfffe852019-08-14 13:55:49 +0430131 bool get_bounds (hb_font_t *font, hb_codepoint_t glyph_id,
132 hb_glyph_extents_t *extents) const
Ebrahim Byagowi60d9f002019-08-11 16:15:19 +0430133 {
Ebrahim Byagowi60d9f002019-08-11 16:15:19 +0430134 switch (format)
135 {
Behdad Esfahbod7a890c22024-09-22 13:19:02 -0600136 case 0: hb_barrier (); return u.format0.get_bounds (font, glyph_id, extents, this);
137 case 1: hb_barrier (); return u.format1.get_bounds (font, glyph_id, extents, this);
Ebrahim Byagowibfffe852019-08-14 13:55:49 +0430138 default:return false;
Ebrahim Byagowi60d9f002019-08-11 16:15:19 +0430139 }
140 }
141
142 bool sanitize (hb_sanitize_context_t *c) const
143 {
144 TRACE_SANITIZE (this);
Ebrahim Byagowibfffe852019-08-14 13:55:49 +0430145 if (unlikely (!c->check_struct (this) || version.major != 1))
146 return_trace (false);
Behdad Esfahbod3a9262c2023-11-04 12:52:46 -0600147 hb_barrier ();
Ebrahim Byagowibfffe852019-08-14 13:55:49 +0430148
149 switch (format)
150 {
Behdad Esfahbod7a890c22024-09-22 13:19:02 -0600151 case 0: hb_barrier (); return_trace (u.format0.sanitize (c, this));
152 case 1: hb_barrier (); return_trace (u.format1.sanitize (c, this));
Ebrahim Byagowibfffe852019-08-14 13:55:49 +0430153 default:return_trace (true);
154 }
Ebrahim Byagowi60d9f002019-08-11 16:15:19 +0430155 }
156
157 protected:
158 FixedVersion<>version; /* Version number of the optical bounds
159 * table (0x00010000 for the current version). */
160 HBUINT16 format; /* Format of the optical bounds table.
161 * Format 0 indicates distance and Format 1 indicates
162 * control point. */
Ebrahim Byagowibfffe852019-08-14 13:55:49 +0430163 union {
Ebrahim Byagowi08428a12020-04-24 23:45:17 +0430164 opbdFormat0 format0;
165 opbdFormat1 format1;
Ebrahim Byagowibfffe852019-08-14 13:55:49 +0430166 } u;
Ebrahim Byagowi60d9f002019-08-11 16:15:19 +0430167 public:
168 DEFINE_SIZE_MIN (8);
169};
170
171} /* namespace AAT */
172
173
174#endif /* HB_AAT_LAYOUT_OPBD_TABLE_HH */