FontFamily.cpp revision 6b1c227da6492a435f0341d7fe95d9992669920e
123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck/*
223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * Copyright (C) 2013 The Android Open Source Project
323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck *
423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * Licensed under the Apache License, Version 2.0 (the "License");
523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * you may not use this file except in compliance with the License.
623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * You may obtain a copy of the License at
723b797ab5151eb2474f3bdd679f2f07bfd723042John Reck *
823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck *      http://www.apache.org/licenses/LICENSE-2.0
923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck *
1023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * Unless required by applicable law or agreed to in writing, software
1123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * distributed under the License is distributed on an "AS IS" BASIS,
1223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * See the License for the specific language governing permissions and
1423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * limitations under the License.
1523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck */
1623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
1723b797ab5151eb2474f3bdd679f2f07bfd723042John Reck#define LOG_TAG "Minikin"
1823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
19d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include <cutils/log.h>
20d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include <stdlib.h>
21d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include <stdint.h>
223b20251a355c88193c439f928a84ae69483fb488John Reck#include <string.h>
23d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck
24d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include <hb.h>
25d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include <hb-ot.h>
264f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
27d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include <utils/JenkinsHash.h>
28d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck
2923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck#include "FontLanguage.h"
3065fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include "FontLanguageListCache.h"
31d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "HbFontCache.h"
32240ff6246a29602539fd0295274e1c769e743a2eJohn Reck#include "MinikinInternal.h"
3365fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include <minikin/MinikinFont.h>
3465fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include <minikin/AnalyzeStyle.h>
35f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck#include <minikin/CmapCoverage.h>
36f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck#include <minikin/FontFamily.h>
37f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck#include <UniquePtr.h>
3823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
3923b797ab5151eb2474f3bdd679f2f07bfd723042John Reckusing std::vector;
4023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
4123b797ab5151eb2474f3bdd679f2f07bfd723042John Recknamespace android {
42119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
43119907cd2575c56b1ebf66348b52e67aaf6a88d8John ReckFontStyle::FontStyle(int variant, int weight, bool italic)
443b20251a355c88193c439f928a84ae69483fb488John Reck        : FontStyle(FontLanguageListCache::kEmptyListId, variant, weight, italic) {
453b20251a355c88193c439f928a84ae69483fb488John Reck}
464f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
4751d6a3db97bdd5315f1a17a4b447d10a92217b98Chris CraikFontStyle::FontStyle(uint32_t languageListId, int variant, int weight, bool italic)
48ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck        : bits(pack(variant, weight, italic)), mLanguageListId(languageListId) {
494c9e59d03c2bca38001225b79d01740b8999adfbJohn Reck}
504c9e59d03c2bca38001225b79d01740b8999adfbJohn Reck
51443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reckhash_t FontStyle::hash() const {
52b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck    uint32_t hash = JenkinsHashMix(0, bits);
5323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    hash = JenkinsHashMix(hash, mLanguageListId);
5423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    return JenkinsHashWhiten(hash);
5523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
5617035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck
57443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck// static
584f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reckuint32_t FontStyle::registerLanguageList(const std::string& languages) {
594f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    AutoMutex _l(gMinikinLock);
6017035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    return FontLanguageListCache::getId(languages);
6117035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck}
62d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik
6317035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck// static
6417035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reckuint32_t FontStyle::pack(int variant, int weight, bool italic) {
65e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reck    return (weight & kWeightMask) | (italic ? kItalicMask : 0) | (variant << kVariantShift);
664f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
674f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
68d41c4d8c732095ae99c955b6b82f7306633004b1Chris CraikFontFamily::FontFamily(int variant) : FontFamily(FontLanguageListCache::kEmptyListId, variant) {
694f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
7023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
7123b797ab5151eb2474f3bdd679f2f07bfd723042John ReckFontFamily::~FontFamily() {
72a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    for (size_t i = 0; i < mFonts.size(); i++) {
73fbc8df03e498baf47ff1a5e05e182f1bcd60c770John Reck        mFonts[i].typeface->UnrefLocked();
74fbc8df03e498baf47ff1a5e05e182f1bcd60c770John Reck    }
75a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck}
76a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
7723b797ab5151eb2474f3bdd679f2f07bfd723042John Reckbool FontFamily::addFont(MinikinFont* typeface) {
783b20251a355c88193c439f928a84ae69483fb488John Reck    AutoMutex _l(gMinikinLock);
7923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    const uint32_t os2Tag = MinikinFont::MakeTag('O', 'S', '/', '2');
8023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    size_t os2Size = 0;
8123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    bool ok = typeface->GetTable(os2Tag, NULL, &os2Size);
8223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    if (!ok) return false;
833b20251a355c88193c439f928a84ae69483fb488John Reck    UniquePtr<uint8_t[]> os2Data(new uint8_t[os2Size]);
8423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    ok = typeface->GetTable(os2Tag, os2Data.get(), &os2Size);
8523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    if (!ok) return false;
8623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    int weight;
871125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    bool italic;
881125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    if (analyzeStyle(os2Data.get(), os2Size, &weight, &italic)) {
894f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        //ALOGD("analyzed weight = %d, italic = %s", weight, italic ? "true" : "false");
90dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck        FontStyle style(weight, italic);
91368cdd85268999997fb495cf90c4417221797de0John Reck        addFontLocked(typeface, style);
92368cdd85268999997fb495cf90c4417221797de0John Reck        return true;
934f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    } else {
944f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        ALOGD("failed to analyze style");
954f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
96d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck    return false;
97d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck}
98d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik
992cdbc7d2283aae3d77b12c8fdbba8ca4bd3db5eaJohn Reckvoid FontFamily::addFont(MinikinFont* typeface, FontStyle style) {
1004f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    AutoMutex _l(gMinikinLock);
1014f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    addFontLocked(typeface, style);
1024f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
103f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck
104f7d9c1dc84671d4e99657ef071d275700d85bb11John Reckvoid FontFamily::addFontLocked(MinikinFont* typeface, FontStyle style) {
105f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck    typeface->RefLocked();
106dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck    mFonts.push_back(Font(typeface, style));
1074f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    mCoverageValid = false;
1084f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1091125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck
1101125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck// Compute a matching metric between two styles - 0 is an exact match
1111125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reckstatic int computeMatch(FontStyle style1, FontStyle style2) {
1121125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    if (style1 == style2) return 0;
113a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    int score = abs(style1.getWeight() - style2.getWeight());
1144f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    if (style1.getItalic() != style2.getItalic()) {
1152cdbc7d2283aae3d77b12c8fdbba8ca4bd3db5eaJohn Reck        score += 2;
1163b20251a355c88193c439f928a84ae69483fb488John Reck    }
1174f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    return score;
1184f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1194f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
1204f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reckstatic FontFakery computeFakery(FontStyle wanted, FontStyle actual) {
121a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    // If desired weight is semibold or darker, and 2 or more grades
1224f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    // higher than actual (for example, medium 500 -> bold 700), then
123f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck    // select fake bold.
124f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck    int wantedWeight = wanted.getWeight();
1259fb42f07784ac9e1ab29fa7d5bcda6c3081d238fJohn Reck    bool isFakeBold = wantedWeight >= 6 && (wantedWeight - actual.getWeight()) >= 2;
12601a5ea35fbba4c5bb1d7790ae1677a2fa752e042John Reck    bool isFakeItalic = wanted.getItalic() && !actual.getItalic();
1274f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    return FontFakery(isFakeBold, isFakeItalic);
1284f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
129284b24358410cb0200e525a5ba36994090c83f20Chris Craik
13050210d912925aef14e4ce69be82e4949122a3cd9Alan ViveretteFakedFont FontFamily::getClosestMatch(FontStyle style) const {
13164bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    const Font* bestFont = NULL;
1324f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    int bestMatch = 0;
13350210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    for (size_t i = 0; i < mFonts.size(); i++) {
13450210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette        const Font& font = mFonts[i];
13550210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette        int match = computeMatch(font.style, style);
13650210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette        if (i == 0 || match < bestMatch) {
13750210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette            bestFont = &font;
13850210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette            bestMatch = match;
1394f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        }
1404f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
14163a06673253914510bbeebd500655008682dade1John Reck    FakedFont result;
14263a06673253914510bbeebd500655008682dade1John Reck    if (bestFont == NULL) {
14363a06673253914510bbeebd500655008682dade1John Reck        result.font = NULL;
14463a06673253914510bbeebd500655008682dade1John Reck    } else {
145860d155f866cc15a725e7ce03763280987f24901John Reck        result.font = bestFont->typeface;
146dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck        result.fakery = computeFakery(style, bestFont->style);
147dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck    }
148f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    return result;
149f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck}
150f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck
151f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Recksize_t FontFamily::getNumFonts() const {
152f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    return mFonts.size();
153860d155f866cc15a725e7ce03763280987f24901John Reck}
154860d155f866cc15a725e7ce03763280987f24901John Reck
15568bfe0a37a0dcef52abd81688d8520c5d16e1a85John ReckMinikinFont* FontFamily::getFont(size_t index) const {
15668bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    return mFonts[index].typeface;
157d72e0a339b54af0c4e731513bbad120dff694723John Reck}
158d72e0a339b54af0c4e731513bbad120dff694723John Reck
159d72e0a339b54af0c4e731513bbad120dff694723John ReckFontStyle FontFamily::getStyle(size_t index) const {
16019b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    return mFonts[index].style;
16119b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
16219b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
163bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reckconst SparseBitSet* FontFamily::getCoverage() {
1641b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik    if (!mCoverageValid) {
165bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck        const FontStyle defaultStyle;
166bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck        MinikinFont* typeface = getClosestMatch(defaultStyle).font;
167be3fba05e823f740f65b2679929347dc3dd282adJohn Reck        const uint32_t cmapTag = MinikinFont::MakeTag('c', 'm', 'a', 'p');
168f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck        size_t cmapSize = 0;
16918f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reck        if (!typeface->GetTable(cmapTag, NULL, &cmapSize)) {
170bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck            ALOGE("Could not get cmap table size!\n");
171bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck            // Note: This means we will retry on the next call to getCoverage, as we can't store
172bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck            //       the failure. This is fine, as we assume this doesn't really happen in practice.
173bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck            return nullptr;
174bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck        }
175ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck        UniquePtr<uint8_t[]> cmapData(new uint8_t[cmapSize]);
176be3fba05e823f740f65b2679929347dc3dd282adJohn Reck        if (!typeface->GetTable(cmapTag, cmapData.get(), &cmapSize)) {
177ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck            ALOGE("Unexpected failure to read cmap table!\n");
178ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck            return nullptr;
179e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        }
18025fbb3fa1138675379102a44405852555cefccbdJohn Reck        // TODO: Error check?
18100e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        CmapCoverage::getCoverage(mCoverage, cmapData.get(), cmapSize, &mHasVSTable);
18200e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck#ifdef VERBOSE_DEBUG
183ec845a215e343cdb3b2e4c7b6aff7b24beb0236bJohn Reck        ALOGD("font coverage length=%d, first ch=%x\n", mCoverage.length(),
184e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck                mCoverage.nextSetBit(0));
185119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#endif
186e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck        mCoverageValid = true;
18700e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck    }
188998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    return &mCoverage;
189aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck}
1901b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik
191aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reckbool FontFamily::hasVariationSelector(uint32_t codepoint, uint32_t variationSelector) {
192aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck    assertMinikinLocked();
193aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck    if (!mHasVSTable) {
194aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck        return false;
195a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    }
196a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
197a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    const FontStyle defaultStyle;
198a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    MinikinFont* minikinFont = getClosestMatch(defaultStyle).font;
199a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    hb_font_t* font = getHbFontLocked(minikinFont);
200a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    uint32_t unusedGlyph;
201a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    return hb_font_get_glyph(font, codepoint, variationSelector, &unusedGlyph);
202a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck}
203aef9dc8d186bd5f78068ab2d5240b5e9c8ab44b6John Reck
2041b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craikbool FontFamily::hasVSTable() const {
205aef9dc8d186bd5f78068ab2d5240b5e9c8ab44b6John Reck    LOG_ALWAYS_FATAL_IF(!mCoverageValid, "Do not call this method before getCoverage() call");
206aef9dc8d186bd5f78068ab2d5240b5e9c8ab44b6John Reck    return mHasVSTable;
207a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck}
208cd028f336e36b22dbe8cf623eb5bd2361314495cJohn Reck
209f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck}  // namespace android
210f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck