15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (c) 2011 Google Inc. All rights reserved.
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met:
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution.
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission.
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "config.h"
32a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/fonts/FontCache.h"
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
347242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "platform/Language.h"
35a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/fonts/SimpleFontData.h"
36f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#include "platform/fonts/FontDescription.h"
37197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "platform/fonts/FontFaceCreationParams.h"
38e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)#include "third_party/skia/include/core/SkTypeface.h"
39e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)#include "third_party/skia/include/ports/SkFontMgr.h"
405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
41c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
437242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccistatic AtomicString getFamilyNameForCharacter(UChar32 c, const FontDescription& fontDescription)
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
45e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    RefPtr<SkFontMgr> fm = adoptRef(SkFontMgr::RefDefault());
46fdbb120d0a4a87db74bcb608de226c85a7d1c920Ben Murdoch    const char* bcp47Locales[2];
47fdbb120d0a4a87db74bcb608de226c85a7d1c920Ben Murdoch    int localeCount = 0;
48fdbb120d0a4a87db74bcb608de226c85a7d1c920Ben Murdoch    CString defaultLocale = defaultLanguage().ascii();
49fdbb120d0a4a87db74bcb608de226c85a7d1c920Ben Murdoch    bcp47Locales[localeCount++] = defaultLocale.data();
50fdbb120d0a4a87db74bcb608de226c85a7d1c920Ben Murdoch    CString fontLocale;
51fdbb120d0a4a87db74bcb608de226c85a7d1c920Ben Murdoch    if (!fontDescription.locale().isEmpty()) {
52fdbb120d0a4a87db74bcb608de226c85a7d1c920Ben Murdoch        fontLocale = fontDescription.locale().ascii();
53fdbb120d0a4a87db74bcb608de226c85a7d1c920Ben Murdoch        bcp47Locales[localeCount++] = fontLocale.data();
54fdbb120d0a4a87db74bcb608de226c85a7d1c920Ben Murdoch    }
55fdbb120d0a4a87db74bcb608de226c85a7d1c920Ben Murdoch    RefPtr<SkTypeface> typeface = adoptRef(fm->matchFamilyStyleCharacter(0, SkFontStyle(), bcp47Locales, localeCount, c));
56e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    if (!typeface)
57d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return emptyAtom;
58d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
59e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    SkString skiaFamilyName;
60e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    typeface->getFamilyName(&skiaFamilyName);
611e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    return skiaFamilyName.c_str();
621e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)}
631e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
645d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)PassRefPtr<SimpleFontData> FontCache::fallbackFontForCharacter(const FontDescription& fontDescription, UChar32 c, const SimpleFontData*)
651e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles){
667242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    AtomicString familyName = getFamilyNameForCharacter(c, fontDescription);
671e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (familyName.isEmpty())
68d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return getLastResortFallbackFont(fontDescription, DoNotRetain);
69197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    return fontDataFromFontPlatformData(getFontPlatformData(fontDescription, FontFaceCreationParams(familyName)), DoNotRetain);
701e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)}
711e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
721e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)// static
737242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano TucciAtomicString FontCache::getGenericFamilyNameForScript(const AtomicString& familyName, const FontDescription& fontDescription)
741e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles){
751e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // This is a hack to use the preferred font for CJK scripts.
761e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // FIXME: Use new Skia API once Android system supports per-family and per-script fallback fonts.
771e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    UChar32 examplerChar;
787242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    switch (fontDescription.script()) {
791e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    case USCRIPT_SIMPLIFIED_HAN:
801e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    case USCRIPT_TRADITIONAL_HAN:
811e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    case USCRIPT_KATAKANA_OR_HIRAGANA:
821e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        examplerChar = 0x4E00; // A common character in Japanese and Chinese.
831e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        break;
841e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    case USCRIPT_HANGUL:
851e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        examplerChar = 0xAC00;
861e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        break;
871e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    default:
881e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        // For other scripts, use the default generic family mapping logic.
891e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        return familyName;
901e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    }
911e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
927242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    return getFamilyNameForCharacter(examplerChar, fontDescription);
935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
95c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
96