OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "skia/ext/vector_platform_device.h" | 5 #include "skia/ext/vector_platform_device.h" |
6 | 6 |
7 #include <cairo.h> | 7 #include <cairo.h> |
8 #include <cairo-ft.h> | 8 #include <cairo-ft.h> |
9 | 9 |
10 #include <ft2build.h> | 10 #include <ft2build.h> |
11 #include FT_FREETYPE_H | 11 #include FT_FREETYPE_H |
12 | 12 |
13 #include <map> | 13 #include <map> |
14 | 14 |
| 15 #include "base/lazy_instance.h" |
15 #include "base/logging.h" | 16 #include "base/logging.h" |
16 #include "base/singleton.h" | |
17 #include "skia/ext/bitmap_platform_device.h" | 17 #include "skia/ext/bitmap_platform_device.h" |
18 #include "third_party/skia/include/core/SkFontHost.h" | 18 #include "third_party/skia/include/core/SkFontHost.h" |
19 #include "third_party/skia/include/core/SkStream.h" | 19 #include "third_party/skia/include/core/SkStream.h" |
20 #include "third_party/skia/include/core/SkTypeface.h" | 20 #include "third_party/skia/include/core/SkTypeface.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 struct FontInfo { | 24 struct FontInfo { |
25 SkStream* font_stream; | 25 SkStream* font_stream; |
26 FT_Face ft_face; | 26 FT_Face ft_face; |
27 cairo_font_face_t* cairo_face; | 27 cairo_font_face_t* cairo_face; |
28 cairo_user_data_key_t data_key; | 28 cairo_user_data_key_t data_key; |
29 }; | 29 }; |
30 | 30 |
31 typedef std::map<uint32_t, FontInfo> MapFontId2FontInfo; | 31 typedef std::map<uint32_t, FontInfo> MapFontId2FontInfo; |
| 32 static base::LazyInstance<MapFontId2FontInfo> g_map_font_id_to_font_info( |
| 33 base::LINKER_INITIALIZED); |
32 | 34 |
33 // Wrapper for FT_Library that handles initialization and cleanup, and allows | 35 // Wrapper for FT_Library that handles initialization and cleanup, and allows |
34 // us to use a singleton. | 36 // us to use a singleton. |
35 class FtLibrary { | 37 class FtLibrary { |
36 public: | 38 public: |
37 FtLibrary() : library_(NULL) { | 39 FtLibrary() : library_(NULL) { |
38 FT_Error ft_error = FT_Init_FreeType(&library_); | 40 FT_Error ft_error = FT_Init_FreeType(&library_); |
39 if (ft_error) { | 41 if (ft_error) { |
40 DLOG(ERROR) << "Cannot initialize FreeType library for " \ | 42 DLOG(ERROR) << "Cannot initialize FreeType library for " \ |
41 << "VectorPlatformDevice."; | 43 << "VectorPlatformDevice."; |
42 } | 44 } |
43 } | 45 } |
44 | 46 |
45 ~FtLibrary() { | 47 ~FtLibrary() { |
46 if (library_) { | 48 if (library_) { |
47 FT_Error ft_error = FT_Done_FreeType(library_); | 49 FT_Error ft_error = FT_Done_FreeType(library_); |
48 library_ = NULL; | 50 library_ = NULL; |
49 DCHECK_EQ(ft_error, 0); | 51 DCHECK_EQ(ft_error, 0); |
50 } | 52 } |
51 } | 53 } |
52 | 54 |
53 FT_Library library() { return library_; } | 55 FT_Library library() { return library_; } |
54 | 56 |
55 private: | 57 private: |
56 FT_Library library_; | 58 FT_Library library_; |
57 }; | 59 }; |
| 60 static base::LazyInstance<FtLibrary> g_ft_library(base::LINKER_INITIALIZED); |
58 | 61 |
59 // Verify cairo surface after creation/modification. | 62 // Verify cairo surface after creation/modification. |
60 bool IsContextValid(cairo_t* context) { | 63 bool IsContextValid(cairo_t* context) { |
61 return cairo_status(context) == CAIRO_STATUS_SUCCESS; | 64 return cairo_status(context) == CAIRO_STATUS_SUCCESS; |
62 } | 65 } |
63 | 66 |
64 } // namespace | 67 } // namespace |
65 | 68 |
66 namespace skia { | 69 namespace skia { |
67 | 70 |
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 m.yx = matrix[SkMatrix::kMSkewY]; | 588 m.yx = matrix[SkMatrix::kMSkewY]; |
586 m.yy = matrix[SkMatrix::kMScaleY]; | 589 m.yy = matrix[SkMatrix::kMScaleY]; |
587 m.y0 = matrix[SkMatrix::kMTransY]; | 590 m.y0 = matrix[SkMatrix::kMTransY]; |
588 cairo_set_matrix(context_, &m); | 591 cairo_set_matrix(context_, &m); |
589 } | 592 } |
590 | 593 |
591 bool VectorPlatformDevice::SelectFontById(uint32_t font_id) { | 594 bool VectorPlatformDevice::SelectFontById(uint32_t font_id) { |
592 DCHECK(IsContextValid(context_)); | 595 DCHECK(IsContextValid(context_)); |
593 DCHECK(SkFontHost::ValidFontID(font_id)); | 596 DCHECK(SkFontHost::ValidFontID(font_id)); |
594 | 597 |
595 FtLibrary* ft_library = Singleton<FtLibrary>::get(); | 598 FtLibrary* ft_library = g_ft_library.Pointer(); |
596 if (!ft_library->library()) | 599 if (!ft_library->library()) |
597 return false; | 600 return false; |
598 | 601 |
599 // Checks if we have a cache hit. | 602 // Checks if we have a cache hit. |
600 MapFontId2FontInfo* g_font_cache = Singleton<MapFontId2FontInfo>::get(); | 603 MapFontId2FontInfo* g_font_cache = g_map_font_id_to_font_info.Pointer(); |
601 DCHECK(g_font_cache); | 604 DCHECK(g_font_cache); |
602 | 605 |
603 MapFontId2FontInfo::iterator it = g_font_cache->find(font_id); | 606 MapFontId2FontInfo::iterator it = g_font_cache->find(font_id); |
604 if (it != g_font_cache->end()) { | 607 if (it != g_font_cache->end()) { |
605 cairo_set_font_face(context_, it->second.cairo_face); | 608 cairo_set_font_face(context_, it->second.cairo_face); |
606 if (IsContextValid(context_)) { | 609 if (IsContextValid(context_)) { |
607 return true; | 610 return true; |
608 } else { | 611 } else { |
609 NOTREACHED() << "Cannot set font face in Cairo!"; | 612 NOTREACHED() << "Cannot set font face in Cairo!"; |
610 return false; | 613 return false; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 if (IsContextValid(context_)) { | 663 if (IsContextValid(context_)) { |
661 return true; | 664 return true; |
662 } | 665 } |
663 | 666 |
664 DLOG(ERROR) << "Connot set font face in Cairo!"; | 667 DLOG(ERROR) << "Connot set font face in Cairo!"; |
665 return false; | 668 return false; |
666 } | 669 } |
667 | 670 |
668 // static | 671 // static |
669 void VectorPlatformDevice::ClearFontCache() { | 672 void VectorPlatformDevice::ClearFontCache() { |
670 MapFontId2FontInfo* g_font_cache = Singleton<MapFontId2FontInfo>::get(); | 673 MapFontId2FontInfo* g_font_cache = g_map_font_id_to_font_info.Pointer(); |
671 DCHECK(g_font_cache); | 674 DCHECK(g_font_cache); |
672 | 675 |
673 for (MapFontId2FontInfo::iterator it = g_font_cache->begin(); | 676 for (MapFontId2FontInfo::iterator it = g_font_cache->begin(); |
674 it !=g_font_cache->end(); | 677 it !=g_font_cache->end(); |
675 ++it) { | 678 ++it) { |
676 DCHECK(it->second.cairo_face); | 679 DCHECK(it->second.cairo_face); |
677 DCHECK(it->second.font_stream); | 680 DCHECK(it->second.font_stream); |
678 | 681 |
679 cairo_font_face_destroy(it->second.cairo_face); | 682 cairo_font_face_destroy(it->second.cairo_face); |
680 // |it->second.ft_face| is handled by Cairo. | 683 // |it->second.ft_face| is handled by Cairo. |
681 it->second.font_stream->unref(); | 684 it->second.font_stream->unref(); |
682 } | 685 } |
683 g_font_cache->clear(); | 686 g_font_cache->clear(); |
684 } | 687 } |
685 | 688 |
686 } // namespace skia | 689 } // namespace skia |
OLD | NEW |