hb-aat-layout-trak-table.hh 6.8 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 31 32 33 34 35 36 37 38 39 40 41
 *
 *  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

#include "hb-aat-layout-common-private.hh"
#include "hb-open-type-private.hh"

#define HB_AAT_TAG_trak HB_TAG('t','r','a','k')


namespace AAT {


struct TrackTableEntry
{
E
Ebrahim Byagowi 已提交
42
  inline bool sanitize (hb_sanitize_context_t *c, const void *base, uint16_t size) const
E
Ebrahim Byagowi 已提交
43 44
  {
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
45 46 47 48 49 50
    return_trace (c->check_struct (this) && ((base+values).sanitize (c, size)));
  }

  inline Fixed get_track_value () const
  {
    return track;
E
Ebrahim Byagowi 已提交
51 52
  }

53 54 55 56 57
  inline float get_value (const void *base, unsigned int index) const
  {
    return (base+values)[index];
  }

E
Ebrahim Byagowi 已提交
58
  protected:
59 60
  Fixed			track;		/* Track value for this record. */
  HBUINT16		trackNameID;	/* The 'name' table index for this track */
61
  OffsetTo<UnsizedArrayOf<HBINT16> >
62 63
			values;		/* Offset from start of tracking table to
					 * per-size tracking values for this track. */
E
Ebrahim Byagowi 已提交
64 65

  public:
66
  DEFINE_SIZE_STATIC (8);
E
Ebrahim Byagowi 已提交
67 68 69 70
};

struct TrackData
{
E
Ebrahim Byagowi 已提交
71
  inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
E
Ebrahim Byagowi 已提交
72 73
  {
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    if (!(c->check_struct (this)))
      return_trace (false);

    uint16_t tracks = (uint16_t) nTracks;
    uint16_t sizes = (uint16_t) nSizes;

    // It should have at least one track
    if (tracks < 1) return_trace (false);

    // We can not do interpolation with less than two
    if (sizes < 2) return_trace (false);

    if (!((base+sizeTable).sanitize (c, sizes)))
      return_trace (false);

    for (uint16_t i = 0; i < tracks; ++i)
      if (!(trackTable[i].sanitize (c, base, sizes)))
        return_trace (false);

    return_trace (true);
E
Ebrahim Byagowi 已提交
94 95
  }

96 97 98 99 100 101 102 103 104 105 106
  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 已提交
107 108
    /* TODO Clean this up. */

E
Ebrahim Byagowi 已提交
109 110 111 112 113 114 115 116 117 118 119
    uint16_t tracks = (uint16_t) nTracks;
    uint16_t sizes = (uint16_t) nSizes;

    const TrackTableEntry *trackTableEntry = nullptr;
    for (unsigned int i = 0; i < sizes; ++i)
      // For now we only seek for track entries with zero tracking value
      if (trackTable[i].get_track_value () == 0)
        trackTableEntry = &trackTable[0];

    // We couldn't match any, exit
    if (!trackTableEntry) return 0.;
120

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

E
Ebrahim Byagowi 已提交
128 129 130 131 132 133
    // 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)
      return trackTableEntry->get_value (base, sizes - 1);
    if (size_index == 0 || size_table[size_index] == fixed_size)
      return trackTableEntry->get_value (base, size_index);
134

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

E
Ebrahim Byagowi 已提交
142
  protected:
143 144 145 146
  HBUINT16		nTracks;	/* Number of separate tracks included in this table. */
  HBUINT16		nSizes;		/* Number of point sizes included in this table. */
  LOffsetTo<UnsizedArrayOf<Fixed> >
			sizeTable;
147
  UnsizedArrayOf<TrackTableEntry>	trackTable;/* Array[nSizes] of size values. */
E
Ebrahim Byagowi 已提交
148 149

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

struct trak
{
  static const hb_tag_t tableTag = HB_AAT_TAG_trak;

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

    if (!(c->check_struct (this)))
      return_trace (false);

    if ((format != 0) || (reserved != 0))
      return_trace (false);

    if (horizData)
    {
      const TrackData &trackData = this+horizData;
      if (!trackData.sanitize (c, this))
        return_trace (false);
    }

    if (vertData)
    {
      const TrackData &trackData = this+horizData;
      if (!trackData.sanitize (c, this))
        return_trace (false);
    }

    return_trace (true);
E
Ebrahim Byagowi 已提交
182 183
  }

184 185 186
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
187

188
    const float ptem = c->font->ptem;
B
Behdad Esfahbod 已提交
189 190 191 192 193
    if (ptem <= 0.f)
      return_trace (false);

    hb_buffer_t *buffer = c->buffer;
    if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
194
    {
B
Behdad Esfahbod 已提交
195 196 197 198
      const TrackData &trackData = this+horizData;
      float tracking = trackData.get_tracking (this, ptem);
      hb_position_t advance_to_add = c->font->em_scalef_x (tracking / 2);
      foreach_grapheme (buffer, start, end)
199
      {
B
Behdad Esfahbod 已提交
200 201 202
	/* TODO This is wrong. */
	buffer->pos[start].x_advance += advance_to_add;
	buffer->pos[end].x_advance += advance_to_add;
203
      }
B
Behdad Esfahbod 已提交
204 205 206 207 208 209 210
    }
    else
    {
      const TrackData &trackData = this+vertData;
      float tracking = trackData.get_tracking (this, ptem);
      hb_position_t advance_to_add = c->font->em_scalef_y (tracking / 2);
      foreach_grapheme (buffer, start, end)
211
      {
B
Behdad Esfahbod 已提交
212 213 214
	/* TODO This is wrong. */
	buffer->pos[start].y_advance += advance_to_add;
	buffer->pos[end].y_advance += advance_to_add;
215 216
      }
    }
B
Behdad Esfahbod 已提交
217 218

    return_trace (true);
219 220
  }

E
Ebrahim Byagowi 已提交
221
  protected:
222
  FixedVersion<>	version;	/* Version of the tracking table--currently
E
Ebrahim Byagowi 已提交
223
					 * 0x00010000u for version 1.0. */
224
  HBUINT16		format; 	/* Format of the tracking table */
B
Behdad Esfahbod 已提交
225 226
  OffsetTo<TrackData>	horizData;	/* TrackData for horizontal text */
  OffsetTo<TrackData>	vertData;	/* TrackData for vertical text */
227
  HBUINT16		reserved;	/* Reserved. Set to 0. */
E
Ebrahim Byagowi 已提交
228 229 230 231 232 233 234 235 236

  public:
  DEFINE_SIZE_MIN (12);
};

} /* namespace AAT */


#endif /* HB_AAT_LAYOUT_TRAK_TABLE_HH */