Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a2a0c8a

Browse files
reed-at-googleSkia Commit-Bot
authored andcommitted
extract font from run
Change-Id: Ice98eeb1f815cf07f842b22ddc26e32e849a1218 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/267256 Reviewed-by: Ben Wagner <[email protected]> Commit-Queue: Mike Reed <[email protected]>
1 parent d960cc3 commit a2a0c8a

3 files changed

Lines changed: 33 additions & 10 deletions

File tree

include/ports/SkTypeface_mac.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* https://crbug.com/413332 .
3333
*/
3434
SK_API extern SkTypeface* SkCreateTypefaceFromCTFont(CTFontRef, CFTypeRef = NULL);
35+
SK_API extern sk_sp<SkTypeface> SkMakeTypefaceFromCTFont(CTFontRef);
3536

3637
/**
3738
* Returns the platform-specific CTFontRef handle for a

modules/skshaper/src/SkShaper_coretext.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,26 @@ static void dump(CFDictionaryRef d) {
146146
}
147147
}
148148

149+
static CTFontRef create_ctfont_from_font(const SkFont& font) {
150+
auto typeface = font.getTypefaceOrDefault();
151+
auto ctfont = SkTypeface_GetCTFontRef(typeface);
152+
return CTFontCreateCopyWithAttributes(ctfont, font.getSize(), nullptr, nullptr);
153+
}
154+
155+
static SkFont run_to_font(CTRunRef run, const SkFont& orig) {
156+
CFDictionaryRef attr = CTRunGetAttributes(run);
157+
CTFontRef ct = (CTFontRef)CFDictionaryGetValue(attr, kCTFontAttributeName);
158+
if (!ct) {
159+
SkDebugf("no ctfont in Run Attributes\n");
160+
dump(attr);
161+
return orig;
162+
}
163+
// Do I need to add a local cache, or allow the caller to manage this lookup?
164+
SkFont font(orig);
165+
font.setTypeface(SkMakeTypefaceFromCTFont(ct));
166+
return font;
167+
}
168+
149169
// kCTTrackingAttributeName not available until 10.12
150170
const CFStringRef kCTTracking_AttributeName = CFSTR("CTTracking");
151171

@@ -167,16 +187,13 @@ void SkShaper_CoreText::shape(const char* utf8, size_t utf8Bytes,
167187
AutoCF<CFStringRef> textString = CFStringCreateWithBytes(nullptr, (const uint8_t*)utf8, utf8Bytes,
168188
kCFStringEncodingUTF8, false);
169189

170-
auto typeface = font.getTypefaceOrDefault();
171-
auto ctfont = SkTypeface_GetCTFontRef(typeface);
172-
AutoCF<CTFontRef> ctfont2 = CTFontCreateCopyWithAttributes(ctfont, font.getSize(),
173-
nullptr, nullptr);
190+
AutoCF<CTFontRef> ctfont = create_ctfont_from_font(font);
174191

175192
AutoCF<CFMutableDictionaryRef> attr =
176193
CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
177194
&kCFTypeDictionaryKeyCallBacks,
178195
&kCFTypeDictionaryValueCallBacks);
179-
CFDictionaryAddValue(attr.get(), kCTFontAttributeName, ctfont2.get());
196+
CFDictionaryAddValue(attr.get(), kCTFontAttributeName, ctfont.get());
180197
if (false) {
181198
// trying to see what these affect
182199
dict_add_double(attr.get(), kCTTracking_AttributeName, 1);
@@ -207,10 +224,6 @@ void SkShaper_CoreText::shape(const char* utf8, size_t utf8Bytes,
207224
for (CFIndex j = 0; j < runCount; ++j) {
208225
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(run_array, j);
209226
CFIndex runGlyphs = CTRunGetGlyphCount(run);
210-
CFDictionaryRef runAttr = CTRunGetAttributes(run);
211-
if (false) {
212-
dump(runAttr);
213-
}
214227

215228
SkASSERT(sizeof(CGGlyph) == sizeof(uint16_t));
216229

@@ -224,8 +237,9 @@ void SkShaper_CoreText::shape(const char* utf8, size_t utf8Bytes,
224237

225238
CFRange range = CTRunGetStringRange(run);
226239

240+
SkFont run_font = run_to_font(run, font);
227241
infos.push_back({
228-
font, // need resolved font
242+
run_font,
229243
0, // need fBidiLevel
230244
{adv, 0},
231245
(size_t)runGlyphs,

src/ports/SkFontHost_mac.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,14 @@ static sk_sp<SkTypeface> create_from_name(const char familyName[], const SkFontS
930930
/* This function is visible on the outside. It first searches the cache, and if
931931
* not found, returns a new entry (after adding it to the cache).
932932
*/
933+
sk_sp<SkTypeface> SkMakeTypefaceFromCTFont(CTFontRef font) {
934+
CFRetain(font);
935+
return create_from_CTFontRef(SkUniqueCFRef<CTFontRef>(font),
936+
nullptr,
937+
OpszVariation(),
938+
nullptr);
939+
}
940+
933941
SkTypeface* SkCreateTypefaceFromCTFont(CTFontRef font, CFTypeRef resource) {
934942
CFRetain(font);
935943
if (resource) {

0 commit comments

Comments
 (0)