hb-ot-metrics.cc 8.3 KB
Newer Older
1
/*
E
Ebrahim Byagowi 已提交
2
 * Copyright © 2018-2019  Ebrahim Byagowi
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 *  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.
 */

#include "hb.hh"

#include "hb-ot-var-mvar-table.hh"
28 29 30
#include "hb-ot-gasp-table.hh" // Just so we compile it; unused otherwise.
#include "hb-ot-os2-table.hh"
#include "hb-ot-post-table.hh"
31
#include "hb-ot-hhea-table.hh"
32
#include "hb-ot-metrics.hh"
33 34
#include "hb-ot-face.hh"

35 36 37 38 39 40 41 42 43 44 45 46 47

static float
_fix_ascender_descender (float value, hb_ot_metrics_t metrics_tag)
{
  if (metrics_tag == HB_OT_METRICS_HORIZONTAL_ASCENDER ||
      metrics_tag == HB_OT_METRICS_VERTICAL_ASCENDER)
    return fabs ((double) value);
  if (metrics_tag == HB_OT_METRICS_HORIZONTAL_DESCENDER ||
      metrics_tag == HB_OT_METRICS_VERTICAL_DESCENDER)
    return -fabs ((double) value);
  return value;
}

E
Ebrahim Byagowi 已提交
48 49
/* The common part of _get_position logic needed on hb-ot-font and here
   to be able to have slim builds without the not always needed parts */
50
bool
51 52 53
_hb_ot_metrics_get_position_common (hb_font_t       *font,
				    hb_ot_metrics_t  metrics_tag,
				    hb_position_t   *position     /* OUT.  May be NULL. */)
54 55
{
  hb_face_t *face = font->face;
56
  switch ((unsigned) metrics_tag)
57 58
  {
#ifndef HB_NO_VAR
59
#define GET_VAR face->table.MVAR->get_var (metrics_tag, font->coords, font->num_coords)
60
#else
61
#define GET_VAR .0f
62 63 64
#endif
#define GET_METRIC_X(TABLE, ATTR) \
  (face->table.TABLE->has_data () && \
65 66
    (position && (*position = font->em_scalef_x (_fix_ascender_descender ( \
      face->table.TABLE->ATTR + GET_VAR, metrics_tag))), true))
67 68
#define GET_METRIC_Y(TABLE, ATTR) \
  (face->table.TABLE->has_data () && \
69 70
    (position && (*position = font->em_scalef_y (_fix_ascender_descender ( \
      face->table.TABLE->ATTR + GET_VAR, metrics_tag))), true))
71
  case HB_OT_METRICS_HORIZONTAL_ASCENDER:
72 73
    return (face->table.OS2->use_typo_metrics () && GET_METRIC_Y (OS2, sTypoAscender)) ||
	   GET_METRIC_Y (hhea, ascender);
74
  case HB_OT_METRICS_HORIZONTAL_DESCENDER:
75 76
    return (face->table.OS2->use_typo_metrics () && GET_METRIC_Y (OS2, sTypoDescender)) ||
	   GET_METRIC_Y (hhea, descender);
77
  case HB_OT_METRICS_HORIZONTAL_LINE_GAP:
78 79
    return (face->table.OS2->use_typo_metrics () && GET_METRIC_Y (OS2, sTypoLineGap)) ||
	   GET_METRIC_Y (hhea, lineGap);
80 81 82 83 84 85 86 87 88 89 90 91
  case HB_OT_METRICS_VERTICAL_ASCENDER:  return GET_METRIC_X (vhea, ascender);
  case HB_OT_METRICS_VERTICAL_DESCENDER: return GET_METRIC_X (vhea, descender);
  case HB_OT_METRICS_VERTICAL_LINE_GAP:  return GET_METRIC_X (vhea, lineGap);
#undef GET_METRIC_Y
#undef GET_METRIC_X
#undef GET_VAR
  default:                               assert (0); return false;
  }
}

#ifndef HB_NO_METRICS

92 93 94 95 96 97
#if 0
static bool
_get_gasp (hb_face_t *face, float *result, hb_ot_metrics_t metrics_tag)
{
  const OT::GaspRange& range = face->table.gasp->get_gasp_range (metrics_tag - HB_TAG ('g','s','p','0'));
  if (&range == &Null (OT::GaspRange)) return false;
98
  if (result) *result = range.rangeMaxPPEM + font->face->table.MVAR->get_var (metrics_tag, font->coords, font->num_coords);
99 100 101 102
  return true;
}
#endif

103 104
/**
 * hb_ot_metrics_get_position:
105 106 107
 * @font: a #hb_font_t object.
 * @metrics_tag: tag of metrics value you like to fetch.
 * @position: (out) (optional): result of metrics value from the font.
108
 *
109
 * It fetches metrics value corresponding to a given tag from a font.
110
 *
111
 * Returns: Whether found the requested metrics in the font.
112 113 114 115 116 117 118
 * Since: REPLACEME
 **/
