Bug 1852321 - Fix wpt tests in jsshell for sync changes, r=Sasha
[gecko.git] / gfx / harfbuzz / src / hb-style.cc
blobbd5cb5c6be04618390b299d32f4da844fb566ba3
1 /*
2 * Copyright © 2019 Ebrahim Byagowi
4 * This is part of HarfBuzz, a text shaping library.
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.
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.
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.
25 #include "hb.hh"
27 #ifndef HB_NO_STYLE
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"
37 /**
38 * SECTION:hb-style
39 * @title: hb-style
40 * @short_description: Font Styles
41 * @include: hb.h
43 * Functions for fetching style information from fonts.
44 **/
46 static inline float
47 _hb_angle_to_ratio (float a)
49 return tanf (a * -HB_PI / 180.f);
52 static inline float
53 _hb_ratio_to_angle (float r)
55 return atanf (r) * -180.f / HB_PI;
58 /**
59 * hb_style_get_value:
60 * @font: a #hb_font_t object.
61 * @style_tag: a style tag.
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.
67 * Returns: Corresponding axis or default value to a style tag.
69 * Since: 3.0.0
70 **/
71 float
72 hb_style_get_value (hb_font_t *font, hb_style_tag_t style_tag)
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));
77 hb_face_t *face = font->face;
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))
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;
87 #endif
89 if (style_tag == HB_STYLE_TAG_OPTICAL_SIZE && font->ptem)
90 return font->ptem;
92 /* STAT */
93 float value;
94 if (face->table.STAT->get_value (style_tag, &value))
95 return value;
97 switch ((unsigned) style_tag)
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:
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;
110 case HB_STYLE_TAG_SLANT_ANGLE:
112 float angle = face->table.post->table->italicAngle.to_float ();
114 if (font->slant)
115 angle = _hb_ratio_to_angle (font->slant + _hb_angle_to_ratio (angle));
117 return angle;
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;
134 #endif