2014-03-24 22:26:36 +01:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2014-05-09 21:35:56 +02:00
|
|
|
#include "hb-ot-cmap-table.hh"
|
2016-12-03 00:11:37 +01:00
|
|
|
#include "hb-ot-cbdt-table.hh"
|
2015-08-23 15:42:20 +02:00
|
|
|
#include "hb-ot-glyf-table.hh"
|
|
|
|
#include "hb-ot-head-table.hh"
|
2014-03-24 22:26:36 +01:00
|
|
|
#include "hb-ot-hhea-table.hh"
|
|
|
|
#include "hb-ot-hmtx-table.hh"
|
2015-10-26 08:23:22 +01:00
|
|
|
#include "hb-ot-os2-table.hh"
|
2017-01-23 20:41:43 +01:00
|
|
|
#include "hb-ot-var-hvar-table.hh"
|
2017-10-16 10:05:42 +02:00
|
|
|
#include "hb-ot-post-table.hh"
|
2014-03-24 22:26:36 +01:00
|
|
|
|
|
|
|
|
2014-09-25 16:26:56 +02:00
|
|
|
struct hb_ot_face_metrics_accelerator_t
|
2014-09-25 15:53:24 +02:00
|
|
|
{
|
|
|
|
unsigned int num_metrics;
|
|
|
|
unsigned int num_advances;
|
2014-09-25 16:15:35 +02:00
|
|
|
unsigned int default_advance;
|
2015-10-26 08:23:22 +01:00
|
|
|
unsigned short ascender;
|
|
|
|
unsigned short descender;
|
|
|
|
unsigned short line_gap;
|
2016-10-26 17:19:07 +02:00
|
|
|
bool has_font_extents;
|
2015-10-26 08:23:22 +01:00
|
|
|
|
2017-01-23 05:09:47 +01:00
|
|
|
const OT::hmtxvmtx *table;
|
2014-09-25 15:53:24 +02:00
|
|
|
hb_blob_t *blob;
|
|
|
|
|
2017-01-24 02:34:44 +01:00
|
|
|
const OT::HVARVVAR *var;
|
|
|
|
hb_blob_t *var_blob;
|
|
|
|
|
2014-09-25 16:26:56 +02:00
|
|
|
inline void init (hb_face_t *face,
|
2015-10-26 08:23:22 +01:00
|
|
|
hb_tag_t _hea_tag,
|
|
|
|
hb_tag_t _mtx_tag,
|
2017-01-24 02:34:44 +01:00
|
|
|
hb_tag_t _var_tag,
|
2016-10-26 17:54:04 +02:00
|
|
|
hb_tag_t os2_tag,
|
|
|
|
unsigned int default_advance = 0)
|
2014-09-25 15:53:24 +02:00
|
|
|
{
|
2016-10-26 17:54:04 +02:00
|
|
|
this->default_advance = default_advance ? default_advance : face->get_upem ();
|
2014-09-25 15:53:24 +02:00
|
|
|
|
2015-10-26 08:23:22 +01:00
|
|
|
bool got_font_extents = false;
|
|
|
|
if (os2_tag)
|
|
|
|
{
|
|
|
|
hb_blob_t *os2_blob = OT::Sanitizer<OT::os2>::sanitize (face->reference_table (os2_tag));
|
|
|
|
const OT::os2 *os2 = OT::Sanitizer<OT::os2>::lock_instance (os2_blob);
|
2015-12-10 16:37:49 +01:00
|
|
|
#define USE_TYPO_METRICS (1u<<7)
|
|
|
|
if (0 != (os2->fsSelection & USE_TYPO_METRICS))
|
|
|
|
{
|
|
|
|
this->ascender = os2->sTypoAscender;
|
|
|
|
this->descender = os2->sTypoDescender;
|
|
|
|
this->line_gap = os2->sTypoLineGap;
|
|
|
|
got_font_extents = (this->ascender | this->descender) != 0;
|
|
|
|
}
|
2015-10-26 08:23:22 +01:00
|
|
|
hb_blob_destroy (os2_blob);
|
|
|
|
}
|
|
|
|
|
2014-09-25 16:26:56 +02:00
|
|
|
hb_blob_t *_hea_blob = OT::Sanitizer<OT::_hea>::sanitize (face->reference_table (_hea_tag));
|
2014-09-25 15:53:24 +02:00
|
|
|
const OT::_hea *_hea = OT::Sanitizer<OT::_hea>::lock_instance (_hea_blob);
|
|
|
|
this->num_advances = _hea->numberOfLongMetrics;
|
2015-10-26 08:23:22 +01:00
|
|
|
if (!got_font_extents)
|
|
|
|
{
|
|
|
|
this->ascender = _hea->ascender;
|
|
|
|
this->descender = _hea->descender;
|
|
|
|
this->line_gap = _hea->lineGap;
|
2016-10-26 17:19:07 +02:00
|
|
|
got_font_extents = (this->ascender | this->descender) != 0;
|
2015-10-26 08:23:22 +01:00
|
|
|
}
|
2014-09-25 15:53:24 +02:00
|
|
|
hb_blob_destroy (_hea_blob);
|
|
|
|
|
2016-10-26 17:19:07 +02:00
|
|
|
this->has_font_extents = got_font_extents;
|
|
|
|
|
2017-01-23 05:09:47 +01:00
|
|
|
this->blob = OT::Sanitizer<OT::hmtxvmtx>::sanitize (face->reference_table (_mtx_tag));
|
2015-11-03 00:37:29 +01:00
|
|
|
|
|
|
|
/* Cap num_metrics() and num_advances() based on table length. */
|
|
|
|
unsigned int len = hb_blob_get_length (this->blob);
|
2015-11-03 21:15:12 +01:00
|
|
|
if (unlikely (this->num_advances * 4 > len))
|
2015-11-03 00:37:29 +01:00
|
|
|
this->num_advances = len / 4;
|
|
|
|
this->num_metrics = this->num_advances + (len - 4 * this->num_advances) / 2;
|
|
|
|
|
2015-11-03 21:15:12 +01:00
|
|
|
/* We MUST set num_metrics to zero if num_advances is zero.
|
2015-11-03 00:37:29 +01:00
|
|
|
* Our get_advance() depends on that. */
|
|
|
|
if (unlikely (!this->num_advances))
|
2014-09-25 15:53:24 +02:00
|
|
|
{
|
2014-09-25 16:15:35 +02:00
|
|
|
this->num_metrics = this->num_advances = 0;
|
2014-09-25 15:53:24 +02:00
|
|
|
hb_blob_destroy (this->blob);
|
|
|
|
this->blob = hb_blob_get_empty ();
|
|
|
|
}
|
2017-01-23 05:09:47 +01:00
|
|
|
this->table = OT::Sanitizer<OT::hmtxvmtx>::lock_instance (this->blob);
|
2017-01-24 02:34:44 +01:00
|
|
|
|
|
|
|
this->var_blob = OT::Sanitizer<OT::HVARVVAR>::sanitize (face->reference_table (_var_tag));
|
|
|
|
this->var = OT::Sanitizer<OT::HVARVVAR>::lock_instance (this->var_blob);
|
2014-09-25 15:53:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void fini (void)
|
|
|
|
{
|
|
|
|
hb_blob_destroy (this->blob);
|
2017-01-24 02:34:44 +01:00
|
|
|
hb_blob_destroy (this->var_blob);
|
2014-09-25 15:53:24 +02:00
|
|
|
}
|
|
|
|
|
2017-01-24 02:34:44 +01:00
|
|
|
inline unsigned int get_advance (hb_codepoint_t glyph,
|
|
|
|
hb_font_t *font) const
|
2014-09-25 15:53:24 +02:00
|
|
|
{
|
|
|
|
if (unlikely (glyph >= this->num_metrics))
|
2014-09-25 16:15:35 +02:00
|
|
|
{
|
|
|
|
/* If this->num_metrics is zero, it means we don't have the metrics table
|
2015-08-23 15:42:20 +02:00
|
|
|
* for this direction: return default advance. Otherwise, it means that the
|
|
|
|
* glyph index is out of bound: return zero. */
|
2014-09-25 16:15:35 +02:00
|
|
|
if (this->num_metrics)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return this->default_advance;
|
|
|
|
}
|
2014-09-25 15:53:24 +02:00
|
|
|
|
2017-06-19 12:17:09 +02:00
|
|
|
return this->table->longMetric[MIN (glyph, (uint32_t) this->num_advances - 1)].advance
|
2017-01-24 02:55:31 +01:00
|
|
|
+ this->var->get_advance_var (glyph, font->coords, font->num_coords); // TODO Optimize?!
|
2014-09-25 15:53:24 +02:00
|
|
|
}
|
|
|
|
};
|
2014-03-24 22:26:36 +01:00
|
|
|
|
2015-08-23 15:42:20 +02:00
|
|
|
struct hb_ot_face_glyf_accelerator_t
|
|
|
|
{
|
|
|
|
bool short_offset;
|
|
|
|
unsigned int num_glyphs;
|
|
|
|
const OT::loca *loca;
|
|
|
|
const OT::glyf *glyf;
|
|
|
|
hb_blob_t *loca_blob;
|
|
|
|
hb_blob_t *glyf_blob;
|
|
|
|
unsigned int glyf_len;
|
|
|
|
|
|
|
|
inline void init (hb_face_t *face)
|
|
|
|
{
|
|
|
|
hb_blob_t *head_blob = OT::Sanitizer<OT::head>::sanitize (face->reference_table (HB_OT_TAG_head));
|
|
|
|
const OT::head *head = OT::Sanitizer<OT::head>::lock_instance (head_blob);
|
|
|
|
if ((unsigned int) head->indexToLocFormat > 1 || head->glyphDataFormat != 0)
|
|
|
|
{
|
|
|
|
/* Unknown format. Leave num_glyphs=0, that takes care of disabling us. */
|
|
|
|
hb_blob_destroy (head_blob);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->short_offset = 0 == head->indexToLocFormat;
|
|
|
|
hb_blob_destroy (head_blob);
|
|
|
|
|
|
|
|
this->loca_blob = OT::Sanitizer<OT::loca>::sanitize (face->reference_table (HB_OT_TAG_loca));
|
|
|
|
this->loca = OT::Sanitizer<OT::loca>::lock_instance (this->loca_blob);
|
|
|
|
this->glyf_blob = OT::Sanitizer<OT::glyf>::sanitize (face->reference_table (HB_OT_TAG_glyf));
|
|
|
|
this->glyf = OT::Sanitizer<OT::glyf>::lock_instance (this->glyf_blob);
|
|
|
|
|
|
|
|
this->num_glyphs = MAX (1u, hb_blob_get_length (this->loca_blob) / (this->short_offset ? 2 : 4)) - 1;
|
|
|
|
this->glyf_len = hb_blob_get_length (this->glyf_blob);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void fini (void)
|
|
|
|
{
|
|
|
|
hb_blob_destroy (this->loca_blob);
|
|
|
|
hb_blob_destroy (this->glyf_blob);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool get_extents (hb_codepoint_t glyph,
|
|
|
|
hb_glyph_extents_t *extents) const
|
|
|
|
{
|
|
|
|
if (unlikely (glyph >= this->num_glyphs))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned int start_offset, end_offset;
|
|
|
|
if (this->short_offset)
|
|
|
|
{
|
2015-08-24 14:45:12 +02:00
|
|
|
start_offset = 2 * this->loca->u.shortsZ[glyph];
|
|
|
|
end_offset = 2 * this->loca->u.shortsZ[glyph + 1];
|
2015-08-23 15:42:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start_offset = this->loca->u.longsZ[glyph];
|
|
|
|
end_offset = this->loca->u.longsZ[glyph + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start_offset > end_offset || end_offset > this->glyf_len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (end_offset - start_offset < OT::glyfGlyphHeader::static_size)
|
|
|
|
return true; /* Empty glyph; zero extents. */
|
|
|
|
|
|
|
|
const OT::glyfGlyphHeader &glyph_header = OT::StructAtOffset<OT::glyfGlyphHeader> (this->glyf, start_offset);
|
|
|
|
|
|
|
|
extents->x_bearing = MIN (glyph_header.xMin, glyph_header.xMax);
|
|
|
|
extents->y_bearing = MAX (glyph_header.yMin, glyph_header.yMax);
|
|
|
|
extents->width = MAX (glyph_header.xMin, glyph_header.xMax) - extents->x_bearing;
|
|
|
|
extents->height = MIN (glyph_header.yMin, glyph_header.yMax) - extents->y_bearing;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-03 00:11:37 +01:00
|
|
|
struct hb_ot_face_cbdt_accelerator_t
|
2016-12-03 00:03:50 +01:00
|
|
|
{
|
2016-12-03 04:25:54 +01:00
|
|
|
hb_blob_t *cblc_blob;
|
|
|
|
hb_blob_t *cbdt_blob;
|
|
|
|
const OT::CBLC *cblc;
|
|
|
|
const OT::CBDT *cbdt;
|
2016-12-03 00:03:50 +01:00
|
|
|
|
2016-12-03 07:43:05 +01:00
|
|
|
unsigned int cbdt_len;
|
2017-08-10 02:09:21 +02:00
|
|
|
unsigned int upem;
|
2016-12-03 00:03:50 +01:00
|
|
|
|
|
|
|
inline void init (hb_face_t *face)
|
|
|
|
{
|
2016-12-03 07:43:05 +01:00
|
|
|
upem = face->get_upem();
|
|
|
|
|
|
|
|
cblc_blob = OT::Sanitizer<OT::CBLC>::sanitize (face->reference_table (HB_OT_TAG_CBLC));
|
|
|
|
cbdt_blob = OT::Sanitizer<OT::CBDT>::sanitize (face->reference_table (HB_OT_TAG_CBDT));
|
|
|
|
cbdt_len = hb_blob_get_length (cbdt_blob);
|
2016-12-03 00:03:50 +01:00
|
|
|
|
2016-12-03 07:43:05 +01:00
|
|
|
if (hb_blob_get_length (cblc_blob) == 0) {
|
2017-10-15 12:11:08 +02:00
|
|
|
cblc = nullptr;
|
|
|
|
cbdt = nullptr;
|
2016-12-03 00:03:50 +01:00
|
|
|
return; /* Not a bitmap font. */
|
|
|
|
}
|
2016-12-03 07:43:05 +01:00
|
|
|
cblc = OT::Sanitizer<OT::CBLC>::lock_instance (cblc_blob);
|
|
|
|
cbdt = OT::Sanitizer<OT::CBDT>::lock_instance (cbdt_blob);
|
2016-12-03 00:03:50 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void fini (void)
|
|
|
|
{
|
2016-12-03 04:25:54 +01:00
|
|
|
hb_blob_destroy (this->cblc_blob);
|
|
|
|
hb_blob_destroy (this->cbdt_blob);
|
2016-12-03 00:03:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool get_extents (hb_codepoint_t glyph, hb_glyph_extents_t *extents) const
|
|
|
|
{
|
2016-12-05 04:12:52 +01:00
|
|
|
unsigned int x_ppem = upem, y_ppem = upem; /* TODO Use font ppem if available. */
|
|
|
|
|
2017-08-10 02:09:21 +02:00
|
|
|
if (!cblc)
|
2016-12-03 00:03:50 +01:00
|
|
|
return false; // Not a color bitmap font.
|
|
|
|
|
2016-12-05 04:12:52 +01:00
|
|
|
const OT::IndexSubtableRecord *subtable_record = this->cblc->find_table(glyph, &x_ppem, &y_ppem);
|
2017-08-10 02:09:21 +02:00
|
|
|
if (!subtable_record || !x_ppem || !y_ppem)
|
2016-12-03 00:03:50 +01:00
|
|
|
return false;
|
|
|
|
|
2016-12-03 06:36:42 +01:00
|
|
|
if (subtable_record->get_extents (extents))
|
|
|
|
return true;
|
2016-12-03 00:03:50 +01:00
|
|
|
|
2016-12-03 06:36:42 +01:00
|
|
|
unsigned int image_offset = 0, image_length = 0, image_format = 0;
|
|
|
|
if (!subtable_record->get_image_data (glyph, &image_offset, &image_length, &image_format))
|
|
|
|
return false;
|
2016-12-03 05:12:57 +01:00
|
|
|
|
|
|
|
{
|
2016-12-05 04:43:33 +01:00
|
|
|
/* TODO Move the following into CBDT struct when adding more formats. */
|
2016-12-03 07:43:05 +01:00
|
|
|
|
2016-12-05 04:43:33 +01:00
|
|
|
if (unlikely (image_offset > cbdt_len || cbdt_len - image_offset < image_length))
|
2016-12-03 05:12:57 +01:00
|
|
|
return false;
|
2016-12-05 04:43:33 +01:00
|
|
|
|
|
|
|
switch (image_format)
|
|
|
|
{
|
|
|
|
case 17: {
|
|
|
|
if (unlikely (image_length < OT::GlyphBitmapDataFormat17::min_size))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const OT::GlyphBitmapDataFormat17& glyphFormat17 =
|
|
|
|
OT::StructAtOffset<OT::GlyphBitmapDataFormat17> (this->cbdt, image_offset);
|
|
|
|
glyphFormat17.glyphMetrics.get_extents (extents);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// TODO: Support other image formats.
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-03 00:03:50 +01:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:25:54 +01:00
|
|
|
/* Convert to the font units. */
|
2016-12-05 04:12:52 +01:00
|
|
|
extents->x_bearing *= upem / (float) x_ppem;
|
|
|
|
extents->y_bearing *= upem / (float) y_ppem;
|
|
|
|
extents->width *= upem / (float) x_ppem;
|
|
|
|
extents->height *= upem / (float) y_ppem;
|
2016-12-03 00:03:50 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-16 10:05:42 +02:00
|
|
|
struct hb_ot_face_post_accelerator_t
|
|
|
|
{
|
|
|
|
hb_blob_t *post_blob;
|
|
|
|
unsigned int post_len;
|
|
|
|
const OT::post *post;
|
|
|
|
|
|
|
|
inline void init (hb_face_t *face)
|
|
|
|
{
|
|
|
|
this->post_blob = OT::Sanitizer<OT::post>::sanitize (face->reference_table (HB_OT_TAG_post));
|
|
|
|
this->post = OT::Sanitizer<OT::post>::lock_instance (this->post_blob);
|
|
|
|
this->post_len = hb_blob_get_length (this->post_blob);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void fini (void)
|
|
|
|
{
|
|
|
|
hb_blob_destroy (this->post_blob);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool get_glyph_name (hb_codepoint_t glyph,
|
|
|
|
char *name, unsigned int size) const
|
|
|
|
{
|
|
|
|
if (unlikely (!name) || unlikely(!size))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return this->post->get_glyph_name (glyph, name, size, this->post_len);
|
|
|
|
}
|
2017-10-18 20:49:16 +02:00
|
|
|
|
|
|
|
inline bool get_glyph_from_name (const char *name, int len,
|
|
|
|
hb_codepoint_t *glyph) const
|
|
|
|
{
|
|
|
|
if (unlikely (!name) || unlikely(!len))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return this->post->get_glyph_from_name (name, len, glyph, this->post_len);
|
|
|
|
}
|
2017-10-16 10:05:42 +02:00
|
|
|
};
|
|
|
|
|
2016-02-24 11:52:36 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-07-20 11:35:54 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-25 16:45:49 +02:00
|
|
|
struct hb_ot_face_cmap_accelerator_t
|
|
|
|
{
|
2016-02-24 11:52:36 +01:00
|
|
|
hb_cmap_get_glyph_func_t get_glyph_func;
|
|
|
|
const void *get_glyph_data;
|
2016-02-24 12:27:13 +01:00
|
|
|
OT::CmapSubtableFormat4::accelerator_t format4_accel;
|
2016-02-24 11:52:36 +01:00
|
|
|
|
2016-02-24 11:32:43 +01:00
|
|
|
const OT::CmapSubtableFormat14 *uvs_table;
|
2014-09-25 16:45:49 +02:00
|
|
|
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);
|
2017-10-15 12:11:08 +02:00
|
|
|
const OT::CmapSubtable *subtable = nullptr;
|
|
|
|
const OT::CmapSubtableFormat14 *subtable_uvs = nullptr;
|
2014-09-25 16:45:49 +02:00
|
|
|
|
2016-07-20 11:35:54 +02:00
|
|
|
bool symbol = false;
|
2014-09-25 16:45:49 +02:00
|
|
|
/* 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);
|
2016-12-14 18:56:17 +01:00
|
|
|
if (!subtable)
|
|
|
|
{
|
|
|
|
subtable = cmap->find_subtable (3, 0);
|
|
|
|
if (subtable) symbol = true;
|
|
|
|
}
|
2014-09-25 16:45:49 +02:00
|
|
|
/* Meh. */
|
|
|
|
if (!subtable) subtable = &OT::Null(OT::CmapSubtable);
|
|
|
|
|
|
|
|
/* UVS subtable. */
|
2016-02-24 11:32:43 +01:00
|
|
|
if (!subtable_uvs)
|
|
|
|
{
|
|
|
|
const OT::CmapSubtable *st = cmap->find_subtable (0, 5);
|
|
|
|
if (st && st->u.format == 14)
|
|
|
|
subtable_uvs = &st->u.format14;
|
|
|
|
}
|
2014-09-25 16:45:49 +02:00
|
|
|
/* Meh. */
|
2016-02-24 11:32:43 +01:00
|
|
|
if (!subtable_uvs) subtable_uvs = &OT::Null(OT::CmapSubtableFormat14);
|
2014-09-25 16:45:49 +02:00
|
|
|
|
|
|
|
this->uvs_table = subtable_uvs;
|
2016-02-24 11:52:36 +01:00
|
|
|
|
|
|
|
this->get_glyph_data = subtable;
|
2016-07-20 11:35:54 +02:00
|
|
|
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;
|
2016-02-24 12:27:13 +01:00
|
|
|
}
|
2014-09-25 16:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void fini (void)
|
|
|
|
{
|
|
|
|
hb_blob_destroy (this->blob);
|
|
|
|
}
|
|
|
|
|
2016-02-24 11:05:23 +01:00
|
|
|
inline bool get_nominal_glyph (hb_codepoint_t unicode,
|
|
|
|
hb_codepoint_t *glyph) const
|
2014-09-25 16:45:49 +02:00
|
|
|
{
|
2016-02-24 11:52:36 +01:00
|
|
|
return this->get_glyph_func (this->get_glyph_data, unicode, glyph);
|
2016-02-24 11:05:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2014-09-25 16:45:49 +02:00
|
|
|
{
|
2016-02-24 11:05:23 +01:00
|
|
|
case OT::GLYPH_VARIANT_NOT_FOUND: return false;
|
|
|
|
case OT::GLYPH_VARIANT_FOUND: return true;
|
|
|
|
case OT::GLYPH_VARIANT_USE_DEFAULT: break;
|
2014-09-25 16:45:49 +02:00
|
|
|
}
|
|
|
|
|
2016-02-24 11:05:23 +01:00
|
|
|
return get_nominal_glyph (unicode, glyph);
|
2014-09-25 16:45:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-24 22:26:36 +01:00
|
|
|
struct hb_ot_font_t
|
|
|
|
{
|
2014-09-25 16:45:49 +02:00
|
|
|
hb_ot_face_cmap_accelerator_t cmap;
|
2014-09-25 16:26:56 +02:00
|
|
|
hb_ot_face_metrics_accelerator_t h_metrics;
|
|
|
|
hb_ot_face_metrics_accelerator_t v_metrics;
|
2017-01-10 07:22:37 +01:00
|
|
|
OT::hb_lazy_loader_t<hb_ot_face_glyf_accelerator_t> glyf;
|
|
|
|
OT::hb_lazy_loader_t<hb_ot_face_cbdt_accelerator_t> cbdt;
|
2017-10-16 10:05:42 +02:00
|
|
|
OT::hb_lazy_loader_t<hb_ot_face_post_accelerator_t> post;
|
2014-03-24 22:26:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static hb_ot_font_t *
|
2015-10-07 23:33:02 +02:00
|
|
|
_hb_ot_font_create (hb_face_t *face)
|
2014-03-24 22:26:36 +01:00
|
|
|
{
|
|
|
|
hb_ot_font_t *ot_font = (hb_ot_font_t *) calloc (1, sizeof (hb_ot_font_t));
|
|
|
|
|
|
|
|
if (unlikely (!ot_font))
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2014-03-24 22:26:36 +01:00
|
|
|
|
2014-09-25 16:45:49 +02:00
|
|
|
ot_font->cmap.init (face);
|
2017-01-24 02:34:44 +01:00
|
|
|
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,
|
2016-10-26 17:54:04 +02:00
|
|
|
ot_font->h_metrics.ascender - ot_font->h_metrics.descender); /* TODO Can we do this lazily? */
|
2015-08-23 15:42:20 +02:00
|
|
|
ot_font->glyf.init (face);
|
2016-12-03 00:11:37 +01:00
|
|
|
ot_font->cbdt.init (face);
|
2017-10-16 10:05:42 +02:00
|
|
|
ot_font->post.init (face);
|
2014-05-12 19:46:29 +02:00
|
|
|
|
2014-03-24 22:26:36 +01:00
|
|
|
return ot_font;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-10-11 15:51:31 +02:00
|
|
|
_hb_ot_font_destroy (void *data)
|
2014-03-24 22:26:36 +01:00
|
|
|
{
|
2017-10-11 15:51:31 +02:00
|
|
|
hb_ot_font_t *ot_font = (hb_ot_font_t *) data;
|
|
|
|
|
2014-09-25 16:45:49 +02:00
|
|
|
ot_font->cmap.fini ();
|
2014-09-25 15:53:24 +02:00
|
|
|
ot_font->h_metrics.fini ();
|
|
|
|
ot_font->v_metrics.fini ();
|
2015-10-13 15:32:56 +02:00
|
|
|
ot_font->glyf.fini ();
|
2016-12-03 00:11:37 +01:00
|
|
|
ot_font->cbdt.fini ();
|
2017-10-16 10:05:42 +02:00
|
|
|
ot_font->post.fini ();
|
2014-03-24 22:26:36 +01:00
|
|
|
|
|
|
|
free (ot_font);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static hb_bool_t
|
2016-02-24 11:05:23 +01:00
|
|
|
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);
|
|
|
|
}
|
2014-03-24 22:26:36 +01:00
|
|
|
|
2016-02-24 11:05:23 +01:00
|
|
|
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)
|
2014-03-24 22:26:36 +01:00
|
|
|
{
|
2014-05-12 19:46:29 +02:00
|
|
|
const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
|
2016-02-24 11:05:23 +01:00
|
|
|
return ot_font->cmap.get_variation_glyph (unicode, variation_selector, glyph);
|
2014-03-24 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static hb_position_t
|
2017-01-24 02:34:44 +01:00
|
|
|
hb_ot_get_glyph_h_advance (hb_font_t *font,
|
2014-03-24 22:26:36 +01:00
|
|
|
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;
|
2017-01-24 02:34:44 +01:00
|
|
|
return font->em_scale_x (ot_font->h_metrics.get_advance (glyph, font));
|
2014-03-24 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static hb_position_t
|
2017-01-24 02:34:44 +01:00
|
|
|
hb_ot_get_glyph_v_advance (hb_font_t *font,
|
2014-03-24 22:26:36 +01:00
|
|
|
void *font_data,
|
|
|
|
hb_codepoint_t glyph,
|
|
|
|
void *user_data HB_UNUSED)
|
|
|
|
{
|
2014-09-25 12:04:08 +02:00
|
|
|
const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
|
2017-01-24 02:34:44 +01:00
|
|
|
return font->em_scale_y (-(int) ot_font->v_metrics.get_advance (glyph, font));
|
2014-03-24 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2015-08-23 15:42:20 +02:00
|
|
|
const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
|
2016-05-02 10:24:00 +02:00
|
|
|
bool ret = ot_font->glyf->get_extents (glyph, extents);
|
2016-12-03 04:25:54 +01:00
|
|
|
if (!ret)
|
2016-12-03 00:11:37 +01:00
|
|
|
ret = ot_font->cbdt->get_extents (glyph, extents);
|
2017-01-24 02:55:31 +01:00
|
|
|
// TODO Hook up side-bearings variations.
|
2015-08-23 15:42:20 +02:00
|
|
|
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;
|
2014-03-24 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
2017-10-16 10:05:42 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-10-18 20:49:16 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-10-26 08:23:22 +01:00
|
|
|
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);
|
2017-01-24 02:55:31 +01:00
|
|
|
// TODO Hook up variations.
|
2016-10-26 17:19:07 +02:00
|
|
|
return ot_font->h_metrics.has_font_extents;
|
2015-10-26 08:23:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-01-24 02:55:31 +01:00
|
|
|
// TODO Hook up variations.
|
2016-10-26 17:19:07 +02:00
|
|
|
return ot_font->v_metrics.has_font_extents;
|
2015-10-26 08:23:22 +01:00
|
|
|
}
|
2014-03-24 22:26:36 +01:00
|
|
|
|
2017-10-15 12:11:08 +02:00
|
|
|
static hb_font_funcs_t *static_ot_funcs = nullptr;
|
2015-11-05 05:43:59 +01:00
|
|
|
|
|
|
|
#ifdef HB_USE_ATEXIT
|
|
|
|
static
|
|
|
|
void free_static_ot_funcs (void)
|
|
|
|
{
|
|
|
|
hb_font_funcs_destroy (static_ot_funcs);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-24 22:26:36 +01:00
|
|
|
static hb_font_funcs_t *
|
|
|
|
_hb_ot_get_font_funcs (void)
|
|
|
|
{
|
2015-11-05 05:43:59 +01:00
|
|
|
retry:
|
|
|
|
hb_font_funcs_t *funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ot_funcs);
|
2014-03-24 22:26:36 +01:00
|
|
|
|
2015-11-05 05:43:59 +01:00
|
|
|
if (unlikely (!funcs))
|
|
|
|
{
|
|
|
|
funcs = hb_font_funcs_create ();
|
|
|
|
|
2017-10-15 12:11:08 +02:00
|
|
|
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);
|
|
|
|
//hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ot_get_glyph_h_kerning, nullptr, nullptr); TODO
|
|
|
|
//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
|
2017-10-16 10:05:42 +02:00
|
|
|
hb_font_funcs_set_glyph_name_func (funcs, hb_ot_get_glyph_name, nullptr, nullptr);
|
2017-10-18 20:49:16 +02:00
|
|
|
hb_font_funcs_set_glyph_from_name_func (funcs, hb_ot_get_glyph_from_name, nullptr, nullptr);
|
2015-11-05 05:43:59 +01:00
|
|
|
|
|
|
|
hb_font_funcs_make_immutable (funcs);
|
|
|
|
|
2017-10-15 12:11:08 +02:00
|
|
|
if (!hb_atomic_ptr_cmpexch (&static_ot_funcs, nullptr, funcs)) {
|
2015-11-05 05:43:59 +01:00
|
|
|
hb_font_funcs_destroy (funcs);
|
|
|
|
goto retry;
|
2014-03-24 22:26:36 +01:00
|
|
|
}
|
2015-11-05 05:43:59 +01:00
|
|
|
|
|
|
|
#ifdef HB_USE_ATEXIT
|
|
|
|
atexit (free_static_ot_funcs); /* First person registers atexit() callback. */
|
|
|
|
#endif
|
2014-03-24 22:26:36 +01:00
|
|
|
};
|
|
|
|
|
2015-11-05 05:43:59 +01:00
|
|
|
return funcs;
|
2014-03-24 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-01 13:22:01 +02:00
|
|
|
/**
|
2015-11-27 01:30:37 +01:00
|
|
|
* hb_ot_font_set_funcs:
|
|
|
|
*
|
2015-06-01 13:22:01 +02:00
|
|
|
* Since: 0.9.28
|
|
|
|
**/
|
2014-03-24 22:26:36 +01:00
|
|
|
void
|
|
|
|
hb_ot_font_set_funcs (hb_font_t *font)
|
|
|
|
{
|
2015-10-07 23:33:02 +02:00
|
|
|
hb_ot_font_t *ot_font = _hb_ot_font_create (font->face);
|
2014-03-24 22:26:36 +01:00
|
|
|
if (unlikely (!ot_font))
|
|
|
|
return;
|
|
|
|
|
|
|
|
hb_font_set_funcs (font,
|
|
|
|
_hb_ot_get_font_funcs (),
|
|
|
|
ot_font,
|
2017-10-11 15:51:31 +02:00
|
|
|
_hb_ot_font_destroy);
|
2014-03-24 22:26:36 +01:00
|
|
|
}
|