2 * Copyright © 2018 Ebrahim Byagowi
3 * Copyright © 2018 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Google Author(s): Behdad Esfahbod
28 #ifndef HB_AAT_LAYOUT_TRAK_TABLE_HH
29 #define HB_AAT_LAYOUT_TRAK_TABLE_HH
31 #include "hb-aat-layout-common-private.hh"
32 #include "hb-open-type-private.hh"
34 #define HB_AAT_TAG_trak HB_TAG('t','r','a','k')
40 struct TrackTableEntry
42 inline bool sanitize (hb_sanitize_context_t *c, const void *base, unsigned int size) const
44 TRACE_SANITIZE (this);
45 return_trace (c->check_struct (this) && (values.sanitize (c, base, size)));
48 inline float get_track_value () const
50 return track.to_float ();
53 inline int get_value (const void *base, unsigned int index) const
55 return (base+values)[index];
59 Fixed track; /* Track value for this record. */
60 HBUINT16 trackNameID; /* The 'name' table index for this track */
61 OffsetTo<UnsizedArrayOf<FWORD> >
62 values; /* Offset from start of tracking table to
63 * per-size tracking values for this track. */
66 DEFINE_SIZE_STATIC (8);
71 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
73 TRACE_SANITIZE (this);
74 return_trace (c->check_struct (this) &&
75 sizeTable.sanitize (c, base, nSizes) &&
76 trackTable.sanitize (c, nTracks, base, nSizes));
79 inline float get_tracking (const void *base, float ptem) const
81 /* CoreText points are CSS pixels (96 per inch),
82 * NOT typographic points (72 per inch).
84 * https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Explained/Explained.html
86 float csspx = ptem * 96.f / 72.f;
88 fixed_size.set_float (csspx);
90 /* XXX Clean this up. Make it work with nSizes==1 and 0. */
92 unsigned int sizes = nSizes;
94 const TrackTableEntry *trackTableEntry = nullptr;
95 for (unsigned int i = 0; i < sizes; ++i)
96 // For now we only seek for track entries with zero tracking value
97 if (trackTable[i].get_track_value () == 0.)
98 trackTableEntry = &trackTable[0];
100 // We couldn't match any, exit
101 if (!trackTableEntry) return 0.;
104 unsigned int size_index;
105 UnsizedArrayOf<Fixed> size_table = base+sizeTable;
106 for (size_index = 0; size_index < sizes; ++size_index)
107 if (size_table[size_index] >= fixed_size)
110 // TODO(ebraminio): We don't attempt to extrapolate to larger or
111 // smaller values for now but we should do, per spec
112 if (size_index == sizes)
113 return trackTableEntry->get_value (base, sizes - 1);
114 if (size_index == 0 || size_table[size_index] == fixed_size)
115 return trackTableEntry->get_value (base, size_index);
117 float s0 = size_table[size_index - 1].to_float ();
118 float s1 = size_table[size_index].to_float ();
119 float t = (csspx - s0) / (s1 - s0);
120 return t * trackTableEntry->get_value (base, size_index) +
121 (1.0 - t) * trackTableEntry->get_value (base, size_index - 1);
125 HBUINT16 nTracks; /* Number of separate tracks included in this table. */
126 HBUINT16 nSizes; /* Number of point sizes included in this table. */
127 LOffsetTo<UnsizedArrayOf<Fixed> > /* Offset to array[nSizes] of size values. */
129 UnsizedArrayOf<TrackTableEntry>
130 trackTable; /* Array[nTracks] of TrackTableEntry records. */
133 DEFINE_SIZE_ARRAY (8, trackTable);
138 static const hb_tag_t tableTag = HB_AAT_TAG_trak;
140 inline bool sanitize (hb_sanitize_context_t *c) const
142 TRACE_SANITIZE (this);
144 return_trace (c->check_struct (this) &&
145 horizData.sanitize (c, this, this) &&
146 vertData.sanitize (c, this, this));
149 inline bool apply (hb_aat_apply_context_t *c) const
153 const float ptem = c->font->ptem;
155 return_trace (false);
157 hb_buffer_t *buffer = c->buffer;
158 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
160 const TrackData &trackData = this+horizData;
161 float tracking = trackData.get_tracking (this, ptem);
162 hb_position_t advance_to_add = c->font->em_scalef_x (tracking / 2);
163 foreach_grapheme (buffer, start, end)
165 /* TODO This is wrong. */
166 buffer->pos[start].x_advance += advance_to_add;
167 buffer->pos[end].x_advance += advance_to_add;
172 const TrackData &trackData = this+vertData;
173 float tracking = trackData.get_tracking (this, ptem);
174 hb_position_t advance_to_add = c->font->em_scalef_y (tracking / 2);
175 foreach_grapheme (buffer, start, end)
177 /* TODO This is wrong. */
178 buffer->pos[start].y_advance += advance_to_add;
179 buffer->pos[end].y_advance += advance_to_add;
187 FixedVersion<> version; /* Version of the tracking table--currently
188 * 0x00010000u for version 1.0. */
189 HBUINT16 format; /* Format of the tracking table */
190 OffsetTo<TrackData> horizData; /* TrackData for horizontal text */
191 OffsetTo<TrackData> vertData; /* TrackData for vertical text */
192 HBUINT16 reserved; /* Reserved. Set to 0. */
195 DEFINE_SIZE_MIN (12);
198 } /* namespace AAT */
201 #endif /* HB_AAT_LAYOUT_TRAK_TABLE_HH */