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