blob: 47e264992e4fc16fce75202a65e37344f4418c1c [file]
/*
* Copyright 2022 Rive
*/
#include "rive/text_engine.hpp"
#ifdef WITH_RIVE_TEXT
#include "rive/text/font_hb.hpp"
#include "rive/factory.hpp"
#include "rive/renderer_utils.hpp"
#include "rive/shapes/paint/color.hpp"
#include "rive/text/text_language.hpp"
#include "hb.h"
#include "hb-ot.h"
#include <algorithm>
#include <atomic>
#include <cstring>
#include <mutex>
#include <unordered_set>
// harfbuzz's own file loader is compiled out (HB_NO_OPEN/HB_NO_MMAP keep it
// out of the wasm builds), so map the file ourselves and hand harfbuzz a
// borrowed pointer. RIVE_HB_FILE_MAPPING is defined in font_hb.hpp.
#ifdef RIVE_HB_FILE_MAPPING
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
extern "C"
{
#include "SheenBidi.h"
}
// Initialized to null. Client can set this to a callback.
rive::Font::FallbackProc rive::Font::gFallbackProc;
rive::Font::FallbackRequestProc rive::Font::gFallbackRequestProc;
bool rive::Font::gFallbackProcEnabled = true;
#ifdef TESTING
std::atomic<uint64_t> HBFont::sm_variantsMade{0};
std::atomic<uint64_t> HBFont::sm_glyphPathsDrawn{0};
void (*HBFont::sm_variantDying)() = nullptr;
std::atomic<uint64_t> HBFont::sm_variantsCompared{0};
#endif
namespace
{
std::atomic<uint32_t> g_fallbackGeneration{0};
std::atomic<uint32_t> g_fallbackFontsGeneration{0};
// The proc and flag the generation last moved for. Written under
// g_fallbackMutex, and only after the generation moved, so a caller that
// reads them equal to the current ones also reads that generation or a
// later one.
std::atomic<rive::Font::FallbackProc> g_seenFallbackProc{nullptr};
std::atomic<rive::Font::FallbackRequestProc> g_seenFallbackRequestProc{nullptr};
std::atomic<bool> g_seenFallbackProcEnabled{true};
std::mutex g_fallbackMutex;
} // namespace
uint32_t rive::Font::fallbackGeneration()
{
if (g_seenFallbackProc.load(std::memory_order_acquire) != gFallbackProc ||
g_seenFallbackRequestProc.load(std::memory_order_acquire) !=
gFallbackRequestProc ||
g_seenFallbackProcEnabled.load(std::memory_order_acquire) !=
gFallbackProcEnabled)
{
std::lock_guard<std::mutex> lock(g_fallbackMutex);
FallbackProc proc = gFallbackProc;
FallbackRequestProc requestProc = gFallbackRequestProc;
bool enabled = gFallbackProcEnabled;
if (g_seenFallbackProc.load(std::memory_order_relaxed) != proc ||
g_seenFallbackRequestProc.load(std::memory_order_relaxed) !=
requestProc ||
g_seenFallbackProcEnabled.load(std::memory_order_relaxed) != enabled)
{
g_fallbackGeneration.fetch_add(1, std::memory_order_relaxed);
// Another proc picks from other fonts; turning them off or on doesn't
// (see fallbackFontsGeneration).
if (g_seenFallbackProc.load(std::memory_order_relaxed) != proc ||
g_seenFallbackRequestProc.load(std::memory_order_relaxed) !=
requestProc)
{
g_fallbackFontsGeneration.fetch_add(1, std::memory_order_release);
}
g_seenFallbackProc.store(proc, std::memory_order_release);
g_seenFallbackRequestProc.store(requestProc, std::memory_order_release);
g_seenFallbackProcEnabled.store(enabled, std::memory_order_release);
}
}
return g_fallbackGeneration.load(std::memory_order_acquire);
}
uint32_t rive::Font::fallbackFontsGeneration()
{
// Notices a new proc first.
fallbackGeneration();
return g_fallbackFontsGeneration.load(std::memory_order_acquire);
}
void rive::Font::fallbackFontsChanged()
{
// fallbackGeneration first: text that reshapes for the new fonts then
// also sees that its cached shapes are stale.
g_fallbackGeneration.fetch_add(1, std::memory_order_acq_rel);
g_fallbackFontsGeneration.fetch_add(1, std::memory_order_acq_rel);
}
namespace
{
// A number for the calling thread that no other thread, then or later, gets.
uint64_t threadToken()
{
static std::atomic<uint64_t> s_nextToken{1};
thread_local uint64_t token = s_nextToken.fetch_add(
1,
std::memory_order_relaxed);
return token;
}
// A hash of what tells variants apart: the thread one is shared on, and its
// merged axes and features, in tag order, as the maps' own order isn't fixed.
// Sorts [axes] and [features].
uint64_t variantKey(
uint64_t thread,
hb_variation_t* axes,
size_t axisCount,
std::pair<hb_tag_t, uint32_t>* features,
size_t featureCount)
{
std::sort(
axes,
axes + axisCount,
[](const hb_variation_t& a, const hb_variation_t& b) {
return a.tag < b.tag;
});
std::sort(features, features + featureCount);
uint64_t hash = 0x9e3779b97f4a7c15ull;
auto mix = [&hash](uint64_t word) {
hash ^= word;
hash *= 0xff51afd7ed558ccdull;
hash ^= hash >> 32;
};
mix(thread);
mix(axisCount);
for (size_t i = 0; i < axisCount; i++)
{
// Adding zero turns -0 into 0, which the maps' == already treats as
// equal, so the two share a key.
float axisValue = axes[i].value + 0.0f;
uint32_t value;
memcpy(&value, &axisValue, sizeof(value));
mix((uint64_t)axes[i].tag << 32 | value);
}
mix(featureCount);
for (size_t i = 0; i < featureCount; i++)
{
mix((uint64_t)features[i].first << 32 | features[i].second);
}
return hash;
}
// Takes ownership of blob, which is null when harfbuzz already released its
// data.
hb_font_t* hbFontFromBlob(hb_blob_t* blob, unsigned faceIndex = 0)
{
if (blob == nullptr)
{
return nullptr;
}
auto face = hb_face_create_or_fail(blob, faceIndex);
hb_blob_destroy(blob);
if (face == nullptr)
{
return nullptr;
}
auto font = hb_font_create(face);
hb_face_destroy(face);
return font;
}
/** Takes ownership of blob and initializes a Rive font from its first face. */
rive::rcp<rive::Font> fontFromBlob(hb_blob_t* blob)
{
auto* font = hbFontFromBlob(blob);
return font ? rive::rcp<rive::Font>(new HBFont(font)) : nullptr;
}
} // namespace
rive::rcp<rive::Font> HBFont::Decode(rive::Span<const uint8_t> span)
{
return fontFromBlob(hb_blob_create_or_fail(
(const char*)span.data(),
(unsigned)span.size(),
HB_MEMORY_MODE_DUPLICATE,
nullptr,
nullptr));
}
rive::rcp<rive::Font> HBFont::Decode(std::vector<uint8_t>&& bytes)
{
auto* owned = new std::vector<uint8_t>(std::move(bytes));
return fontFromBlob(hb_blob_create_or_fail(
reinterpret_cast<const char*>(owned->data()),
static_cast<unsigned>(owned->size()),
HB_MEMORY_MODE_WRITABLE,
owned,
[](void* context) {
delete static_cast<std::vector<uint8_t>*>(context);
}));
}
#ifdef RIVE_HB_FILE_MAPPING
namespace
{
struct HBFileMapping
{
void* data;
size_t size;
};
void destroyHBFileMapping(void* context)
{
auto* mapping = static_cast<HBFileMapping*>(context);
munmap(mapping->data, mapping->size);
delete mapping;
}
} // namespace
#endif
HBFont::FileProbe::FileProbe(hb_font_t* font) : m_font(font)
{
hb_ot_font_set_funcs(m_font);
}
HBFont::FileProbe::~FileProbe() { hb_font_destroy(m_font); }
bool HBFont::FileProbe::hasGlyph(rive::Unichar codepoint) const
{
hb_codepoint_t glyph;
return m_font && hb_font_get_nominal_glyph(m_font, codepoint, &glyph);
}
rive::rcp<rive::Font> HBFont::FileProbe::makeFont()
{
if (!m_font)
{
return nullptr;
}
auto* font = m_font;
m_font = nullptr; // HBFont now owns the same face and mapped bytes.
return rive::rcp<rive::Font>(new HBFont(font));
}
rive::rcp<rive::Font> HBFont::DecodeFile(const char* path, unsigned faceIndex)
{
auto probe = ProbeFile(path, faceIndex);
return probe ? probe->makeFont() : nullptr;
}
std::unique_ptr<HBFont::FileProbe> HBFont::ProbeFile(
const char* path,
unsigned faceIndex)
{
#ifdef RIVE_HB_FILE_MAPPING
if (path == nullptr)
{
return nullptr;
}
int fd = open(path, O_RDONLY);
if (fd == -1)
{
return nullptr;
}
struct stat info;
// hb_blob_create_or_fail rejects >= 2GB, so bail before mapping.
if (fstat(fd, &info) == -1 || info.st_size <= 0 ||
static_cast<uint64_t>(info.st_size) >= (1ull << 31))
{
close(fd);
return nullptr;
}
size_t size = static_cast<size_t>(info.st_size);
void* data = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
// The mapping holds its own reference to the file.
close(fd);
if (data == MAP_FAILED)
{
return nullptr;
}
// READONLY_MAY_MAKE_WRITABLE: harfbuzz reads our clean, evictable file
// pages and only copies if it ever needs to write.
auto* font = hbFontFromBlob(
hb_blob_create_or_fail(
static_cast<const char*>(data),
static_cast<unsigned>(size),
HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
new HBFileMapping{data, size},
&destroyHBFileMapping),
faceIndex);
return font ? std::unique_ptr<FileProbe>(new FileProbe(font)) : nullptr;
#else
return nullptr;
#endif
}
#if defined(RIVE_NO_CORETEXT) || !defined(__APPLE__)
rive::rcp<rive::Font> HBFont::FromSystem(
void* systemFont,
bool useSystemShaper,
uint16_t weight,
uint8_t width)
{
return nullptr;
}
#endif
float HBFont::GetStyle(hb_font_t* font, uint32_t styleTag)
{
return hb_style_get_value(font, (hb_style_tag_t)styleTag);
}
//////////////
constexpr int kStdScale = 2048;
constexpr float gInvScale = 1.0f / kStdScale;
extern "C"
{
static void rpath_move_to(
hb_draw_funcs_t*,
void* rpath,
hb_draw_state_t*,
float x,
float y,
void*)
{
((rive::RawPath*)rpath)->moveTo(x * gInvScale, -y * gInvScale);
}
static void rpath_line_to(
hb_draw_funcs_t*,
void* rpath,
hb_draw_state_t*,
float x1,
float y1,
void*)
{
((rive::RawPath*)rpath)->lineTo(x1 * gInvScale, -y1 * gInvScale);
}
static void rpath_quad_to(
hb_draw_funcs_t*,
void* rpath,
hb_draw_state_t*,
float x1,
float y1,
float x2,
float y2,
void*)
{
((rive::RawPath*)rpath)
->quadToCubic(
x1 * gInvScale,
-y1 * gInvScale,
x2 * gInvScale,
-y2 * gInvScale);
}
static void rpath_cubic_to(
hb_draw_funcs_t*,
void* rpath,
hb_draw_state_t*,
float x1,
float y1,
float x2,
float y2,
float x3,
float y3,
void*)
{
((rive::RawPath*)rpath)
->cubicTo(
x1 * gInvScale,
-y1 * gInvScale,
x2 * gInvScale,
-y2 * gInvScale,
x3 * gInvScale,
-y3 * gInvScale);
}
static void rpath_close(
hb_draw_funcs_t*,
void* rpath,
hb_draw_state_t*,
void*)
{
((rive::RawPath*)rpath)->close();
}
}
// Convert HarfBuzz color (BGRA byte order) to Rive ColorInt (ARGB).
static rive::ColorInt hbColorToColorInt(hb_color_t c)
{
uint8_t b = hb_color_get_blue(c);
uint8_t g = hb_color_get_green(c);
uint8_t r = hb_color_get_red(c);
uint8_t a = hb_color_get_alpha(c);
return rive::colorARGB(a, r, g, b);
}
// ── COLRv1 paint callbacks ──────────────────────────────────────────────
// We flatten the COLRv1 paint tree into a list of (path, fill) layers.
struct PaintState
{
hb_font_t* font;
hb_draw_funcs_t* drawFuncs;
std::vector<rive::Font::ColorGlyphLayer>* layers;
rive::RawPath clipPath;
bool hasClip = false;
rive::ColorInt foreground = 0xFF000000;
// HarfBuzz transforms conjugated into Rive glyph space (scaled, Y down).
std::vector<rive::Mat2D> transformStack;
void pushTransform(float xx, float yx, float xy, float yy, float dx, float dy)
{
rive::Mat2D m(xx, -yx, -xy, yy, dx * gInvScale, -dy * gInvScale);
transformStack.push_back(
transformStack.empty() ? m : transformStack.back() * m);
}
void popTransform()
{
if (!transformStack.empty())
{
transformStack.pop_back();
}
}
rive::Mat2D glyphTransform() const
{
rive::Mat2D scale = rive::Mat2D::fromScale(gInvScale, -gInvScale);
return transformStack.empty() ? scale : transformStack.back() * scale;
}
rive::Vec2D mapPoint(float x, float y) const
{
return glyphTransform() * rive::Vec2D(x, y);
}
float mapRadius(float r) const
{
float scaled = r * gInvScale;
if (transformStack.empty())
{
return scaled;
}
const auto& m = transformStack.back();
return scaled * std::sqrt(m[0] * m[0] + m[1] * m[1]);
}
void pushClipGlyph(hb_codepoint_t glyph)
{
clipPath.rewind();
hb_font_draw_glyph(font, glyph, drawFuncs, &clipPath);
if (!transformStack.empty())
{
clipPath.transformInPlace(transformStack.back());
}
hasClip = true;
}
// Helper: extract color stops from hb_color_line_t.
static std::vector<rive::Font::GradientStop> extractStops(
hb_color_line_t* colorLine,
rive::ColorInt foreground)
{
unsigned int count = hb_color_line_get_color_stops(
colorLine,
0,
nullptr,
nullptr);
std::vector<hb_color_stop_t> hbStops(count);
hb_color_line_get_color_stops(colorLine, 0, &count, hbStops.data());
std::vector<rive::Font::GradientStop> stops;
stops.reserve(count);
for (auto& s : hbStops)
{
rive::ColorInt c = s.is_foreground ? foreground
: hbColorToColorInt(s.color);
stops.push_back(
{std::clamp(s.offset, 0.0f, 1.0f), c, (bool)s.is_foreground});
}
// Variations can reorder stops and renderers expect them ascending.
std::stable_sort(
stops.begin(),
stops.end(),
[](const auto& a, const auto& b) { return a.offset < b.offset; });
return stops;
}
rive::Font::ColorGlyphLayer makeClipLayer() const
{
rive::Font::ColorGlyphLayer layer;
layer.path = clipPath;
return layer;
}
};
static void paint_push_transform(
hb_paint_funcs_t*,
void* paint_data,
float xx,
float yx,
float xy,
float yy,
float dx,
float dy,
void*)
{
((PaintState*)paint_data)->pushTransform(xx, yx, xy, yy, dx, dy);
}
static void paint_pop_transform(hb_paint_funcs_t*, void* paint_data, void*)
{
((PaintState*)paint_data)->popTransform();
}
static void paint_push_clip_glyph(
hb_paint_funcs_t*,
void* paint_data,
hb_codepoint_t glyph,
hb_font_t*,
void*)
{
((PaintState*)paint_data)->pushClipGlyph(glyph);
}
static void paint_push_clip_rectangle(
hb_paint_funcs_t*,
void*,
float,
float,
float,
float,
void*)
{}
static void paint_pop_clip(hb_paint_funcs_t*, void* paint_data, void*)
{
((PaintState*)paint_data)->hasClip = false;
}
static void paint_solid(
hb_paint_funcs_t*,
void* paint_data,
hb_bool_t is_foreground,
hb_color_t color,
void*)
{
auto* state = (PaintState*)paint_data;
if (!state->hasClip)
return;
auto layer = state->makeClipLayer();
layer.paintType = rive::Font::ColorGlyphPaintType::solid;
if (is_foreground)
{
layer.useForeground = true;
layer.color = state->foreground;
}
else
{
layer.color = hbColorToColorInt(color);
}
state->layers->push_back(std::move(layer));
}
static void paint_linear_gradient(
hb_paint_funcs_t*,
void* paint_data,
hb_color_line_t* colorLine,
float x0,
float y0,
float x1,
float y1,
float x2,
float y2,
void*)
{
auto* state = (PaintState*)paint_data;
if (!state->hasClip)
return;
auto layer = state->makeClipLayer();
layer.paintType = rive::Font::ColorGlyphPaintType::linearGradient;
layer.stops = PaintState::extractStops(colorLine, state->foreground);
// OpenType linear gradient uses 3 points: p0 (start), p1 (end), p2
// (rotation). The gradient line goes from p0 toward p1, and p2 is used
// to rotate the direction. For Rive we need a 2-point definition.
// The actual gradient direction is: start = p0, end = p1 projected
// along the p0->p2 direction. For simplicity, just use p0 and p1.
auto sp0 = state->mapPoint(x0, y0);
auto sp1 = state->mapPoint(x1, y1);
layer.x0 = sp0.x;
layer.y0 = sp0.y;
layer.x1 = sp1.x;
layer.y1 = sp1.y;
state->layers->push_back(std::move(layer));
}
static void paint_radial_gradient(
hb_paint_funcs_t*,
void* paint_data,
hb_color_line_t* colorLine,
float x0,
float y0,
float radius0,
float x1,
float y1,
float radius1,
void*)
{
auto* state = (PaintState*)paint_data;
if (!state->hasClip)
return;
auto layer = state->makeClipLayer();
layer.paintType = rive::Font::ColorGlyphPaintType::radialGradient;
layer.stops = PaintState::extractStops(colorLine, state->foreground);
auto sp0 = state->mapPoint(x0, y0);
auto sp1 = state->mapPoint(x1, y1);
layer.x0 = sp0.x;
layer.y0 = sp0.y;
layer.x1 = sp1.x;
layer.y1 = sp1.y;
layer.r0 = state->mapRadius(radius0);
layer.r1 = state->mapRadius(radius1);
layer.radialTransform = state->glyphTransform() *
rive::Mat2D::fromScaleAndTranslation(
radius1,
radius1,
x1,
y1);
state->layers->push_back(std::move(layer));
}
static void paint_sweep_gradient(
hb_paint_funcs_t*,
void* paint_data,
hb_color_line_t* colorLine,
float cx,
float cy,
float startAngle,
float endAngle,
void*)
{
auto* state = (PaintState*)paint_data;
if (!state->hasClip)
return;
auto layer = state->makeClipLayer();
layer.paintType = rive::Font::ColorGlyphPaintType::sweepGradient;
layer.stops = PaintState::extractStops(colorLine, state->foreground);
auto sc = state->mapPoint(cx, cy);
layer.x0 = sc.x;
layer.y0 = sc.y;
layer.startAngle = startAngle;
layer.endAngle = endAngle;
state->layers->push_back(std::move(layer));
}
static void paint_push_group(hb_paint_funcs_t*, void*, void*) {}
static void paint_pop_group(
hb_paint_funcs_t*,
void*,
hb_paint_composite_mode_t,
void*)
{}
static hb_bool_t paint_color_glyph(
hb_paint_funcs_t*,
void*,
hb_codepoint_t,
hb_font_t*,
void*)
{
return false;
}
static hb_bool_t paint_image(
hb_paint_funcs_t*,
void* paint_data,
hb_blob_t* blob,
unsigned int width,
unsigned int height,
hb_tag_t format,
float slant,
hb_glyph_extents_t* extents,
void*)
{
// We only support PNG images (SBIX / CBDT).
if (format != HB_TAG('p', 'n', 'g', ' '))
{
return false;
}
unsigned int length;
const char* data = hb_blob_get_data(blob, &length);
if (data == nullptr || length == 0)
{
return false;
}
auto* state = (PaintState*)paint_data;
rive::Font::ColorGlyphLayer layer;
layer.paintType = rive::Font::ColorGlyphPaintType::image;
layer.imageBytes.assign(
reinterpret_cast<const uint8_t*>(data),
reinterpret_cast<const uint8_t*>(data) + length);
layer.imageWidth = width;
layer.imageHeight = height;
if (extents != nullptr)
{
layer.imageBearingX = extents->x_bearing * gInvScale;
layer.imageBearingY = -extents->y_bearing * gInvScale;
layer.imageExtentX = extents->width * gInvScale;
layer.imageExtentY = -extents->height * gInvScale;
}
state->layers->push_back(std::move(layer));
return true;
}
// ────────────────────────────────────────────────────────────────────────
static rive::Font::LineMetrics make_lmx(hb_font_t* font)
{
// premable on font...
hb_ot_font_set_funcs(font);
hb_font_set_scale(font, kStdScale, kStdScale);
hb_font_extents_t extents;
hb_font_get_h_extents(font, &extents);
// Cap height / x-height: distances from the baseline to the top of capital
// and lowercase letters, stored negative (up is -Y) to match ascent. We
// measure the 'H' and 'x' glyphs directly (their top-side bearing) rather
// than the OS/2 metrics, since the metrics API is compiled out of this
// HarfBuzz build (HB_NO_METRICS). Each falls back to the ascent for fonts
// without the reference glyph so vertical trim becomes a no-op at the top.
float ascent = -extents.ascender * gInvScale;
auto measureGlyphTop = [&](hb_codepoint_t unicode) -> float {
hb_codepoint_t glyph = 0;
hb_glyph_extents_t glyphExtents;
if (hb_font_get_nominal_glyph(font, unicode, &glyph) &&
hb_font_get_glyph_extents(font, glyph, &glyphExtents))
{
return -glyphExtents.y_bearing * gInvScale;
}
return ascent;
};
float capHeight = measureGlyphTop('H');
float xHeight = measureGlyphTop('x');
return {ascent, -extents.descender * gInvScale, capHeight, xHeight};
}
HBFont::HBFont(hb_font_t* font) : HBFont(font, {}, {}, {}) {}
HBFont::HBFont(
hb_font_t* font,
std::unordered_map<hb_tag_t, float> axisValues,
std::unordered_map<hb_tag_t, uint32_t> featureValues,
std::vector<hb_feature_t> features)
: HBFont(
font,
make_lmx(font),
std::move(axisValues),
std::move(featureValues),
std::move(features))
{}
HBFont::HBFont(
hb_font_t* font,
const LineMetrics& lineMetrics,
std::unordered_map<hb_tag_t, float> axisValues,
std::unordered_map<hb_tag_t, uint32_t> featureValues,
std::vector<hb_feature_t> features)
: Font(lineMetrics)
, m_font(font)
, m_features(features)
, m_featureValues(featureValues)
, m_axisValues(axisValues)
{
m_drawFuncs = hb_draw_funcs_create();
hb_draw_funcs_set_move_to_func(m_drawFuncs, rpath_move_to, nullptr, nullptr);
hb_draw_funcs_set_line_to_func(m_drawFuncs, rpath_line_to, nullptr, nullptr);
hb_draw_funcs_set_quadratic_to_func(
m_drawFuncs,
rpath_quad_to,
nullptr,
nullptr);
hb_draw_funcs_set_cubic_to_func(
m_drawFuncs,
rpath_cubic_to,
nullptr,
nullptr);
hb_draw_funcs_set_close_path_func(m_drawFuncs, rpath_close, nullptr, nullptr);
hb_draw_funcs_make_immutable(m_drawFuncs);
// Initialize COLRv1 paint funcs.
m_paintFuncs = hb_paint_funcs_create();
hb_paint_funcs_set_push_transform_func(
m_paintFuncs,
paint_push_transform,
nullptr,
nullptr);
hb_paint_funcs_set_pop_transform_func(
m_paintFuncs,
paint_pop_transform,
nullptr,
nullptr);
hb_paint_funcs_set_push_clip_glyph_func(
m_paintFuncs,
paint_push_clip_glyph,
nullptr,
nullptr);
hb_paint_funcs_set_push_clip_rectangle_func(
m_paintFuncs,
paint_push_clip_rectangle,
nullptr,
nullptr);
hb_paint_funcs_set_pop_clip_func(
m_paintFuncs,
paint_pop_clip,
nullptr,
nullptr);
hb_paint_funcs_set_color_func(m_paintFuncs, paint_solid, nullptr, nullptr);
hb_paint_funcs_set_push_group_func(
m_paintFuncs,
paint_push_group,
nullptr,
nullptr);
hb_paint_funcs_set_pop_group_func(
m_paintFuncs,
paint_pop_group,
nullptr,
nullptr);
hb_paint_funcs_set_color_glyph_func(
m_paintFuncs,
paint_color_glyph,
nullptr,
nullptr);
hb_paint_funcs_set_linear_gradient_func(
m_paintFuncs,
paint_linear_gradient,
nullptr,
nullptr);
hb_paint_funcs_set_radial_gradient_func(
m_paintFuncs,
paint_radial_gradient,
nullptr,
nullptr);
hb_paint_funcs_set_sweep_gradient_func(
m_paintFuncs,
paint_sweep_gradient,
nullptr,
nullptr);
hb_paint_funcs_set_image_func(m_paintFuncs, paint_image, nullptr, nullptr);
hb_paint_funcs_make_immutable(m_paintFuncs);
// Initialize color glyph (COLR/CPAL/SBIX/CBDT) support.
hb_face_t* face = hb_font_get_face(font);
m_hasColorLayers = hb_ot_color_has_layers(face);
m_hasColorPaint = hb_ot_color_has_paint(face);
m_hasPNG = hb_ot_color_has_png(face);
if (m_hasColorLayers || m_hasColorPaint)
{
unsigned int colorCount = 0;
hb_ot_color_palette_get_colors(face, 0, 0, &colorCount, nullptr);
if (colorCount > 0)
{
m_paletteColors.resize(colorCount);
hb_ot_color_palette_get_colors(
face,
0,
0,
&colorCount,
m_paletteColors.data());
}
}
}
HBFont::~HBFont()
{
if (m_variantOf != nullptr)
{
#ifdef TESTING
if (sm_variantDying != nullptr)
{
sm_variantDying();
}
#endif
// Until this is out of the map, sharedWithOptions may still compare this
// font's options, which outlive this block; it can't take a reference
// to it (see RefCnt::tryRef).
std::lock_guard<std::mutex> lock(m_variantOf->m_variantsMutex);
auto& variants = m_variantOf->m_variants;
auto range = variants.equal_range(m_variantKey);
for (auto itr = range.first; itr != range.second; itr++)
{
if (itr->second == this)
{
variants.erase(itr);
break;
}
}
}
hb_draw_funcs_destroy(m_drawFuncs);
hb_paint_funcs_destroy(m_paintFuncs);
hb_font_destroy(m_font);
}
static void fillLanguageFeatures(
hb_face_t* face,
hb_tag_t tag,
uint32_t scriptIndex,
uint32_t languageIndex,
std::unordered_set<uint32_t>& features)
{
auto featureCount = hb_ot_layout_language_get_feature_tags(
face,
tag,
scriptIndex,
languageIndex,
0,
nullptr,
nullptr);
auto featureTags = std::vector<hb_tag_t>(featureCount);
hb_ot_layout_language_get_feature_tags(
face,
tag,
scriptIndex,
languageIndex,
0,
&featureCount,
featureTags.data());
for (auto featureTag : featureTags)
{
features.emplace(featureTag);
}
}
static void fillFeatures(
hb_face_t* face,
hb_tag_t tag,
std::unordered_set<uint32_t>& features)
{
auto scriptCount = hb_ot_layout_table_get_script_tags(
face,
tag,
0,
nullptr,
nullptr);
auto scripts = std::vector<hb_tag_t>(scriptCount);
hb_ot_layout_table_get_script_tags(
face,
tag,
0,
&scriptCount,
scripts.data());
for (uint32_t i = 0; i < scriptCount; ++i)
{
auto languageCount = hb_ot_layout_script_get_language_tags(
face,
tag,
i,
0,
nullptr,
nullptr);
if (languageCount > 0)
{
auto languages = std::vector<hb_tag_t>(languageCount);
hb_ot_layout_script_get_language_tags(
face,
tag,
i,
0,
&languageCount,
languages.data());
for (uint32_t j = 0; j < languageCount; ++j)
{
fillLanguageFeatures(face, tag, i, j, features);
}
}
else
{
fillLanguageFeatures(
face,
tag,
i,
HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX,
features);
}
}
}
rive::SimpleArray<uint32_t> HBFont::features() const
{
std::unordered_set<uint32_t> features;
auto face = hb_font_get_face(m_font);
fillFeatures(face, HB_OT_TAG_GSUB, features);
fillFeatures(face, HB_OT_TAG_GPOS, features);
rive::SimpleArray<uint32_t> result(features.size());
uint32_t index = 0;
for (auto tag : features)
{
result[index++] = tag;
}
return result;
}
rive::Font::Axis HBFont::getAxis(uint16_t index) const
{
auto face = hb_font_get_face(m_font);
assert(index < hb_ot_var_get_axis_count(face));
unsigned n = 1;
hb_ot_var_axis_info_t info;
hb_ot_var_get_axis_infos(face, index, &n, &info);
assert(n == 1);
return {info.tag, info.min_value, info.default_value, info.max_value};
}
uint16_t HBFont::getAxisCount() const
{
auto face = hb_font_get_face(m_font);
return (uint16_t)hb_ot_var_get_axis_count(face);
}
float HBFont::getAxisValue(uint32_t axisTag) const
{
auto itr = m_axisValues.find(axisTag);
if (itr != m_axisValues.end())
{
return itr->second;
}
auto face = hb_font_get_face(m_font);
// No value specified, we're using a default.
uint32_t axisCount = hb_ot_var_get_axis_count(face);
for (uint32_t i = 0; i < axisCount; ++i)
{
hb_ot_var_axis_info_t info;
uint32_t n = 1;
hb_ot_var_get_axis_infos(face, i, &n, &info);
if (info.tag == axisTag)
{
return info.default_value;
}
}
return 0.0f;
}
uint32_t HBFont::getFeatureValue(uint32_t featureTag) const
{
auto itr = m_featureValues.find(featureTag);
if (itr != m_featureValues.end())
{
return itr->second;
}
return (uint32_t)-1;
}
uint16_t HBFont::getWeight() const
{
uint32_t tag = HB_TAG('w', 'g', 'h', 't');
float res = HBFont::GetStyle(m_font, tag);
return static_cast<uint16_t>(res);
}
bool HBFont::isItalic() const
{
uint32_t tag = HB_TAG('i', 't', 'a', 'l');
float res = HBFont::GetStyle(m_font, tag);
return res != 0.0;
}
rive::rcp<rive::Font> HBFont::withOptions(
rive::Span<const Coord> coords,
rive::Span<const Feature> features) const
{
return makeVariant(coords, features, false);
}
rive::rcp<rive::Font> HBFont::sharedWithOptions(
rive::Span<const Coord> coords,
rive::Span<const Feature> features) const
{
return makeVariant(coords, features, true);
}
rive::rcp<rive::Font> HBFont::makeVariant(
rive::Span<const Coord> coords,
rive::Span<const Feature> features,
bool shared) const
{
// Merges previous options with current ones.
std::unordered_map<hb_tag_t, float> axisValues = m_axisValues;
for (size_t i = 0; i < coords.size(); ++i)
{
axisValues[coords[i].axis] = coords[i].value;
}
AutoSTArray<16, hb_variation_t> vars(axisValues.size());
size_t i = 0;
for (auto itr = axisValues.begin(); itr != axisValues.end(); itr++)
{
vars[i++] = {itr->first, itr->second};
}
std::unordered_map<hb_tag_t, uint32_t> featureValues = m_featureValues;
for (auto feature : features)
{
featureValues[feature.tag] = feature.value;
}
uint64_t thread = 0;
uint64_t key = 0;
if (shared)
{
thread = threadToken();
AutoSTArray<16, std::pair<hb_tag_t, uint32_t>> featurePairs(
featureValues.size());
i = 0;
for (auto itr = featureValues.begin(); itr != featureValues.end(); itr++)
{
featurePairs[i++] = {itr->first, itr->second};
}
key = variantKey(
thread,
vars.data(),
vars.size(),
featurePairs.data(),
featurePairs.size());
std::lock_guard<std::mutex> lock(m_variantsMutex);
auto range = m_variants.equal_range(key);
for (auto itr = range.first; itr != range.second; itr++)
{
HBFont* variant = itr->second;
#ifdef TESTING
sm_variantsCompared++;
#endif
// A variant whose count already reached zero waits on this lock to
// leave the map, so its options can still be compared; tryRef then
// refuses it.
if (variant->m_madeOn == thread && variant->m_axisValues == axisValues &&
variant->m_featureValues == featureValues && variant->tryRef())
{
return rive::rcp<rive::Font>(variant);
}
}
}
auto font = hb_font_create_sub_font(m_font);
hb_font_set_variations(font, vars.data(), (unsigned int)vars.size());
std::vector<hb_feature_t> hbFeatures;
for (auto itr = featureValues.begin(); itr != featureValues.end(); itr++)
{
hbFeatures.push_back(
{itr->first,
itr->second,
HB_FEATURE_GLOBAL_START,
HB_FEATURE_GLOBAL_END});
}
auto variant = new HBFont(font, axisValues, featureValues, hbFeatures);
#ifdef TESTING
sm_variantsMade++;
#endif
if (shared)
{
variant->m_variantOf = rive::ref_rcp(this);
variant->m_madeOn = thread;
variant->m_variantKey = key;
std::lock_guard<std::mutex> lock(m_variantsMutex);
m_variants.emplace(key, variant);
}
return rive::rcp<rive::Font>(variant);
}
rive::rcp<rive::Font> HBFont::forThisThread() const
{
const uint64_t thread = threadToken();
// Left at zero when this thread is the first to ask.
uint64_t first = 0;
m_firstThread.compare_exchange_strong(
first,
thread,
std::memory_order_relaxed);
if (first == 0 || first == thread)
{
return rive::ref_rcp<rive::Font>(const_cast<HBFont*>(this));
}
{
std::lock_guard<std::mutex> lock(m_variantsMutex);
auto range = m_variants.equal_range(thread);
for (auto itr = range.first; itr != range.second; itr++)
{
// A copy whose count already reached zero waits on this lock to leave
// the map, as a variant does; tryRef then refuses it.
HBFont* copy = itr->second;
if (copy->m_isCopy && copy->m_madeOn == thread && copy->tryRef())
{
return rive::rcp<rive::Font>(copy);
}
}
}
// A sub font starts at this font's scale and coordinates, which a
// subclass may set without listing them in m_axisValues (CoreTextHBFont),
// so unlike a variant, a copy sets none of its own. It gets the font
// functions HBFont's constructor gives the fonts it measures.
hb_font_t* font = hb_font_create_sub_font(m_font);
hb_ot_font_set_funcs(font);
auto copy = new HBFont(
font,
lineMetrics(),
m_axisValues,
m_featureValues,
m_features);
copy->m_variantOf = rive::ref_rcp(this);
copy->m_madeOn = thread;
copy->m_variantKey = thread;
copy->m_firstThread.store(thread, std::memory_order_relaxed);
copy->m_isCopy = true;
std::lock_guard<std::mutex> lock(m_variantsMutex);
m_variants.emplace(thread, copy);
return rive::rcp<rive::Font>(copy);
}
rive::RawPath HBFont::getPath(rive::GlyphID glyph) const
{
return glyphPath(glyph);
}
rive::Font::InkExtents HBFont::glyphInkExtents(rive::GlyphID glyph) const
{
// HarfBuzz reads these from the font's own bounds (glyf, CFF, COLR clip
// boxes and bitmap strikes), so this never draws the outline.
hb_glyph_extents_t extents;
if (!hb_font_get_glyph_extents(m_font, glyph, &extents) ||
extents.height == 0)
{
return {};
}
// y_bearing is the top with up as +Y and height runs down from it.
return {
-extents.y_bearing * gInvScale,
-(extents.y_bearing + extents.height) * gInvScale};
}
const rive::RawPath& HBFont::glyphPath(rive::GlyphID glyph) const
{
auto cached = m_glyphPathCache.find(glyph);
if (cached != m_glyphPathCache.end())
{
return cached->second;
}
rive::RawPath rpath = drawGlyphPath(glyph);
#ifdef TESTING
sm_glyphPathsDrawn++;
#endif
if (m_glyphPathCache.size() >= kMaxCachedGlyphPaths)
{
m_glyphPathCache.clear();
}
// The entry stays put until the cache starts over, which only a later
// insert does.
return m_glyphPathCache.emplace(glyph, std::move(rpath)).first->second;
}
const rive::RawPath* HBFont::cachedGlyphPath(rive::GlyphID glyph) const
{
auto cached = m_glyphPathCache.find(glyph);
return cached == m_glyphPathCache.end() ? nullptr : &cached->second;
}
size_t HBFont::glyphPathCacheRoom() const
{
return m_glyphPathCache.size() >= kMaxCachedGlyphPaths
? 0
: kMaxCachedGlyphPaths - m_glyphPathCache.size();
}
rive::RawPath HBFont::drawGlyphPath(rive::GlyphID glyph) const
{
if (m_isCopy)
{
// Drawing keeps nothing, so the font copied draws on any thread.
return m_variantOf->drawGlyphPath(glyph);
}
rive::RawPath rpath;
hb_font_draw_glyph(m_font, glyph, m_drawFuncs, &rpath);
return rpath;
}
bool HBFont::hasColorGlyphs() const
{
return m_hasColorLayers || m_hasColorPaint || m_hasPNG;
}
bool HBFont::isColorGlyph(rive::GlyphID glyph) const
{
if (!m_hasColorLayers && !m_hasColorPaint && !m_hasPNG)
{
return false;
}
hb_face_t* face = hb_font_get_face(m_font);
// Check COLRv0 layers first.
if (m_hasColorLayers)
{
unsigned int
total = hb_ot_color_glyph_get_layers(face, glyph, 0, nullptr, nullptr);
if (total > 0)
{
return true;
}
}
// Check COLRv1 paint.
if (m_hasColorPaint)
{
if (hb_ot_color_glyph_has_paint(face, glyph))
{
return true;
}
}
// Check SBIX/CBDT PNG.
if (m_hasPNG)
{
hb_blob_t* blob = hb_ot_color_glyph_reference_png(m_font, glyph);
if (blob != nullptr)
{
bool has = hb_blob_get_length(blob) > 0;
hb_blob_destroy(blob);
return has;
}
}
return false;
}
size_t HBFont::getColorLayers(
rive::GlyphID glyph,
std::vector<ColorGlyphLayer>& out,
rive::ColorInt foreground) const
{
if (!m_hasColorLayers && !m_hasColorPaint && !m_hasPNG)
{
return 0;
}
// Check cache first.
auto cacheIt = m_colorLayerCache.find(glyph);
if (cacheIt != m_colorLayerCache.end())
{
// Copy from cache, but update foreground colors.
for (ColorGlyphLayer layer : cacheIt->second)
{
if (layer.useForeground)
{
layer.color = foreground;
}
for (auto& stop : layer.stops)
{
if (stop.isForeground)
{
stop.color = foreground;
}
}
out.push_back(std::move(layer));
}
return cacheIt->second.size();
}
std::vector<ColorGlyphLayer> layers;
hb_face_t* face = hb_font_get_face(m_font);
// Try COLRv0 first.
if (m_hasColorLayers)
{
unsigned int layerCount = hb_ot_color_glyph_get_layers(
face,
glyph,
0,
nullptr,
nullptr);
if (layerCount > 0)
{
std::vector<hb_ot_color_layer_t> hbLayers(layerCount);
hb_ot_color_glyph_get_layers(
face,
glyph,
0,
&layerCount,
hbLayers.data());
layers.reserve(layerCount);
for (unsigned int i = 0; i < layerCount; i++)
{
ColorGlyphLayer layer;
hb_font_draw_glyph(m_font, hbLayers[i].glyph, m_drawFuncs, &layer.path);
if (hbLayers[i].color_index == 0xFFFF)
{
layer.useForeground = true;
layer.color = foreground;
}
else
{
layer.useForeground = false;
if (hbLayers[i].color_index < m_paletteColors.size())
{
layer.color = hbColorToColorInt(
m_paletteColors[hbLayers[i].color_index]);
}
else
{
layer.color = 0xFF000000;
}
}
layers.push_back(std::move(layer));
}
}
}
// Fall back to COLRv1 paint if no COLRv0 layers found.
if (layers.empty() && (m_hasColorPaint || m_hasPNG))
{
// hb_font_paint_glyph handles both COLRv1 and SBIX/CBDT.
// For COLRv1 it calls paint_solid/gradient callbacks;
// for SBIX/CBDT it calls paint_image.
PaintState state;
state.font = m_font;
state.drawFuncs = m_drawFuncs;
state.layers = &layers;
state.foreground = foreground;
hb_font_paint_glyph(
m_font,
glyph,
m_paintFuncs,
&state,
0, // palette_index
HB_COLOR(0, 0, 0, 255)); // foreground
}
if (layers.empty())
{
return 0;
}
size_t count = layers.size();
// Cache the result.
m_colorLayerCache[glyph] = layers;
// Move into output.
for (auto& layer : layers)
{
out.push_back(std::move(layer));
}
return count;
}
///////////////////////////////////////////////////////////
// One HarfBuzz buffer per thread, cleared for each run instead of made and
// freed for it: cleared, it keeps the storage it grew, which every run
// would otherwise allocate again.
namespace
{
class ShapingBuffer
{
public:
~ShapingBuffer() { hb_buffer_destroy(m_buffer); }
// Empty, as hb_buffer_create makes it.
hb_buffer_t* take()
{
hb_buffer_clear_contents(m_buffer);
return m_buffer;
}
// Lets go of what a run of [unicharCount] grew, when it's more than a
// usual run needs, so one long text doesn't hold it for the thread's life.
void release(uint32_t unicharCount)
{
if (unicharCount > kKeptUnichars)
{
hb_buffer_destroy(m_buffer);
m_buffer = hb_buffer_create();
}
}
private:
static constexpr uint32_t kKeptUnichars = 4096;
hb_buffer_t* m_buffer = hb_buffer_create();
};
} // namespace
static rive::GlyphRun shape_run(
const rive::Unichar text[],
const rive::TextRun& tr,
unsigned textOffset)
{
static thread_local ShapingBuffer shapingBuffer;
hb_buffer_t* buf = shapingBuffer.take();
hb_buffer_add_utf32(buf, text, tr.unicharCount, 0, tr.unicharCount);
hb_buffer_set_direction(
buf,
tr.level & 1 ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
hb_buffer_set_script(buf, (hb_script_t)tr.script);
// The run's language, else the system's, else the process locale. It's
// what makes a font's locl forms apply.
std::string language = rive::TextLanguages::shapingLanguage(tr.language);
hb_buffer_set_language(
buf,
language.empty()
? hb_language_get_default()
: hb_language_from_string(language.data(), (int)language.size()));
auto hbfont = (HBFont*)tr.font.get();
hb_shape(
hbfont->m_font,
buf,
hbfont->m_features.data(),
(unsigned int)hbfont->m_features.size());
unsigned int glyph_count;
hb_glyph_info_t* glyph_info = hb_buffer_get_glyph_infos(buf, &glyph_count);
hb_glyph_position_t* glyph_pos = hb_buffer_get_glyph_positions(
buf,
&glyph_count);
// todo: check for missing glyphs, and perform font-substitution
rive::GlyphRun gr(glyph_count);
gr.font = tr.font;
gr.size = tr.size;
gr.lineHeight = tr.lineHeight;
gr.letterSpacing = tr.letterSpacing;
gr.styleId = tr.styleId;
gr.level = tr.level;
gr.language = tr.language;
const float scale = tr.size / kStdScale;
for (unsigned int i = 0; i < glyph_count; i++)
{
unsigned int index = tr.level & 1 ? glyph_count - 1 - i : i;
gr.glyphs[i] = (uint16_t)glyph_info[index].codepoint;
gr.textIndices[i] = textOffset + glyph_info[index].cluster;
gr.advances[i] = gr.xpos[i] = glyph_pos[index].x_advance * scale +
tr.letterSpacing;
gr.offsets[i] = rive::Vec2D(
glyph_pos[index].x_offset * scale,
-glyph_pos[index].y_offset * scale);
}
gr.xpos[glyph_count] = 0; // so the next run can line up snug
shapingBuffer.release(tr.unicharCount);
return gr;
}
static rive::GlyphRun extract_subset(
const rive::GlyphRun& orig,
size_t start,
size_t end)
{
auto count = end - start;
rive::GlyphRun subset(count);
std::copy_n(orig.glyphs.data() + start, count, subset.glyphs.data());
std::copy_n(
orig.textIndices.data() + start,
count,
subset.textIndices.data());
std::copy_n(orig.advances.data() + start, count, subset.advances.data());
std::copy_n(orig.xpos.data() + start, count + 1, subset.xpos.data());
std::copy_n(orig.offsets.data() + start, count, subset.offsets.data());
subset.font = std::move(orig.font);
subset.size = orig.size;
subset.lineHeight = orig.lineHeight;
subset.letterSpacing = orig.letterSpacing;
subset.level = orig.level;
subset.language = orig.language;
subset.xpos.back() = 0; // since we're now the end of a run
subset.styleId = orig.styleId;
return subset;
}
static bool hasFallback()
{
return (rive::Font::gFallbackRequestProc != nullptr ||
rive::Font::gFallbackProc != nullptr) &&
rive::Font::gFallbackProcEnabled;
}
// Asks the host for a font with [missing] in it, through the request proc
// when there is one, which also gets the whole [cluster] it's in.
static rive::rcp<rive::Font> requestFallback(
rive::Unichar missing,
rive::Span<const rive::Unichar> cluster,
uint32_t fallbackIndex,
const rive::Font* font,
const rive::TextRun& run)
{
if (rive::Font::gFallbackRequestProc != nullptr)
{
std::vector<std::string> languages = rive::TextLanguages::fallbackLanguages(
run.language);
rive::Font::FallbackRequest request = {
missing,
fallbackIndex,
font,
run.script,
languages,
!rive::TextLanguages::tag(run.language).empty(),
cluster,
run.font->getWeight(),
};
return rive::Font::gFallbackRequestProc(request);
}
if (rive::Font::gFallbackProc != nullptr)
{
return rive::Font::gFallbackProc(missing, fallbackIndex, font);
}
return nullptr;
}
struct HBFont::FallbackChain
{
const rive::Font* font;
const FallbackChain* parent;
bool contains(const rive::Font* other) const
{
for (auto link = this; link != nullptr; link = link->parent)
{
if (link->font == other)
{
return true;
}
}
return false;
}
};
// The end of the cluster (the glyphs sharing a text index) starting at
// [start], and whether any of its glyphs is missing.
static size_t cluster_end(const rive::GlyphRun& gr, size_t start, bool* missing)
{
const size_t count = gr.glyphs.size();
size_t end = start;
*missing = false;
do
{
*missing |= gr.glyphs[end] == 0;
++end;
} while (end < count && gr.textIndices[end] == gr.textIndices[start]);
return end;
}
// Default ignorables (variation selectors, joiners like ZWJ and CGJ, and
// other controls) choose how their neighbors draw. HarfBuzz hides them
// rather than drawing them, so no font is expected to have a glyph for them.
// The same set as HarfBuzz's is_default_ignorable.
static bool draws_nothing(rive::Unichar c)
{
return c == 0x00AD || c == 0x034F || c == 0x061C ||
(c >= 0x17B4 && c <= 0x17B5) || (c >= 0x180B && c <= 0x180E) ||
(c >= 0x200B && c <= 0x200F) || (c >= 0x202A && c <= 0x202E) ||
(c >= 0x2060 && c <= 0x206F) || (c >= 0xFE00 && c <= 0xFE0F) ||
c == 0xFEFF || (c >= 0xFFF0 && c <= 0xFFF8) ||
(c >= 0x1D173 && c <= 0x1D17A) || (c >= 0xE0000 && c <= 0xE0FFF);
}
// Where the text of the cluster holding the glyph at [glyphIndex] ends, in a
// run whose text ends at [runTextEnd].
static uint32_t cluster_text_end(
const rive::GlyphRun& gr,
size_t glyphIndex,
uint32_t runTextEnd)
{
for (size_t i = glyphIndex + 1; i < gr.glyphs.size(); i++)
{
if (gr.textIndices[i] != gr.textIndices[glyphIndex])
{
return gr.textIndices[i];
}
}
return runTextEnd;
}
// What to ask for as missing, for the cluster at text [start, end): the
// first codepoint in it that [font] can't map. A cluster's text index is its
// first codepoint, which the font often has, like the "1" of a keycap whose
// U+20E3 is what's missing. When nothing else is unmapped (a glyph forced to
// fallback for emoji presentation), it's that first one.
static rive::Unichar missing_unichar(
const HBFont* font,
const rive::Unichar text[],
uint32_t start,
uint32_t end)
{
for (uint32_t i = start; i < end; i++)
{
if (!font->hasGlyph(text[i]) && !draws_nothing(text[i]))
{
return text[i];
}
}
return text[start];
}
// Asks for a font to draw the cluster holding the missing glyph at
// [glyphIndex] of [gr], which [font] shaped from [run].
static rive::rcp<rive::Font> pick_fallback(
const HBFont* font,
const rive::Unichar text[],
const rive::GlyphRun& gr,
size_t glyphIndex,
uint32_t runTextEnd,
uint32_t fallbackIndex,
const rive::TextRun& run)
{
const uint32_t start = gr.textIndices[glyphIndex];
const uint32_t end = cluster_text_end(gr, glyphIndex, runTextEnd);
return requestFallback(
missing_unichar(font, text, start, end),
rive::Span<const rive::Unichar>(&text[start], end - start),
fallbackIndex,
font,
run);
}
// Adds [gr], which [font] shaped from [textRun] (whose text ends at
// [runTextEnd]), drawing each cluster with a missing glyph in the font the
// host picks for it, asked with [fallbackIndex]. A cluster falls back whole:
// the fallback font reshapes all of its text, so keeping the glyphs [font]
// has (the "1" of a keycap missing its U+20E3) would draw them twice. Each
// cluster is asked for on its own, and stays in [font] when the host picks
// nothing or a font already in [chain]. Neighbors that picked the same font
// are shaped together, so they still join and kern.
static void fall_back_clusters(
const HBFont* font,
rive::SimpleArrayBuilder<rive::GlyphRun>& gruns,
const rive::Unichar text[],
rive::GlyphRun&& gr,
const rive::TextRun& originalTextRun,
uint32_t runTextEnd,
uint32_t fallbackIndex,
const HBFont::FallbackChain* chain)
{
const size_t count = gr.glyphs.size();
assert(count > 0);
// The font for the cluster starting at glyph [start], or null to keep it
// in [font]. Sets [end] to the cluster's end.
auto pick = [&](size_t start, size_t* end) {
bool missing;
*end = cluster_end(gr, start, &missing);
rive::rcp<rive::Font> picked;
if (missing && fallbackIndex < rive::Font::kMaxFallbacks)
{
picked = pick_fallback(
font,
text,
gr,
start,
runTextEnd,
fallbackIndex,
originalTextRun);
if (picked && chain->contains(picked.get()))
{
picked = nullptr;
}
}
return picked;
};
size_t startI = 0;
size_t endI;
rive::rcp<rive::Font> picked = pick(startI, &endI);
while (startI < count)
{
// Take in the clusters after it that picked the same font.
size_t nextEnd = endI;
rive::rcp<rive::Font> nextPicked;
while (endI < count)
{
nextPicked = pick(endI, &nextEnd);
if (nextPicked != picked)
{
break;
}
endI = nextEnd;
}
if (picked == nullptr)
{
if (startI == 0 && endI == count)
{
gruns.add(std::move(gr));
}
else
{
gruns.add(extract_subset(gr, startI, endI));
}
}
else
{
const uint32_t textStart = gr.textIndices[startI];
const uint32_t textEnd = endI == count ? runTextEnd
: gr.textIndices[endI];
auto tr = rive::TextRun{
picked,
gr.size,
gr.lineHeight,
originalTextRun.letterSpacing,
textEnd - textStart,
originalTextRun.script,
gr.styleId,
gr.level,
gr.language,
};
const size_t firstRun = gruns.size();
static_cast<HBFont*>(picked.get())
->shapeFallbackRun(
gruns,
text,
textStart,
tr,
originalTextRun,
fallbackIndex + 1,
chain);
// The proc hands every thread this font, so the runs it shaped here
// draw with this thread's own (see HBFont::forThisThread). Fonts it
// fell back to in turn swapped theirs in their own calls.
rive::rcp<rive::Font> drawnHere;
for (auto run = gruns.begin() + firstRun; run != gruns.end(); run++)
{
if (run->font == picked)
{
if (drawnHere == nullptr)
{
drawnHere = static_cast<HBFont*>(picked.get())->forThisThread();
}
run->font = drawnHere;
}
}
}
startI = endI;
endI = nextEnd;
picked = std::move(nextPicked);
}
}
void HBFont::shapeFallbackRun(
rive::SimpleArrayBuilder<rive::GlyphRun>& gruns,
const rive::Unichar text[],
const unsigned textStart,
const rive::TextRun& textRun,
const rive::TextRun& originalTextRun,
const uint32_t fallbackIndex,
const FallbackChain* chain)
{
auto gr = shape_run(&text[textStart], textRun, textStart);
auto end = gr.glyphs.end();
if (std::find(gr.glyphs.begin(), end, 0) == end)
{
if (gr.glyphs.size() > 0)
{
gruns.add(std::move(gr));
}
}
else
{
// found at least 1 zero in glyphs, so need to perform
// font-fallback
const FallbackChain here = {this, chain};
fall_back_clusters(
this,
gruns,
text,
std::move(gr),
originalTextRun,
textStart + textRun.unicharCount,
fallbackIndex,
&here);
}
}
rive::SimpleArray<rive::Paragraph> HBFont::onShapeText(
rive::Span<const rive::Unichar> text,
rive::Span<const rive::TextRun> truns,
int textDirectionFlag) const
{
rive::SimpleArrayBuilder<rive::Paragraph> paragraphs;
SBCodepointSequence codepointSequence = {
SBStringEncodingUTF32,
(void*)text.data(),
text.size()};
hb_unicode_funcs_t* ufuncs = hb_unicode_funcs_get_default();
// Split runs by bidi types.
uint32_t textIndex = 0;
uint32_t runIndex = 0;
uint32_t runStartTextIndex = 0;
SBUInteger paragraphStart = 0;
SBAlgorithmRef bidiAlgorithm = SBAlgorithmCreate(&codepointSequence);
uint32_t unicharIndex = 0;
uint32_t runTextIndex = 0;
SBLevel defaultLevel;
switch (textDirectionFlag)
{
case 0:
defaultLevel = 0;
break;
case 1:
defaultLevel = 1;
break;
default:
defaultLevel = SBLevelDefaultLTR;
break;
}
while (paragraphStart < text.size())
{
SBParagraphRef paragraph = SBAlgorithmCreateParagraph(
bidiAlgorithm,
paragraphStart,
INT32_MAX,
defaultLevel);
SBUInteger paragraphLength = SBParagraphGetLength(paragraph);
// Next iteration reads the next paragraph (if any remain).
paragraphStart += paragraphLength;
const SBLevel* bidiLevels = SBParagraphGetLevelsPtr(paragraph);
SBLevel paragraphLevel = SBParagraphGetBaseLevel(paragraph);
uint32_t paragraphTextIndex = 0;
std::vector<rive::TextRun> bidiRuns;
bidiRuns.reserve(truns.size());
while (runIndex < truns.size())
{
const auto& tr = truns[runIndex];
assert(tr.unicharCount != 0);
SBLevel lastLevel = bidiLevels[paragraphTextIndex];
auto point = text[textIndex];
hb_script_t lastScript = hb_unicode_script(ufuncs, point);
rive::TextRun splitRun = {
tr.font,
tr.size,
tr.lineHeight,
tr.letterSpacing,
tr.unicharCount - runTextIndex,
(uint32_t)lastScript,
tr.styleId,
(uint8_t)lastLevel,
tr.language,
};
runStartTextIndex = textIndex;
runTextIndex++;
textIndex++;
paragraphTextIndex++;
bidiRuns.push_back(splitRun);
while (runTextIndex < tr.unicharCount &&
paragraphTextIndex < paragraphLength)
{
auto point = text[textIndex];
hb_script_t
script = hb_unicode_general_category(ufuncs, point) ==
HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK
? HB_SCRIPT_INHERITED
: hb_unicode_script(ufuncs, point);
switch (script)
{
case HB_SCRIPT_COMMON:
case HB_SCRIPT_INHERITED:
// Propagate last seen "real" script value.
script = lastScript;
break;
default:
break;
}
if (bidiLevels[paragraphTextIndex] != lastLevel || script != lastScript)
{
lastScript = script;
auto& back = bidiRuns.back();
back.unicharCount = textIndex - runStartTextIndex;
lastLevel = bidiLevels[paragraphTextIndex];
rive::TextRun backRun = {
back.font,
back.size,
back.lineHeight,
back.letterSpacing,
tr.unicharCount - runTextIndex,
(uint32_t)script,
back.styleId,
(uint8_t)lastLevel,
back.language,
};
runStartTextIndex = textIndex;
bidiRuns.push_back(backRun);
}
runTextIndex++;
textIndex++;
paragraphTextIndex++;
}
// Reached the end of the run?
if (runTextIndex == tr.unicharCount)
{
runIndex++;
runTextIndex = 0;
}
// We consumed the whole paragraph.
if (paragraphTextIndex == paragraphLength)
{
// Close off the last run.
auto& back = bidiRuns.back();
back.unicharCount = textIndex - runStartTextIndex;
break;
}
}
rive::SimpleArrayBuilder<rive::GlyphRun> gruns(bidiRuns.size());
for (const auto& tr : bidiRuns)
{
auto gr = shape_run(&text[unicharIndex], tr, unicharIndex);
unicharIndex += tr.unicharCount;
// The font the run is shaped in, which need not be this one.
auto runFont = static_cast<const HBFont*>(tr.font.get());
// Handle dual-presentation characters (e.g. ☠ U+2620) that have a
// text-form glyph in the current font but are followed by U+FE0F
// (VARIATION SELECTOR-16), which requests emoji presentation.
// The standard fallback only fires for glyph ID 0, so we explicitly
// force such glyphs to 0 here — but only when a color emoji
// fallback font is actually available for that codepoint, to avoid
// regressing the rendering when no emoji font is loaded. One 0 sends
// its whole cluster to fallback, so each cluster is checked once.
if (hasFallback() && !runFont->hasColorGlyphs())
{
for (size_t gi = 0; gi < gr.glyphs.size();)
{
bool missing;
size_t clusterEnd = cluster_end(gr, gi, &missing);
auto textPos = gr.textIndices[gi];
if (!missing && textPos + 1 < text.size() &&
text[textPos + 1] == 0xFE0F)
{
auto textEnd = cluster_text_end(gr, gi, unicharIndex);
auto emojiFont = requestFallback(
text[textPos],
rive::Span<const rive::Unichar>(
&text[textPos],
textEnd - textPos),
0,
runFont,
tr);
if (emojiFont && emojiFont->hasColorGlyphs())
{
gr.glyphs[gi] = 0;
}
}
gi = clusterEnd;
}
}
auto end = gr.glyphs.end();
if (std::find(gr.glyphs.begin(), end, 0) == end || !hasFallback())
{
if (gr.glyphs.size() > 0)
{
gruns.add(std::move(gr));
}
}
else
{
// found at least 1 zero in glyphs, so need to perform
// font-fallback
const FallbackChain chain = {runFont, nullptr};
fall_back_clusters(
runFont,
gruns,
text.data(),
std::move(gr),
tr,
unicharIndex,
0,
&chain);
}
}
// turn the advances we stored in xpos[] into actual x-positions
// for logical order.
float pos = 0;
for (auto& gr : gruns)
{
for (auto& xp : gr.xpos)
{
float adv = xp;
xp = pos;
pos += adv;
}
}
paragraphs.add({
std::move(gruns),
(uint8_t)paragraphLevel,
});
SBParagraphRelease(paragraph);
}
SBAlgorithmRelease(bidiAlgorithm);
return paragraphs;
}
std::vector<rive::Unichar> HBFont::codepoints() const
{
auto* coverage = hb_set_create();
hb_face_collect_unicodes(hb_font_get_face(m_font), coverage);
std::vector<rive::Unichar> result;
result.reserve(hb_set_get_population(coverage));
hb_codepoint_t cp = HB_SET_VALUE_INVALID;
while (hb_set_next(coverage, &cp))
{
if (cp > 0x10ffff)
break;
if ((cp < 0xd800 || cp > 0xdfff) && hasGlyph(cp))
result.push_back(cp);
}
hb_set_destroy(coverage);
return result;
}
bool HBFont::hasGlyph(const rive::Unichar missing) const
{
hb_codepoint_t glyph;
return hb_font_get_nominal_glyph(m_font, missing, &glyph);
}
#endif