Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / src / hb-aat-layout-trak-table.hh
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-private.hh"
32 #include "hb-open-type-private.hh"
33
34 #define HB_AAT_TAG_trak HB_TAG('t','r','a','k')
35
36
37 namespace AAT {
38
39
40 struct TrackTableEntry
41 {
42   inline bool sanitize (hb_sanitize_context_t *c, const void *base, unsigned int size) const
43   {
44     TRACE_SANITIZE (this);
45     return_trace (c->check_struct (this) && (values.sanitize (c, base, size)));
46   }
47
48   inline float get_track_value () const
49   {
50     return track.to_float ();
51   }
52
53   inline int get_value (const void *base, unsigned int index) const
54   {
55     return (base+values)[index];
56   }
57
58   protected:
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. */
64
65   public:
66   DEFINE_SIZE_STATIC (8);
67 };
68
69 struct TrackData
70 {
71   inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
72   {
73     TRACE_SANITIZE (this);
74     return_trace (c->check_struct (this) &&
75                   sizeTable.sanitize (c, base, nSizes) &&
76                   trackTable.sanitize (c, nTracks, base, nSizes));
77   }
78
79   inline float get_tracking (const void *base, float ptem) const
80   {
81     /* CoreText points are CSS pixels (96 per inch),
82      * NOT typographic points (72 per inch).
83      *
84      * https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Explained/Explained.html
85      */
86     float csspx = ptem * 96.f / 72.f;
87     Fixed fixed_size;
88     fixed_size.set_float (csspx);
89
90     /* XXX Clean this up. Make it work with nSizes==1 and 0. */
91
92     unsigned int sizes = nSizes;
93
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];
99
100     // We couldn't match any, exit
101     if (!trackTableEntry) return 0.;
102
103     /* TODO bfind() */
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)
108         break;
109
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);
116
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);
122   }
123
124   protected:
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. */
128                         sizeTable;
129   UnsizedArrayOf<TrackTableEntry>
130                         trackTable;     /* Array[nTracks] of TrackTableEntry records. */
131
132   public:
133   DEFINE_SIZE_ARRAY (8, trackTable);
134 };
135
136 struct trak
137 {
138   static const hb_tag_t tableTag = HB_AAT_TAG_trak;
139
140   inline bool sanitize (hb_sanitize_context_t *c) const
141   {
142     TRACE_SANITIZE (this);
143
144     return_trace (c->check_struct (this) &&
145                   horizData.sanitize (c, this, this) &&
146                   vertData.sanitize (c, this, this));
147   }
148
149   inline bool apply (hb_aat_apply_context_t *c) const
150   {
151     TRACE_APPLY (this);
152
153     const float ptem = c->font->ptem;
154     if (ptem <= 0.f)
155       return_trace (false);
156
157     hb_buffer_t *buffer = c->buffer;
158     if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
159     {
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)
164       {
165         /* TODO This is wrong. */
166         buffer->pos[start].x_advance += advance_to_add;
167         buffer->pos[end].x_advance += advance_to_add;
168       }
169     }
170     else
171     {
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)
176       {
177         /* TODO This is wrong. */
178         buffer->pos[start].y_advance += advance_to_add;
179         buffer->pos[end].y_advance += advance_to_add;
180       }
181     }
182
183     return_trace (true);
184   }
185
186   protected:
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. */
193
194   public:
195   DEFINE_SIZE_MIN (12);
196 };
197
198 } /* namespace AAT */
199
200
201 #endif /* HB_AAT_LAYOUT_TRAK_TABLE_HH */