hb-ot-font.cc 8.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright © 2011,2014  Google, Inc.
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Google Author(s): Behdad Esfahbod, Roozbeh Pournader
 */

27
#include "hb.hh"
28 29 30

#include "hb-ot.h"

31 32
#include "hb-font.hh"
#include "hb-machinery.hh"
33
#include "hb-ot-face.hh"
34

35 36 37 38 39 40 41
#include "hb-ot-cmap-table.hh"
#include "hb-ot-hmtx-table.hh"
#include "hb-ot-kern-table.hh"
#include "hb-ot-post-table.hh"
#include "hb-ot-glyf-table.hh"
#include "hb-ot-color-cbdt-table.hh"

42 43

static hb_bool_t
44 45 46 47 48 49 50
hb_ot_get_nominal_glyph (hb_font_t *font HB_UNUSED,
			 void *font_data,
			 hb_codepoint_t unicode,
			 hb_codepoint_t *glyph,
			 void *user_data HB_UNUSED)

{
51
  const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data;
52
  return ot_font->cmap.get_relaxed()->get_nominal_glyph (unicode, glyph);
53
}
54

55 56 57 58 59 60 61
static hb_bool_t
hb_ot_get_variation_glyph (hb_font_t *font HB_UNUSED,
			   void *font_data,
			   hb_codepoint_t unicode,
			   hb_codepoint_t variation_selector,
			   hb_codepoint_t *glyph,
			   void *user_data HB_UNUSED)
62
{
63
  const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data;
64
  return ot_font->cmap.get_relaxed ()->get_variation_glyph (unicode, variation_selector, glyph);
65 66 67
}

static hb_position_t
68
hb_ot_get_glyph_h_advance (hb_font_t *font,
69 70 71 72
			   void *font_data,
			   hb_codepoint_t glyph,
			   void *user_data HB_UNUSED)
{
73
  const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data;
B
Behdad Esfahbod 已提交
74
  return font->em_scale_x (ot_font->hmtx.get_relaxed ()->get_advance (glyph, font));
75 76 77
}

static hb_position_t
78
hb_ot_get_glyph_v_advance (hb_font_t *font,
79 80 81 82
			   void *font_data,
			   hb_codepoint_t glyph,
			   void *user_data HB_UNUSED)
{
83
  const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data;
B
Behdad Esfahbod 已提交
84
  return font->em_scale_y (-(int) ot_font->vmtx.get_relaxed ()->get_advance (glyph, font));
85 86
}

B
Behdad Esfahbod 已提交
87 88 89 90 91 92 93
static hb_position_t
hb_ot_get_glyph_h_kerning (hb_font_t *font,
			   void *font_data,
			   hb_codepoint_t left_glyph,
			   hb_codepoint_t right_glyph,
			   void *user_data HB_UNUSED)
{
94
  const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data;
B
Behdad Esfahbod 已提交
95 96 97
  return font->em_scale_x (ot_font->kern->get_h_kerning (left_glyph, right_glyph));
}

98
static hb_bool_t
99
hb_ot_get_glyph_extents (hb_font_t *font,
100 101 102 103 104
			 void *font_data,
			 hb_codepoint_t glyph,
			 hb_glyph_extents_t *extents,
			 void *user_data HB_UNUSED)
{
105
  const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data;
106
  bool ret = ot_font->glyf->get_extents (glyph, extents);
107
  if (!ret)
108
    ret = ot_font->CBDT->get_extents (glyph, extents);
109
  // TODO Hook up side-bearings variations.
110 111 112 113 114
  extents->x_bearing = font->em_scale_x (extents->x_bearing);
  extents->y_bearing = font->em_scale_y (extents->y_bearing);
  extents->width     = font->em_scale_x (extents->width);
  extents->height    = font->em_scale_y (extents->height);
  return ret;
115 116
}

117 118 119 120 121 122 123
static hb_bool_t
hb_ot_get_glyph_name (hb_font_t *font HB_UNUSED,
                      void *font_data,
                      hb_codepoint_t glyph,
                      char *name, unsigned int size,
                      void *user_data HB_UNUSED)
{
124
  const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data;
125 126 127
  return ot_font->post->get_glyph_name (glyph, name, size);
}

128 129 130 131 132 133 134
static hb_bool_t
hb_ot_get_glyph_from_name (hb_font_t *font HB_UNUSED,
                           void *font_data,
                           const char *name, int len,
                           hb_codepoint_t *glyph,
                           void *user_data HB_UNUSED)
{
135
  const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data;
136 137 138
  return ot_font->post->get_glyph_from_name (name, len, glyph);
}

