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 "SkFontHost.h" | 8 #include "SkFontHost.h" |
9 #include "SkFontHost_FreeType_common.h" | 9 #include "SkFontHost_FreeType_common.h" |
10 #include "SkFontDescriptor.h" | 10 #include "SkFontDescriptor.h" |
11 #include "SkFontMgr.h" | 11 #include "SkFontMgr.h" |
12 #include "SkDescriptor.h" | 12 #include "SkDescriptor.h" |
13 #include "SkOSFile.h" | 13 #include "SkOSFile.h" |
14 #include "SkPaint.h" | 14 #include "SkPaint.h" |
15 #include "SkString.h" | 15 #include "SkString.h" |
16 #include "SkStream.h" | 16 #include "SkStream.h" |
17 #include "SkThread.h" | 17 #include "SkThread.h" |
18 #include "SkTSearch.h" | 18 #include "SkTSearch.h" |
19 #include "SkTypefaceCache.h" | 19 #include "SkTypefaceCache.h" |
20 #include "SkTArray.h" | 20 #include "SkTArray.h" |
21 | 21 |
22 #include <limits> | 22 #include <limits> |
23 | 23 |
24 #ifndef SK_FONT_FILE_PREFIX | 24 #ifndef SK_FONT_FILE_PREFIX |
25 # define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/" | 25 # define SK_FONT_FILE_PREFIX "/usr/share/fonts/" |
26 #endif | 26 #endif |
27 | 27 |
28 /////////////////////////////////////////////////////////////////////////////// | 28 /////////////////////////////////////////////////////////////////////////////// |
29 | 29 |
30 /** The base SkTypeface implementation for the custom font manager. */ | 30 /** The base SkTypeface implementation for the custom font manager. */ |
31 class SkTypeface_Custom : public SkTypeface_FreeType { | 31 class SkTypeface_Custom : public SkTypeface_FreeType { |
32 public: | 32 public: |
33 SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch, | 33 SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch, |
34 bool sysFont, const SkString familyName, int index) | 34 bool sysFont, const SkString familyName, int index) |
35 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) | 35 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 | 268 |
269 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const
SK_OVERRIDE { | 269 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const
SK_OVERRIDE { |
270 if (NULL == stream || stream->getLength() <= 0) { | 270 if (NULL == stream || stream->getLength() <= 0) { |
271 SkDELETE(stream); | 271 SkDELETE(stream); |
272 return NULL; | 272 return NULL; |
273 } | 273 } |
274 | 274 |
275 bool isFixedPitch; | 275 bool isFixedPitch; |
276 SkFontStyle style; | 276 SkFontStyle style; |
277 SkString name; | 277 SkString name; |
278 if (SkTypeface_FreeType::ScanFont(stream, ttcIndex, &name, &style, &isFi
xedPitch)) { | 278 if (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) { |
279 return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, na
me, | 279 return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, na
me, |
280 stream, ttcIndex)); | 280 stream, ttcIndex)); |
281 } else { | 281 } else { |
282 return NULL; | 282 return NULL; |
283 } | 283 } |
284 } | 284 } |
285 | 285 |
286 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const
SK_OVERRIDE { | 286 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const
SK_OVERRIDE { |
287 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); | 287 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); |
288 return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL; | 288 return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL; |
(...skipping 18 matching lines...) Expand all Loading... |
307 | 307 |
308 if (NULL == tf) { | 308 if (NULL == tf) { |
309 tf = gDefaultFamily->matchStyle(style); | 309 tf = gDefaultFamily->matchStyle(style); |
310 } | 310 } |
311 | 311 |
312 return SkSafeRef(tf); | 312 return SkSafeRef(tf); |
313 } | 313 } |
314 | 314 |
315 private: | 315 private: |
316 | 316 |
317 static bool get_name_and_style(const char path[], SkString* name, | 317 void load_directory_fonts(const SkString& directory, const char* suffix) { |
318 SkFontStyle* style, bool* isFixedPitch) { | 318 SkOSFile::Iter iter(directory.c_str(), suffix); |
319 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); | |
320 if (stream.get()) { | |
321 return SkTypeface_FreeType::ScanFont(stream, 0, name, style, isFixed
Pitch); | |
322 } else { | |
323 SkDebugf("---- failed to open <%s> as a font\n", path); | |
324 return false; | |
325 } | |
326 } | |
327 | |
328 void load_directory_fonts(const SkString& directory) { | |
329 SkOSFile::Iter iter(directory.c_str(), ".ttf"); | |
330 SkString name; | 319 SkString name; |
331 | 320 |
332 while (iter.next(&name, false)) { | 321 while (iter.next(&name, false)) { |
333 SkString filename( | 322 SkString filename(SkOSPath::Join(directory.c_str(), name.c_str())); |
334 SkOSPath::Join(directory.c_str(), name.c_str())); | 323 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(filename.c_str()
)); |
335 | 324 if (!stream.get()) { |
336 bool isFixedPitch; | 325 SkDebugf("---- failed to open <%s>\n", filename.c_str()); |
337 SkString realname; | |
338 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning | |
339 if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixe
dPitch)) { | |
340 SkDebugf("------ can't load <%s> as a font\n", filename.c_str())
; | |
341 continue; | 326 continue; |
342 } | 327 } |
343 | 328 |
344 SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, ( | 329 int numFaces; |
345 style, | 330 if (!fScanner.recognizedFont(stream, &numFaces)) { |
346 isFixedPitch, | 331 SkDebugf("---- failed to open <%s> as a font\n", filename.c_str(
)); |
347 true, // system-font (cannot de
lete) | 332 continue; |
348 realname, | 333 } |
349 filename.c_str(), 0)); | |
350 | 334 |
351 SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str())
; | 335 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) { |
352 if (NULL == addTo) { | 336 bool isFixedPitch; |
353 addTo = new SkFontStyleSet_Custom(realname); | 337 SkString realname; |
354 fFamilies.push_back().reset(addTo); | 338 SkFontStyle style = SkFontStyle(); // avoid uninitialized warnin
g |
| 339 if (!fScanner.scanFont(stream, faceIndex, &realname, &style, &is
FixedPitch)) { |
| 340 SkDebugf("---- failed to open <%s> <%d> as a font\n", |
| 341 filename.c_str(), faceIndex); |
| 342 continue; |
| 343 } |
| 344 |
| 345 SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, ( |
| 346 style, |
| 347 isFixedPitch, |
| 348 true, // system-font (canno
t delete) |
| 349 realname, |
| 350 filename.c_str(), 0)); |
| 351 |
| 352 SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_st
r()); |
| 353 if (NULL == addTo) { |
| 354 addTo = new SkFontStyleSet_Custom(realname); |
| 355 fFamilies.push_back().reset(addTo); |
| 356 } |
| 357 addTo->appendTypeface(tf); |
355 } | 358 } |
356 addTo->appendTypeface(tf); | |
357 } | 359 } |
358 | 360 |
359 SkOSFile::Iter dirIter(directory.c_str()); | 361 SkOSFile::Iter dirIter(directory.c_str()); |
360 while (dirIter.next(&name, true)) { | 362 while (dirIter.next(&name, true)) { |
361 if (name.startsWith(".")) { | 363 if (name.startsWith(".")) { |
362 continue; | 364 continue; |
363 } | 365 } |
364 SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str())); | 366 SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str())); |
365 load_directory_fonts(dirname); | 367 load_directory_fonts(dirname, suffix); |
366 } | 368 } |
367 } | 369 } |
368 | 370 |
369 void load_system_fonts(const char* dir) { | 371 void load_system_fonts(const char* dir) { |
370 SkString baseDirectory(dir); | 372 SkString baseDirectory(dir); |
371 load_directory_fonts(baseDirectory); | 373 load_directory_fonts(baseDirectory, ".ttf"); |
| 374 load_directory_fonts(baseDirectory, ".ttc"); |
| 375 load_directory_fonts(baseDirectory, ".otf"); |
| 376 load_directory_fonts(baseDirectory, ".pfb"); |
372 | 377 |
373 if (fFamilies.empty()) { | 378 if (fFamilies.empty()) { |
374 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString()
); | 379 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString()
); |
375 fFamilies.push_back().reset(family); | 380 fFamilies.push_back().reset(family); |
376 family->appendTypeface(SkNEW(SkTypeface_Empty)); | 381 family->appendTypeface(SkNEW(SkTypeface_Empty)); |
377 } | 382 } |
378 | 383 |
379 // Try to pick a default font. | 384 // Try to pick a default font. |
380 static const char* gDefaultNames[] = { | 385 static const char* gDefaultNames[] = { |
381 "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL | 386 "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL |
(...skipping 17 matching lines...) Expand all Loading... |
399 } | 404 } |
400 if (NULL == gDefaultNormal) { | 405 if (NULL == gDefaultNormal) { |
401 gDefaultFamily = fFamilies[0]; | 406 gDefaultFamily = fFamilies[0]; |
402 gDefaultNormal = gDefaultFamily->fStyles[0]; | 407 gDefaultNormal = gDefaultFamily->fStyles[0]; |
403 } | 408 } |
404 } | 409 } |
405 | 410 |
406 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies; | 411 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies; |
407 SkFontStyleSet_Custom* gDefaultFamily; | 412 SkFontStyleSet_Custom* gDefaultFamily; |
408 SkTypeface* gDefaultNormal; | 413 SkTypeface* gDefaultNormal; |
| 414 SkTypeface_FreeType::Scanner fScanner; |
409 }; | 415 }; |
410 | 416 |
411 SkFontMgr* SkFontMgr::Factory() { | 417 SkFontMgr* SkFontMgr::Factory() { |
412 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX); | 418 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX); |
413 } | 419 } |
OLD | NEW |