1 /*
2 * Copyright © 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 #ifndef HB_NO_STYLE
28
29 #include "hb-ot-var-avar-table.hh"
30 #include "hb-ot-var-fvar-table.hh"
31 #include "hb-ot-stat-table.hh"
32 #include "hb-ot-os2-table.hh"
33 #include "hb-ot-head-table.hh"
34 #include "hb-ot-post-table.hh"
35 #include "hb-ot-face.hh"
36
37 /**
38 * SECTION:hb-style
39 * @title: hb-style
40 * @short_description: Font Styles
41 * @include: hb.h
42 *
43 * Functions for fetching style information from fonts.
44 **/
45
46 static inline float
_hb_angle_to_ratio(float a)47 _hb_angle_to_ratio (float a)
48 {
49 return tanf (a * float (-M_PI / 180.));
50 }
51
52 static inline float
_hb_ratio_to_angle(float r)53 _hb_ratio_to_angle (float r)
54 {
55 return atanf (r) * float (-180. / M_PI);
56 }
57
58 /**
59 * hb_style_get_value:
60 * @font: a #hb_font_t object.
61 * @style_tag: a style tag.
62 *
63 * Searches variation axes of a #hb_font_t object for a specific axis first,
64 * if not set, then tries to get default style values from different
65 * tables of the font.
66 *
67 * Returns: Corresponding axis or default value to a style tag.
68 *
69 * Since: 3.0.0
70 **/
71 float
hb_style_get_value(hb_font_t * font,hb_style_tag_t style_tag)72 hb_style_get_value (hb_font_t *font, hb_style_tag_t style_tag)
73 {
74 if (unlikely (style_tag == HB_STYLE_TAG_SLANT_RATIO))
75 return _hb_angle_to_ratio (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE));
76
77 hb_face_t *face = font->face;
78
79 #ifndef HB_NO_VAR
80 hb_ot_var_axis_info_t axis;
81 if (hb_ot_var_find_axis_info (face, style_tag, &axis))
82 {
83 if (axis.axis_index < font->num_coords) return font->design_coords[axis.axis_index];
84 /* If a face is variable, fvar's default_value is better than STAT records */
85 return axis.default_value;
86 }
87 #endif
88
89 if (style_tag == HB_STYLE_TAG_OPTICAL_SIZE && font->ptem)
90 return font->ptem;
91
92 /* STAT */
93 float value;
94 if (face->table.STAT->get_value (style_tag, &value))
95 return value;
96
97 switch ((unsigned) style_tag)
98 {
99 case HB_STYLE_TAG_ITALIC:
100 return face->table.OS2->is_italic () || face->table.head->is_italic () ? 1 : 0;
101 case HB_STYLE_TAG_OPTICAL_SIZE:
102 {
103 unsigned int lower, design, upper;
104 return face->table.OS2->v5 ().get_optical_size (&lower, &upper)
105 ? (float) (lower + upper) / 2.f
106 : hb_ot_layout_get_size_params (face, &design, nullptr, nullptr, nullptr, nullptr)
107 ? design / 10.f
108 : 12.f;
109 }
110 case HB_STYLE_TAG_SLANT_ANGLE:
111 {
112 float angle = face->table.post->table->italicAngle.to_float ();
113
114 if (font->slant)
115 angle = _hb_ratio_to_angle (font->slant + _hb_angle_to_ratio (angle));
116
117 return angle;
118 }
119 case HB_STYLE_TAG_WIDTH:
120 return face->table.OS2->has_data ()
121 ? face->table.OS2->get_width ()
122 : (face->table.head->is_condensed () ? 75 :
123 face->table.head->is_expanded () ? 125 :
124 100);
125 case HB_STYLE_TAG_WEIGHT:
126 return face->table.OS2->has_data ()
127 ? face->table.OS2->usWeightClass
128 : (face->table.head->is_bold () ? 700 : 400);
129 default:
130 return 0;
131 }
132 }
133
134 #endif
135