hb-ot-font.cc 12.9 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 27 28 29 30 31 32
/*
 * 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
 */

#include "hb-private.hh"

#include "hb-ot.h"

#include "hb-font-private.hh"

33
#include "hb-ot-cmap-table.hh"
34
#include "hb-ot-cbdt-table.hh"
35 36
#include "hb-ot-glyf-table.hh"
#include "hb-ot-head-table.hh"
37 38
#include "hb-ot-hhea-table.hh"
#include "hb-ot-hmtx-table.hh"
39
#include "hb-ot-kern-table.hh"
40
#include "hb-ot-post-table.hh"
41 42


43 44 45 46 47 48 49 50 51 52 53 54 55
typedef bool (*hb_cmap_get_glyph_func_t) (const void *obj,
					  hb_codepoint_t codepoint,
					  hb_codepoint_t *glyph);

template <typename Type>
static inline bool get_glyph_from (const void *obj,
				   hb_codepoint_t codepoint,
				   hb_codepoint_t *glyph)
{
  const Type *typed_obj = (const Type *) obj;
  return typed_obj->get_glyph (codepoint, glyph);
}

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
template <typename Type>
static inline bool get_glyph_from_symbol (const void *obj,
					  hb_codepoint_t codepoint,
					  hb_codepoint_t *glyph)
{
  const Type *typed_obj = (const Type *) obj;
  if (likely (typed_obj->get_glyph (codepoint, glyph)))
    return true;

  if (codepoint <= 0x00FFu)
  {
    /* For symbol-encoded OpenType fonts, we duplicate the
     * U+F000..F0FF range at U+0000..U+00FF.  That's what
     * Windows seems to do, and that's hinted about at:
     * http://www.microsoft.com/typography/otspec/recom.htm
     * under "Non-Standard (Symbol) Fonts". */
    return typed_obj->get_glyph (0xF000u + codepoint, glyph);
  }

  return false;
}

78 79
struct hb_ot_face_cmap_accelerator_t
{
80 81
  hb_cmap_get_glyph_func_t get_glyph_func;
  const void *get_glyph_data;
82
  OT::CmapSubtableFormat4::accelerator_t format4_accel;
83

84
  const OT::CmapSubtableFormat14 *uvs_table;
85 86 87 88 89 90
  hb_blob_t *blob;

  inline void init (hb_face_t *face)
  {
    this->blob = OT::Sanitizer<OT::cmap>::sanitize (face->reference_table (HB_OT_TAG_cmap));
    const OT::cmap *cmap = OT::Sanitizer<OT::cmap>::lock_instance (this->blob);
B
Behdad Esfahbod 已提交
91 92
    const OT::CmapSubtable *subtable = nullptr;
    const OT::CmapSubtableFormat14 *subtable_uvs = nullptr;
93

94
    bool symbol = false;
95 96 97 98 99 100 101 102 103 104
    /* 32-bit subtables. */
    if (!subtable) subtable = cmap->find_subtable (3, 10);
    if (!subtable) subtable = cmap->find_subtable (0, 6);
    if (!subtable) subtable = cmap->find_subtable (0, 4);
    /* 16-bit subtables. */
    if (!subtable) subtable = cmap->find_subtable (3, 1);
    if (!subtable) subtable = cmap->find_subtable (0, 3);
    if (!subtable) subtable = cmap->find_subtable (0, 2);
    if (!subtable) subtable = cmap->find_subtable (0, 1);
    if (!subtable) subtable = cmap->find_subtable (0, 0);
105 106 107 108 109
    if (!subtable)
    {
      subtable = cmap->find_subtable (3, 0);
      if (subtable) symbol = true;
    }
110 111 112 113
    /* Meh. */
    if (!subtable) subtable = &OT::Null(OT::CmapSubtable);

    /* UVS subtable. */
114 115 116 117 118 119
    if (!subtable_uvs)
    {
      const OT::CmapSubtable *st = cmap->find_subtable (0, 5);
      if (st && st->u.format == 14)
        subtable_uvs = &st->u.format14;
    }
120
    /* Meh. */
121
    if (!subtable_uvs) subtable_uvs = &OT::Null(OT::CmapSubtableFormat14);
122 123

    this->uvs_table = subtable_uvs;
124 125

    this->get_glyph_data = subtable;
126 127 128 129 130 131 132 133 134 135 136 137 138 139
    if (unlikely (symbol))
      this->get_glyph_func = get_glyph_from_symbol<OT::CmapSubtable>;
    else
      switch (subtable->u.format) {
      /* Accelerate format 4 and format 12. */
      default: this->get_glyph_func = get_glyph_from<OT::CmapSubtable>;		break;
      case 12: this->get_glyph_func = get_glyph_from<OT::CmapSubtableFormat12>;	break;
      case  4:
	{
	  this->format4_accel.init (&subtable->u.format4);
	  this->get_glyph_data = &this->format4_accel;
	  this->get_glyph_func = this->format4_accel.get_glyph_func;
	}
	break;
140
      }
141 142 143 144 145 146 147
  }

