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 	 + font->slant;
77 
78   hb_face_t *face = font->face;
79 
80 #ifndef HB_NO_VAR
81   hb_ot_var_axis_info_t axis;
82   if (hb_ot_var_find_axis_info (face, style_tag, &axis))
83   {
84     if (axis.axis_index < font->num_coords) return font->design_coords[axis.axis_index];
85     /* If a face is variable, fvar's default_value is better than STAT records */
86     return axis.default_value;
87   }
88 #endif
89 
90   if (style_tag == HB_STYLE_TAG_OPTICAL_SIZE && font->ptem)
91     return font->ptem;
92 
93   /* STAT */
94   float value;
95   if (face->table.STAT->get_value (style_tag, &value))
96     return value;
97 
98   switch ((unsigned) style_tag)
99   {
100   case HB_STYLE_TAG_ITALIC:
101     return face->table.OS2->is_italic () || face->table.head->is_italic () ? 1 : 0;
102   case HB_STYLE_TAG_OPTICAL_SIZE:
103   {
104     unsigned int lower, design, upper;
105     return face->table.OS2->v5 ().get_optical_size (&lower, &upper)
106 	   ? (float) (lower + upper) / 2.f
107 	   : hb_ot_layout_get_size_params (face, &design, nullptr, nullptr, nullptr, nullptr)
108 	   ? design / 10.f
109 	   : 12.f;
110   }
111   case HB_STYLE_TAG_SLANT_ANGLE:
112   {
113     float angle = face->table.post->table->italicAngle.to_float ();
114 
115     if (font->slant)
116       angle = _hb_ratio_to_angle (font->slant + _hb_angle_to_ratio (angle));
117 
118     return angle;
119   }
120   case HB_STYLE_TAG_WIDTH:
121     return face->table.OS2->has_data ()
122 	   ? face->table.OS2->get_width ()
123 	   : (face->table.head->is_condensed () ? 75 :
124 	      face->table.head->is_expanded () ? 125 :
125 	      100);
126   case HB_STYLE_TAG_WEIGHT:
127     return face->table.OS2->has_data ()
128 	   ? face->table.OS2->usWeightClass
129 	   : (face->table.head->is_bold () ? 700 : 400);
130   default:
131     return 0;
132   }
133 }
134 
135 #endif
136