OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 The Android Open Source Project | 3 * Copyright 2011 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 "SkDescriptor.h" | 11 #include "SkDescriptor.h" |
12 #include "SkMMapStream.h" | |
13 #include "SkPaint.h" | 12 #include "SkPaint.h" |
14 #include "SkString.h" | 13 #include "SkString.h" |
15 #include "SkStream.h" | 14 #include "SkStream.h" |
16 #include "SkThread.h" | 15 #include "SkThread.h" |
17 #include "SkTSearch.h" | 16 #include "SkTSearch.h" |
18 #include <stdio.h> | 17 #include <stdio.h> |
19 | 18 |
20 #ifdef SK_BUILD_FOR_MAC | 19 #ifdef SK_BUILD_FOR_MAC |
21 #define SK_FONT_FILE_PREFIX "/Library/Fonts/" | 20 #define SK_FONT_FILE_PREFIX "/Library/Fonts/" |
22 #else | 21 #else |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 : INHERITED(style, sysFont, familyMember) { | 304 : INHERITED(style, sysFont, familyMember) { |
306 SkString fullpath; | 305 SkString fullpath; |
307 | 306 |
308 if (sysFont) { | 307 if (sysFont) { |
309 GetFullPathForSysFonts(&fullpath, path); | 308 GetFullPathForSysFonts(&fullpath, path); |
310 path = fullpath.c_str(); | 309 path = fullpath.c_str(); |
311 } | 310 } |
312 fPath.set(path); | 311 fPath.set(path); |
313 } | 312 } |
314 | 313 |
315 // overrides | 314 virtual SkStream* openStream() SK_OVERRIDE { |
316 virtual SkStream* openStream() { | 315 return SkStream::NewFromFile(fPath.c_str()); |
317 SkStream* stream = SkNEW_ARGS(SkMMAPStream, (fPath.c_str())); | 316 } |
318 | 317 |
319 // check for failure | 318 virtual const char* getUniqueString() const SK_OVERRIDE { |
320 if (stream->getLength() <= 0) { | |
321 SkDELETE(stream); | |
322 // maybe MMAP isn't supported. try FILE | |
323 stream = SkNEW_ARGS(SkFILEStream, (fPath.c_str())); | |
324 if (stream->getLength() <= 0) { | |
325 SkDELETE(stream); | |
326 stream = NULL; | |
327 } | |
328 } | |
329 return stream; | |
330 } | |
331 virtual const char* getUniqueString() const { | |
332 const char* str = strrchr(fPath.c_str(), '/'); | 319 const char* str = strrchr(fPath.c_str(), '/'); |
333 if (str) { | 320 if (str) { |
334 str += 1; // skip the '/' | 321 str += 1; // skip the '/' |
335 } | 322 } |
336 return str; | 323 return str; |
337 } | 324 } |
338 virtual const char* getFilePath() const { | 325 virtual const char* getFilePath() const SK_OVERRIDE { |
339 return fPath.c_str(); | 326 return fPath.c_str(); |
340 } | 327 } |
341 | 328 |
342 private: | 329 private: |
343 SkString fPath; | 330 SkString fPath; |
344 | 331 |
345 typedef FamilyTypeface INHERITED; | 332 typedef FamilyTypeface INHERITED; |
346 }; | 333 }; |
347 | 334 |
348 /////////////////////////////////////////////////////////////////////////////// | 335 /////////////////////////////////////////////////////////////////////////////// |
349 /////////////////////////////////////////////////////////////////////////////// | 336 /////////////////////////////////////////////////////////////////////////////// |
350 | 337 |
351 static bool get_name_and_style(const char path[], SkString* name, | 338 static bool get_name_and_style(const char path[], SkString* name, |
352 SkTypeface::Style* style, bool isExpected) { | 339 SkTypeface::Style* style, bool isExpected) { |
353 SkString fullpath; | 340 SkString fullpath; |
354 GetFullPathForSysFonts(&fullpath, path); | 341 GetFullPathForSysFonts(&fullpath, path); |
355 | 342 |
356 SkMMAPStream stream(fullpath.c_str()); | 343 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(fullpath.c_str())); |
357 if (stream.getLength() > 0) { | 344 if (stream.get()) { |
358 return find_name_and_attributes(&stream, name, style, NULL); | 345 return find_name_and_attributes(&stream, name, style, NULL); |
| 346 } else { |
| 347 if (isExpected) { |
| 348 SkDebugf("---- failed to open <%s> as a font\n", fullpath.c_str()); |
| 349 } |
| 350 return false; |
359 } | 351 } |
360 else { | |
361 SkFILEStream stream(fullpath.c_str()); | |
362 if (stream.getLength() > 0) { | |
363 return find_name_and_attributes(&stream, name, style, NULL); | |
364 } | |
365 } | |
366 | |
367 if (isExpected) { | |
368 SkDebugf("---- failed to open <%s> as a font\n", fullpath.c_str()); | |
369 } | |
370 return false; | |
371 } | 352 } |
372 | 353 |
373 // used to record our notion of the pre-existing fonts | 354 // used to record our notion of the pre-existing fonts |
374 struct FontInitRec { | 355 struct FontInitRec { |
375 const char* fFileName; | 356 const char* fFileName; |
376 const char* const* fNames; // null-terminated list | 357 const char* const* fNames; // null-terminated list |
377 }; | 358 }; |
378 | 359 |
379 static const char* gSansNames[] = { | 360 static const char* gSansNames[] = { |
380 "sans-serif", "arial", "helvetica", "tahoma", "verdana", NULL | 361 "sans-serif", "arial", "helvetica", "tahoma", "verdana", NULL |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 | 610 |
630 SkTypeface::Style style; | 611 SkTypeface::Style style; |
631 if (find_name_and_attributes(stream, NULL, &style, NULL)) { | 612 if (find_name_and_attributes(stream, NULL, &style, NULL)) { |
632 return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream)); | 613 return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream)); |
633 } else { | 614 } else { |
634 return NULL; | 615 return NULL; |
635 } | 616 } |
636 } | 617 } |
637 | 618 |
638 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { | 619 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { |
639 SkStream* stream = SkNEW_ARGS(SkMMAPStream, (path)); | 620 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); |
640 SkTypeface* face = SkFontHost::CreateTypefaceFromStream(stream); | 621 return stream.get() ? SkFontHost::CreateTypefaceFromStream(stream) : NULL; |
641 // since we created the stream, we let go of our ref() here | |
642 stream->unref(); | |
643 return face; | |
644 } | 622 } |
OLD | NEW |