harfbuzz/src/hb-graphite2.cc

450 lines
12 KiB
C++
Raw Permalink Normal View History

/*
2011-08-24 03:22:49 +02:00
* Copyright © 2011 Martin Hosken
* Copyright © 2011 SIL International
* Copyright © 2011,2012 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.
2011-08-24 03:22:49 +02:00
*
* Google Author(s): Behdad Esfahbod
*/
#include "hb.hh"
#ifdef HAVE_GRAPHITE2
#include "hb-shaper-impl.hh"
2011-08-24 01:47:25 +02:00
#include "hb-graphite2.h"
2013-09-14 02:36:43 +02:00
#include <graphite2/Segment.h>
#include "hb-ot-layout.h"
2018-10-12 02:15:00 +02:00
2018-10-27 13:40:43 +02:00
/**
* SECTION:hb-graphite2
* @title: hb-graphite2
* @short_description: Graphite2 integration
* @include: hb-graphite2.h
*
* Functions for using HarfBuzz with fonts that include Graphite features.
*
* For Graphite features to work, you must be sure that HarfBuzz was compiled
* with the `graphite2` shaping engine enabled. Currently, the default is to
* not enable `graphite2` shaping.
2018-10-27 13:40:43 +02:00
**/
/*
* shaper face data
*/
typedef struct hb_graphite2_tablelist_t
{
struct hb_graphite2_tablelist_t *next;
2012-08-07 20:11:16 +02:00
hb_blob_t *blob;
2011-08-24 03:22:49 +02:00
unsigned int tag;
} hb_graphite2_tablelist_t;
struct hb_graphite2_face_data_t
{
hb_face_t *face;
2011-08-24 03:22:49 +02:00
gr_face *grface;
hb_atomic_ptr_t<hb_graphite2_tablelist_t> tlist;
};
2012-08-07 20:11:16 +02:00
static const void *hb_graphite2_get_table (const void *data, unsigned int tag, size_t *len)
{
2018-08-02 03:03:32 +02:00
hb_graphite2_face_data_t *face_data = (hb_graphite2_face_data_t *) data;
hb_graphite2_tablelist_t *tlist = face_data->tlist;
2012-08-07 20:11:16 +02:00
2017-10-15 12:11:08 +02:00
hb_blob_t *blob = nullptr;
2012-08-07 20:11:16 +02:00
for (hb_graphite2_tablelist_t *p = tlist; p; p = p->next)
if (p->tag == tag) {
blob = p->blob;
break;
}
if (unlikely (!blob))
2011-08-24 03:22:49 +02:00
{
2012-08-08 23:44:19 +02:00
blob = face_data->face->reference_table (tag);
2012-08-07 20:11:16 +02:00
hb_graphite2_tablelist_t *p = (hb_graphite2_tablelist_t *) hb_calloc (1, sizeof (hb_graphite2_tablelist_t));
2012-08-07 20:11:16 +02:00
if (unlikely (!p)) {
2011-08-26 09:33:06 +02:00
hb_blob_destroy (blob);
2017-10-15 12:11:08 +02:00
return nullptr;
2011-08-24 03:22:49 +02:00
}
2012-08-07 20:11:16 +02:00
p->blob = blob;
p->tag = tag;
retry:
hb_graphite2_tablelist_t *tlist = face_data->tlist;
p->next = tlist;
if (unlikely (!face_data->tlist.cmpexch (tlist, p)))
goto retry;
2011-08-24 03:22:49 +02:00
}
unsigned int tlen;
const char *d = hb_blob_get_data (blob, &tlen);
*len = tlen;
return d;
}
2018-08-02 03:03:32 +02:00
hb_graphite2_face_data_t *
_hb_graphite2_shaper_face_data_create (hb_face_t *face)
{
2012-08-08 23:44:19 +02:00
hb_blob_t *silf_blob = face->reference_table (HB_GRAPHITE2_TAG_SILF);
2012-08-07 04:42:47 +02:00
/* Umm, we just reference the table to check whether it exists.
* Maybe add better API for this? */
if (!hb_blob_get_length (silf_blob))
{
hb_blob_destroy (silf_blob);
2017-10-15 12:11:08 +02:00
return nullptr;
}
2012-08-07 04:42:47 +02:00
hb_blob_destroy (silf_blob);
hb_graphite2_face_data_t *data = (hb_graphite2_face_data_t *) hb_calloc (1, sizeof (hb_graphite2_face_data_t));
2012-08-07 04:42:47 +02:00
if (unlikely (!data))
2017-10-15 12:11:08 +02:00
return nullptr;
data->face = face;
const gr_face_ops ops = {sizeof(gr_face_ops), &hb_graphite2_get_table, NULL};
data->grface = gr_make_face_with_ops (data, &ops, gr_face_preloadAll);
2012-08-07 04:42:47 +02:00
if (unlikely (!data->grface)) {
hb_free (data);
2017-10-15 12:11:08 +02:00
return nullptr;
2012-08-07 04:42:47 +02:00
}
return data;
}
void
2018-08-02 03:03:32 +02:00
_hb_graphite2_shaper_face_data_destroy (hb_graphite2_face_data_t *data)
{
hb_graphite2_tablelist_t *tlist = data->tlist;
2011-08-24 03:22:49 +02:00
while (tlist)
{
hb_graphite2_tablelist_t *old = tlist;
2011-08-24 03:22:49 +02:00
hb_blob_destroy (tlist->blob);
tlist = tlist->next;
hb_free (old);
2011-08-24 03:22:49 +02:00
}
gr_face_destroy (data->grface);
hb_free (data);
}
/**
* hb_graphite2_face_get_gr_face: (skip)
* @face: @hb_face_t to query
*
* Fetches the Graphite2 gr_face corresponding to the specified
* #hb_face_t face object.
*
* Return value: the gr_face found
*
* Since: 0.9.10
*/
gr_face *
hb_graphite2_face_get_gr_face (hb_face_t *face)
{
const hb_graphite2_face_data_t *data = face->data.graphite2;
return data ? data->grface : nullptr;
}
2011-08-24 03:22:49 +02:00
/*
* shaper font data
*/
2011-08-24 03:22:49 +02:00
2018-08-02 03:03:32 +02:00
struct hb_graphite2_font_data_t {};
2011-08-24 03:22:49 +02:00
2018-08-02 03:03:32 +02:00
hb_graphite2_font_data_t *
_hb_graphite2_shaper_font_data_create (hb_font_t *font HB_UNUSED)
{
2018-08-02 03:03:32 +02:00
return (hb_graphite2_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
}
void
2018-08-02 03:03:32 +02:00
_hb_graphite2_shaper_font_data_destroy (hb_graphite2_font_data_t *data HB_UNUSED)
{
}
2011-08-24 03:22:49 +02:00
#ifndef HB_DISABLE_DEPRECATED
/**
* hb_graphite2_font_get_gr_font: (skip)
* @font: An #hb_font_t
*
* Always returns `NULL`. Use hb_graphite2_face_get_gr_face() instead.
*
* Return value: (nullable): Graphite2 font associated with @font.
*
* Since: 0.9.10
* Deprecated: 1.4.2
*/
gr_font *
hb_graphite2_font_get_gr_font (hb_font_t *font HB_UNUSED)
{
2017-10-15 12:11:08 +02:00
return nullptr;
}
#endif
2011-08-24 03:22:49 +02:00
/*
* shaper
*/
struct hb_graphite2_cluster_t {
unsigned int base_char;
unsigned int num_chars;
unsigned int base_glyph;
unsigned int num_glyphs;
unsigned int cluster;
hb_graphite2_cluster_t::advance can apparently be negative ...as seen with HarfBuzz used by LibreOffice, with `instdir/program/soffice --headless --convert-to pdf` of doc/abi6073-2.doc from the LibreOffice crash- testing corpus when run under UBSan, > hb-graphite2.cc:361:15: runtime error: -1024 is outside the range of representable values of type 'unsigned int' > #0 in _hb_graphite2_shape at workdir/UnpackedTarball/harfbuzz/src/hb-graphite2.cc:361:15 > #1 in _hb_shape_plan_execute_internal(hb_shape_plan_t*, hb_font_t*, hb_buffer_t*, hb_feature_t const*, unsigned int) at workdir/UnpackedTarball/harfbuzz/src/./hb-shaper-list.hh:38:1 > #2 in hb_shape_plan_execute at workdir/UnpackedTarball/harfbuzz/src/hb-shape-plan.cc:453:14 > #3 in hb_shape_full at workdir/UnpackedTarball/harfbuzz/src/hb-shape.cc:139:19 > #4 in GenericSalLayout::LayoutText(ImplLayoutArgs&, SalLayoutGlyphsImpl const*) at vcl/source/gdi/CommonSalLayout.cxx:495:23 > #5 in OutputDevice::getFallbackLayout(LogicalFontInstance*, int, ImplLayoutArgs&, SalLayoutGlyphs const*) const at vcl/source/outdev/font.cxx:1232:21 > #6 in OutputDevice::ImplGlyphFallbackLayout(std::unique_ptr<SalLayout, std::default_delete<SalLayout> >, ImplLayoutArgs&, SalLayoutGlyphs const*) const at vcl/source/outdev/font.cxx:1300:48 > #7 in OutputDevice::ImplLayout(rtl::OUString const&, int, int, Point const&, long, long const*, SalLayoutFlags, vcl::TextLayoutCache const*, SalLayoutGlyphs const*) const at vcl/source/outdev/text.cxx:1332:22 > #8 in lcl_CreateLayout(SwTextGlyphsKey const&, __gnu_debug::_Safe_iterator<std::_Rb_tree_iterator<std::pair<SwTextGlyphsKey const, SwTextGlyphsData> >, std::__debug::map<SwTextGlyphsKey, SwTextGlyphsData, std::less<SwTextGlyphsKey>, std::allocator<std::pair<SwTextGlyphsKey const, SwTextGlyphsData> > >, std::bidirectional_iterator_tag>) at sw/source/core/txtnode/fntcache.cxx:233:33 > #9 in SwFntObj::GetCachedSalLayoutGlyphs(SwTextGlyphsKey const&) at sw/source/core/txtnode/fntcache.cxx:257:12 > #10 in SwFont::GetTextBreak(SwDrawTextInfo const&, long) at sw/source/core/txtnode/fntcache.cxx:2551:58 > #11 in SwTextSizeInfo::GetTextBreak(long, o3tl::strong_int<int, Tag_TextFrameIndex>, unsigned short, vcl::TextLayoutCache const*) const at sw/source/core/text/inftxt.cxx:450:20 > #12 in SwTextGuess::Guess(SwTextPortion const&, SwTextFormatInfo&, unsigned short) at sw/source/core/text/guess.cxx:205:26 > #13 in SwTextPortion::Format_(SwTextFormatInfo&) at sw/source/core/text/portxt.cxx:305:32 > #14 in SwTextPortion::Format(SwTextFormatInfo&) at sw/source/core/text/portxt.cxx:456:12 > #15 in SwLineLayout::Format(SwTextFormatInfo&) at sw/source/core/text/porlay.cxx:260:31 (where in frame #4 GenericSalLayout::LayoutText, pHbBuffer->props.direction is HB_DIRECTION_RTL, in case that is relevant). It is unclear to me whether it is sufficient to only change hb_graphite2_cluster_t::advance from signed to unsigned int, as there are other unsigned int variables (like curradv in _hb_graphite2_shape) whose value depend on hb_graphite2_cluster_t::advance, and which thus might also become negative. But unlike the float -> unsigned int conversion that UBSan warned about here (where gr_slot_origin_X() and xscale are float), those are signed int -> unsigned int conversions that do not cause undefined behavior. At least, with this change, the above --convert-to pdf and a full `make check screenshot` succeeded for me under without further UBSan warnings. (For the version of HarfBuzz optionally built as part of the LibreOffice build, this has been addressed with <https://git.libreoffice.org/core/+/6e53e03f752c2f85283c4d47efaaf0683299783c%5E!/> "external/harfbuzz: hb_graphite2_cluster_t::advance can apparently be negative.")
2021-08-09 17:17:48 +02:00
int advance;
};
2011-08-24 03:22:49 +02:00
hb_bool_t
_hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED,
hb_font_t *font,
hb_buffer_t *buffer,
const hb_feature_t *features,
unsigned int num_features)
{
hb_face_t *face = font->face;
gr_face *grface = face->data.graphite2->grface;
2011-08-24 03:22:49 +02:00
const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
2017-10-15 12:11:08 +02:00
const char *lang_end = lang ? strchr (lang, '-') : nullptr;
int lang_len = lang_end ? lang_end - lang : -1;
gr_feature_val *feats = gr_face_featureval_for_lang (grface, lang ? hb_tag_from_string (lang, lang_len) : 0);
2011-08-24 03:22:49 +02:00
for (unsigned int i = 0; i < num_features; i++)
2011-08-24 03:22:49 +02:00
{
const gr_feature_ref *fref = gr_face_find_fref (grface, features[i].tag);
2011-08-24 03:22:49 +02:00
if (fref)
gr_fref_set_feature_value (fref, features[i].value, feats);
2011-08-24 03:22:49 +02:00
}
2017-10-15 12:11:08 +02:00
gr_segment *seg = nullptr;
2011-08-24 03:22:49 +02:00
const gr_slot *is;
unsigned int ci = 0, ic = 0;
unsigned int curradvx = 0, curradvy = 0;
2011-08-24 03:22:49 +02:00
unsigned int scratch_size;
2013-11-13 20:44:01 +01:00
hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
2013-11-12 23:22:49 +01:00
uint32_t *chars = (uint32_t *) scratch;
2011-08-24 03:22:49 +02:00
for (unsigned int i = 0; i < buffer->len; ++i)
chars[i] = buffer->info[i].codepoint;
2011-08-24 03:22:49 +02:00
/* TODO ensure_native_direction. */
2018-10-12 02:37:49 +02:00
hb_tag_t script_tag[HB_OT_MAX_TAGS_PER_SCRIPT];
unsigned int count = HB_OT_MAX_TAGS_PER_SCRIPT;
hb_ot_tags_from_script_and_language (hb_buffer_get_script (buffer),
HB_LANGUAGE_INVALID,
&count,
script_tag,
nullptr, nullptr);
2011-08-24 03:22:49 +02:00
2017-10-15 12:11:08 +02:00
seg = gr_make_seg (nullptr, grface,
2018-10-12 02:15:00 +02:00
count ? script_tag[count - 1] : HB_OT_TAG_DEFAULT_SCRIPT,
2011-08-24 03:22:49 +02:00
feats,
gr_utf32, chars, buffer->len,
2011-08-26 09:33:06 +02:00
2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0));
2011-08-24 03:22:49 +02:00
if (unlikely (!seg)) {
if (feats) gr_featureval_destroy (feats);
return false;
}
unsigned int glyph_count = gr_seg_n_slots (seg);
if (unlikely (!glyph_count)) {
if (feats) gr_featureval_destroy (feats);
gr_seg_destroy (seg);
buffer->len = 0;
return true;
}
2021-03-15 20:43:29 +01:00
(void) buffer->ensure (glyph_count);
2013-11-12 23:22:49 +01:00
scratch = buffer->get_scratch_buffer (&scratch_size);
while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) +
DIV_CEIL (sizeof (hb_codepoint_t) * glyph_count, sizeof (*scratch))) > scratch_size)
{
2014-08-11 19:42:42 +02:00
if (unlikely (!buffer->ensure (buffer->allocated * 2)))
{
if (feats) gr_featureval_destroy (feats);
gr_seg_destroy (seg);
return false;
}
2013-11-12 23:22:49 +01:00
scratch = buffer->get_scratch_buffer (&scratch_size);
}
#define ALLOCATE_ARRAY(Type, name, len) \
Type *name = (Type *) scratch; \
2019-05-14 02:28:59 +02:00
do { \
2013-11-12 23:22:49 +01:00
unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
assert (_consumed <= scratch_size); \
scratch += _consumed; \
scratch_size -= _consumed; \
2019-05-14 02:28:59 +02:00
} while (0)
2011-08-24 03:22:49 +02:00
ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len);
ALLOCATE_ARRAY (hb_codepoint_t, gids, glyph_count);
2011-08-24 03:22:49 +02:00
2013-11-12 23:22:49 +01:00
#undef ALLOCATE_ARRAY
hb_memset (clusters, 0, sizeof (clusters[0]) * buffer->len);
hb_codepoint_t *pg = gids;
clusters[0].cluster = buffer->info[0].cluster;
unsigned int upem = hb_face_get_upem (face);
float xscale = (float) font->x_scale / upem;
float yscale = (float) font->y_scale / upem;
yscale *= yscale / xscale;
unsigned int curradv = 0;
if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
{
curradv = gr_slot_origin_X(gr_seg_first_slot(seg)) * xscale;
clusters[0].advance = gr_seg_advance_X(seg) * xscale - curradv;
}
else
clusters[0].advance = 0;
2011-08-24 03:22:49 +02:00
for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++)
{
unsigned int before = gr_slot_before (is);
unsigned int after = gr_slot_after (is);
*pg = gr_slot_gid (is);
pg++;
while (clusters[ci].base_char > before && ci)
{
2011-08-24 03:22:49 +02:00
clusters[ci-1].num_chars += clusters[ci].num_chars;
clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
clusters[ci-1].advance += clusters[ci].advance;
2011-08-26 09:27:13 +02:00
ci--;
}
2011-08-24 03:22:49 +02:00
if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
{
hb_graphite2_cluster_t *c = clusters + ci + 1;
2011-08-24 03:22:49 +02:00
c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
c->cluster = buffer->info[c->base_char].cluster;
2011-08-24 03:22:49 +02:00
c->num_chars = before - c->base_char;
c->base_glyph = ic;
c->num_glyphs = 0;
if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
{
c->advance = curradv - gr_slot_origin_X(is) * xscale;
curradv -= c->advance;
}
2017-04-20 20:13:22 +02:00
else
{
c->advance = 0;
clusters[ci].advance += gr_slot_origin_X(is) * xscale - curradv;
curradv += clusters[ci].advance;
}
2017-04-20 20:13:22 +02:00
ci++;
}
2011-08-26 09:27:13 +02:00
clusters[ci].num_glyphs++;
2011-08-24 03:22:49 +02:00
if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
}
2017-04-20 20:13:22 +02:00
if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
clusters[ci].advance += curradv;
else
clusters[ci].advance += gr_seg_advance_X(seg) * xscale - curradv;
2011-08-26 09:27:13 +02:00
ci++;
2011-08-24 03:22:49 +02:00
2011-08-26 09:27:13 +02:00
for (unsigned int i = 0; i < ci; ++i)
{
for (unsigned int j = 0; j < clusters[i].num_glyphs; ++j)
{
hb_glyph_info_t *info = &buffer->info[clusters[i].base_glyph + j];
info->codepoint = gids[clusters[i].base_glyph + j];
info->cluster = clusters[i].cluster;
info->var1.i32 = clusters[i].advance; // all glyphs in the cluster get the same advance
}
}
buffer->len = glyph_count;
2011-08-24 03:22:49 +02:00
/* Positioning. */
unsigned int currclus = UINT_MAX;
2017-04-20 20:13:22 +02:00
const hb_glyph_info_t *info = buffer->info;
2017-10-15 12:11:08 +02:00
hb_glyph_position_t *pPos = hb_buffer_get_glyph_positions (buffer, nullptr);
if (!HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
2011-08-24 03:22:49 +02:00
{
curradvx = 0;
for (is = gr_seg_first_slot (seg); is; pPos++, ++info, is = gr_slot_next_in_segment (is))
{
pPos->x_offset = gr_slot_origin_X (is) * xscale - curradvx;
2015-11-23 04:03:56 +01:00
pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
if (info->cluster != currclus) {
pPos->x_advance = info->var1.i32;
curradvx += pPos->x_advance;
currclus = info->cluster;
} else
pPos->x_advance = 0.;
2017-10-15 12:11:08 +02:00
pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
curradvy += pPos->y_advance;
}
}
else
{
curradvx = gr_seg_advance_X(seg) * xscale;
for (is = gr_seg_first_slot (seg); is; pPos++, info++, is = gr_slot_next_in_segment (is))
{
if (info->cluster != currclus)
{
pPos->x_advance = info->var1.i32;
curradvx -= pPos->x_advance;
currclus = info->cluster;
} else
pPos->x_advance = 0.;
2017-10-15 12:11:08 +02:00
pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
curradvy -= pPos->y_advance;
pPos->x_offset = gr_slot_origin_X (is) * xscale - info->var1.i32 - curradvx + pPos->x_advance;
2015-11-23 04:03:56 +01:00
pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
}
hb_buffer_reverse_clusters (buffer);
}
2012-08-07 04:42:47 +02:00
if (feats) gr_featureval_destroy (feats);
gr_seg_destroy (seg);
buffer->clear_glyph_flags ();
buffer->unsafe_to_break ();
return true;
}
#endif