| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkFontDescriptor.h" | 8 #include "SkFontDescriptor.h" |
| 9 #include "SkFontHost_FreeType_common.h" | 9 #include "SkFontHost_FreeType_common.h" |
| 10 #include "SkFontMgr.h" | 10 #include "SkFontMgr.h" |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 void appendTypeface(SkTypeface_Custom* typeface) { | 126 void appendTypeface(SkTypeface_Custom* typeface) { |
| 127 fStyles.push_back().reset(typeface); | 127 fStyles.push_back().reset(typeface); |
| 128 } | 128 } |
| 129 | 129 |
| 130 int count() override { | 130 int count() override { |
| 131 return fStyles.count(); | 131 return fStyles.count(); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void getStyle(int index, SkFontStyle* style, SkString* name) override { | 134 void getStyle(int index, SkFontStyle* style, SkString* name) override { |
| 135 SkASSERT(index < fStyles.count()); | 135 SkASSERT(index < fStyles.count()); |
| 136 bool bold = fStyles[index]->isBold(); | 136 if (style) { |
| 137 bool italic = fStyles[index]->isItalic(); | 137 *style = fStyles[index]->fontStyle(); |
| 138 *style = SkFontStyle(bold ? SkFontStyle::kBold_Weight : SkFontStyle::kNo
rmal_Weight, | 138 } |
| 139 SkFontStyle::kNormal_Width, | 139 if (name) { |
| 140 italic ? SkFontStyle::kItalic_Slant : SkFontStyle::
kUpright_Slant); | 140 name->reset(); |
| 141 name->reset(); | 141 } |
| 142 } | 142 } |
| 143 | 143 |
| 144 SkTypeface* createTypeface(int index) override { | 144 SkTypeface* createTypeface(int index) override { |
| 145 SkASSERT(index < fStyles.count()); | 145 SkASSERT(index < fStyles.count()); |
| 146 return SkRef(fStyles[index].get()); | 146 return SkRef(fStyles[index].get()); |
| 147 } | 147 } |
| 148 | 148 |
| 149 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candid
ate) { | |
| 150 int score = 0; | |
| 151 score += (pattern.width() - candidate.width()) * 100; | |
| 152 score += (pattern.slant() == candidate.slant()) ? 0 : 1000; | |
| 153 score += pattern.weight() - candidate.weight(); | |
| 154 return score; | |
| 155 } | |
| 156 | |
| 157 SkTypeface* matchStyle(const SkFontStyle& pattern) override { | 149 SkTypeface* matchStyle(const SkFontStyle& pattern) override { |
| 158 if (0 == fStyles.count()) { | 150 return this->matchStyleCSS3(pattern); |
| 159 return nullptr; | |
| 160 } | |
| 161 | |
| 162 SkTypeface_Custom* closest = fStyles[0]; | |
| 163 int minScore = std::numeric_limits<int>::max(); | |
| 164 for (int i = 0; i < fStyles.count(); ++i) { | |
| 165 bool bold = fStyles[i]->isBold(); | |
| 166 bool italic = fStyles[i]->isItalic(); | |
| 167 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight | |
| 168 : SkFontStyle::kNormal_Weight, | |
| 169 SkFontStyle::kNormal_Width, | |
| 170 italic ? SkFontStyle::kItalic_Slant | |
| 171 : SkFontStyle::kUpright_Slant
); | |
| 172 | |
| 173 int score = match_score(pattern, style); | |
| 174 if (score < minScore) { | |
| 175 closest = fStyles[i]; | |
| 176 minScore = score; | |
| 177 } | |
| 178 } | |
| 179 return SkRef(closest); | |
| 180 } | 151 } |
| 181 | 152 |
| 182 SkString getFamilyName() { return fFamilyName; } | 153 SkString getFamilyName() { return fFamilyName; } |
| 183 | 154 |
| 184 private: | 155 private: |
| 185 SkTArray<SkAutoTUnref<SkTypeface_Custom>, true> fStyles; | 156 SkTArray<SkAutoTUnref<SkTypeface_Custom>, true> fStyles; |
| 186 SkString fFamilyName; | 157 SkString fFamilyName; |
| 187 | 158 |
| 188 friend class SkFontMgr_Custom; | 159 friend class SkFontMgr_Custom; |
| 189 }; | 160 }; |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString()); | 482 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString()); |
| 512 families->push_back().reset(family); | 483 families->push_back().reset(family); |
| 513 family->appendTypeface(new SkTypeface_Empty); | 484 family->appendTypeface(new SkTypeface_Empty); |
| 514 } | 485 } |
| 515 | 486 |
| 516 }; | 487 }; |
| 517 | 488 |
| 518 SK_API SkFontMgr* SkFontMgr_New_Custom_Empty() { | 489 SK_API SkFontMgr* SkFontMgr_New_Custom_Empty() { |
| 519 return new SkFontMgr_Custom(EmptyFontLoader()); | 490 return new SkFontMgr_Custom(EmptyFontLoader()); |
| 520 } | 491 } |
| OLD | NEW |