  inline void fini (void)
  {
    hb_blob_destroy (this->blob);
  }

148 149
  inline bool get_nominal_glyph (hb_codepoint_t  unicode,
				 hb_codepoint_t *glyph) const
150
  {
151
    return this->get_glyph_func (this->get_glyph_data, unicode, glyph);
152 153 154 155 156 157 158 159 160
  }

  inline bool get_variation_glyph (hb_codepoint_t  unicode,
				   hb_codepoint_t  variation_selector,
				   hb_codepoint_t *glyph) const
  {
    switch (this->uvs_table->get_glyph_variant (unicode,
						variation_selector,
						glyph))
161
    {
162 163 164
      case OT::GLYPH_VARIANT_NOT_FOUND:		return false;
      case OT::GLYPH_VARIANT_FOUND:		return true;
      case OT::GLYPH_VARIANT_USE_DEFAULT:	break;
165 166
    }

167
    return get_nominal_glyph (unicode, glyph);
168 169 170
  }
};

171 172
struct hb_ot_font_t
{
173
  hb_ot_face_cmap_accelerator_t cmap;
174 175
  OT::hmtxvmtx::accelerator_t h_metrics;
  OT::hmtxvmtx::accelerator_t v_metrics;
176
  OT::hb_lazy_loader_t<OT::glyf::accelerator_t> glyf;
177
  OT::hb_lazy_loader_t<OT::CBDT::accelerator_t> cbdt;
178
  OT::hb_lazy_loader_t<OT::post::accelerator_t> post;
179
  OT::hb_lazy_loader_t<OT::kern::accelerator_t> kern;
180 181 182 183
};


static hb_ot_font_t *
B
Minor  
Behdad Esfahbod 已提交
184
_hb_ot_font_create (hb_face_t *face)
185 186 187 188
{
  hb_ot_font_t *ot_font = (hb_ot_font_t *) calloc (1, sizeof (hb_ot_font_t));

  if (unlikely (!ot_font))
B
Behdad Esfahbod 已提交
189
    return nullptr;
190

191
  ot_font->cmap.init (face);
192 193
  ot_font->h_metrics.init (face, HB_OT_TAG_hhea, HB_OT_TAG_hmtx, HB_OT_TAG_HVAR, HB_OT_TAG_os2);
  ot_font->v_metrics.init (face, HB_OT_TAG_vhea, HB_OT_TAG_vmtx, HB_OT_TAG_VVAR, HB_TAG_NONE,
194
			   ot_font->h_metrics.ascender - ot_font->h_metrics.descender); /* TODO Can we do this lazily? */
195
  ot_font->glyf.init (face);
196
  ot_font->cbdt.init (face);
197
  ot_font->post.init (face);
B
Behdad Esfahbod 已提交
198
  ot_font->kern.init (face);
199

200 201 202 203
  return ot_font;
}

static void
204
_hb_ot_font_destroy (void *data)
205
{
206 207
  hb_ot_font_t *ot_font = (hb_ot_font_t *) data;

208
  ot_font->cmap.fini ();
B
Behdad Esfahbod 已提交
209 210
  ot_font->h_metrics.fini ();
  ot_font->v_metrics.fini ();
B
Behdad Esfahbod 已提交
211
  ot_font->glyf.fini ();
212
  ot_font->cbdt.fini ();
213
  ot_font->post.fini ();
B
Behdad Esfahbod 已提交
214
  ot_font->kern.fini ();
215 216 217 218 219 220

  free (ot_font);
}


static hb_bool_t
221 222 223 224 225 226 227 228 229 230
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)

{
  const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
  return ot_font->cmap.get_nominal_glyph (unicode, glyph);
}
231

232 233 234 235 236 237 238
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)
239
{
240
  const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
241
  return ot_font->cmap.get_variation_glyph (unicode, variation_selector, glyph);
242 243 244
}

static hb_position_t
245
hb_ot_get_glyph_h_advance (hb_font_t *font,
246 247 248 249 250
			   void *font_data,
			   hb_codepoint_t glyph,
			   void *user_data HB_UNUSED)
{
  const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
251
  return font->em_scale_x (ot_font->h_metrics.get_advance (glyph, font));
252 253 254
}

static hb_position_t
255
hb_ot_get_glyph_v_advance (hb_font_t *font,
256 257 258 259
			   void *font_data,
			   hb_codepoint_t glyph,
			   void *user_data HB_UNUSED)
{
260
  const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
261
  return font->em_scale_y (-(int) ot_font->v_metrics.get_advance (glyph, font));
262 263
}

