1 | /* |
2 | * Copyright © 2018 Ebrahim Byagowi |
3 | * Copyright © 2018 Google, Inc. |
4 | * |
5 | * This is part of HarfBuzz, a text shaping library. |
6 | * |
7 | * Permission is hereby granted, without written agreement and without |
8 | * license or royalty fees, to use, copy, modify, and distribute this |
9 | * software and its documentation for any purpose, provided that the |
10 | * above copyright notice and the following two paragraphs appear in |
11 | * all copies of this software. |
12 | * |
13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
14 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
15 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
16 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
17 | * DAMAGE. |
18 | * |
19 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
20 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
21 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
22 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
23 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
24 | * |
25 | * Google Author(s): Behdad Esfahbod |
26 | */ |
27 | |
28 | #ifndef HB_AAT_LAYOUT_TRAK_TABLE_HH |
29 | #define HB_AAT_LAYOUT_TRAK_TABLE_HH |
30 | |
31 | #include "hb-aat-layout-common.hh" |
32 | #include "hb-ot-layout.hh" |
33 | #include "hb-open-type.hh" |
34 | |
35 | /* |
36 | * trak -- Tracking |
37 | * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6trak.html |
38 | */ |
39 | #define HB_AAT_TAG_trak HB_TAG('t','r','a','k') |
40 | |
41 | |
42 | namespace AAT { |
43 | |
44 | |
45 | struct TrackTableEntry |
46 | { |
47 | friend struct TrackData; |
48 | |
49 | inline bool sanitize (hb_sanitize_context_t *c, const void *base, |
50 | unsigned int size) const |
51 | { |
52 | TRACE_SANITIZE (this); |
53 | return_trace (likely (c->check_struct (this) && |
54 | (valuesZ.sanitize (c, base, size)))); |
55 | } |
56 | |
57 | private: |
58 | inline float get_track_value () const |
59 | { |
60 | return track.to_float (); |
61 | } |
62 | |
63 | inline int get_value (const void *base, unsigned int index) const |
64 | { |
65 | return (base+valuesZ)[index]; |
66 | } |
67 | |
68 | protected: |
69 | Fixed track; /* Track value for this record. */ |
70 | NameID trackNameID; /* The 'name' table index for this track */ |
71 | OffsetTo<UnsizedArrayOf<FWORD> > |
72 | valuesZ; /* Offset from start of tracking table to |
73 | * per-size tracking values for this track. */ |
74 | |
75 | public: |
76 | DEFINE_SIZE_STATIC (8); |
77 | }; |
78 | |
79 | struct TrackData |
80 | { |
81 | inline bool sanitize (hb_sanitize_context_t *c, const void *base) const |
82 | { |
83 | TRACE_SANITIZE (this); |
84 | return_trace (c->check_struct (this) && |
85 | sizeTable.sanitize (c, base, nSizes) && |
86 | trackTable.sanitize (c, nTracks, base, nSizes)); |
87 | } |
88 | |
89 | inline float get_tracking (const void *base, float ptem) const |
90 | { |
91 | /* CoreText points are CSS pixels (96 per inch), |
92 | * NOT typographic points (72 per inch). |
93 | * |
94 | * https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Explained/Explained.html |
95 | */ |
96 | float csspx = ptem * 96.f / 72.f; |
97 | Fixed fixed_size; |
98 | fixed_size.set_float (csspx); |
99 | |
100 | /* XXX Clean this up. Make it work with nSizes==1 and 0. */ |
101 | |
102 | unsigned int sizes = nSizes; |
103 | |
104 | const TrackTableEntry *trackTableEntry = nullptr; |
105 | for (unsigned int i = 0; i < sizes; ++i) |
106 | // For now we only seek for track entries with zero tracking value |
107 | if (trackTable[i].get_track_value () == 0.f) |
108 | trackTableEntry = &trackTable[0]; |
109 | |
110 | // We couldn't match any, exit |
111 | if (!trackTableEntry) return 0.; |
112 | |
113 | /* TODO bfind() */ |
114 | unsigned int size_index; |
115 | UnsizedArrayOf<Fixed> size_table = base+sizeTable; |
116 | for (size_index = 0; size_index < sizes; ++size_index) |
117 | if (size_table[size_index] >= fixed_size) |
118 | break; |
119 | |
120 | // TODO(ebraminio): We don't attempt to extrapolate to larger or |
121 | // smaller values for now but we should do, per spec |
122 | if (size_index == sizes) |
123 | return trackTableEntry->get_value (base, sizes - 1); |
124 | if (size_index == 0 || size_table[size_index] == fixed_size) |
125 | return trackTableEntry->get_value (base, size_index); |
126 | |
127 | float s0 = size_table[size_index - 1].to_float (); |
128 | float s1 = size_table[size_index].to_float (); |
129 | float t = (csspx - s0) / (s1 - s0); |
130 | return (float) t * trackTableEntry->get_value (base, size_index) + |
131 | ((float) 1.0 - t) * trackTableEntry->get_value (base, size_index - 1); |
132 | } |
133 | |
134 | protected: |
135 | HBUINT16 nTracks; /* Number of separate tracks included in this table. */ |
136 | HBUINT16 nSizes; /* Number of point sizes included in this table. */ |
137 | LOffsetTo<UnsizedArrayOf<Fixed> > |
138 | sizeTable; /* Offset to array[nSizes] of size values. */ |
139 | UnsizedArrayOf<TrackTableEntry> |
140 | trackTable; /* Array[nTracks] of TrackTableEntry records. */ |
141 | |
142 | public: |
143 | DEFINE_SIZE_ARRAY (8, trackTable); |
144 | }; |
145 | |
146 | struct trak |
147 | { |
148 | static const hb_tag_t tableTag = HB_AAT_TAG_trak; |
149 | |
150 | inline bool sanitize (hb_sanitize_context_t *c) const |
151 | { |
152 | TRACE_SANITIZE (this); |
153 | |
154 | return_trace (unlikely (c->check_struct (this) && |
155 | horizData.sanitize (c, this, this) && |
156 | vertData.sanitize (c, this, this))); |
157 | } |
158 | |
159 | inline bool apply (hb_aat_apply_context_t *c) const |
160 | { |
161 | TRACE_APPLY (this); |
162 | |
163 | const float ptem = c->font->ptem; |
164 | if (unlikely (ptem <= 0.f)) |
165 | return_trace (false); |
166 | |
167 | hb_buffer_t *buffer = c->buffer; |
168 | if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) |
169 | { |
170 | const TrackData &trackData = this+horizData; |
171 | float tracking = trackData.get_tracking (this, ptem); |
172 | hb_position_t advance_to_add = c->font->em_scalef_x (tracking / 2); |
173 | foreach_grapheme (buffer, start, end) |
174 | { |
175 | buffer->pos[start].x_offset += advance_to_add; |
176 | buffer->pos[start].x_advance += advance_to_add; |
177 | buffer->pos[end].x_advance += advance_to_add; |
178 | } |
179 | } |
180 | else |
181 | { |
182 | const TrackData &trackData = this+vertData; |
183 | float tracking = trackData.get_tracking (this, ptem); |
184 | hb_position_t advance_to_add = c->font->em_scalef_y (tracking / 2); |
185 | foreach_grapheme (buffer, start, end) |
186 | { |
187 | buffer->pos[start].y_offset += advance_to_add; |
188 | buffer->pos[start].y_advance += advance_to_add; |
189 | buffer->pos[end].y_advance += advance_to_add; |
190 | } |
191 | } |
192 | |
193 | return_trace (true); |
194 | } |
195 | |
196 | protected: |
197 | FixedVersion<> version; /* Version of the tracking table--currently |
198 | * 0x00010000u for version 1.0. */ |
199 | HBUINT16 format; /* Format of the tracking table */ |
200 | OffsetTo<TrackData> horizData; /* TrackData for horizontal text */ |
201 | OffsetTo<TrackData> vertData; /* TrackData for vertical text */ |
202 | HBUINT16 reserved; /* Reserved. Set to 0. */ |
203 | |
204 | public: |
205 | DEFINE_SIZE_MIN (12); |
206 | }; |
207 | |
208 | } /* namespace AAT */ |
209 | |
210 | |
211 | #endif /* HB_AAT_LAYOUT_TRAK_TABLE_HH */ |
212 | |