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 #ifdef HB_EXPERIMENTAL_API
29 
30 #include "hb-ot-var-avar-table.hh"
31 #include "hb-ot-var-fvar-table.hh"
32 #include "hb-ot-stat-table.hh"
33 #include "hb-ot-os2-table.hh"
34 #include "hb-ot-head-table.hh"
35 #include "hb-ot-post-table.hh"
36 #include "hb-ot-face.hh"
37 
38 /**
39  * hb_style_tag_t:
40  * @HB_STYLE_TAG_ITALIC: Used to vary between non-italic and italic.
41  * A value of 0 can be interpreted as "Roman" (non-italic); a value of 1 can
42  * be interpreted as (fully) italic.
43  * @HB_STYLE_TAG_OPTICAL_SIZE: Used to vary design to suit different text sizes.
44  * Non-zero. Values can be interpreted as text size, in points.
45  * @HB_STYLE_TAG_SLANT: Used to vary between upright and slanted text. Values
46  * must be greater than -90 and less than +90. Values can be interpreted as
47  * the angle, in counter-clockwise degrees, of oblique slant from whatever the
48  * designer considers to be upright for that font design.
49  * @HB_STYLE_TAG_WIDTH: Used to vary width of text from narrower to wider.
50  * Non-zero. Values can be interpreted as a percentage of whatever the font
51  * designer considers “normal width” for that font design.
52  * @HB_STYLE_TAG_WEIGHT: Used to vary stroke thicknesses or other design details
53  * to give variation from lighter to blacker. Values can be interpreted in direct
54  * comparison to values for usWeightClass in the OS/2 table,
55  * or the CSS font-weight property.
56  *
57  * Defined by https://docs.microsoft.com/en-us/typography/opentype/spec/dvaraxisreg
58  *
59  * Since: EXPERIMENTAL
60  **/
61 typedef enum {
62   HB_STYLE_TAG_ITALIC		= HB_TAG ('i','t','a','l'),
63   HB_STYLE_TAG_OPTICAL_SIZE	= HB_TAG ('o','p','s','z'),
64   HB_STYLE_TAG_SLANT		= HB_TAG ('s','l','n','t'),
65   HB_STYLE_TAG_WIDTH		= HB_TAG ('w','d','t','h'),
66   HB_STYLE_TAG_WEIGHT		= HB_TAG ('w','g','h','t'),
67 
68   /*< private >*/
69   _HB_STYLE_TAG_MAX_VALUE	= HB_TAG_MAX_SIGNED /*< skip >*/
70 } hb_style_tag_t;
71 
72 /**
73  * hb_style_get_value:
74  * @font: a #hb_font_t object.
75  * @style_tag: a style tag.
76  *
77  * Searches variation axes of a hb_font_t object for a specific axis first,
78  * if not set, then tries to get default style values from different
79  * tables of the font.
80  *
81  * Returns: Corresponding axis or default value to a style tag.
82  *
83  * Since: EXPERIMENTAL
84  **/
85 float
hb_style_get_value(hb_font_t * font,hb_tag_t tag)86 hb_style_get_value (hb_font_t *font, hb_tag_t tag)
87 {
88   hb_style_tag_t style_tag = (hb_style_tag_t) tag;
89   hb_face_t *face = font->face;
90 
91 #ifndef HB_NO_VAR
92   hb_ot_var_axis_info_t axis;
93   if (hb_ot_var_find_axis_info (face, style_tag, &axis))
94   {
95     if (axis.axis_index < font->num_coords) return font->design_coords[axis.axis_index];
96     /* If a face is variable, fvar's default_value is better than STAT records */
97     return axis.default_value;
98   }
99 #endif
100 
101   if (style_tag == HB_STYLE_TAG_OPTICAL_SIZE && font->ptem)
102     return font->ptem;
103 
104   /* STAT */
105   float value;
106   if (face->table.STAT->get_value (style_tag, &value))
107     return value;
108 
109   switch ((unsigned) style_tag)
110   {
111   case HB_STYLE_TAG_ITALIC:
112     return face->table.OS2->is_italic () || face->table.head->is_italic () ? 1 : 0;
113   case HB_STYLE_TAG_OPTICAL_SIZE:
114   {
115     unsigned int lower, upper;
116     return face->table.OS2->v5 ().get_optical_size (&lower, &upper)
117 	   ? (float) (lower + upper) / 2.f
118 	   : 12.f;
119   }
120   case HB_STYLE_TAG_SLANT:
121     return face->table.post->table->italicAngle.to_float ();
122   case HB_STYLE_TAG_WIDTH:
123     return face->table.OS2->has_data ()
124 	   ? face->table.OS2->get_width ()
125 	   : (face->table.head->is_condensed () ? 75 : 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 #endif
137