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
36 /**
37 * SECTION:hb-ot-metrics
38 * @title: hb-ot-metrics
39 * @short_description: OpenType Metrics
40 * @include: hb-ot.h
41 *
42 * Functions for fetching metrics from fonts.
43 **/
44
45 static float
_fix_ascender_descender(float value,hb_ot_metrics_tag_t metrics_tag)46 _fix_ascender_descender (float value, hb_ot_metrics_tag_t metrics_tag)
47 {
48 if (metrics_tag == HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER ||
49 metrics_tag == HB_OT_METRICS_TAG_VERTICAL_ASCENDER)
50 return fabs ((double) value);
51 if (metrics_tag == HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER ||
52 metrics_tag == HB_OT_METRICS_TAG_VERTICAL_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
_hb_ot_metrics_get_position_common(hb_font_t * font,hb_ot_metrics_tag_t metrics_tag,hb_position_t * position)60 _hb_ot_metrics_get_position_common (hb_font_t *font,
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 case HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER:
81 return (face->table.OS2->use_typo_metrics () && GET_METRIC_Y (OS2, sTypoAscender)) ||
82 GET_METRIC_Y (hhea, ascender);
83 case HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER:
84 return (face->table.OS2->use_typo_metrics () && GET_METRIC_Y (OS2, sTypoDescender)) ||
85 GET_METRIC_Y (hhea, descender);
86 case HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP:
87 return (face->table.OS2->use_typo_metrics () && GET_METRIC_Y (OS2, sTypoLineGap)) ||
88 GET_METRIC_Y (hhea, lineGap);
89 case HB_OT_METRICS_TAG_VERTICAL_ASCENDER: return GET_METRIC_X (vhea, ascender);
90 case HB_OT_METRICS_TAG_VERTICAL_DESCENDER: return GET_METRIC_X (vhea, descender);
91 case HB_OT_METRICS_TAG_VERTICAL_LINE_GAP: return GET_METRIC_X (vhea, lineGap);
92 #undef GET_METRIC_Y
93 #undef GET_METRIC_X
94 #undef GET_VAR
95 default: assert (0); return false;
96 }
97 }
98
99 #ifndef HB_NO_METRICS
100
101 #if 0
102 static bool
103 _get_gasp (hb_face_t *face, float *result, hb_ot_metrics_tag_t metrics_tag)
104 {
105 const OT::GaspRange& range = face->table.gasp->get_gasp_range (metrics_tag - HB_TAG ('g','s','p','0'));
106 if (&range == &Null (OT::GaspRange)) return false;
107 if (result) *result = range.rangeMaxPPEM + font->face->table.MVAR->get_var (metrics_tag, font->coords, font->num_coords);
108 return true;
109 }
110 #endif
111
112 /* Private tags for https://github.com/harfbuzz/harfbuzz/issues/1866 */
113 #define _HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER_OS2 HB_TAG ('O','a','s','c')
114 #define _HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER_HHEA HB_TAG ('H','a','s','c')
115 #define _HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER_OS2 HB_TAG ('O','d','s','c')
116 #define _HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER_HHEA HB_TAG ('H','d','s','c')
117 #define _HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP_OS2 HB_TAG ('O','l','g','p')
118 #define _HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP_HHEA HB_TAG ('H','l','g','p')
119
120 /**
121 * hb_ot_metrics_get_position:
122 * @font: an #hb_font_t object.
123 * @metrics_tag: tag of metrics value you like to fetch.
124 * @position: (out) (optional): result of metrics value from the font.
125 *
126 * Fetches metrics value corresponding to @metrics_tag from @font.
127 *
128 * Returns: Whether found the requested metrics in the font.
129 * Since: 2.6.0
130 **/
131 hb_bool_t
hb_ot_metrics_get_position(hb_font_t * font,hb_ot_metrics_tag_t metrics_tag,hb_position_t * position)132 hb_ot_metrics_get_position (hb_font_t *font,
133 hb_ot_metrics_tag_t metrics_tag,
134 hb_position_t *position /* OUT. May be NULL. */)
135 {
136 hb_face_t *face = font->face;
137 switch ((unsigned) metrics_tag)
138 {
139 case HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER:
140 case HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER:
141 case HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP:
142 case HB_OT_METRICS_TAG_VERTICAL_ASCENDER:
143 case HB_OT_METRICS_TAG_VERTICAL_DESCENDER:
144 case HB_OT_METRICS_TAG_VERTICAL_LINE_GAP: return _hb_ot_metrics_get_position_common (font, metrics_tag, position);
145 #ifndef HB_NO_VAR
146 #define GET_VAR hb_ot_metrics_get_variation (font, metrics_tag)
147 #else
148 #define GET_VAR 0
149 #endif
150 #define GET_METRIC_X(TABLE, ATTR) \
151 (face->table.TABLE->has_data () && \
152 (position && (*position = font->em_scalef_x (face->table.TABLE->ATTR + GET_VAR)), true))
153 #define GET_METRIC_Y(TABLE, ATTR) \
154 (face->table.TABLE->has_data () && \
155 (position && (*position = font->em_scalef_y (face->table.TABLE->ATTR + GET_VAR)), true))
156 case HB_OT_METRICS_TAG_HORIZONTAL_CLIPPING_ASCENT: return GET_METRIC_Y (OS2, usWinAscent);
157 case HB_OT_METRICS_TAG_HORIZONTAL_CLIPPING_DESCENT: return GET_METRIC_Y (OS2, usWinDescent);
158 case HB_OT_METRICS_TAG_HORIZONTAL_CARET_RISE: return GET_METRIC_Y (hhea, caretSlopeRise);
159 case HB_OT_METRICS_TAG_HORIZONTAL_CARET_RUN: return GET_METRIC_X (hhea, caretSlopeRun);
160 case HB_OT_METRICS_TAG_HORIZONTAL_CARET_OFFSET: return GET_METRIC_X (hhea, caretOffset);
161 case HB_OT_METRICS_TAG_VERTICAL_CARET_RISE: return GET_METRIC_X (vhea, caretSlopeRise);
162 case HB_OT_METRICS_TAG_VERTICAL_CARET_RUN: return GET_METRIC_Y (vhea, caretSlopeRun);
163 case HB_OT_METRICS_TAG_VERTICAL_CARET_OFFSET: return GET_METRIC_Y (vhea, caretOffset);
164 case HB_OT_METRICS_TAG_X_HEIGHT: return GET_METRIC_Y (OS2->v2 (), sxHeight);
165 case HB_OT_METRICS_TAG_CAP_HEIGHT: return GET_METRIC_Y (OS2->v2 (), sCapHeight);
166 case HB_OT_METRICS_TAG_SUBSCRIPT_EM_X_SIZE: return GET_METRIC_X (OS2, ySubscriptXSize);
167 case HB_OT_METRICS_TAG_SUBSCRIPT_EM_Y_SIZE: return GET_METRIC_Y (OS2, ySubscriptYSize);
168 case HB_OT_METRICS_TAG_SUBSCRIPT_EM_X_OFFSET: return GET_METRIC_X (OS2, ySubscriptXOffset);
169 case HB_OT_METRICS_TAG_SUBSCRIPT_EM_Y_OFFSET: return GET_METRIC_Y (OS2, ySubscriptYOffset);
170 case HB_OT_METRICS_TAG_SUPERSCRIPT_EM_X_SIZE: return GET_METRIC_X (OS2, ySuperscriptXSize);
171 case HB_OT_METRICS_TAG_SUPERSCRIPT_EM_Y_SIZE: return GET_METRIC_Y (OS2, ySuperscriptYSize);
172 case HB_OT_METRICS_TAG_SUPERSCRIPT_EM_X_OFFSET: return GET_METRIC_X (OS2, ySuperscriptXOffset);
173 case HB_OT_METRICS_TAG_SUPERSCRIPT_EM_Y_OFFSET: return GET_METRIC_Y (OS2, ySuperscriptYOffset);
174 case HB_OT_METRICS_TAG_STRIKEOUT_SIZE: return GET_METRIC_Y (OS2, yStrikeoutSize);
175 case HB_OT_METRICS_TAG_STRIKEOUT_OFFSET: return GET_METRIC_Y (OS2, yStrikeoutPosition);
176 case HB_OT_METRICS_TAG_UNDERLINE_SIZE: return GET_METRIC_Y (post->table, underlineThickness);
177 case HB_OT_METRICS_TAG_UNDERLINE_OFFSET: return GET_METRIC_Y (post->table, underlinePosition);
178
179 /* Private tags */
180 case _HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER_OS2: return GET_METRIC_Y (OS2, sTypoAscender);
181 case _HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER_HHEA: return GET_METRIC_Y (hhea, ascender);
182 case _HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER_OS2: return GET_METRIC_Y (OS2, sTypoDescender);
183 case _HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER_HHEA: return GET_METRIC_Y (hhea, descender);
184 case _HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP_OS2: return GET_METRIC_Y (OS2, sTypoLineGap);
185 case _HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP_HHEA: return GET_METRIC_Y (hhea, lineGap);
186 #undef GET_METRIC_Y
187 #undef GET_METRIC_X
188 #undef GET_VAR
189 default: return false;
190 }
191 }
192
193 #ifndef HB_NO_VAR
194 /**
195 * hb_ot_metrics_get_variation:
196 * @font: an #hb_font_t object.
197 * @metrics_tag: tag of metrics value you like to fetch.
198 *
199 * Fetches metrics value corresponding to @metrics_tag from @font with the
200 * current font variation settings applied.
201 *
202 * Returns: The requested metric value.
203 *
204 * Since: 2.6.0
205 **/
206 float
hb_ot_metrics_get_variation(hb_font_t * font,hb_ot_metrics_tag_t metrics_tag)207 hb_ot_metrics_get_variation (hb_font_t *font, hb_ot_metrics_tag_t metrics_tag)
208 {
209 return font->face->table.MVAR->get_var (metrics_tag, font->coords, font->num_coords);
210 }
211
212 /**
213 * hb_ot_metrics_get_x_variation:
214 * @font: an #hb_font_t object.
215 * @metrics_tag: tag of metrics value you like to fetch.
216 *
217 * Fetches horizontal metrics value corresponding to @metrics_tag from @font
218 * with the current font variation settings applied.
219 *
220 * Returns: The requested metric value.
221 *
222 * Since: 2.6.0
223 **/
224 hb_position_t
hb_ot_metrics_get_x_variation(hb_font_t * font,hb_ot_metrics_tag_t metrics_tag)225 hb_ot_metrics_get_x_variation (hb_font_t *font, hb_ot_metrics_tag_t metrics_tag)
226 {
227 return font->em_scalef_x (hb_ot_metrics_get_variation (font, metrics_tag));
228 }
229
230 /**
231 * hb_ot_metrics_get_y_variation:
232 * @font: an #hb_font_t object.
233 * @metrics_tag: tag of metrics value you like to fetch.
234 *
235 * Fetches vertical metrics value corresponding to @metrics_tag from @font with
236 * the current font variation settings applied.
237 *
238 * Returns: The requested metric value.
239 *
240 * Since: 2.6.0
241 **/
242 hb_position_t
hb_ot_metrics_get_y_variation(hb_font_t * font,hb_ot_metrics_tag_t metrics_tag)243 hb_ot_metrics_get_y_variation (hb_font_t *font, hb_ot_metrics_tag_t metrics_tag)
244 {
245 return font->em_scalef_y (hb_ot_metrics_get_variation (font, metrics_tag));
246 }
247 #endif
248
249 #endif
250