hb_bool_t
hb_ot_metrics_get_position (hb_font_t       *font,
			    hb_ot_metrics_t  metrics_tag,
			    hb_position_t   *position     /* OUT.  May be NULL. */)
{
119
  hb_face_t *face = font->face;
120
  switch ((unsigned) metrics_tag)
121
  {
122 123 124 125 126
  case HB_OT_METRICS_HORIZONTAL_ASCENDER:
  case HB_OT_METRICS_HORIZONTAL_DESCENDER:
  case HB_OT_METRICS_HORIZONTAL_LINE_GAP:
  case HB_OT_METRICS_VERTICAL_ASCENDER:
  case HB_OT_METRICS_VERTICAL_DESCENDER:
127
  case HB_OT_METRICS_VERTICAL_LINE_GAP:           return _hb_ot_metrics_get_position_common (font, metrics_tag, position);
128
#ifndef HB_NO_VAR
129
#define GET_VAR hb_ot_metrics_get_variation (font, metrics_tag)
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
#else
#define GET_VAR 0
#endif
#define GET_METRIC_X(TABLE, ATTR) \
  (face->table.TABLE->has_data () && \
    (position && (*position = font->em_scalef_x (face->table.TABLE->ATTR + GET_VAR)), true))
#define GET_METRIC_Y(TABLE, ATTR) \
  (face->table.TABLE->has_data () && \
    (position && (*position = font->em_scalef_y (face->table.TABLE->ATTR + GET_VAR)), true))
  case HB_OT_METRICS_HORIZONTAL_CLIPPING_ASCENT:  return GET_METRIC_Y (OS2, usWinAscent);
  case HB_OT_METRICS_HORIZONTAL_CLIPPING_DESCENT: return GET_METRIC_Y (OS2, usWinDescent);
  case HB_OT_METRICS_HORIZONTAL_CARET_RISE:       return GET_METRIC_Y (hhea, caretSlopeRise);
  case HB_OT_METRICS_HORIZONTAL_CARET_RUN:        return GET_METRIC_X (hhea, caretSlopeRun);
  case HB_OT_METRICS_HORIZONTAL_CARET_OFFSET:     return GET_METRIC_X (hhea, caretOffset);
  case HB_OT_METRICS_VERTICAL_CARET_RISE:         return GET_METRIC_X (vhea, caretSlopeRise);
  case HB_OT_METRICS_VERTICAL_CARET_RUN:          return GET_METRIC_Y (vhea, caretSlopeRun);
  case HB_OT_METRICS_VERTICAL_CARET_OFFSET:       return GET_METRIC_Y (vhea, caretOffset);
  case HB_OT_METRICS_X_HEIGHT:                    return GET_METRIC_Y (OS2->v2 (), sxHeight);
  case HB_OT_METRICS_CAP_HEIGHT:                  return GET_METRIC_Y (OS2->v2 (), sCapHeight);
  case HB_OT_METRICS_SUBSCRIPT_EM_X_SIZE:         return GET_METRIC_X (OS2, ySubscriptXSize);
  case HB_OT_METRICS_SUBSCRIPT_EM_Y_SIZE:         return GET_METRIC_Y (OS2, ySubscriptYSize);
  case HB_OT_METRICS_SUBSCRIPT_EM_X_OFFSET:       return GET_METRIC_X (OS2, ySubscriptXOffset);
  case HB_OT_METRICS_SUBSCRIPT_EM_Y_OFFSET:       return GET_METRIC_Y (OS2, ySubscriptYOffset);
  case HB_OT_METRICS_SUPERSCRIPT_EM_X_SIZE:       return GET_METRIC_X (OS2, ySuperscriptXSize);
  case HB_OT_METRICS_SUPERSCRIPT_EM_Y_SIZE:       return GET_METRIC_Y (OS2, ySuperscriptYSize);
  case HB_OT_METRICS_SUPERSCRIPT_EM_X_OFFSET:     return GET_METRIC_X (OS2, ySuperscriptXOffset);
  case HB_OT_METRICS_SUPERSCRIPT_EM_Y_OFFSET:     return GET_METRIC_Y (OS2, ySuperscriptYOffset);
  case HB_OT_METRICS_STRIKEOUT_SIZE:              return GET_METRIC_Y (OS2, yStrikeoutSize);
  case HB_OT_METRICS_STRIKEOUT_OFFSET:            return GET_METRIC_Y (OS2, yStrikeoutPosition);
  case HB_OT_METRICS_UNDERLINE_SIZE:              return GET_METRIC_Y (post->table, underlineThickness);
  case HB_OT_METRICS_UNDERLINE_OFFSET:            return GET_METRIC_Y (post->table, underlinePosition);
#undef GET_METRIC_Y
#undef GET_METRIC_X
#undef GET_VAR
  default:                                        return false;
165 166 167
  }
}

168
#ifndef HB_NO_VAR
169 170
/**
 * hb_ot_metrics_get_variation:
171
 * @font:
172 173 174 175 176 177 178
 * @metrics_tag:
 *
 * Returns:
 *
 * Since: REPLACEME
 **/
float
179
hb_ot_metrics_get_variation (hb_font_t *font, hb_ot_metrics_t metrics_tag)
180
{
181
  return font->face->table.MVAR->get_var (metrics_tag, font->coords, font->num_coords);
182 183 184 185 186 187 188 189 190 191 192 193 194 195
}

/**
 * hb_ot_metrics_get_x_variation:
 * @font:
 * @metrics_tag:
 *
 * Returns:
 *
 * Since: REPLACEME
 **/
hb_position_t
hb_ot_metrics_get_x_variation (hb_font_t *font, hb_ot_metrics_t metrics_tag)
{
196
  return font->em_scalef_x (hb_ot_metrics_get_variation (font, metrics_tag));
197 198 199 200 201 202 203 204 205 206 207 208 209 210
}

/**
 * hb_ot_metrics_get_y_variation:
 * @font:
 * @metrics_tag:
 *
 * Returns:
 *
 * Since: REPLACEME
 **/
hb_position_t
hb_ot_metrics_get_y_variation (hb_font_t *font, hb_ot_metrics_t metrics_tag)
{
211
  return font->em_scalef_y (hb_ot_metrics_get_variation (font, metrics_tag));
212
}
213 214 215
#endif

#endif