hb-aat-layout-trak-table.hh 6.9 KB
Newer Older
E
Ebrahim Byagowi 已提交
1 2
/*
 * Copyright © 2018  Ebrahim Byagowi
3
 * Copyright © 2018  Google, Inc.
E
Ebrahim Byagowi 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Google Author(s): Behdad Esfahbod
 */

#ifndef HB_AAT_LAYOUT_TRAK_TABLE_HH
#define HB_AAT_LAYOUT_TRAK_TABLE_HH

31 32 33
#include "hb-aat-layout-common.hh"
#include "hb-ot-layout.hh"
#include "hb-open-type.hh"
E
Ebrahim Byagowi 已提交
34

35 36 37 38
/*
 * trak -- Tracking
 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6trak.html
 */
39
#define HB_AAT_TAG_trak HB_TAG('t','r','a','k')
E
Ebrahim Byagowi 已提交
40 41 42 43 44 45 46


namespace AAT {


struct TrackTableEntry
{
47 48
  friend struct TrackData;

49
  float get_track_value () const { return track.to_float (); }
E
Ebrahim Byagowi 已提交
50

51 52
  int get_value (const void *base, unsigned int index,
		 unsigned int table_size) const
53
  { return (base+valuesZ).as_array (table_size)[index]; }
E
Ebrahim Byagowi 已提交
54

B
Behdad Esfahbod 已提交
55
  public:
56 57
  bool sanitize (hb_sanitize_context_t *c, const void *base,
		 unsigned int table_size) const
58
  {
B
Behdad Esfahbod 已提交
59 60
    TRACE_SANITIZE (this);
    return_trace (likely (c->check_struct (this) &&
61
			  (valuesZ.sanitize (c, base, table_size))));
62 63
  }

E
Ebrahim Byagowi 已提交
64
  protected:
B
Behdad Esfahbod 已提交
65
  Fixed		track;		/* Track value for this record. */
B
Behdad Esfahbod 已提交
66 67 68
  NameID	trackNameID;	/* The 'name' table index for this track.
				 * (a short word or phrase like "loose"
				 * or "very tight") */
69
  NNOffsetTo<UnsizedArrayOf<FWORD>>
70
		valuesZ;	/* Offset from start of tracking table to
B
Behdad Esfahbod 已提交
71
				 * per-size tracking values for this track. */
E
Ebrahim Byagowi 已提交
72 73

  public:
74
  DEFINE_SIZE_STATIC (8);
E
Ebrahim Byagowi 已提交
75 76 77 78
};

struct TrackData
{
79 80 81 82
  float interpolate_at (unsigned int idx,
			float target_size,
			const TrackTableEntry &trackTableEntry,
			const void *base) const
B
Behdad Esfahbod 已提交
83 84
  {
    unsigned int sizes = nSizes;
85
    hb_array_t<const Fixed> size_table ((base+sizeTable).arrayZ, sizes);
B
Behdad Esfahbod 已提交
86 87 88 89

    float s0 = size_table[idx].to_float ();
    float s1 = size_table[idx + 1].to_float ();
    float t = unlikely (s0 == s1) ? 0.f : (target_size - s0) / (s1 - s0);
B
Behdad Esfahbod 已提交
90 91
    return t * trackTableEntry.get_value (base, idx + 1, sizes) +
	   (1.f - t) * trackTableEntry.get_value (base, idx, sizes);
B
Behdad Esfahbod 已提交
92 93
  }

94
  int get_tracking (const void *base, float ptem) const
95
  {
B
Behdad Esfahbod 已提交
96 97 98
    /*
     * Choose track.
     */
E
Ebrahim Byagowi 已提交
99
    const TrackTableEntry *trackTableEntry = nullptr;
B
Behdad Esfahbod 已提交
100 101
    unsigned int count = nTracks;
    for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
102 103 104 105 106 107
    {
      /* Note: Seems like the track entries are sorted by values.  But the
       * spec doesn't explicitly say that.  It just mentions it in the example. */

      /* For now we only seek for track entries with zero tracking value */

108
      if (trackTable[i].get_track_value () == 0.f)
B
Behdad Esfahbod 已提交
109
      {
B
Behdad Esfahbod 已提交
110
	trackTableEntry = &trackTable[i];
B
Behdad Esfahbod 已提交
111 112 113
	break;
      }
    }
E
Ebrahim Byagowi 已提交
114
    if (!trackTableEntry) return 0.;
115

B
Behdad Esfahbod 已提交
116 117 118
    /*
     * Choose size.
     */
B
Behdad Esfahbod 已提交
119
    unsigned int sizes = nSizes;
B
Behdad Esfahbod 已提交
120 121 122
    if (!sizes) return 0.;
    if (sizes == 1) return trackTableEntry->get_value (base, 0, sizes);

123
    hb_array_t<const Fixed> size_table ((base+sizeTable).arrayZ, sizes);
B
Behdad Esfahbod 已提交
124
    unsigned int size_index;
125
    for (size_index = 0; size_index < sizes - 1; size_index++)
126
      if (size_table[size_index].to_float () >= ptem)
127 128
        break;

129 130
    return roundf (interpolate_at (size_index ? size_index - 1 : 0, ptem,
				   *trackTableEntry, base));
B
Behdad Esfahbod 已提交
131 132
  }

133
  bool sanitize (hb_sanitize_context_t *c, const void *base) const
B
Behdad Esfahbod 已提交
134 135
  {
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
136 137 138
    return_trace (likely (c->check_struct (this) &&
			  sizeTable.sanitize (c, base, nSizes) &&
			  trackTable.sanitize (c, nTracks, base, nSizes)));
139 140
  }

E
Ebrahim Byagowi 已提交
141
  protected:
E
Ebrahim Byagowi 已提交
142 143
  HBUINT16	nTracks;	/* Number of separate tracks included in this table. */
  HBUINT16	nSizes;		/* Number of point sizes included in this table. */
144
  LOffsetTo<UnsizedArrayOf<Fixed>, false>
B
Behdad Esfahbod 已提交
145 146
		sizeTable;	/* Offset from start of the tracking table to
				 * Array[nSizes] of size values.. */
B
Behdad Esfahbod 已提交
147
  UnsizedArrayOf<TrackTableEntry>
E
Ebrahim Byagowi 已提交
148
		trackTable;	/* Array[nTracks] of TrackTableEntry records. */
E
Ebrahim Byagowi 已提交
149 150

