1/*
2 * Copyright © 2017  Google, Inc.
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 * Google Author(s): Behdad Esfahbod
25 */
26
27#include "hb-open-type-private.hh"
28
29#include "hb-ot-layout-private.hh"
30#include "hb-ot-var-avar-table.hh"
31#include "hb-ot-var-fvar-table.hh"
32#include "hb-ot-var.h"
33
34HB_SHAPER_DATA_ENSURE_DECLARE(ot, face)
35
36/*
37 * fvar/avar
38 */
39
40static inline const OT::fvar&
41_get_fvar (hb_face_t *face)
42{
43  if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::fvar);
44  hb_ot_layout_t * layout = hb_ot_layout_from_face (face);
45  return *(layout->fvar.get ());
46}
47static inline const OT::avar&
48_get_avar (hb_face_t *face)
49{
50  if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::avar);
51  hb_ot_layout_t * layout = hb_ot_layout_from_face (face);
52  return *(layout->avar.get ());
53}
54
55/**
56 * hb_ot_var_has_data:
57 * @face: #hb_face_t to test
58 *
59 * This function allows to verify the presence of OpenType variation data on the face.
60 * Alternatively, use hb_ot_var_get_axis_count().
61 *
62 * Return value: true if face has a `fvar' table and false otherwise
63 *
64 * Since: 1.4.2
65 **/
66hb_bool_t
67hb_ot_var_has_data (hb_face_t *face)
68{
69  return &_get_fvar (face) != &OT::Null(OT::fvar);
70}
71
72/**
73 * hb_ot_var_get_axis_count:
74 *
75 * Since: 1.4.2
76 **/
77unsigned int
78hb_ot_var_get_axis_count (hb_face_t *face)
79{
80  const OT::fvar &fvar = _get_fvar (face);
81  return fvar.get_axis_count ();
82}
83
84/**
85 * hb_ot_var_get_axes:
86 *
87 * Since: 1.4.2
88 **/
89unsigned int
90hb_ot_var_get_axes (hb_face_t        *face,
91		    unsigned int      start_offset,
92		    unsigned int     *axes_count /* IN/OUT */,
93		    hb_ot_var_axis_t *axes_array /* OUT */)
94{
95  const OT::fvar &fvar = _get_fvar (face);
96  return fvar.get_axis_infos (start_offset, axes_count, axes_array);
97}
98
99/**
100 * hb_ot_var_find_axis:
101 *
102 * Since: 1.4.2
103 **/
104hb_bool_t
105hb_ot_var_find_axis (hb_face_t        *face,
106		     hb_tag_t          axis_tag,
107		     unsigned int     *axis_index,
108		     hb_ot_var_axis_t *axis_info)
109{
110  const OT::fvar &fvar = _get_fvar (face);
111  return fvar.find_axis (axis_tag, axis_index, axis_info);
112}
113
114
115/**
116 * hb_ot_var_normalize_variations:
117 *
118 * Since: 1.4.2
119 **/
120void
121hb_ot_var_normalize_variations (hb_face_t            *face,
122				const hb_variation_t *variations, /* IN */
123				unsigned int          variations_length,
124				int                  *coords, /* OUT */
125				unsigned int          coords_length)
126{
127  for (unsigned int i = 0; i < coords_length; i++)
128    coords[i] = 0;
129
130  const OT::fvar &fvar = _get_fvar (face);
131  for (unsigned int i = 0; i < variations_length; i++)
132  {
133    unsigned int axis_index;
134    if (hb_ot_var_find_axis (face, variations[i].tag, &axis_index, NULL) &&
135	axis_index < coords_length)
136      coords[axis_index] = fvar.normalize_axis_value (axis_index, variations[i].value);
137  }
138
139  const OT::avar &avar = _get_avar (face);
140  avar.map_coords (coords, coords_length);
141}
142
143/**
144 * hb_ot_var_normalize_coords:
145 *
146 * Since: 1.4.2
147 **/
148void
149hb_ot_var_normalize_coords (hb_face_t    *face,
150			    unsigned int coords_length,
151			    const float *design_coords, /* IN */
152			    int *normalized_coords /* OUT */)
153{
154  const OT::fvar &fvar = _get_fvar (face);
155  for (unsigned int i = 0; i < coords_length; i++)
156    normalized_coords[i] = fvar.normalize_axis_value (i, design_coords[i]);
157
158  const OT::avar &avar = _get_avar (face);
159  avar.map_coords (normalized_coords, coords_length);
160}
161