FontFamily.cpp revision aaa4e3470270496e6eb80704eadecb2cb7c56bf0
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#define LOG_TAG "Minikin" 18 19#include <cutils/log.h> 20#include <stdlib.h> 21#include <stdint.h> 22#include <string.h> 23 24#include <hb.h> 25#include <hb-ot.h> 26 27#include <utils/JenkinsHash.h> 28 29#include "FontLanguage.h" 30#include "FontLanguageListCache.h" 31#include "HbFontCache.h" 32#include "MinikinInternal.h" 33#include <minikin/MinikinFont.h> 34#include <minikin/AnalyzeStyle.h> 35#include <minikin/CmapCoverage.h> 36#include <minikin/FontFamily.h> 37#include <UniquePtr.h> 38 39using std::vector; 40 41namespace android { 42 43FontStyle::FontStyle(int variant, int weight, bool italic) 44 : FontStyle(FontLanguageListCache::kEmptyListId, variant, weight, italic) { 45} 46 47FontStyle::FontStyle(uint32_t languageListId, int variant, int weight, bool italic) 48 : bits(pack(variant, weight, italic)), mLanguageListId(languageListId) { 49} 50 51hash_t FontStyle::hash() const { 52 uint32_t hash = JenkinsHashMix(0, bits); 53 hash = JenkinsHashMix(hash, mLanguageListId); 54 return JenkinsHashWhiten(hash); 55} 56 57// static 58uint32_t FontStyle::registerLanguageList(const std::string& languages) { 59 AutoMutex _l(gMinikinLock); 60 return FontLanguageListCache::getId(languages); 61} 62 63// static 64uint32_t FontStyle::pack(int variant, int weight, bool italic) { 65 return (weight & kWeightMask) | (italic ? kItalicMask : 0) | (variant << kVariantShift); 66} 67 68FontFamily::FontFamily(int variant) : FontFamily(FontLanguageListCache::kEmptyListId, variant) { 69} 70 71FontFamily::~FontFamily() { 72 for (size_t i = 0; i < mFonts.size(); i++) { 73 mFonts[i].typeface->UnrefLocked(); 74 } 75} 76 77bool FontFamily::addFont(MinikinFont* typeface) { 78 AutoMutex _l(gMinikinLock); 79 const uint32_t os2Tag = MinikinFont::MakeTag('O', 'S', '/', '2'); 80 HbBlob os2Table(getFontTable(typeface, os2Tag)); 81 if (os2Table.get() == nullptr) return false; 82 int weight; 83 bool italic; 84 if (analyzeStyle(os2Table.get(), os2Table.size(), &weight, &italic)) { 85 //ALOGD("analyzed weight = %d, italic = %s", weight, italic ? "true" : "false"); 86 FontStyle style(weight, italic); 87 addFontLocked(typeface, style); 88 return true; 89 } else { 90 ALOGD("failed to analyze style"); 91 } 92 return false; 93} 94 95void FontFamily::addFont(MinikinFont* typeface, FontStyle style) { 96 AutoMutex _l(gMinikinLock); 97 addFontLocked(typeface, style); 98} 99 100void FontFamily::addFontLocked(MinikinFont* typeface, FontStyle style) { 101 typeface->RefLocked(); 102 mFonts.push_back(Font(typeface, style)); 103 mCoverageValid = false; 104} 105 106// Compute a matching metric between two styles - 0 is an exact match 107static int computeMatch(FontStyle style1, FontStyle style2) { 108 if (style1 == style2) return 0; 109 int score = abs(style1.getWeight() - style2.getWeight()); 110 if (style1.getItalic() != style2.getItalic()) { 111 score += 2; 112 } 113 return score; 114} 115 116static FontFakery computeFakery(FontStyle wanted, FontStyle actual) { 117 // If desired weight is semibold or darker, and 2 or more grades 118 // higher than actual (for example, medium 500 -> bold 700), then 119 // select fake bold. 120 int wantedWeight = wanted.getWeight(); 121 bool isFakeBold = wantedWeight >= 6 && (wantedWeight - actual.getWeight()) >= 2; 122 bool isFakeItalic = wanted.getItalic() && !actual.getItalic(); 123 return FontFakery(isFakeBold, isFakeItalic); 124} 125 126FakedFont FontFamily::getClosestMatch(FontStyle style) const { 127 const Font* bestFont = NULL; 128 int bestMatch = 0; 129 for (size_t i = 0; i < mFonts.size(); i++) { 130 const Font& font = mFonts[i]; 131 int match = computeMatch(font.style, style); 132 if (i == 0 || match < bestMatch) { 133 bestFont = &font; 134 bestMatch = match; 135 } 136 } 137 FakedFont result; 138 if (bestFont == NULL) { 139 result.font = NULL; 140 } else { 141 result.font = bestFont->typeface; 142 result.fakery = computeFakery(style, bestFont->style); 143 } 144 return result; 145} 146 147size_t FontFamily::getNumFonts() const { 148 return mFonts.size(); 149} 150 151MinikinFont* FontFamily::getFont(size_t index) const { 152 return mFonts[index].typeface; 153} 154 155FontStyle FontFamily::getStyle(size_t index) const { 156 return mFonts[index].style; 157} 158 159const SparseBitSet* FontFamily::getCoverage() { 160 if (!mCoverageValid) { 161 const FontStyle defaultStyle; 162 MinikinFont* typeface = getClosestMatch(defaultStyle).font; 163 const uint32_t cmapTag = MinikinFont::MakeTag('c', 'm', 'a', 'p'); 164 HbBlob cmapTable(getFontTable(typeface, cmapTag)); 165 if (cmapTable.get() == nullptr) { 166 ALOGE("Could not get cmap table size!\n"); 167 // Note: This means we will retry on the next call to getCoverage, as we can't store 168 // the failure. This is fine, as we assume this doesn't really happen in practice. 169 return nullptr; 170 } 171 // TODO: Error check? 172 CmapCoverage::getCoverage(mCoverage, cmapTable.get(), cmapTable.size(), &mHasVSTable); 173#ifdef VERBOSE_DEBUG 174 ALOGD("font coverage length=%d, first ch=%x\n", mCoverage.length(), 175 mCoverage.nextSetBit(0)); 176#endif 177 mCoverageValid = true; 178 } 179 return &mCoverage; 180} 181 182bool FontFamily::hasVariationSelector(uint32_t codepoint, uint32_t variationSelector) { 183 assertMinikinLocked(); 184 if (!mHasVSTable) { 185 return false; 186 } 187 188 const FontStyle defaultStyle; 189 MinikinFont* minikinFont = getClosestMatch(defaultStyle).font; 190 hb_font_t* font = getHbFontLocked(minikinFont); 191 uint32_t unusedGlyph; 192 bool result = hb_font_get_glyph(font, codepoint, variationSelector, &unusedGlyph); 193 hb_font_destroy(font); 194 return result; 195} 196 197bool FontFamily::hasVSTable() const { 198 LOG_ALWAYS_FATAL_IF(!mCoverageValid, "Do not call this method before getCoverage() call"); 199 return mHasVSTable; 200} 201 202} // namespace android 203