  public:
151
  DEFINE_SIZE_ARRAY (8, trackTable);
E
Ebrahim Byagowi 已提交
152 153 154 155
};

struct trak
{
156
  static constexpr hb_tag_t tableTag = HB_AAT_TAG_trak;
E
Ebrahim Byagowi 已提交
157

158
  bool has_data () const { return version.to_int (); }
B
Behdad Esfahbod 已提交
159

160
  bool apply (hb_aat_apply_context_t *c) const
161 162
  {
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
163

164 165
    hb_mask_t trak_mask = c->plan->trak_mask;

166
    const float ptem = c->font->ptem;
167
    if (unlikely (ptem <= 0.f))
B
Behdad Esfahbod 已提交
168 169 170 171
      return_trace (false);

    hb_buffer_t *buffer = c->buffer;
    if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
172
    {
B
Behdad Esfahbod 已提交
173
      const TrackData &trackData = this+horizData;
B
Behdad Esfahbod 已提交
174
      int tracking = trackData.get_tracking (this, ptem);
175 176
      hb_position_t offset_to_add = c->font->em_scalef_x (tracking / 2);
      hb_position_t advance_to_add = c->font->em_scalef_x (tracking);
B
Behdad Esfahbod 已提交
177
      foreach_grapheme (buffer, start, end)
178
      {
179
        if (!(buffer->info[start].mask & trak_mask)) continue;
B
Behdad Esfahbod 已提交
180
	buffer->pos[start].x_advance += advance_to_add;
181
	buffer->pos[start].x_offset += offset_to_add;
182
      }
B
Behdad Esfahbod 已提交
183 184 185 186
    }
    else
    {
      const TrackData &trackData = this+vertData;
B
Behdad Esfahbod 已提交
187
      int tracking = trackData.get_tracking (this, ptem);
188 189
      hb_position_t offset_to_add = c->font->em_scalef_y (tracking / 2);
      hb_position_t advance_to_add = c->font->em_scalef_y (tracking);
B
Behdad Esfahbod 已提交
190
      foreach_grapheme (buffer, start, end)
191
      {
192
        if (!(buffer->info[start].mask & trak_mask)) continue;
B
Behdad Esfahbod 已提交
193
	buffer->pos[start].y_advance += advance_to_add;
194
	buffer->pos[start].y_offset += offset_to_add;
195 196
      }
    }
B
Behdad Esfahbod 已提交
197 198

    return_trace (true);
199 200
  }

201
  bool sanitize (hb_sanitize_context_t *c) const
E
Ebrahim Byagowi 已提交
202 203 204 205 206 207 208 209 210
  {
    TRACE_SANITIZE (this);

    return_trace (likely (c->check_struct (this) &&
			  version.major == 1 &&
			  horizData.sanitize (c, this, this) &&
			  vertData.sanitize (c, this, this)));
  }

E
Ebrahim Byagowi 已提交
211
  protected:
E
Ebrahim Byagowi 已提交
212
  FixedVersion<>version;	/* Version of the tracking table
B
Behdad Esfahbod 已提交
213
					 * (0x00010000u for version 1.0). */
E
Ebrahim Byagowi 已提交
214 215 216 217 218 219 220 221
  HBUINT16	format; 	/* Format of the tracking table (set to 0). */
  OffsetTo<TrackData>
		horizData;	/* Offset from start of tracking table to TrackData
				 * for horizontal text (or 0 if none). */
  OffsetTo<TrackData>
		vertData;	/* Offset from start of tracking table to TrackData
				 * for vertical text (or 0 if none). */
  HBUINT16	reserved;	/* Reserved. Set to 0. */
E
Ebrahim Byagowi 已提交
222 223

  public:
B
Behdad Esfahbod 已提交
224
  DEFINE_SIZE_STATIC (12);
E
Ebrahim Byagowi 已提交
225 226 227 228 229 230
};

} /* namespace AAT */


#endif /* HB_AAT_LAYOUT_TRAK_TABLE_HH */