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 **/
61typedef 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 _HB_STYLE_TAG_MAX_VALUE = HB_TAG_MAX_SIGNED /*< skip >*/
69} hb_style_tag_t;
70
71/**
72 * hb_style_get_value:
73 * @font: a #hb_font_t object.
74 * @style_tag: a style tag.
75 *
76 * Searches variation axes of a hb_font_t object for a specific axis first,
77 * if not set, then tries to get default style values from different
78 * tables of the font.
79 *
80 * Returns: Corresponding axis or default value to a style tag.
81 *
82 * Since: EXPERIMENTAL
83 **/
84float
85hb_style_get_value (hb_font_t *font, hb_tag_t tag)
86{
87 hb_style_tag_t style_tag = (hb_style_tag_t) tag;
88 hb_face_t *face = font->face;
89
90#ifndef HB_NO_VAR
91 hb_ot_var_axis_info_t axis;
92 if (hb_ot_var_find_axis_info (face, style_tag, &axis))
93 {
94 if (axis.axis_index < font->num_coords) return font->design_coords[axis.axis_index];
95 /* If a face is variable, fvar's default_value is better than STAT records */
96 return axis.default_value;
97 }
98#endif
99
100 if (style_tag == HB_STYLE_TAG_OPTICAL_SIZE && font->ptem)
101 return font->ptem;
102
103 /* STAT */
104 float value;
105 if (face->table.STAT->get_value (style_tag, &value))
106 return value;
107
108 switch ((unsigned) style_tag)
109 {
110 case HB_STYLE_TAG_ITALIC:
111 return face->table.OS2->is_italic () || face->table.head->is_italic () ? 1 : 0;
112 case HB_STYLE_TAG_OPTICAL_SIZE:
113 {
114 unsigned int lower, upper;
115 return face->table.OS2->v5 ().get_optical_size (&lower, &upper)
116 ? (float) (lower + upper) / 2.f
117 : 12.f;
118 }
119 case HB_STYLE_TAG_SLANT:
120 return face->table.post->table->italicAngle.to_float ();
121 case HB_STYLE_TAG_WIDTH:
122 return face->table.OS2->has_data ()
123 ? face->table.OS2->get_width ()
124 : (face->table.head->is_condensed () ? 75 : 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;
131 }
132}
133
134#endif
135#endif
136