Skip to content

Commit

Permalink
Fix multi-font ttc rendering for iOS (flutter#3418)
Browse files Browse the repository at this point in the history
Merged iOS and Android font fallback mechanisms and push font discovery logic into skia
  • Loading branch information
xster authored Feb 15, 2017
1 parent ff087dd commit ffde33d
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 269 deletions.
9 changes: 2 additions & 7 deletions sky/engine/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,12 @@ source_set("platform") {
sources += [ "fonts/apple/FontPlatformDataApple.cpp" ]
}

if (is_ios) {
deps += [ "//base" ]
sources += [ "fonts/apple/FontCacheIOS.mm" ]
}

if (is_mac && !is_ios) {
sources += [ "fonts/apple/FontCacheMac.cpp" ]
}

if (is_android) {
sources += [ "fonts/android/FontCacheAndroid.cpp" ]
if (is_android || is_ios) {
sources += [ "fonts/mobile/FontCacheMobile.cpp" ]
}

if (is_linux && !is_android) {
Expand Down
4 changes: 1 addition & 3 deletions sky/engine/platform/fonts/FontCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ class PLATFORM_EXPORT FontCache {
PassRefPtr<OpenTypeVerticalData> getVerticalData(const FontFileKey&, const FontPlatformData&);
#endif

#if OS(ANDROID)
static AtomicString getGenericFamilyNameForScript(const AtomicString& familyName, const FontDescription&);
#else
#if !OS(ANDROID) && !OS(IOS)
struct PlatformFallbackFont {
String name;
CString filename;
Expand Down
190 changes: 0 additions & 190 deletions sky/engine/platform/fonts/apple/FontCacheIOS.mm

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011 Google Inc. All rights reserved.
* Copyright (c) 2017 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
Expand Down Expand Up @@ -34,21 +34,38 @@
#include "flutter/sky/engine/platform/fonts/FontDescription.h"
#include "flutter/sky/engine/platform/fonts/FontFaceCreationParams.h"
#include "flutter/sky/engine/platform/fonts/SimpleFontData.h"
#include "flutter/sky/engine/platform/text/LocaleToScriptMapping.h"
#include "third_party/skia/include/core/SkTypeface.h"
#include "third_party/skia/include/ports/SkFontMgr.h"

namespace blink {

// SkFontMgr requires script-based locale names, like "zh-Hant" and "zh-Hans",
// instead of "zh-CN" and "zh-TW".
static CString toSkFontMgrLocale(const String& locale)
{
if (!locale.startsWith("zh", TextCaseInsensitive))
return locale.ascii();
switch (localeToScriptCodeForFontSelection(locale)) {
case USCRIPT_SIMPLIFIED_HAN:
return "zh-Hans";
case USCRIPT_TRADITIONAL_HAN:
return "zh-Hant";
default:
return locale.ascii();
}
}

static AtomicString getFamilyNameForCharacter(UChar32 c, const FontDescription& fontDescription)
{
sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
const char* bcp47Locales[2];
int localeCount = 0;
CString defaultLocale = defaultLanguage().ascii();
CString defaultLocale = toSkFontMgrLocale(defaultLanguage());
bcp47Locales[localeCount++] = defaultLocale.data();
CString fontLocale;
if (!fontDescription.locale().isEmpty()) {
fontLocale = fontDescription.locale().ascii();
fontLocale = toSkFontMgrLocale(fontDescription.locale());
bcp47Locales[localeCount++] = fontLocale.data();
}
sk_sp<SkTypeface> typeface(fm->matchFamilyStyleCharacter(0, SkFontStyle(), bcp47Locales, localeCount, c));
Expand All @@ -68,27 +85,4 @@ PassRefPtr<SimpleFontData> FontCache::fallbackFontForCharacter(const FontDescrip
return fontDataFromFontPlatformData(getFontPlatformData(fontDescription, FontFaceCreationParams(familyName)), DoNotRetain);
}

// static
AtomicString FontCache::getGenericFamilyNameForScript(const AtomicString& familyName, const FontDescription& fontDescription)
{
// This is a hack to use the preferred font for CJK scripts.
// FIXME: Use new Skia API once Android system supports per-family and per-script fallback fonts.
UChar32 examplerChar;
switch (fontDescription.script()) {
case USCRIPT_SIMPLIFIED_HAN:
case USCRIPT_TRADITIONAL_HAN:
case USCRIPT_KATAKANA_OR_HIRAGANA:
examplerChar = 0x4E00; // A common character in Japanese and Chinese.
break;
case USCRIPT_HANGUL:
examplerChar = 0xAC00;
break;
default:
// For other scripts, use the default generic family mapping logic.
return familyName;
}

return getFamilyNameForCharacter(examplerChar, fontDescription);
}

} // namespace blink
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace blink {

TEST(FontCacheAndroid, fallbackFontForCharacter)
TEST(FontCacheMobile, fallbackFontForCharacter)
{
// A Latin character in the common locale system font, but not in the
// Chinese locale-preferred font.
Expand Down
2 changes: 1 addition & 1 deletion sky/engine/platform/fonts/skia/FontCacheSkia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ PassRefPtr<SimpleFontData> FontCache::fallbackOnStandardFontStyle(
return nullptr;
}

#if !OS(WIN) && !OS(ANDROID)
#if !OS(WIN) && !OS(ANDROID) && !OS(IOS)
PassRefPtr<SimpleFontData> FontCache::fallbackFontForCharacter(const FontDescription& fontDescription, UChar32 c, const SimpleFontData*)
{
// First try the specified font with standard style & weight.
Expand Down
30 changes: 30 additions & 0 deletions sky/packages/sky_engine/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -9053,6 +9053,36 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------
engine

Copyright (c) 2017 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.
--------------------------------------------------------------------------------
engine

Copyright 2017 The Chromium Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
Loading

0 comments on commit ffde33d

Please sign in to comment.