Skip to content

Commit

Permalink
add bold support for cjk
Browse files Browse the repository at this point in the history
  • Loading branch information
Yingfang committed Dec 5, 2024
1 parent 112edb6 commit 73b61a9
Showing 1 changed file with 29 additions and 3 deletions.
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");
}
}

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);
}

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

0 comments on commit 73b61a9

Please sign in to comment.