提交 c8769743 编写于 作者: C Chinmay Garde 提交者: GitHub

Attempt font fallback resolution using CoreText on iOS. (#2800)

上级 0b1986a5
......@@ -332,6 +332,14 @@ source_set("platform") {
sources += [ "fonts/apple/FontPlatformDataApple.cpp" ]
}
if (is_ios) {
sources += [ "fonts/apple/FontCacheIOS.mm" ]
}
if (is_mac && !is_ios) {
sources += [ "fonts/apple/FontCacheMac.cpp" ]
}
configs += [
"//sky/engine:config",
"//sky/engine:non_test_config",
......
/*
* Copyright (c) 2016, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#import <CoreText/CoreText.h>
#import <UIKit/UIKit.h>
#include "sky/engine/platform/fonts/FontCache.h"
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/mac/scoped_nsobject.h"
#include "base/lazy_instance.h"
#include "base/macros.h"
#include "base/logging.h"
namespace blink {
template <class T>
class CFRef {
public:
CFRef() : _ref(nullptr) {}
CFRef(T t) : _ref(t) {}
CFRef(const CFRef&) = delete;
void operator=(const CFRef&) = delete;
CFRef(CFRef&& o) : _ref(o._ref) { o._ref = nullptr; }
void operator=(CFRef&& o) {
_ref = o._ref;
o._ref = nullptr;
}
operator bool() const { return _ref != nullptr; }
T get() const { return _ref; }
~CFRef() {
if (_ref != nullptr) {
CFRelease(reinterpret_cast<CFTypeRef>(_ref));
}
}
private:
T _ref;
};
class FontFallbackSelector {
public:
FontFallbackSelector() {
base::mac::ScopedNSAutoreleasePool pool;
UIFontDescriptor* ui_desc =
[UIFont systemFontOfSize:[UIFont systemFontSize]].fontDescriptor;
CFRef<CTFontDescriptorRef> desc(CTFontDescriptorCreateWithNameAndSize(
reinterpret_cast<CFStringRef>(ui_desc.postscriptName),
ui_desc.pointSize));
if (!desc) {
return;
}
_prototype = CFRef<CTFontRef>{
CTFontCreateWithFontDescriptor(desc.get(), ui_desc.pointSize, nullptr)};
}
CFRef<CTFontDescriptorRef> fallbackFont(unichar glyph) {
if (!_prototype) {
return {};
}
base::mac::ScopedNSAutoreleasePool pool;
CFRef<CFStringRef> unicode_string(CFStringCreateWithFormat(
kCFAllocatorDefault, nullptr, CFSTR("%C"), glyph));
if (!unicode_string) {
return {};
}
CFRef<CTFontRef> font(CTFontCreateForString(
_prototype.get(), unicode_string.get(),
CFRangeMake(0, CFStringGetLength(unicode_string.get()))));
if (!font) {
return {};
}
return CTFontCopyFontDescriptor(font.get());
}
private:
CFRef<CTFontRef> _prototype;
DISALLOW_COPY_AND_ASSIGN(FontFallbackSelector);
};
static base::LazyInstance<FontFallbackSelector> g_fallback_selector =
LAZY_INSTANCE_INITIALIZER;
void FontCache::getFontForCharacter(
UChar32 c,
const char*,
FontCache::PlatformFallbackFont* fallbackFont) {
if (fallbackFont == nullptr) {
return;
}
base::mac::ScopedNSAutoreleasePool pool;
fallbackFont->name = "";
fallbackFont->filename = "";
fallbackFont->ttcIndex = 0;
fallbackFont->fontconfigInterfaceId = 0;
fallbackFont->isBold = false;
fallbackFont->isItalic = false;
CFRef<CTFontDescriptorRef> font =
g_fallback_selector.Pointer()->fallbackFont(c);
if (!font) {
return;
}
CFRef<CFURLRef> cf_url(reinterpret_cast<CFURLRef>(
CTFontDescriptorCopyAttribute(font.get(), kCTFontURLAttribute)));
if (!cf_url) {
return;
}
CFRef<CFURLRef> cf_absolute_url(CFURLCopyAbsoluteURL(cf_url.get()));
if (!cf_absolute_url) {
return;
}
const NSURL* url = reinterpret_cast<const NSURL*>(cf_absolute_url.get());
fallbackFont->filename = [url fileSystemRepresentation];
CFRef<CFStringRef> cf_name(reinterpret_cast<CFStringRef>(
CTFontDescriptorCopyAttribute(font.get(), kCTFontNameAttribute)));
if (!cf_name) {
return;
}
const NSString* name = reinterpret_cast<const NSString*>(cf_name.get());
fallbackFont->name = [name UTF8String];
}
} // namespace blink
/*
* Copyright (c) 2016, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "sky/engine/platform/fonts/FontCache.h"
#include "base/logging.h"
namespace blink {
void FontCache::getFontForCharacter(UChar32,
const char*,
FontCache::PlatformFallbackFont*) {
LOG(INFO) << "Font fallbacks unimplemented on Mac.";
}
} // namespace blink
......@@ -28,9 +28,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "base/logging.h"
#include "sky/engine/platform/fonts/FontPlatformData.h"
#include "sky/engine/platform/fonts/FontCache.h"
#include "third_party/skia/include/core/SkTypeface.h"
namespace blink {
......@@ -47,7 +45,7 @@ void FontPlatformData::setupPaint(SkPaint* paint,
paint->setTypeface(m_typeface);
paint->setFakeBoldText(m_syntheticBold);
paint->setTextSkewX(m_syntheticItalic ? -SK_Scalar1 / 4 : 0);
paint->setAutohinted(false); // freetype specific
paint->setAutohinted(false); // freetype specific
paint->setLCDRenderText(shouldSmoothFonts);
paint->setSubpixelText(useSubpixelText);
paint->setHinting(SkPaint::kNo_Hinting);
......@@ -75,11 +73,4 @@ bool FontPlatformData::defaultUseSubpixelPositioning() {
return false;
}
void FontCache::getFontForCharacter(
UChar32 c,
const char* preferredLocale,
FontCache::PlatformFallbackFont* fallbackFont) {
DCHECK(false);
}
} // namespace blink
} // namespace blink
......@@ -215,17 +215,9 @@ PassRefPtr<SimpleFontData> FontCache::getLastResortFallbackFont(const FontDescri
sk_sp<SkTypeface> FontCache::createTypeface(const FontDescription& fontDescription, const FontFaceCreationParams& creationParams, CString& name)
{
#if !OS(WIN) && !OS(ANDROID) && !OS(IOS) && !OS(MACOSX)
#if !OS(WIN) && !OS(ANDROID)
if (creationParams.creationType() == CreateFontByFciIdAndTtcIndex) {
// TODO(dro): crbug.com/381620 Use creationParams.ttcIndex() after
// https://code.google.com/p/skia/issues/detail?id=1186 gets fixed.
sk_sp<SkTypeface> typeface;
if (Platform::current()->sandboxSupport())
typeface = SkTypeface::MakeFromStream(streamForFontconfigInterfaceId(creationParams.fontconfigInterfaceId()));
else
typeface = SkTypeface::MakeFromFile(creationParams.filename().data());
return typeface;
return SkTypeface::MakeFromFile(creationParams.filename().data());
}
#endif
......
......@@ -139,10 +139,6 @@ public:
#if USE(CF)
AtomicString(CFStringRef s) : m_string(add(s)) { }
#endif
#ifdef __OBJC__
AtomicString(NSString* s) : m_string(add((CFStringRef)s)) { }
operator NSString*() const { return m_string; }
#endif
// AtomicString::fromUTF8 will return a null string if
// the input data contains invalid UTF-8 sequences.
......
......@@ -376,14 +376,6 @@ public:
RetainPtr<CFStringRef> createCFString() const;
#endif
#ifdef __OBJC__
String(NSString*);
// This conversion maps NULL to "", which loses the meaning of NULL, but we
// need this mapping because AppKit crashes when passed nil NSStrings.
operator NSString*() const { if (!m_impl) return @""; return *m_impl; }
#endif
static String make8BitFrom16BitSource(const UChar*, size_t);
template<size_t inlineCapacity>
static String make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>& buffer)
......@@ -512,14 +504,6 @@ inline bool String::containsOnlyLatin1() const
return !(ored & 0xFF00);
}
#ifdef __OBJC__
// This is for situations in WebKit where the long standing behavior has been
// "nil if empty", so we try to maintain longstanding behavior for the sake of
// entrenched clients
inline NSString* nsStringNilIfEmpty(const String& str) { return str.isEmpty() ? nil : (NSString*)str; }
#endif
inline bool String::containsOnlyASCII() const
{
if (isEmpty())
......
......@@ -17,7 +17,8 @@
namespace sky {
namespace shell {
RasterizerDirect::RasterizerDirect() : binding_(this), weak_factory_(this) {}
RasterizerDirect::RasterizerDirect()
: binding_(this), platform_view_(nullptr), weak_factory_(this) {}
RasterizerDirect::~RasterizerDirect() {
weak_factory_.InvalidateWeakPtrs();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册