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/" | 25 # define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/" |
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 (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) { | 278 if (SkTypeface_FreeType::ScanFont(stream, ttcIndex, &name, &style, &isFi
xedPitch)) { |
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 void load_directory_fonts(const SkString& directory, const char* suffix) { | 317 static bool get_name_and_style(const char path[], SkString* name, |
318 SkOSFile::Iter iter(directory.c_str(), suffix); | 318 SkFontStyle* style, bool* isFixedPitch) { |
| 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"); |
319 SkString name; | 330 SkString name; |
320 | 331 |
321 while (iter.next(&name, false)) { | 332 while (iter.next(&name, false)) { |
322 SkString filename(SkOSPath::Join(directory.c_str(), name.c_str())); | 333 SkString filename( |
323 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(filename.c_str()
)); | 334 SkOSPath::Join(directory.c_str(), name.c_str())); |
324 if (!stream.get()) { | 335 |
325 SkDebugf("---- failed to open <%s>\n", filename.c_str()); | 336 bool isFixedPitch; |
| 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())
; |
326 continue; | 341 continue; |
327 } | 342 } |
328 | 343 |
329 int numFaces; | 344 SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, ( |
330 if (!fScanner.recognizedFont(stream, &numFaces)) { | 345 style, |
331 SkDebugf("---- failed to open <%s> as a font\n", filename.c_str(
)); | 346 isFixedPitch, |
332 continue; | 347 true, // system-font (cannot de
lete) |
| 348 realname, |
| 349 filename.c_str(), 0)); |
| 350 |
| 351 SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str())
; |
| 352 if (NULL == addTo) { |
| 353 addTo = new SkFontStyleSet_Custom(realname); |
| 354 fFamilies.push_back().reset(addTo); |
333 } | 355 } |
334 | 356 addTo->appendTypeface(tf); |
335 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) { | |
336 bool isFixedPitch; | |
337 SkString realname; | |
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); | |
358 } | |
359 } | 357 } |
360 | 358 |
361 SkOSFile::Iter dirIter(directory.c_str()); | 359 SkOSFile::Iter dirIter(directory.c_str()); |
362 while (dirIter.next(&name, true)) { | 360 while (dirIter.next(&name, true)) { |
363 if (name.startsWith(".")) { | 361 if (name.startsWith(".")) { |
364 continue; | 362 continue; |
365 } | 363 } |
366 SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str())); | 364 SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str())); |
367 load_directory_fonts(dirname, suffix); | 365 load_directory_fonts(dirname); |
368 } | 366 } |
369 } | 367 } |
370 | 368 |
371 void load_system_fonts(const char* dir) { | 369 void load_system_fonts(const char* dir) { |
372 SkString baseDirectory(dir); | 370 SkString baseDirectory(dir); |
373 load_directory_fonts(baseDirectory, ".ttf"); | 371 load_directory_fonts(baseDirectory); |
374 load_directory_fonts(baseDirectory, ".ttc"); | |
375 load_directory_fonts(baseDirectory, ".otf"); | |
376 load_directory_fonts(baseDirectory, ".pfb"); | |
377 | 372 |
378 if (fFamilies.empty()) { | 373 if (fFamilies.empty()) { |
379 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString()
); | 374 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString()
); |
380 fFamilies.push_back().reset(family); | 375 fFamilies.push_back().reset(family); |
381 family->appendTypeface(SkNEW(SkTypeface_Empty)); | 376 family->appendTypeface(SkNEW(SkTypeface_Empty)); |
382 } | 377 } |
383 | 378 |
384 // Try to pick a default font. | 379 // Try to pick a default font. |
385 static const char* gDefaultNames[] = { | 380 static const char* gDefaultNames[] = { |
386 "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL | 381 "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL |
(...skipping 17 matching lines...) Expand all Loading... |
404 } | 399 } |
405 if (NULL == gDefaultNormal) { | 400 if (NULL == gDefaultNormal) { |
406 gDefaultFamily = fFamilies[0]; | 401 gDefaultFamily = fFamilies[0]; |
407 gDefaultNormal = gDefaultFamily->fStyles[0]; | 402 gDefaultNormal = gDefaultFamily->fStyles[0]; |
408 } | 403 } |
409 } | 404 } |
410 | 405 |
411 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies; | 406 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies; |
412 SkFontStyleSet_Custom* gDefaultFamily; | 407 SkFontStyleSet_Custom* gDefaultFamily; |
413 SkTypeface* gDefaultNormal; | 408 SkTypeface* gDefaultNormal; |
414 SkTypeface_FreeType::Scanner fScanner; | |
415 }; | 409 }; |
416 | 410 |
417 SkFontMgr* SkFontMgr::Factory() { | 411 SkFontMgr* SkFontMgr::Factory() { |
418 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX); | 412 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX); |
419 } | 413 } |
OLD | NEW |