QtBase  v6.3.1
hb-ot-metrics.cc
Go to the documentation of this file.
1 /*
2  * Copyright © 2018-2019 Ebrahim Byagowi
3  *
4  * This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  */
24 
25 #include "hb.hh"
26 
27 #include "hb-ot-var-mvar-table.hh"
28 #include "hb-ot-gasp-table.hh" // Just so we compile it; unused otherwise.
29 #include "hb-ot-os2-table.hh"
30 #include "hb-ot-post-table.hh"
31 #include "hb-ot-hhea-table.hh"
32 #include "hb-ot-metrics.hh"
33 #include "hb-ot-face.hh"
34 
35 
45 static float
46 _fix_ascender_descender (float value, hb_ot_metrics_tag_t metrics_tag)
47 {
48  if (metrics_tag == HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER ||
50  return fabs ((double) value);
51  if (metrics_tag == HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER ||
53  return -fabs ((double) value);
54  return value;
55 }
56 
57 /* The common part of _get_position logic needed on hb-ot-font and here
58  to be able to have slim builds without the not always needed parts */
59 bool
61  hb_ot_metrics_tag_t metrics_tag,
62  hb_position_t *position /* OUT. May be NULL. */)
63 {
64  hb_face_t *face = font->face;
65  switch ((unsigned) metrics_tag)
66  {
67 #ifndef HB_NO_VAR
68 #define GET_VAR face->table.MVAR->get_var (metrics_tag, font->coords, font->num_coords)
69 #else
70 #define GET_VAR .0f
71 #endif
72 #define GET_METRIC_X(TABLE, ATTR) \
73  (face->table.TABLE->has_data () && \
74  (position && (*position = font->em_scalef_x (_fix_ascender_descender ( \
75  face->table.TABLE->ATTR + GET_VAR, metrics_tag))), true))
76 #define GET_METRIC_Y(TABLE, ATTR) \
77  (face->table.TABLE->has_data () && \
78  (position && (*position = font->em_scalef_y (_fix_ascender_descender ( \
79  face->table.TABLE->ATTR + GET_VAR, metrics_tag))), true))
80 
82  return (face->table.OS2->use_typo_metrics () && GET_METRIC_Y (OS2, sTypoAscender)) ||
83  GET_METRIC_Y (hhea, ascender);
85  return (face->table.OS2->use_typo_metrics () && GET_METRIC_Y (OS2, sTypoDescender)) ||
86  GET_METRIC_Y (hhea, descender);
88  return (face->table.OS2->use_typo_metrics () && GET_METRIC_Y (OS2, sTypoLineGap)) ||
89  GET_METRIC_Y (hhea, lineGap);
90 
91 #ifndef HB_NO_VERTICAL
92  case HB_OT_METRICS_TAG_VERTICAL_ASCENDER: return GET_METRIC_X (vhea, ascender);
93  case HB_OT_METRICS_TAG_VERTICAL_DESCENDER: return GET_METRIC_X (vhea, descender);
94  case HB_OT_METRICS_TAG_VERTICAL_LINE_GAP: return GET_METRIC_X (vhea, lineGap);
95 #endif
96 
97 #undef GET_METRIC_Y
98 #undef GET_METRIC_X
99 #undef GET_VAR
100  default: assert (0); return false;
101  }
102 }
103 
104 #ifndef HB_NO_METRICS
105 
106 #if 0
107 static bool
108 _get_gasp (hb_face_t *face, float *result, hb_ot_metrics_tag_t metrics_tag)
109 {
110  const OT::GaspRange& range = face->table.gasp->get_gasp_range (metrics_tag - HB_TAG ('g','s','p','0'));
111  if (&range == &Null (OT::GaspRange)) return false;
112  if (result) *result = range.rangeMaxPPEM + font->face->table.MVAR->get_var (metrics_tag, font->coords, font->num_coords);
113  return true;
114 }
115 #endif
116 
117 /* Private tags for https://github.com/harfbuzz/harfbuzz/issues/1866 */
118 #define _HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER_OS2 HB_TAG ('O','a','s','c')
119 #define _HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER_HHEA HB_TAG ('H','a','s','c')
120 #define _HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER_OS2 HB_TAG ('O','d','s','c')
121 #define _HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER_HHEA HB_TAG ('H','d','s','c')
122 #define _HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP_OS2 HB_TAG ('O','l','g','p')
123 #define _HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP_HHEA HB_TAG ('H','l','g','p')
124 
136 hb_bool_t
138  hb_ot_metrics_tag_t metrics_tag,
139  hb_position_t *position /* OUT. May be NULL. */)
140 {
141  hb_face_t *face = font->face;
142  switch ((unsigned) metrics_tag)
143  {
150 #ifndef HB_NO_VAR
151 #define GET_VAR hb_ot_metrics_get_variation (font, metrics_tag)
152 #else
153 #define GET_VAR 0
154 #endif
155 #define GET_METRIC_X(TABLE, ATTR) \
156  (face->table.TABLE->has_data () && \
157  (position && (*position = font->em_scalef_x (face->table.TABLE->ATTR + GET_VAR)), true))
158 #define GET_METRIC_Y(TABLE, ATTR) \
159  (face->table.TABLE->has_data () && \
160  (position && (*position = font->em_scalef_y (face->table.TABLE->ATTR + GET_VAR)), true))
161  case HB_OT_METRICS_TAG_HORIZONTAL_CLIPPING_ASCENT: return GET_METRIC_Y (OS2, usWinAscent);
162  case HB_OT_METRICS_TAG_HORIZONTAL_CLIPPING_DESCENT: return GET_METRIC_Y (OS2, usWinDescent);
163 
166  {
167  unsigned mult = 1u;
168 
169  if (font->slant)
170  {
171  unsigned rise = face->table.hhea->caretSlopeRise;
172  unsigned upem = face->get_upem ();
173  mult = (rise && rise < upem) ? hb_min (upem / rise, 256u) : 1u;
174  }
175 
176  if (metrics_tag == HB_OT_METRICS_TAG_HORIZONTAL_CARET_RISE)
177  {
178  bool ret = GET_METRIC_Y (hhea, caretSlopeRise);
179 
180  if (position)
181  *position *= mult;
182 
183  return ret;
184  }
185  else
186  {
187  hb_position_t rise = 0;
188 
189  if (font->slant && position && GET_METRIC_Y (hhea, caretSlopeRise))
190  rise = *position;
191 
192  bool ret = GET_METRIC_X (hhea, caretSlopeRun);
193 
194  if (position)
195  {
196  *position *= mult;
197 
198  if (font->slant)
199  *position += _hb_roundf (mult * font->slant_xy * rise);
200  }
201 
202  return ret;
203  }
204  }
205  case HB_OT_METRICS_TAG_HORIZONTAL_CARET_OFFSET: return GET_METRIC_X (hhea, caretOffset);
206 
207 #ifndef HB_NO_VERTICAL
208  case HB_OT_METRICS_TAG_VERTICAL_CARET_RISE: return GET_METRIC_X (vhea, caretSlopeRise);
209  case HB_OT_METRICS_TAG_VERTICAL_CARET_RUN: return GET_METRIC_Y (vhea, caretSlopeRun);
210  case HB_OT_METRICS_TAG_VERTICAL_CARET_OFFSET: return GET_METRIC_Y (vhea, caretOffset);
211 #endif
212  case HB_OT_METRICS_TAG_X_HEIGHT: return GET_METRIC_Y (OS2->v2 (), sxHeight);
213  case HB_OT_METRICS_TAG_CAP_HEIGHT: return GET_METRIC_Y (OS2->v2 (), sCapHeight);
214  case HB_OT_METRICS_TAG_SUBSCRIPT_EM_X_SIZE: return GET_METRIC_X (OS2, ySubscriptXSize);
215  case HB_OT_METRICS_TAG_SUBSCRIPT_EM_Y_SIZE: return GET_METRIC_Y (OS2, ySubscriptYSize);
216  case HB_OT_METRICS_TAG_SUBSCRIPT_EM_X_OFFSET: return GET_METRIC_X (OS2, ySubscriptXOffset);
217  case HB_OT_METRICS_TAG_SUBSCRIPT_EM_Y_OFFSET: return GET_METRIC_Y (OS2, ySubscriptYOffset);
218  case HB_OT_METRICS_TAG_SUPERSCRIPT_EM_X_SIZE: return GET_METRIC_X (OS2, ySuperscriptXSize);
219  case HB_OT_METRICS_TAG_SUPERSCRIPT_EM_Y_SIZE: return GET_METRIC_Y (OS2, ySuperscriptYSize);
220  case HB_OT_METRICS_TAG_SUPERSCRIPT_EM_X_OFFSET: return GET_METRIC_X (OS2, ySuperscriptXOffset);
221  case HB_OT_METRICS_TAG_SUPERSCRIPT_EM_Y_OFFSET: return GET_METRIC_Y (OS2, ySuperscriptYOffset);
222  case HB_OT_METRICS_TAG_STRIKEOUT_SIZE: return GET_METRIC_Y (OS2, yStrikeoutSize);
223  case HB_OT_METRICS_TAG_STRIKEOUT_OFFSET: return GET_METRIC_Y (OS2, yStrikeoutPosition);
224  case HB_OT_METRICS_TAG_UNDERLINE_SIZE: return GET_METRIC_Y (post->table, underlineThickness);
225  case HB_OT_METRICS_TAG_UNDERLINE_OFFSET: return GET_METRIC_Y (post->table, underlinePosition);
226 
227  /* Private tags */
228  case _HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER_OS2: return GET_METRIC_Y (OS2, sTypoAscender);
229  case _HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER_HHEA: return GET_METRIC_Y (hhea, ascender);
230  case _HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER_OS2: return GET_METRIC_Y (OS2, sTypoDescender);
231  case _HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER_HHEA: return GET_METRIC_Y (hhea, descender);
232  case _HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP_OS2: return GET_METRIC_Y (OS2, sTypoLineGap);
233  case _HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP_HHEA: return GET_METRIC_Y (hhea, lineGap);
234 #undef GET_METRIC_Y
235 #undef GET_METRIC_X
236 #undef GET_VAR
237  default: return false;
238  }
239 }
240 
252 void
254  hb_ot_metrics_tag_t metrics_tag,
255  hb_position_t *position /* OUT */)
256 {
257  hb_font_extents_t font_extents;
258  hb_codepoint_t glyph;
259  hb_glyph_extents_t extents;
260 
261  if (hb_ot_metrics_get_position (font, metrics_tag, position))
262  {
263  if ((metrics_tag != HB_OT_METRICS_TAG_STRIKEOUT_SIZE &&
264  metrics_tag != HB_OT_METRICS_TAG_UNDERLINE_SIZE) ||
265  *position != 0)
266  return;
267  }
268 
269  switch (metrics_tag)
270  {
274  *position = font_extents.ascender;
275  break;
276 
279  *position = font_extents.ascender;
280  break;
281 
285  *position = font_extents.descender;
286  break;
287 
290  *position = font_extents.ascender;
291  break;
292 
295  *position = font_extents.line_gap;
296  break;
297 
300  *position = font_extents.line_gap;
301  break;
302 
305  *position = 1;
306  break;
307 
310  *position = 0;
311  break;
312 
315  *position = 0;
316  break;
317 
319  if (hb_font_get_nominal_glyph (font, 'x', &glyph) &&
320  hb_font_get_glyph_extents (font, glyph, &extents))
321  *position = extents.y_bearing;
322  else
323  *position = font->y_scale / 2;
324  break;
325 
327  if (hb_font_get_nominal_glyph (font, 'O', &glyph) &&
328  hb_font_get_glyph_extents (font, glyph, &extents))
329  *position = extents.height + 2 * extents.y_bearing;
330  else
331  *position = font->y_scale * 2 / 3;
332  break;
333 
336  *position = font->y_scale / 18;
337  break;
338 
340  {
341  hb_position_t ascender;
344  &ascender);
345  *position = ascender / 2;
346  }
347  break;
348 
350  *position = - font->y_scale / 18;
351  break;
352 
355  *position = font->x_scale * 10 / 12;
356  break;
357 
360  *position = font->y_scale * 10 / 12;
361  break;
362 
365  *position = 0;
366  break;
367 
370  *position = font->y_scale / 5;
371  break;
372 
374  default:
375  *position = 0;
376  break;
377  }
378 }
379 
380 #ifndef HB_NO_VAR
393 float
395 {
396  return font->face->table.MVAR->get_var (metrics_tag, font->coords, font->num_coords);
397 }
398 
413 {
414  return font->em_scalef_x (hb_ot_metrics_get_variation (font, metrics_tag));
415 }
416 
431 {
432  return font->em_scalef_y (hb_ot_metrics_get_variation (font, metrics_tag));
433 }
434 #endif
435 
436 #endif
#define value
[5]
void hb_font_get_extents_for_direction(hb_font_t *font, hb_direction_t direction, hb_font_extents_t *extents)
Definition: hb-font.cc:1340
hb_bool_t hb_font_get_nominal_glyph(hb_font_t *font, hb_codepoint_t unicode, hb_codepoint_t *glyph)
Definition: hb-font.cc:966
hb_bool_t hb_font_get_glyph_extents(hb_font_t *font, hb_codepoint_t glyph, hb_glyph_extents_t *extents)
Definition: hb-font.cc:1224
#define GET_METRIC_Y(TABLE, ATTR)
#define _HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP_OS2
float hb_ot_metrics_get_variation(hb_font_t *font, hb_ot_metrics_tag_t metrics_tag)
hb_position_t hb_ot_metrics_get_x_variation(hb_font_t *font, hb_ot_metrics_tag_t metrics_tag)
#define _HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER_HHEA
hb_bool_t hb_ot_metrics_get_position(hb_font_t *font, hb_ot_metrics_tag_t metrics_tag, hb_position_t *position)
#define _HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER_OS2
#define GET_METRIC_X(TABLE, ATTR)
#define _HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER_HHEA
#define _HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP_HHEA
#define _HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER_OS2
hb_position_t hb_ot_metrics_get_y_variation(hb_font_t *font, hb_ot_metrics_tag_t metrics_tag)
void hb_ot_metrics_get_position_with_fallback(hb_font_t *font, hb_ot_metrics_tag_t metrics_tag, hb_position_t *position)
bool _hb_ot_metrics_get_position_common(hb_font_t *font, hb_ot_metrics_tag_t metrics_tag, hb_position_t *position)
hb_ot_metrics_tag_t
Definition: hb-ot-metrics.h:74
@ HB_OT_METRICS_TAG_X_HEIGHT
Definition: hb-ot-metrics.h:89
@ HB_OT_METRICS_TAG_HORIZONTAL_CLIPPING_DESCENT
Definition: hb-ot-metrics.h:79
@ HB_OT_METRICS_TAG_VERTICAL_LINE_GAP
Definition: hb-ot-metrics.h:82
@ HB_OT_METRICS_TAG_SUBSCRIPT_EM_Y_OFFSET
Definition: hb-ot-metrics.h:94
@ HB_OT_METRICS_TAG_SUBSCRIPT_EM_X_SIZE
Definition: hb-ot-metrics.h:91
@ _HB_OT_METRICS_TAG_MAX_VALUE
@ HB_OT_METRICS_TAG_VERTICAL_ASCENDER
Definition: hb-ot-metrics.h:80
@ HB_OT_METRICS_TAG_CAP_HEIGHT
Definition: hb-ot-metrics.h:90
@ HB_OT_METRICS_TAG_SUPERSCRIPT_EM_X_SIZE
Definition: hb-ot-metrics.h:95
@ HB_OT_METRICS_TAG_STRIKEOUT_SIZE
Definition: hb-ot-metrics.h:99
@ HB_OT_METRICS_TAG_UNDERLINE_SIZE
@ HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER
Definition: hb-ot-metrics.h:75
@ HB_OT_METRICS_TAG_SUBSCRIPT_EM_X_OFFSET
Definition: hb-ot-metrics.h:93
@ HB_OT_METRICS_TAG_UNDERLINE_OFFSET
@ HB_OT_METRICS_TAG_HORIZONTAL_CARET_RUN
Definition: hb-ot-metrics.h:84
@ HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER
Definition: hb-ot-metrics.h:76
@ HB_OT_METRICS_TAG_SUPERSCRIPT_EM_X_OFFSET
Definition: hb-ot-metrics.h:97
@ HB_OT_METRICS_TAG_HORIZONTAL_CARET_RISE
Definition: hb-ot-metrics.h:83
@ HB_OT_METRICS_TAG_VERTICAL_CARET_RISE
Definition: hb-ot-metrics.h:86
@ HB_OT_METRICS_TAG_HORIZONTAL_CLIPPING_ASCENT
Definition: hb-ot-metrics.h:78
@ HB_OT_METRICS_TAG_SUPERSCRIPT_EM_Y_OFFSET
Definition: hb-ot-metrics.h:98
@ HB_OT_METRICS_TAG_SUPERSCRIPT_EM_Y_SIZE
Definition: hb-ot-metrics.h:96
@ HB_OT_METRICS_TAG_HORIZONTAL_CARET_OFFSET
Definition: hb-ot-metrics.h:85
@ HB_OT_METRICS_TAG_VERTICAL_DESCENDER
Definition: hb-ot-metrics.h:81
@ HB_OT_METRICS_TAG_VERTICAL_CARET_RUN
Definition: hb-ot-metrics.h:87
@ HB_OT_METRICS_TAG_STRIKEOUT_OFFSET
@ HB_OT_METRICS_TAG_SUBSCRIPT_EM_Y_SIZE
Definition: hb-ot-metrics.h:92
@ HB_OT_METRICS_TAG_VERTICAL_CARET_OFFSET
Definition: hb-ot-metrics.h:88
@ HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP
Definition: hb-ot-metrics.h:77
QHighDpiScaling::Point position(T, QHighDpiScaling::Point::Kind)
#define assert
Definition: qcborcommon_p.h:63
EGLOutputLayerEXT EGLint EGLAttrib value
GLenum face
GLsizei range
GLuint64EXT * result
[6]
Definition: qopenglext.h:10932
HB_BEGIN_DECLS typedef int hb_bool_t
Definition: hb-common.h:97
uint32_t hb_codepoint_t
Definition: hb-common.h:106
#define HB_TAG(c1, c2, c3, c4)
Definition: hb-common.h:169
int32_t hb_position_t
Definition: hb-common.h:115
@ HB_DIRECTION_TTB
Definition: hb-common.h:233
@ HB_DIRECTION_LTR
Definition: hb-common.h:231
http post("register.html", data)
Definition: hb-null.hh:93
HBUINT16 rangeMaxPPEM
hb_position_t descender
Definition: hb-font.h:115
hb_position_t ascender
Definition: hb-font.h:114
hb_position_t line_gap
Definition: hb-font.h:116
hb_position_t y_bearing
Definition: hb-font.h:142
hb_position_t height
Definition: hb-font.h:144