OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
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 "SkFontConfigParser_android.h" | 8 #include "SkFontConfigParser_android.h" |
9 #include "SkFontDescriptor.h" | 9 #include "SkFontDescriptor.h" |
10 #include "SkFontHost_FreeType_common.h" | 10 #include "SkFontHost_FreeType_common.h" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 }; | 113 }; |
114 | 114 |
115 void get_path_for_sys_fonts(SkString* full, const SkString& name) { | 115 void get_path_for_sys_fonts(SkString* full, const SkString& name) { |
116 full->set(getenv("ANDROID_ROOT")); | 116 full->set(getenv("ANDROID_ROOT")); |
117 full->append(SK_FONT_FILE_PREFIX); | 117 full->append(SK_FONT_FILE_PREFIX); |
118 full->append(name); | 118 full->append(name); |
119 } | 119 } |
120 | 120 |
121 class SkFontStyleSet_Android : public SkFontStyleSet { | 121 class SkFontStyleSet_Android : public SkFontStyleSet { |
122 public: | 122 public: |
123 explicit SkFontStyleSet_Android(FontFamily* family) : fFontFamily(family) { | 123 explicit SkFontStyleSet_Android(FontFamily* family) { |
124 // TODO? make this lazy | 124 // TODO? make this lazy |
125 for (int i = 0; i < family->fFontFiles.count(); ++i) { | 125 for (int i = 0; i < family->fFontFiles.count(); ++i) { |
126 const SkString& fileName = family->fFontFiles[i].fFileName; | 126 const SkString& fileName = family->fFontFiles[i].fFileName; |
127 | 127 |
128 SkString pathName; | 128 SkString pathName; |
129 get_path_for_sys_fonts(&pathName, fileName); | 129 get_path_for_sys_fonts(&pathName, fileName); |
130 | 130 |
131 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(pathName.c_str()
)); | 131 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(pathName.c_str()
)); |
132 if (!stream.get()) { | 132 if (!stream.get()) { |
133 DEBUG_FONT(("---- SystemFonts[%d] file=%s (NOT EXIST)", i, fileN
ame.c_str())); | 133 DEBUG_FONT(("---- SystemFonts[%d] file=%s (NOT EXIST)", i, fileN
ame.c_str())); |
134 continue; | 134 continue; |
135 } | 135 } |
136 | 136 |
| 137 const int ttcIndex = family->fFontFiles[i].fIndex; |
137 SkString fontName; | 138 SkString fontName; |
138 SkTypeface::Style style; | 139 SkTypeface::Style style; |
139 bool isFixedWidth; | 140 bool isFixedWidth; |
140 if (!SkTypeface_FreeType::ScanFont(stream.get(), family->fFontFiles[
i].fIndex, | 141 if (!SkTypeface_FreeType::ScanFont(stream.get(), ttcIndex, |
141 &fontName, &style, &isFixedWidth)
) { | 142 &fontName, &style, &isFixedWidth)
) { |
142 DEBUG_FONT(("---- SystemFonts[%d] file=%s (INVALID)", i, fileNam
e.c_str())); | 143 DEBUG_FONT(("---- SystemFonts[%d] file=%s (INVALID)", i, fileNam
e.c_str())); |
143 continue; | 144 continue; |
144 } | 145 } |
145 | 146 |
146 fStyles.push_back().reset(SkNEW_ARGS(SkTypeface_AndroidSystem, | 147 fStyles.push_back().reset(SkNEW_ARGS(SkTypeface_AndroidSystem, |
147 (pathName, 0, | 148 (pathName, ttcIndex, |
148 style, isFixedWidth, fontName)
)); | 149 style, isFixedWidth, fontName)
)); |
149 } | 150 } |
150 } | 151 } |
151 | 152 |
152 virtual int count() SK_OVERRIDE { | 153 virtual int count() SK_OVERRIDE { |
153 return fStyles.count(); | 154 return fStyles.count(); |
154 } | 155 } |
155 virtual void getStyle(int index, SkFontStyle* style, SkString* name) SK_OVER
RIDE { | 156 virtual void getStyle(int index, SkFontStyle* style, SkString* name) SK_OVER
RIDE { |
156 if (index < 0 || fStyles.count() <= index) { | 157 if (index < 0 || fStyles.count() <= index) { |
157 return; | 158 return; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 return SkFontStyle::kUpright_Slant; | 206 return SkFontStyle::kUpright_Slant; |
206 } | 207 } |
207 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candid
ate) { | 208 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candid
ate) { |
208 int score = 0; | 209 int score = 0; |
209 score += abs((pattern.width() - candidate.width()) * 100); | 210 score += abs((pattern.width() - candidate.width()) * 100); |
210 score += abs((pattern.isItalic() == candidate.isItalic()) ? 0 : 1000); | 211 score += abs((pattern.isItalic() == candidate.isItalic()) ? 0 : 1000); |
211 score += abs(pattern.weight() - candidate.weight()); | 212 score += abs(pattern.weight() - candidate.weight()); |
212 return score; | 213 return score; |
213 } | 214 } |
214 | 215 |
215 | |
216 FontFamily* fFontFamily; | |
217 SkTArray<SkAutoTUnref<SkTypeface>, true> fStyles; | 216 SkTArray<SkAutoTUnref<SkTypeface>, true> fStyles; |
218 | 217 |
219 friend struct NameToFamily; | 218 friend struct NameToFamily; |
220 friend class SkFontMgr_Android; | 219 friend class SkFontMgr_Android; |
221 | 220 |
222 typedef SkFontStyleSet INHERITED; | 221 typedef SkFontStyleSet INHERITED; |
223 }; | 222 }; |
224 | 223 |
225 /** On Android a single family can have many names, but our API assumes unique n
ames. | 224 /** On Android a single family can have many names, but our API assumes unique n
ames. |
226 * Map names to the back end so that all names for a given family refer to the
same | 225 * Map names to the back end so that all names for a given family refer to the
same |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], | 321 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
323 unsigned styleBits) const SK_OVER
RIDE { | 322 unsigned styleBits) const SK_OVER
RIDE { |
324 SkTypeface::Style oldStyle = (SkTypeface::Style)styleBits; | 323 SkTypeface::Style oldStyle = (SkTypeface::Style)styleBits; |
325 SkFontStyle style = SkFontStyle(oldStyle & SkTypeface::kBold | 324 SkFontStyle style = SkFontStyle(oldStyle & SkTypeface::kBold |
326 ? SkFontStyle::kBold_Weight | 325 ? SkFontStyle::kBold_Weight |
327 : SkFontStyle::kNormal_Weight, | 326 : SkFontStyle::kNormal_Weight, |
328 SkFontStyle::kNormal_Width, | 327 SkFontStyle::kNormal_Width, |
329 oldStyle & SkTypeface::kItalic | 328 oldStyle & SkTypeface::kItalic |
330 ? SkFontStyle::kItalic_Slant | 329 ? SkFontStyle::kItalic_Slant |
331 : SkFontStyle::kUpright_Slant); | 330 : SkFontStyle::kUpright_Slant); |
332 SkTypeface* tf = NULL; | |
333 | 331 |
334 if (NULL != familyName) { | 332 if (NULL != familyName) { |
335 // On Android, we must return NULL when we can't find the requested | 333 // On Android, we must return NULL when we can't find the requested |
336 // named typeface so that the system/app can provide their own recov
ery | 334 // named typeface so that the system/app can provide their own recov
ery |
337 // mechanism. On other platforms we'd provide a typeface from the | 335 // mechanism. On other platforms we'd provide a typeface from the |
338 // default family instead. | 336 // default family instead. |
339 tf = this->onMatchFamilyStyle(familyName, style); | 337 return this->onMatchFamilyStyle(familyName, style); |
340 } else { | |
341 tf = fDefaultFamily->matchStyle(style); | |
342 } | 338 } |
343 | 339 return fDefaultFamily->matchStyle(style); |
344 // TODO: double ref? qv matchStyle() | |
345 return SkSafeRef(tf); | |
346 } | 340 } |
347 | 341 |
348 | 342 |
349 private: | 343 private: |
350 | 344 |
351 SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; | 345 SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; |
352 SkFontStyleSet* fDefaultFamily; | 346 SkFontStyleSet* fDefaultFamily; |
353 SkTypeface* fDefaultTypeface; | 347 SkTypeface* fDefaultTypeface; |
354 | 348 |
355 SkTDArray<NameToFamily> fNameToFamilyMap; | 349 SkTDArray<NameToFamily> fNameToFamilyMap; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 } | 386 } |
393 | 387 |
394 typedef SkFontMgr INHERITED; | 388 typedef SkFontMgr INHERITED; |
395 }; | 389 }; |
396 | 390 |
397 /////////////////////////////////////////////////////////////////////////////// | 391 /////////////////////////////////////////////////////////////////////////////// |
398 | 392 |
399 SkFontMgr* SkFontMgr::Factory() { | 393 SkFontMgr* SkFontMgr::Factory() { |
400 return SkNEW(SkFontMgr_Android); | 394 return SkNEW(SkFontMgr_Android); |
401 } | 395 } |
OLD | NEW |