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
42namespace AAT {
43
44
45struct TrackTableEntry
46{
47 friend struct TrackData;
48
49 float get_track_value () const { return track.to_float (); }
50
51 int get_value (const void *base, unsigned int index,
52 unsigned int table_size) const
53 { return (base+valuesZ).as_array (table_size)[index]; }
54
55 public:
56 bool sanitize (hb_sanitize_context_t *c, const void *base,
57 unsigned int table_size) const
58 {
59 TRACE_SANITIZE (this);
60 return_trace (likely (c->check_struct (this) &&
61 (valuesZ.sanitize (c, base, table_size))));
62 }
63
64 protected:
65 Fixed track; /* Track value for this record. */
66 NameID trackNameID; /* The 'name' table index for this track.
67 * (a short word or phrase like "loose"
68 * or "very tight") */
69 NNOffsetTo<UnsizedArrayOf<FWORD> >
70 valuesZ; /* Offset from start of tracking table to
71 * per-size tracking values for this track. */
72
73 public:
74 DEFINE_SIZE_STATIC (8);
75};
76
77struct TrackData
78{
79 float interpolate_at (unsigned int idx,
80 float target_size,
81 const TrackTableEntry &trackTableEntry,
82 const void *base) const
83 {
84 unsigned int sizes = nSizes;
85 hb_array_t<const Fixed> size_table ((base+sizeTable).arrayZ, sizes);
86
87 float s0 = size_table[idx].to_float ();
88 float s1 = size_table[idx + 1].to_float ();
89 float t = unlikely (s0 == s1) ? 0.f : (target_size - s0) / (s1 - s0);
90 return t * trackTableEntry.get_value (base, idx + 1, sizes) +
91 (1.f - t) * trackTableEntry.get_value (base, idx, sizes);
92 }
93
94 int get_tracking (const void *base, float ptem) const
95 {
96 /* CoreText points are CSS pixels (96 per inch),
97 * NOT typographic points (72 per inch).
98 *
99 * https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Explained/Explained.html
100 */
101 float csspx = ptem * 96.f / 72.f;
102
103 /*
104 * Choose track.
105 */
106 const TrackTableEntry *trackTableEntry = nullptr;
107 unsigned int count = nTracks;
108 for (unsigned int i = 0; i < count; i++)
109 {
110 /* Note: Seems like the track entries are sorted by values. But the
111 * spec doesn't explicitly say that. It just mentions it in the example. */
112
113 /* For now we only seek for track entries with zero tracking value */
114
115 if (trackTable[i].get_track_value () == 0.f)
116 {
117 trackTableEntry = &trackTable[i];
118 break;
119 }
120 }
121 if (!trackTableEntry) return 0.;
122
123 /*
124 * Choose size.
125 */
126 unsigned int sizes = nSizes;
127 if (!sizes) return 0.;
128 if (sizes == 1) return trackTableEntry->get_value (base, 0, sizes);
129
130 hb_array_t<const Fixed> size_table ((base+sizeTable).arrayZ, sizes);
131 unsigned int size_index;
132 for (size_index = 0; size_index < sizes - 1; size_index++)
133 if (size_table[size_index].to_float () >= csspx)
134 break;
135
136 return round (interpolate_at (size_index ? size_index - 1 : 0, csspx,
137 *trackTableEntry, base));
138 }
139
140 bool sanitize (hb_sanitize_context_t *c, const void *base) const
141 {
142 TRACE_SANITIZE (this);
143 return_trace (likely (c->check_struct (this) &&
144 sizeTable.sanitize (c, base, nSizes) &&
145 trackTable.sanitize (c, nTracks, base, nSizes)));
146 }
147
148 protected:
149 HBUINT16 nTracks; /* Number of separate tracks included in this table. */
150 HBUINT16 nSizes; /* Number of point sizes included in this table. */
151 LOffsetTo<UnsizedArrayOf<Fixed>, false>
152 sizeTable; /* Offset from start of the tracking table to
153 * Array[nSizes] of size values.. */
154 UnsizedArrayOf<TrackTableEntry>
155 trackTable; /* Array[nTracks] of TrackTableEntry records. */
156
157 public:
158 DEFINE_SIZE_ARRAY (8, trackTable);
159};
160
161struct trak
162{
163 static constexpr hb_tag_t tableTag = HB_AAT_TAG_trak;
164
165 bool has_data () const { return version.to_int (); }
166
167 bool apply (hb_aat_apply_context_t *c) const
168 {
169 TRACE_APPLY (this);
170
171 hb_mask_t trak_mask = c->plan->trak_mask;
172
173 const float ptem = c->font->ptem;
174 if (unlikely (ptem <= 0.f))
175 return_trace (false);
176
177 hb_buffer_t *buffer = c->buffer;
178 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
179 {
180 const TrackData &trackData = this+horizData;
181 int tracking = trackData.get_tracking (this, ptem);
182 hb_position_t offset_to_add = c->font->em_scalef_x (tracking / 2);
183 hb_position_t advance_to_add = c->font->em_scalef_x (tracking);
184 foreach_grapheme (buffer, start, end)
185 {
186 if (!(buffer->info[start].mask & trak_mask)) continue;
187 buffer->pos[start].x_advance += advance_to_add;
188 buffer->pos[start].x_offset += offset_to_add;
189 }
190 }
191 else
192 {
193 const TrackData &trackData = this+vertData;
194 int tracking = trackData.get_tracking (this, ptem);
195 hb_position_t offset_to_add = c->font->em_scalef_y (tracking / 2);
196 hb_position_t advance_to_add = c->font->em_scalef_y (tracking);
197 foreach_grapheme (buffer, start, end)
198 {
199 if (!(buffer->info[start].mask & trak_mask)) continue;
200 buffer->pos[start].y_advance += advance_to_add;
201 buffer->pos[start].y_offset += offset_to_add;
202 }
203 }
204
205 return_trace (true);
206 }
207
208 bool sanitize (hb_sanitize_context_t *c) const
209 {
210 TRACE_SANITIZE (this);
211
212 return_trace (likely (c->check_struct (this) &&
213 version.major == 1 &&
214 horizData.sanitize (c, this, this) &&
215 vertData.sanitize (c, this, this)));
216 }
217
218 protected:
219 FixedVersion<>version; /* Version of the tracking table
220 * (0x00010000u for version 1.0). */
221 HBUINT16 format; /* Format of the tracking table (set to 0). */
222 OffsetTo<TrackData>
223 horizData; /* Offset from start of tracking table to TrackData
224 * for horizontal text (or 0 if none). */
225 OffsetTo<TrackData>
226 vertData; /* Offset from start of tracking table to TrackData
227 * for vertical text (or 0 if none). */
228 HBUINT16 reserved; /* Reserved. Set to 0. */
229
230 public:
231 DEFINE_SIZE_STATIC (12);
232};
233
234} /* namespace AAT */
235
236
237#endif /* HB_AAT_LAYOUT_TRAK_TABLE_HH */
238