Add bold support for CJK characters (#3069)
Co-authored-by: Yingfang <junew@microsoft.com> Co-authored-by: Bart Louwers <bart@emeel.net>
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
#include <mbgl/text/local_glyph_rasterizer.hpp>
|
||||
#include <mbgl/util/i18n.hpp>
|
||||
#include <mbgl/util/logging.hpp>
|
||||
#include <mbgl/util/platform.hpp>
|
||||
#include <mbgl/util/constants.hpp>
|
||||
|
||||
#include <mbgl/interface/native_apple_interface.h>
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
@@ -191,16 +194,28 @@ bool LocalGlyphRasterizer::canRasterizeGlyph(const FontStack&, GlyphID glyphID)
|
||||
@param font The font to apply to the codepoint.
|
||||
@param metrics Upon return, the metrics match the font’s metrics for the glyph
|
||||
representing the codepoint.
|
||||
@param isBold use kCTFontBoldTrait if it is true.
|
||||
@returns An image containing the glyph.
|
||||
*/
|
||||
PremultipliedImage drawGlyphBitmap(GlyphID glyphID, CTFontRef font, GlyphMetrics& metrics) {
|
||||
PremultipliedImage drawGlyphBitmap(GlyphID glyphID, CTFontRef font, GlyphMetrics& metrics, BOOL isBold) {
|
||||
CFStringRefHandle string(CFStringCreateWithCharacters(NULL, reinterpret_cast<UniChar*>(&glyphID), 1));
|
||||
if (!string) {
|
||||
throw std::runtime_error("Unable to create string from codepoint");
|
||||
}
|
||||
|
||||
// Create a bold variant of the font
|
||||
CTFontRefHandle boldFont(CTFontCreateCopyWithSymbolicTraits(font, 0.0, NULL, kCTFontBoldTrait, kCTFontBoldTrait));
|
||||
if (!boldFont) {
|
||||
CFStringRefHandle familyNameHandle(CTFontCopyFamilyName(font));
|
||||
NSString* familyName = (__bridge NSString *)(*familyNameHandle);
|
||||
std::string stdFamilyName(familyName.UTF8String);
|
||||
Log::Error(Event::General, "Unable to create bold font for " + stdFamilyName);
|
||||
}
|
||||
|
||||
CTFontRef drawFont = isBold && boldFont ? *boldFont : font;
|
||||
|
||||
CFStringRef keys[] = { kCTFontAttributeName };
|
||||
CFTypeRef values[] = { font };
|
||||
CFTypeRef values[] = { drawFont };
|
||||
|
||||
CFDictionaryRefHandle attributes(
|
||||
CFDictionaryCreate(kCFAllocatorDefault, (const void**)&keys,
|
||||
@@ -288,8 +303,16 @@ Glyph LocalGlyphRasterizer::rasterizeGlyph(const FontStack& fontStack, GlyphID g
|
||||
}
|
||||
|
||||
manufacturedGlyph.id = glyphID;
|
||||
BOOL isBold = NO;
|
||||
// Only check the first font name to detect if the user prefers using bold
|
||||
if (!fontStack.empty()) {
|
||||
std::string lowercaseFont = platform::lowercase(fontStack.front());
|
||||
if (lowercaseFont.find("bold") != std::string::npos && lowercaseFont.find("semibold") == std::string::npos) {
|
||||
isBold = YES;
|
||||
}
|
||||
}
|
||||
|
||||
PremultipliedImage rgbaBitmap = drawGlyphBitmap(glyphID, *font, manufacturedGlyph.metrics);
|
||||
PremultipliedImage rgbaBitmap = drawGlyphBitmap(glyphID, *font, manufacturedGlyph.metrics, isBold);
|
||||
|
||||
Size size(manufacturedGlyph.metrics.width, manufacturedGlyph.metrics.height);
|
||||
// Copy alpha values from RGBA bitmap into the AlphaImage output
|
||||
|
||||
BIN
test/fixtures/local_glyphs/ping_fang_with_bold_in_style/expected.png
vendored
Normal file
BIN
test/fixtures/local_glyphs/ping_fang_with_bold_in_style/expected.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
@@ -10,6 +10,8 @@
|
||||
#include <mbgl/gfx/headless_frontend.hpp>
|
||||
#include <mbgl/style/style.hpp>
|
||||
|
||||
#include <regex>
|
||||
|
||||
/*
|
||||
LoadLocalCJKGlyph in glyph_manager.test.cpp exercises the
|
||||
platform-independent part of LocalGlyphRasterizer. This test actually
|
||||
@@ -17,13 +19,8 @@
|
||||
on. Different platforms have different default fonts, so adding a new
|
||||
platform requires new "expected" fixtures.
|
||||
|
||||
At the time of writing, we don't run `mbgl-test` on iOS or Android, so the
|
||||
only supported test platform is macOS. Supporting Android would require
|
||||
adding a new test case (probably using the "Droid" font family). iOS should
|
||||
theoretically work -- the "PingFang" font family used below is expected to be
|
||||
available on all iOS devices, and we use a relatively high image diff
|
||||
tolerance (0.05) to account for small changes between the many possible
|
||||
variants of the PingFang family.
|
||||
At the time of writing, we don't run this test on Android, that would require
|
||||
adding a new test case (probably using the "Droid" font family).
|
||||
*/
|
||||
|
||||
using namespace mbgl;
|
||||
@@ -33,7 +30,14 @@ namespace {
|
||||
class LocalGlyphRasterizerTest {
|
||||
public:
|
||||
LocalGlyphRasterizerTest(const std::optional<std::string> fontFamily)
|
||||
: frontend(1, gfx::HeadlessBackend::SwapBehaviour::NoFlush, gfx::ContextMode::Unique, fontFamily) {}
|
||||
: frontend(1, gfx::HeadlessBackend::SwapBehaviour::NoFlush, gfx::ContextMode::Unique, fontFamily) {
|
||||
this->fileSource->glyphsResponse = [&](const Resource& resource) {
|
||||
EXPECT_EQ(Resource::Kind::Glyphs, resource.kind);
|
||||
Response response;
|
||||
response.data = std::make_shared<std::string>(util::read_file("test/fixtures/resources/glyphs.pbf"));
|
||||
return response;
|
||||
};
|
||||
}
|
||||
|
||||
util::RunLoop loop;
|
||||
std::shared_ptr<StubFileSource> fileSource = std::make_shared<StubFileSource>();
|
||||
@@ -59,12 +63,6 @@ public:
|
||||
TEST(LocalGlyphRasterizer, PingFang) {
|
||||
LocalGlyphRasterizerTest test(std::string("PingFang TC"));
|
||||
|
||||
test.fileSource->glyphsResponse = [&](const Resource& resource) {
|
||||
EXPECT_EQ(Resource::Kind::Glyphs, resource.kind);
|
||||
Response response;
|
||||
response.data = std::make_shared<std::string>(util::read_file("test/fixtures/resources/glyphs.pbf"));
|
||||
return response;
|
||||
};
|
||||
test.map.getStyle().loadJSON(util::read_file("test/fixtures/local_glyphs/mixed.json"));
|
||||
#if defined(__APPLE__) && !defined(__QT__)
|
||||
test.checkRendering("ping_fang", 0.0161);
|
||||
@@ -73,16 +71,19 @@ TEST(LocalGlyphRasterizer, PingFang) {
|
||||
#endif // defined(__APPLE__)
|
||||
}
|
||||
|
||||
TEST(LocalGlyphRasterizer, PingFangWithBoldInStyle) {
|
||||
LocalGlyphRasterizerTest test(std::string("PingFang TC"));
|
||||
std::stringstream ss;
|
||||
ss << std::regex_replace(
|
||||
util::read_file("test/fixtures/local_glyphs/mixed.json"), std::regex("NotoCJK"), "NotoCJK Bold");
|
||||
test.map.getStyle().loadJSON(ss.str());
|
||||
test.checkRendering("ping_fang_with_bold_in_style");
|
||||
}
|
||||
|
||||
#if !defined(__QT__)
|
||||
TEST(LocalGlyphRasterizer, PingFangSemibold) {
|
||||
LocalGlyphRasterizerTest test(std::string("PingFang TC Semibold"));
|
||||
|
||||
test.fileSource->glyphsResponse = [&](const Resource& resource) {
|
||||
EXPECT_EQ(Resource::Kind::Glyphs, resource.kind);
|
||||
Response response;
|
||||
response.data = std::make_shared<std::string>(util::read_file("test/fixtures/resources/glyphs.pbf"));
|
||||
return response;
|
||||
};
|
||||
test.map.getStyle().loadJSON(util::read_file("test/fixtures/local_glyphs/mixed.json"));
|
||||
test.checkRendering("ping_fang_semibold", 0.0161);
|
||||
}
|
||||
@@ -94,13 +95,6 @@ TEST(LocalGlyphRasterizer, PingFangSemibold) {
|
||||
TEST(LocalGlyphRasterizer, NotoSansCJK) {
|
||||
LocalGlyphRasterizerTest test(std::string("Noto Sans CJK KR Regular"));
|
||||
|
||||
test.fileSource->glyphsResponse = [&](const Resource& resource) {
|
||||
EXPECT_EQ(Resource::Kind::Glyphs, resource.kind);
|
||||
Response response;
|
||||
response.data = std::make_shared<std::string>(util::read_file("test/fixtures/resources/glyphs.pbf"));
|
||||
return response;
|
||||
};
|
||||
|
||||
test.map.getStyle().loadJSON(util::read_file("test/fixtures/local_glyphs/mixed.json"));
|
||||
test.checkRendering("noto_sans_cjk_kr_regular_qt");
|
||||
}
|
||||
@@ -110,13 +104,6 @@ TEST(LocalGlyphRasterizer, NoLocal) {
|
||||
// Expectation: without any local fonts set, and without any CJK glyphs
|
||||
// provided, the output should just contain basic latin characters.
|
||||
LocalGlyphRasterizerTest test({});
|
||||
|
||||
test.fileSource->glyphsResponse = [&](const Resource& resource) {
|
||||
EXPECT_EQ(Resource::Kind::Glyphs, resource.kind);
|
||||
Response response;
|
||||
response.data = std::make_shared<std::string>(util::read_file("test/fixtures/resources/glyphs.pbf"));
|
||||
return response;
|
||||
};
|
||||
test.map.getStyle().loadJSON(util::read_file("test/fixtures/local_glyphs/mixed.json"));
|
||||
test.checkRendering("no_local", 0.001, 0.1);
|
||||
}
|
||||
@@ -126,13 +113,6 @@ TEST(LocalGlyphRasterizer, NoLocalWithContentInsets) {
|
||||
// center. Rendered text should be on the same offset and keep the same size
|
||||
// as with no offset.
|
||||
LocalGlyphRasterizerTest test({});
|
||||
|
||||
test.fileSource->glyphsResponse = [&](const Resource& resource) {
|
||||
EXPECT_EQ(Resource::Kind::Glyphs, resource.kind);
|
||||
Response response;
|
||||
response.data = std::make_shared<std::string>(util::read_file("test/fixtures/resources/glyphs.pbf"));
|
||||
return response;
|
||||
};
|
||||
auto viewSize = test.frontend.getSize();
|
||||
test.map.getStyle().loadJSON(util::read_file("test/fixtures/local_glyphs/mixed.json"));
|
||||
|
||||
@@ -149,13 +129,6 @@ TEST(LocalGlyphRasterizer, NoLocalWithContentInsetsAndPitch) {
|
||||
// center. Rendered text should be on the same offset and keep the same size
|
||||
// as with no offset.
|
||||
LocalGlyphRasterizerTest test({});
|
||||
|
||||
test.fileSource->glyphsResponse = [&](const Resource& resource) {
|
||||
EXPECT_EQ(Resource::Kind::Glyphs, resource.kind);
|
||||
Response response;
|
||||
response.data = std::make_shared<std::string>(util::read_file("test/fixtures/resources/glyphs.pbf"));
|
||||
return response;
|
||||
};
|
||||
auto viewSize = test.frontend.getSize();
|
||||
test.map.getStyle().loadJSON(util::read_file("test/fixtures/local_glyphs/mixed.json"));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user