• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if 0
52 static inline float
53 _hb_ratio_to_angle (float r)
54 {
55   return atanf (r) * float (180. / M_PI);
56 }
57 #endif
58 
59 /**
60  * hb_style_get_value:
61  * @font: a #hb_font_t object.
62  * @style_tag: a style tag.
63  *
64  * Searches variation axes of a #hb_font_t object for a specific axis first,
65  * if not set, then tries to get default style values from different
66  * tables of the font.
67  *
68  * Returns: Corresponding axis or default value to a style tag.
69  *
70  * Since: 3.0.0
71  **/
72 float
hb_style_get_value(hb_font_t * font,hb_style_tag_t style_tag)73 hb_style_get_value (hb_font_t *font, hb_style_tag_t style_tag)
74 {
75   if (unlikely (style_tag == HB_STYLE_TAG_SLANT_RATIO))
76     return _hb_angle_to_ratio (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE));
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     return face->table.post->table->italicAngle.to_float ();
113   case HB_STYLE_TAG_WIDTH:
114     return face->table.OS2->has_data ()
115 	   ? face->table.OS2->get_width ()
116 	   : (face->table.head->is_condensed () ? 75 :
117 	      face->table.head->is_expanded () ? 125 :
118 	      100);
119   case HB_STYLE_TAG_WEIGHT:
120     return face->table.OS2->has_data ()
121 	   ? face->table.OS2->usWeightClass
122 	   : (face->table.head->is_bold () ? 700 : 400);
123   default:
124     return 0;
125   }
126 }
127 
128 #endif
129