Clean up SkShaper_coretext.cpp & SkCGBase.h

- Use SkUniqueCFRef.
- Use CFShow to print dictionary.
- Use CGFLOAT_IS_DOUBLE define.
- Use kCFAllocatorDefault consistently.

Change-Id: If0276cab0182d33aacf1060a734b8b025e16f99e
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/295010
Commit-Queue: Adlai Holler <adlai@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
Auto-Submit: Adlai Holler <adlai@google.com>
This commit is contained in:
Adlai Holler 2020-06-08 14:22:40 -04:00 committed by Skia Commit-Bot
parent d470f40272
commit f54dbf71e8
2 changed files with 22 additions and 64 deletions

View File

@ -20,6 +20,8 @@
#include "include/ports/SkTypeface_mac.h"
#include "src/core/SkArenaAlloc.h"
#include "src/utils/mac/SkCGBase.h"
#include "src/utils/mac/SkUniqueCFRef.h"
#include <vector>
@ -94,15 +96,6 @@ void SkShaper_CoreText::shape(const char* utf8, size_t utf8Bytes,
width, handler);
}
template <typename T> class AutoCF {
T fObj;
public:
AutoCF(T obj) : fObj(obj) {}
~AutoCF() { CFRelease(fObj); }
T get() const { return fObj; }
};
// CTFramesetter/CTFrame can do this, but require version 10.14
class LineBreakIter {
CTTypesetterRef fTypesetter;
@ -126,28 +119,16 @@ public:
};
static void dict_add_double(CFMutableDictionaryRef d, const void* name, double value) {
AutoCF<CFNumberRef> number = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &value);
SkUniqueCFRef<CFNumberRef> number(
CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &value));
CFDictionaryAddValue(d, name, number.get());
}
static void dump(CFDictionaryRef d) {
CFIndex count = CFDictionaryGetCount(d);
std::vector<const void*> keys(count);
std::vector<const void*> vals(count);
CFDictionaryGetKeysAndValues(d, keys.data(), vals.data());
for (CFIndex i = 0; i < count; ++i) {
CFStringRef kstr = (CFStringRef)keys[i];
const char* ckstr = CFStringGetCStringPtr(kstr, kCFStringEncodingUTF8);
SkDebugf("dict[%d] %s %p\n", i, ckstr, vals[i]);
}
}
static CTFontRef create_ctfont_from_font(const SkFont& font) {
static SkUniqueCFRef<CTFontRef> create_ctfont_from_font(const SkFont& font) {
auto typeface = font.getTypefaceOrDefault();
auto ctfont = SkTypeface_GetCTFontRef(typeface);
return CTFontCreateCopyWithAttributes(ctfont, font.getSize(), nullptr, nullptr);
return SkUniqueCFRef<CTFontRef>(
CTFontCreateCopyWithAttributes(ctfont, font.getSize(), nullptr, nullptr));
}
static SkFont run_to_font(CTRunRef run, const SkFont& orig) {
@ -155,7 +136,7 @@ static SkFont run_to_font(CTRunRef run, const SkFont& orig) {
CTFontRef ct = (CTFontRef)CFDictionaryGetValue(attr, kCTFontAttributeName);
if (!ct) {
SkDebugf("no ctfont in Run Attributes\n");
dump(attr);
CFShow(attr);
return orig;
}
// Do I need to add a local cache, or allow the caller to manage this lookup?
@ -172,25 +153,16 @@ void SkShaper_CoreText::shape(const char* utf8, size_t utf8Bytes,
bool /* leftToRight */,
SkScalar width,
RunHandler* handler) const {
auto cgfloat_to_scalar = [](CGFloat x) {
SkScalar s;
if (sizeof(CGFloat) == sizeof(double)) {
s = SkDoubleToScalar(x);
} else {
s = x;
}
return s;
};
SkUniqueCFRef<CFStringRef> textString(
CFStringCreateWithBytes(kCFAllocatorDefault, (const uint8_t*)utf8, utf8Bytes,
kCFStringEncodingUTF8, false));
AutoCF<CFStringRef> textString = CFStringCreateWithBytes(nullptr, (const uint8_t*)utf8, utf8Bytes,
kCFStringEncodingUTF8, false);
SkUniqueCFRef<CTFontRef> ctfont = create_ctfont_from_font(font);
AutoCF<CTFontRef> ctfont = create_ctfont_from_font(font);
AutoCF<CFMutableDictionaryRef> attr =
SkUniqueCFRef<CFMutableDictionaryRef> attr(
CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
&kCFTypeDictionaryValueCallBacks));
CFDictionaryAddValue(attr.get(), kCTFontAttributeName, ctfont.get());
if (false) {
// trying to see what these affect
@ -198,10 +170,11 @@ void SkShaper_CoreText::shape(const char* utf8, size_t utf8Bytes,
dict_add_double(attr.get(), kCTKernAttributeName, 0.0);
}
AutoCF<CFAttributedStringRef> attrString =
CFAttributedStringCreate(nullptr, textString.get(), attr.get());
SkUniqueCFRef<CFAttributedStringRef> attrString(
CFAttributedStringCreate(kCFAllocatorDefault, textString.get(), attr.get()));
AutoCF<CTTypesetterRef> typesetter = CTTypesetterCreateWithAttributedString(attrString.get());
SkUniqueCFRef<CTTypesetterRef> typesetter(
CTTypesetterCreateWithAttributedString(attrString.get()));
SkSTArenaAlloc<4096> arena;
@ -269,7 +242,7 @@ void SkShaper_CoreText::shape(const char* utf8, size_t utf8Bytes,
for (CFIndex k = 0; k < runGlyphs; ++k) {
buffer.positions[k] = {
buffer.point.fX + cgfloat_to_scalar(positions[k].x),
buffer.point.fX + SkScalarFromCGFloat(positions[k].x),
buffer.point.fY,
};
if (buffer.offsets) {

View File

@ -22,30 +22,15 @@
// Skia extensions for types in CGBase.h
static inline CGFloat SkScalarToCGFloat(SkScalar scalar) {
if (sizeof(CGFloat) == sizeof(float)) {
return SkScalarToFloat(scalar);
} else {
SkASSERT(sizeof(CGFloat) == sizeof(double));
return (CGFloat) SkScalarToDouble(scalar);
}
return CGFLOAT_IS_DOUBLE ? SkScalarToDouble(scalar) : SkScalarToFloat(scalar);
}
static inline SkScalar SkScalarFromCGFloat(CGFloat cgFloat) {
if (sizeof(CGFloat) == sizeof(float)) {
return SkFloatToScalar(cgFloat);
} else {
SkASSERT(sizeof(CGFloat) == sizeof(double));
return SkDoubleToScalar(cgFloat);
}
return CGFLOAT_IS_DOUBLE ? SkDoubleToScalar(cgFloat) : SkFloatToScalar(cgFloat);
}
static inline float SkFloatFromCGFloat(CGFloat cgFloat) {
if (sizeof(CGFloat) == sizeof(float)) {
return cgFloat;
} else {
SkASSERT(sizeof(CGFloat) == sizeof(double));
return static_cast<float>(cgFloat);
}
return CGFLOAT_IS_DOUBLE ? static_cast<float>(cgFloat) : cgFloat;
}
#endif