hb-aat-layout-trak-table.hh 7.0 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;

B
Behdad Esfahbod 已提交
49
  inline float get_track_value () const
E
Ebrahim Byagowi 已提交
50
  {
B
Behdad Esfahbod 已提交
51
    return track.to_float ();
E
Ebrahim Byagowi 已提交
52 53
  }

B
Behdad Esfahbod 已提交
54 55 56
  inline int get_value (const void *base,
			unsigned int index,
			unsigned int nSizes) const
E
Ebrahim Byagowi 已提交
57
  {
B
Behdad Esfahbod 已提交
58
    return hb_array_t<FWORD> ((base+valuesZ).arrayZ, nSizes)[index];
E
Ebrahim Byagowi 已提交
59 60
  }

B
Behdad Esfahbod 已提交
61 62 63
  public:
  inline bool sanitize (hb_sanitize_context_t *c, const void *base,
			unsigned int nSizes) const
64
  {
B
Behdad Esfahbod 已提交
65 66 67
    TRACE_SANITIZE (this);
    return_trace (likely (c->check_struct (this) &&
			  (valuesZ.sanitize (c, base, nSizes))));
68 69
  }

E
Ebrahim Byagowi 已提交
70
  protected:
B
Behdad Esfahbod 已提交
71
  Fixed		track;		/* Track value for this record. */
B
Behdad Esfahbod 已提交
72 73 74
  NameID	trackNameID;	/* The 'name' table index for this track.
				 * (a short word or phrase like "loose"
				 * or "very tight") */
75
  OffsetTo<UnsizedArrayOf<FWORD>, HBUINT16, false>
76
		valuesZ;	/* Offset from start of tracking table to
B
Behdad Esfahbod 已提交
77
				 * per-size tracking values for this track. */
E
Ebrahim Byagowi 已提交
78 79

  public:
80
  DEFINE_SIZE_STATIC (8);
E
Ebrahim Byagowi 已提交
81 82 83 84
};

struct TrackData
{
85 86 87 88 89 90 91 92 93 94 95
  inline float get_tracking (const void *base, float ptem) const
  {
    /* CoreText points are CSS pixels (96 per inch),
     * NOT typographic points (72 per inch).
     *
     * https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Explained/Explained.html
     */
    float csspx = ptem * 96.f / 72.f;
    Fixed fixed_size;
    fixed_size.set_float (csspx);

B
Behdad Esfahbod 已提交
96
    /* XXX Clean this up. Make it work with nSizes==1 and 0. */
B
Behdad Esfahbod 已提交
97

E
Ebrahim Byagowi 已提交
98
    const TrackTableEntry *trackTableEntry = nullptr;
B
Behdad Esfahbod 已提交
99 100
    unsigned int count = nTracks;
    for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
101 102 103 104 105 106
    {
      /* 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 */

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

B
Behdad Esfahbod 已提交
115 116
    unsigned int sizes = nSizes;

B
Behdad Esfahbod 已提交
117
    /* TODO bfind() */
118
    unsigned int size_index;
E
Ebrahim Byagowi 已提交
119
    UnsizedArrayOf<Fixed> size_table = base+sizeTable;
B
Behdad Esfahbod 已提交
120
    for (size_index = 0; size_index < sizes; size_index++)
E
Ebrahim Byagowi 已提交
121
      if (size_table[size_index] >= fixed_size)
122 123
        break;

E
Ebrahim Byagowi 已提交
124 125 126
    // TODO(ebraminio): We don't attempt to extrapolate to larger or
    // smaller values for now but we should do, per spec
    if (size_index == sizes)
B
Behdad Esfahbod 已提交
127
      return trackTableEntry->get_value (base, sizes - 1, sizes);
E
Ebrahim Byagowi 已提交
128
    if (size_index == 0 || size_table[size_index] == fixed_size)
B
Behdad Esfahbod 已提交
129
      return trackTableEntry->get_value (base, size_index, sizes);
130

E
Ebrahim Byagowi 已提交
131 132
    float s0 = size_table[size_index - 1].to_float ();
    float s1 = size_table[size_index].to_float ();
133
    float t = (csspx - s0) / (s1 - s0);
B
Behdad Esfahbod 已提交
134 135
    return (float) t * trackTableEntry->get_value (base, size_index, sizes) +
	   ((float) 1.0 - t) * trackTableEntry->get_value (base, size_index - 1, sizes);
B
Behdad Esfahbod 已提交
136 137 138 139 140 141 142 143
  }

  inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) &&
		  sizeTable.sanitize (c, base, nSizes) &&
		  trackTable.sanitize (c, nTracks, base, nSizes));
144 145
  }

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

  public:
156
  DEFINE_SIZE_ARRAY (8, trackTable);
E
Ebrahim Byagowi 已提交
157 158 159 160
};

struct trak
{
161
  static const hb_tag_t tableTag = HB_AAT_TAG_trak;
E
Ebrahim Byagowi 已提交
162 163 164 165

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
166

167 168 169
    return_trace (unlikely (c->check_struct (this) &&
			    horizData.sanitize (c, this, this) &&
			    vertData.sanitize (c, this, this)));
E
Ebrahim Byagowi 已提交
170 171
  }

172 173 174
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
175

176
    const float ptem = c->font->ptem;
177
    if (unlikely (ptem <= 0.f))
B
Behdad Esfahbod 已提交
178 179 180 181
      return_trace (false);

    hb_buffer_t *buffer = c->buffer;
    if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
182
    {
B
Behdad Esfahbod 已提交
183 184
      const TrackData &trackData = this+horizData;
      float tracking = trackData.get_tracking (this, ptem);
185 186
      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 已提交
187
      foreach_grapheme (buffer, start, end)
188
      {
B
Behdad Esfahbod 已提交
189
	buffer->pos[start].x_advance += advance_to_add;
190
	buffer->pos[start].x_offset += offset_to_add;
191
      }
B
Behdad Esfahbod 已提交
192 193 194 195 196
    }
    else
    {
      const TrackData &trackData = this+vertData;
      float tracking = trackData.get_tracking (this, ptem);
197 198
      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 已提交
199
      foreach_grapheme (buffer, start, end)
200
      {
B
Behdad Esfahbod 已提交
201
	buffer->pos[start].y_advance += advance_to_add;
202
	buffer->pos[start].y_offset += offset_to_add;
203 204
      }
    }
B
Behdad Esfahbod 已提交
205 206

    return_trace (true);
207 208
  }

E
Ebrahim Byagowi 已提交
209
  protected:
B
Behdad Esfahbod 已提交
210 211 212 213 214 215 216
  FixedVersion<>	version;	/* Version of the tracking table
					 * (0x00010000u for version 1.0). */
  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). */
217
  HBUINT16		reserved;	/* Reserved. Set to 0. */
E
Ebrahim Byagowi 已提交
218 219

  public:
B
Behdad Esfahbod 已提交
220
  DEFINE_SIZE_STATIC (12);
E
Ebrahim Byagowi 已提交
221 222 223 224 225 226
};

} /* namespace AAT */


#endif /* HB_AAT_LAYOUT_TRAK_TABLE_HH */