139
static hb_bool_t
140
hb_ot_get_font_h_extents (hb_font_t *font,
141 142 143 144
			  void *font_data,
			  hb_font_extents_t *metrics,
			  void *user_data HB_UNUSED)
{
145
  const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data;
B
Behdad Esfahbod 已提交
146 147 148
  metrics->ascender = font->em_scale_y (ot_font->hmtx.get_relaxed ()->ascender);
  metrics->descender = font->em_scale_y (ot_font->hmtx.get_relaxed ()->descender);
  metrics->line_gap = font->em_scale_y (ot_font->hmtx.get_relaxed ()->line_gap);
149
  // TODO Hook up variations.
B
Behdad Esfahbod 已提交
150
  return ot_font->hmtx.get_relaxed ()->has_font_extents;
151 152 153
}

static hb_bool_t
154
hb_ot_get_font_v_extents (hb_font_t *font,
155 156 157 158
			  void *font_data,
			  hb_font_extents_t *metrics,
			  void *user_data HB_UNUSED)
{
159
  const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data;
B
Behdad Esfahbod 已提交
160 161 162
  metrics->ascender = font->em_scale_x (ot_font->vmtx.get_relaxed ()->ascender);
  metrics->descender = font->em_scale_x (ot_font->vmtx.get_relaxed ()->descender);
  metrics->line_gap = font->em_scale_x (ot_font->vmtx.get_relaxed ()->line_gap);
163
  // TODO Hook up variations.
B
Behdad Esfahbod 已提交
164
  return ot_font->vmtx.get_relaxed ()->has_font_extents;
165
}
166

167
#ifdef HB_USE_ATEXIT
168
static void free_static_ot_funcs (void);
169
#endif
170

171
static struct hb_ot_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_ot_font_funcs_lazy_loader_t>
172
{
173
  static inline hb_font_funcs_t *create (void)
174
  {
175
    hb_font_funcs_t *funcs = hb_font_funcs_create ();
176

B
Behdad Esfahbod 已提交
177 178 179 180 181 182 183 184
    hb_font_funcs_set_font_h_extents_func (funcs, hb_ot_get_font_h_extents, nullptr, nullptr);
    hb_font_funcs_set_font_v_extents_func (funcs, hb_ot_get_font_v_extents, nullptr, nullptr);
    hb_font_funcs_set_nominal_glyph_func (funcs, hb_ot_get_nominal_glyph, nullptr, nullptr);
    hb_font_funcs_set_variation_glyph_func (funcs, hb_ot_get_variation_glyph, nullptr, nullptr);
    hb_font_funcs_set_glyph_h_advance_func (funcs, hb_ot_get_glyph_h_advance, nullptr, nullptr);
    hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ot_get_glyph_v_advance, nullptr, nullptr);
    //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ot_get_glyph_h_origin, nullptr, nullptr);
    //hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ot_get_glyph_v_origin, nullptr, nullptr);
185
    hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ot_get_glyph_h_kerning, nullptr, nullptr);
B
Behdad Esfahbod 已提交
186 187
    //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ot_get_glyph_v_kerning, nullptr, nullptr);
    hb_font_funcs_set_glyph_extents_func (funcs, hb_ot_get_glyph_extents, nullptr, nullptr);
B
Behdad Esfahbod 已提交
188
    //hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ot_get_glyph_contour_point, nullptr, nullptr);
189
    hb_font_funcs_set_glyph_name_func (funcs, hb_ot_get_glyph_name, nullptr, nullptr);
190
    hb_font_funcs_set_glyph_from_name_func (funcs, hb_ot_get_glyph_from_name, nullptr, nullptr);
191 192 193

    hb_font_funcs_make_immutable (funcs);

194 195 196 197 198 199 200
#ifdef HB_USE_ATEXIT
    atexit (free_static_ot_funcs);
#endif

    return funcs;
  }
} static_ot_funcs;
201 202

#ifdef HB_USE_ATEXIT
203 204 205
static
void free_static_ot_funcs (void)
{
B
Behdad Esfahbod 已提交
206
  static_ot_funcs.free_instance ();
207
}
208
#endif
209

210 211 212
static hb_font_funcs_t *
_hb_ot_get_font_funcs (void)
{
B
Behdad Esfahbod 已提交
213
  return static_ot_funcs.get_unconst ();
214 215 216
}


S
Sascha Brawer 已提交
217
/**
B
Behdad Esfahbod 已提交
218 219
 * hb_ot_font_set_funcs:
 *
S
Sascha Brawer 已提交
220 221
 * Since: 0.9.28
 **/
222 223 224
void
hb_ot_font_set_funcs (hb_font_t *font)
{
225
  if (unlikely (!hb_ot_shaper_face_data_ensure (font->face))) return;
226
  hb_ot_face_data_t *ot_font = hb_ot_face_data (font->face);
227

228
  /* Load them lazies.  We access them with get_relaxed() for performance. */
229
  ot_font->cmap.get ();
B
Behdad Esfahbod 已提交
230 231
  ot_font->hmtx.get ();
  ot_font->vmtx.get ();
232

233 234 235
  hb_font_set_funcs (font,
		     _hb_ot_get_font_funcs (),
		     ot_font,
236
		     nullptr);
237
}