/work/workdir/UnpackedTarball/harfbuzz/src/hb-style.cc
Line | Count | Source |
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 |
47 | | _hb_angle_to_ratio (float a) |
48 | 0 | { |
49 | 0 | return tanf (a * -HB_PI / 180.f); |
50 | 0 | } |
51 | | |
52 | | static inline float |
53 | | _hb_ratio_to_angle (float r) |
54 | 0 | { |
55 | 0 | return atanf (r) * -180.f / HB_PI; |
56 | 0 | } |
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, first tries to get default style values in `STAT` table |
65 | | * then tries to polyfill from different 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 |
72 | | hb_style_get_value (hb_font_t *font, hb_style_tag_t style_tag) |
73 | 23 | { |
74 | 23 | if (unlikely (style_tag == HB_STYLE_TAG_SLANT_RATIO)) |
75 | 0 | return _hb_angle_to_ratio (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE)); |
76 | | |
77 | 23 | hb_face_t *face = font->face; |
78 | | |
79 | 23 | #ifndef HB_NO_VAR |
80 | 23 | hb_ot_var_axis_info_t axis; |
81 | 23 | if (hb_ot_var_find_axis_info (face, style_tag, &axis)) |
82 | 0 | { |
83 | 0 | 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 | 0 | return axis.default_value; |
86 | 0 | } |
87 | 23 | #endif |
88 | | |
89 | 23 | if (style_tag == HB_STYLE_TAG_OPTICAL_SIZE && font->ptem) |
90 | 0 | return font->ptem; |
91 | | |
92 | | /* STAT */ |
93 | 23 | float value; |
94 | 23 | if (face->table.STAT->get_value (style_tag, &value)) |
95 | 0 | return value; |
96 | | |
97 | 23 | switch ((unsigned) style_tag) |
98 | 23 | { |
99 | 0 | case HB_STYLE_TAG_ITALIC: |
100 | 0 | return face->table.OS2->is_italic () || face->table.head->is_italic () ? 1 : 0; |
101 | 0 | case HB_STYLE_TAG_OPTICAL_SIZE: |
102 | 0 | { |
103 | 0 | unsigned int lower, design, upper; |
104 | 0 | return face->table.OS2->v5 ().get_optical_size (&lower, &upper) |
105 | 0 | ? (float) (lower + upper) / 2.f |
106 | 0 | : hb_ot_layout_get_size_params (face, &design, nullptr, nullptr, nullptr, nullptr) |
107 | 0 | ? design / 10.f |
108 | 0 | : 12.f; |
109 | 0 | } |
110 | 0 | case HB_STYLE_TAG_SLANT_ANGLE: |
111 | 0 | { |
112 | 0 | float angle = face->table.post->table->italicAngle.to_float (); |
113 | |
|
114 | 0 | if (font->slant) |
115 | 0 | angle = _hb_ratio_to_angle (font->slant + _hb_angle_to_ratio (angle)); |
116 | |
|
117 | 0 | return angle; |
118 | 0 | } |
119 | 0 | case HB_STYLE_TAG_WIDTH: |
120 | 0 | return face->table.OS2->has_data () |
121 | 0 | ? face->table.OS2->get_width () |
122 | 0 | : (face->table.head->is_condensed () ? 75 : |
123 | 0 | face->table.head->is_expanded () ? 125 : |
124 | 0 | 100); |
125 | 23 | case HB_STYLE_TAG_WEIGHT: |
126 | 23 | return face->table.OS2->has_data () |
127 | 23 | ? face->table.OS2->usWeightClass |
128 | 23 | : (face->table.head->is_bold () ? 700 : 400); |
129 | 0 | default: |
130 | 0 | return 0; |
131 | 23 | } |
132 | 23 | } |
133 | | |
134 | | #endif |