| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #include "SkFontHost.h" | 10 #include "SkFontHost.h" |
| 11 #include "SkFontDescriptor.h" | 11 #include "SkFontDescriptor.h" |
| 12 #include "SkDescriptor.h" | 12 #include "SkDescriptor.h" |
| 13 #include "SkMMapStream.h" | |
| 14 #include "SkOSFile.h" | 13 #include "SkOSFile.h" |
| 15 #include "SkPaint.h" | 14 #include "SkPaint.h" |
| 16 #include "SkString.h" | 15 #include "SkString.h" |
| 17 #include "SkStream.h" | 16 #include "SkStream.h" |
| 18 #include "SkThread.h" | 17 #include "SkThread.h" |
| 19 #include "SkTSearch.h" | 18 #include "SkTSearch.h" |
| 20 | 19 |
| 21 #ifndef SK_FONT_FILE_PREFIX | 20 #ifndef SK_FONT_FILE_PREFIX |
| 22 #define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/" | 21 #define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/" |
| 23 #endif | 22 #endif |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 | 272 |
| 274 /* This subclass is just a place holder for when we have no fonts available. | 273 /* This subclass is just a place holder for when we have no fonts available. |
| 275 It exists so that our globals (e.g. gFamilyHead) that expect *something* | 274 It exists so that our globals (e.g. gFamilyHead) that expect *something* |
| 276 will not be null. | 275 will not be null. |
| 277 */ | 276 */ |
| 278 class EmptyTypeface : public FamilyTypeface { | 277 class EmptyTypeface : public FamilyTypeface { |
| 279 public: | 278 public: |
| 280 EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {} | 279 EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {} |
| 281 | 280 |
| 282 // overrides | 281 // overrides |
| 283 virtual SkStream* openStream() { return NULL; } | 282 virtual SkStream* openStream() SK_OVERRIDE { return NULL; } |
| 284 virtual const char* getUniqueString() const { return NULL; } | 283 virtual const char* getUniqueString() SK_OVERRIDE const { return NULL; } |
| 285 | 284 |
| 286 private: | 285 private: |
| 287 typedef FamilyTypeface INHERITED; | 286 typedef FamilyTypeface INHERITED; |
| 288 }; | 287 }; |
| 289 | 288 |
| 290 class StreamTypeface : public FamilyTypeface { | 289 class StreamTypeface : public FamilyTypeface { |
| 291 public: | 290 public: |
| 292 StreamTypeface(Style style, bool sysFont, FamilyRec* family, | 291 StreamTypeface(Style style, bool sysFont, FamilyRec* family, |
| 293 SkStream* stream, bool isFixedWidth) | 292 SkStream* stream, bool isFixedWidth) |
| 294 : INHERITED(style, sysFont, family, isFixedWidth) { | 293 : INHERITED(style, sysFont, family, isFixedWidth) { |
| 295 stream->ref(); | 294 stream->ref(); |
| 296 fStream = stream; | 295 fStream = stream; |
| 297 } | 296 } |
| 298 virtual ~StreamTypeface() { | 297 virtual ~StreamTypeface() { |
| 299 fStream->unref(); | 298 fStream->unref(); |
| 300 } | 299 } |
| 301 | 300 |
| 302 // overrides | 301 virtual SkStream* openStream() SK_OVERRIDE { |
| 303 virtual SkStream* openStream() | |
| 304 { | |
| 305 // openStream returns a refed stream. | 302 // openStream returns a refed stream. |
| 306 fStream->ref(); | 303 fStream->ref(); |
| 307 return fStream; | 304 return fStream; |
| 308 } | 305 } |
| 309 virtual const char* getUniqueString() const { return NULL; } | 306 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; } |
| 310 | 307 |
| 311 private: | 308 private: |
| 312 SkStream* fStream; | 309 SkStream* fStream; |
| 313 | 310 |
| 314 typedef FamilyTypeface INHERITED; | 311 typedef FamilyTypeface INHERITED; |
| 315 }; | 312 }; |
| 316 | 313 |
| 317 class FileTypeface : public FamilyTypeface { | 314 class FileTypeface : public FamilyTypeface { |
| 318 public: | 315 public: |
| 319 FileTypeface(Style style, bool sysFont, FamilyRec* family, | 316 FileTypeface(Style style, bool sysFont, FamilyRec* family, |
| 320 const char path[], bool isFixedWidth) | 317 const char path[], bool isFixedWidth) |
| 321 : INHERITED(style, sysFont, family, isFixedWidth) { | 318 : INHERITED(style, sysFont, family, isFixedWidth) { |
| 322 fPath.set(path); | 319 fPath.set(path); |
| 323 } | 320 } |
| 324 | 321 |
| 325 // overrides | 322 virtual SkStream* openStream() SK_OVERRIDE { |
| 326 virtual SkStream* openStream() | 323 return SkStream::NewFromFile(fPath.c_str()); |
| 327 { | |
| 328 SkStream* stream = SkNEW_ARGS(SkMMAPStream, (fPath.c_str())); | |
| 329 | |
| 330 // check for failure | |
| 331 if (stream->getLength() <= 0) { | |
| 332 SkDELETE(stream); | |
| 333 // maybe MMAP isn't supported. try FILE | |
| 334 stream = SkNEW_ARGS(SkFILEStream, (fPath.c_str())); | |
| 335 if (stream->getLength() <= 0) { | |
| 336 SkDELETE(stream); | |
| 337 stream = NULL; | |
| 338 } | |
| 339 } | |
| 340 return stream; | |
| 341 } | 324 } |
| 342 | 325 |
| 343 virtual const char* getUniqueString() const { | 326 virtual const char* getUniqueString() const SK_OVERRIDE { |
| 344 const char* str = strrchr(fPath.c_str(), '/'); | 327 const char* str = strrchr(fPath.c_str(), '/'); |
| 345 if (str) { | 328 if (str) { |
| 346 str += 1; // skip the '/' | 329 str += 1; // skip the '/' |
| 347 } | 330 } |
| 348 return str; | 331 return str; |
| 349 } | 332 } |
| 350 | 333 |
| 351 private: | 334 private: |
| 352 SkString fPath; | 335 SkString fPath; |
| 353 | 336 |
| 354 typedef FamilyTypeface INHERITED; | 337 typedef FamilyTypeface INHERITED; |
| 355 }; | 338 }; |
| 356 | 339 |
| 357 /////////////////////////////////////////////////////////////////////////////// | 340 /////////////////////////////////////////////////////////////////////////////// |
| 358 /////////////////////////////////////////////////////////////////////////////// | 341 /////////////////////////////////////////////////////////////////////////////// |
| 359 | 342 |
| 360 static bool get_name_and_style(const char path[], SkString* name, | 343 static bool get_name_and_style(const char path[], SkString* name, |
| 361 SkTypeface::Style* style, bool* isFixedWidth) { | 344 SkTypeface::Style* style, bool* isFixedWidth) { |
| 362 SkMMAPStream stream(path); | 345 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); |
| 363 if (stream.getLength() > 0) { | 346 if (stream.get()) { |
| 364 return find_name_and_attributes(&stream, name, style, isFixedWidth); | 347 return find_name_and_attributes(stream, name, style, isFixedWidth); |
| 348 } else { |
| 349 SkDebugf("---- failed to open <%s> as a font\n", path); |
| 350 return false; |
| 365 } | 351 } |
| 366 else { | |
| 367 SkFILEStream stream(path); | |
| 368 if (stream.getLength() > 0) { | |
| 369 return find_name_and_attributes(&stream, name, style, isFixedWidth); | |
| 370 } | |
| 371 } | |
| 372 | |
| 373 SkDebugf("---- failed to open <%s> as a font\n", path); | |
| 374 return false; | |
| 375 } | 352 } |
| 376 | 353 |
| 377 // these globals are assigned (once) by load_system_fonts() | 354 // these globals are assigned (once) by load_system_fonts() |
| 378 static SkTypeface* gFallBackTypeface; | 355 static SkTypeface* gFallBackTypeface; |
| 379 static FamilyRec* gDefaultFamily; | 356 static FamilyRec* gDefaultFamily; |
| 380 static SkTypeface* gDefaultNormal; | 357 static SkTypeface* gDefaultNormal; |
| 381 | 358 |
| 382 static void load_directory_fonts(const SkString& directory, unsigned int* count)
{ | 359 static void load_directory_fonts(const SkString& directory, unsigned int* count)
{ |
| 383 SkOSFile::Iter iter(directory.c_str(), ".ttf"); | 360 SkOSFile::Iter iter(directory.c_str(), ".ttf"); |
| 384 SkString name; | 361 SkString name; |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { | 569 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { |
| 593 SkTypeface* face = NULL; | 570 SkTypeface* face = NULL; |
| 594 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path)); | 571 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path)); |
| 595 | 572 |
| 596 if (stream->isValid()) { | 573 if (stream->isValid()) { |
| 597 face = CreateTypefaceFromStream(stream); | 574 face = CreateTypefaceFromStream(stream); |
| 598 } | 575 } |
| 599 stream->unref(); | 576 stream->unref(); |
| 600 return face; | 577 return face; |
| 601 } | 578 } |
| OLD | NEW |