B
Behdad Esfahbod 已提交
264 265 266 267 268 269 270 271 272 273 274
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)
{
  const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
  return font->em_scale_x (ot_font->kern->get_h_kerning (left_glyph, right_glyph));
}

275 276 277 278 279 280 281
static hb_bool_t
hb_ot_get_glyph_extents (hb_font_t *font HB_UNUSED,
			 void *font_data,
			 hb_codepoint_t glyph,
			 hb_glyph_extents_t *extents,
			 void *user_data HB_UNUSED)
{
282
  const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
283
  bool ret = ot_font->glyf->get_extents (glyph, extents);
284
  if (!ret)
285
    ret = ot_font->cbdt->get_extents (glyph, extents);
286
  // TODO Hook up side-bearings variations.
287 288 289 290 291
  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;
292 293
}

294 295 296 297 298 299 300 301 302 303 304
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)
{
  const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
  return ot_font->post->get_glyph_name (glyph, name, size);
}

305 306 307 308 309 310 311 312 313 314 315
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)
{
  const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
  return ot_font->post->get_glyph_from_name (name, len, glyph);
}

316 317 318 319 320 321 322 323 324 325
static hb_bool_t
hb_ot_get_font_h_extents (hb_font_t *font HB_UNUSED,
			  void *font_data,
			  hb_font_extents_t *metrics,
			  void *user_data HB_UNUSED)
{
  const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
  metrics->ascender = font->em_scale_y (ot_font->h_metrics.ascender);
  metrics->descender = font->em_scale_y (ot_font->h_metrics.descender);
  metrics->line_gap = font->em_scale_y (ot_font->h_metrics.line_gap);
326
  // TODO Hook up variations.
B
Behdad Esfahbod 已提交
327
  return ot_font->h_metrics.has_font_extents;
328 329 330 331 332 333 334 335 336 337 338 339
}

static hb_bool_t
hb_ot_get_font_v_extents (hb_font_t *font HB_UNUSED,
			  void *font_data,
			  hb_font_extents_t *metrics,
			  void *user_data HB_UNUSED)
{
  const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
  metrics->ascender = font->em_scale_x (ot_font->v_metrics.ascender);
  metrics->descender = font->em_scale_x (ot_font->v_metrics.descender);
  metrics->line_gap = font->em_scale_x (ot_font->v_metrics.line_gap);
340
  // TODO Hook up variations.
B
Behdad Esfahbod 已提交
341
  return ot_font->v_metrics.has_font_extents;
342
}
343

B
Behdad Esfahbod 已提交
344
static hb_font_funcs_t *static_ot_funcs = nullptr;
345 346 347 348 349 350 351 352 353

#ifdef HB_USE_ATEXIT
static
void free_static_ot_funcs (void)
{
  hb_font_funcs_destroy (static_ot_funcs);
}
#endif

354 355 356
static hb_font_funcs_t *
_hb_ot_get_font_funcs (void)
{
357 358
retry:
  hb_font_funcs_t *funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ot_funcs);
359

360 361 362 363
  if (unlikely (!funcs))
  {
    funcs = hb_font_funcs_create ();

B
Behdad Esfahbod 已提交
364 365 366 367 368 369 370 371
    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);
372
    hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ot_get_glyph_h_kerning, nullptr, nullptr);
B
Behdad Esfahbod 已提交
373 374 375
    //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);
    //hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ot_get_glyph_contour_point, nullptr, nullptr); TODO
376
    hb_font_funcs_set_glyph_name_func (funcs, hb_ot_get_glyph_name, nullptr, nullptr);
377
    hb_font_funcs_set_glyph_from_name_func (funcs, hb_ot_get_glyph_from_name, nullptr, nullptr);
378 379 380

    hb_font_funcs_make_immutable (funcs);

B
Behdad Esfahbod 已提交
381
    if (!hb_atomic_ptr_cmpexch (&static_ot_funcs, nullptr, funcs)) {
382 383
      hb_font_funcs_destroy (funcs);
      goto retry;
384
    }
385 386 387 388

#ifdef HB_USE_ATEXIT
    atexit (free_static_ot_funcs); /* First person registers atexit() callback. */
#endif
389 390
  };

391
  return funcs;
392 393 394
}


S
Sascha Brawer 已提交
395
/**
B
Behdad Esfahbod 已提交
396 397
 * hb_ot_font_set_funcs:
 *
S
Sascha Brawer 已提交
398 399
 * Since: 0.9.28
 **/
400 401 402
void
hb_ot_font_set_funcs (hb_font_t *font)
{
B
Minor  
Behdad Esfahbod 已提交
403
  hb_ot_font_t *ot_font = _hb_ot_font_create (font->face);
404 405 406 407 408 409
  if (unlikely (!ot_font))
    return;

  hb_font_set_funcs (font,
		     _hb_ot_get_font_funcs (),
		     ot_font,
410
		     _hb_ot_font_destroy);
411
}