Add HarfBuzz Text Shaping and Font Fallback Support (#3611)

Co-authored-by: alan wei chen <alanchenboy@gmail.com>
This commit is contained in:
Stefan Karschti
2025-07-29 23:00:27 +03:00
committed by GitHub
parent 60264349c1
commit 09d395d0db
82 changed files with 27006 additions and 151 deletions

View File

@@ -7,6 +7,10 @@
#include <mbgl/util/async_request.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/tiny_sdf.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/logging.hpp>
#include <fstream>
namespace mbgl {
@@ -16,11 +20,12 @@ GlyphManager::GlyphManager(std::unique_ptr<LocalGlyphRasterizer> localGlyphRaste
: observer(&nullObserver),
localGlyphRasterizer(std::move(localGlyphRasterizer_)) {}
GlyphManager::~GlyphManager() = default;
GlyphManager::~GlyphManager() {
hbShapers.clear(); // clear harfbuzz + freetype face before library;
}
void GlyphManager::getGlyphs(GlyphRequestor& requestor, GlyphDependencies glyphDependencies, FileSource& fileSource) {
auto dependencies = std::make_shared<GlyphDependencies>(std::move(glyphDependencies));
{
std::lock_guard<std::recursive_mutex> readWriteLock(rwLock);
// Figure out which glyph ranges need to be fetched. For each range that
@@ -28,7 +33,7 @@ void GlyphManager::getGlyphs(GlyphRequestor& requestor, GlyphDependencies glyphD
// shared pointer containing the dependencies. When the shared pointer
// becomes unique, we know that all the dependencies for that requestor have
// been fetched, and can notify it of completion.
for (const auto& dependency : *dependencies) {
for (const auto& dependency : dependencies->glyphs) {
const FontStack& fontStack = dependency.first;
Entry& entry = entries[fontStack];
@@ -75,12 +80,26 @@ void GlyphManager::requestRange(GlyphRequest& request,
if (request.req) {
return;
}
Resource res(Resource::Kind::Unknown, "");
switch (range.type) {
case GlyphIDType::FontPBF:
res = Resource::glyphs(glyphURL, fontStack, std::pair<uint16_t, uint16_t>{range.first, range.second});
break;
default: {
std::string url = getFontFaceURL(range.type);
if (url.size()) {
res = Resource::fontFace(url);
} else {
Log::Error(Event::Style, "Try download a glyph doesn't in current faces");
}
} break;
}
observer->onGlyphsRequested(fontStack, range);
request.req = fileSource.request(
Resource::glyphs(glyphURL, fontStack, range),
[this, fontStack, range](const Response& res) { processResponse(res, fontStack, range); });
res, [this, fontStack, range](const Response& response) { processResponse(response, fontStack, range); });
}
void GlyphManager::processResponse(const Response& res, const FontStack& fontStack, const GlyphRange& range) {
@@ -103,7 +122,15 @@ void GlyphManager::processResponse(const Response& res, const FontStack& fontSta
std::vector<Glyph> glyphs;
try {
glyphs = parseGlyphPBF(range, *res.data);
if (range.type == GlyphIDType::FontPBF) {
glyphs = parseGlyphPBF(range, *res.data);
} else {
if (loadHBShaper(fontStack, range.type, *res.data)) {
Glyph temp;
temp.id = GlyphID(0, range.type);
glyphs.emplace_back(std::move(temp));
}
}
} catch (...) {
observer->onGlyphsError(fontStack, range, std::current_exception());
return;
@@ -141,7 +168,7 @@ void GlyphManager::setObserver(GlyphManagerObserver* observer_) {
void GlyphManager::notify(GlyphRequestor& requestor, const GlyphDependencies& glyphDependencies) {
GlyphMap response;
for (const auto& dependency : glyphDependencies) {
for (const auto& dependency : glyphDependencies.glyphs) {
const FontStack& fontStack = dependency.first;
const GlyphIDs& glyphIDs = dependency.second;
@@ -158,7 +185,7 @@ void GlyphManager::notify(GlyphRequestor& requestor, const GlyphDependencies& gl
}
}
requestor.onGlyphsAvailable(response);
requestor.onGlyphsAvailable(response, glyphDependencies.shapes);
}
void GlyphManager::removeRequestor(GlyphRequestor& requestor) {
@@ -175,4 +202,68 @@ void GlyphManager::evict(const std::set<FontStack>& keep) {
util::erase_if(entries, [&](const auto& entry) { return keep.count(entry.first) == 0; });
}
std::shared_ptr<HBShaper> GlyphManager::getHBShaper(FontStack fontStack, GlyphIDType type) {
if (hbShapers.find(fontStack) != hbShapers.end()) {
auto& glyphs = hbShapers[fontStack];
if (glyphs.find(type) != glyphs.end()) {
return glyphs[type];
}
}
return nullptr;
}
bool GlyphManager::loadHBShaper(const FontStack& fontStack, GlyphIDType type, const std::string& data) {
auto shaper = std::make_shared<HBShaper>(type, data, ftLibrary);
if (!shaper->valid()) return false;
hbShapers[fontStack][type] = shaper;
return true;
}
Immutable<Glyph> GlyphManager::getGlyph(const FontStack& fontStack, GlyphID glyphID) {
auto& entry = entries[fontStack];
if (entry.glyphs.find(glyphID) != entry.glyphs.end()) return entry.glyphs.at(glyphID);
if (glyphID.complex.type != FontPBF) {
auto shaper = getHBShaper(fontStack, glyphID.complex.type);
if (shaper) {
auto glyph = shaper->rasterizeGlyph(glyphID);
glyph.bitmap = util::transformRasterToSDF(glyph.bitmap, 8, .25);
entry.glyphs.emplace(glyphID, makeMutable<Glyph>(std::move(glyph)));
return entry.glyphs.at(glyphID);
}
}
Glyph empty;
return makeMutable<Glyph>(std::move(empty));
}
void GlyphManager::hbShaping(const std::u16string& text,
const FontStack& font,
GlyphIDType type,
std::vector<GlyphID>& glyphIDs,
std::vector<HBShapeAdjust>& adjusts) {
auto shaper = getHBShaper(font, type);
if (shaper) {
shaper->createComplexGlyphIDs(text, glyphIDs, adjusts);
}
}
std::string GlyphManager::getFontFaceURL(GlyphIDType type) {
std::string url;
if (fontFaces) {
for (auto& face : *fontFaces) {
if (face.type == type) {
url = face.url;
break;
}
}
}
return url;
}
} // namespace mbgl