Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bold support for CJK characters #3069

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions platform/darwin/src/local_glyph_rasterizer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,29 @@ CFDictionaryRefHandle attributes(
@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");
}

// Can't use CTFontRefHandle
// because the boldFont will be released after it is out of the isBold condition
CTFontRef boldFont = NULL;
if (isBold)
{
// Create a bold variant of the font
boldFont = CTFontCreateCopyWithSymbolicTraits(font, 0.0, NULL, kCTFontBoldTrait, kCTFontBoldTrait);
if (!boldFont) {
throw std::runtime_error("Unable to create bold font");
wangyingfang marked this conversation as resolved.
Show resolved Hide resolved
}
}

CFStringRef keys[] = { kCTFontAttributeName };
CFTypeRef values[] = { font };
CFTypeRef values[] = { boldFont ? boldFont : font };

CFDictionaryRefHandle attributes(
CFDictionaryCreate(kCFAllocatorDefault, (const void**)&keys,
Expand Down Expand Up @@ -266,6 +279,11 @@ CGContextHandle context(CGBitmapContextCreate(

CTLineDraw(*line, *context);

// Release the bold font if it was created
if (boldFont) {
CFRelease(boldFont);
wangyingfang marked this conversation as resolved.
Show resolved Hide resolved
}

return rgbaBitmap;
}

Expand All @@ -288,8 +306,16 @@ CGContextHandle context(CGBitmapContextCreate(
}

manufacturedGlyph.id = glyphID;
BOOL isBold = NO;
for (auto& fontName : fontStack) {
std::string lowercaseFont = platform::lowercase(fontName);
if (lowercaseFont.find("bold") != std::string::npos) {
isBold = YES;
break;
}
}

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
Expand Down
Loading