OLD | NEW |
| (Empty) |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "cc/font_atlas.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/string_split.h" | |
10 #include "skia/ext/refptr.h" | |
11 #include "third_party/skia/include/core/SkCanvas.h" | |
12 #include "third_party/skia/include/effects/SkColorMatrixFilter.h" | |
13 | |
14 namespace { | |
15 | |
16 // The FontAtlas uses a bitmap with pre-rendered RED glyps to display text. | |
17 // Therefore using SkPaint::setColor() has no effect on the font's color when te
xt is drawn. | |
18 // This helper class uses color filtering as a workaround to change the font's c
olor. | |
19 class ScopedFontColorFilter { | |
20 public: | |
21 ScopedFontColorFilter() | |
22 : m_paint(0) | |
23 { | |
24 } | |
25 | |
26 void init(SkPaint* paint, const SkColor& color) | |
27 { | |
28 if (color == SK_ColorRED) | |
29 return; | |
30 | |
31 SkColorMatrix matrix; | |
32 memset(matrix.fMat, 0, sizeof(matrix.fMat)); | |
33 | |
34 // Set the matrix to transform the pre-rendered RED font into the provid
ed color. | |
35 matrix.fMat[5 * 0] = SkColorGetR(color) / 255.0; | |
36 matrix.fMat[5 * 1] = SkColorGetG(color) / 255.0; | |
37 matrix.fMat[5 * 2] = SkColorGetB(color) / 255.0; | |
38 matrix.fMat[5 * 3 + 3] = SkColorGetA(color) / 255.0; | |
39 | |
40 // SkPaint can only use one color filter, so the old one needs to be sav
ed. | |
41 SkColorFilter* oldFilter = paint->getColorFilter(); | |
42 if (oldFilter) { | |
43 // Increase the reference count, so the old filter is not released w
hen it's replaced. | |
44 oldFilter->ref(); | |
45 | |
46 // Save the old filter for later restoring. | |
47 // Reference count will get decreased on destruction. | |
48 m_oldFilter = skia::AdoptRef(oldFilter); | |
49 | |
50 // If the old filter had a color matrix, the new filter will use bot
h. | |
51 SkColorMatrix oldMatrix; | |
52 if (oldFilter->asColorMatrix(oldMatrix.fMat)) | |
53 matrix.setConcat(oldMatrix, matrix); | |
54 } | |
55 | |
56 // Set the new color filter and replace the old one. | |
57 skia::RefPtr<SkColorMatrixFilter> filter = skia::AdoptRef(new SkColorMat
rixFilter(matrix)); | |
58 paint->setColorFilter(filter.get()); | |
59 | |
60 m_paint = paint; | |
61 } | |
62 | |
63 ~ScopedFontColorFilter() | |
64 { | |
65 // Remove the color filter and restore the old one. | |
66 if (m_paint) | |
67 m_paint->setColorFilter(m_oldFilter.get()); | |
68 } | |
69 | |
70 private: | |
71 SkPaint* m_paint; | |
72 skia::RefPtr<SkColorFilter> m_oldFilter; | |
73 }; | |
74 | |
75 } // namespace | |
76 | |
77 namespace cc { | |
78 | |
79 FontAtlas::FontAtlas(SkBitmap bitmap, gfx::Rect asciiToRectTable[128], int fontH
eight) | |
80 : m_atlas(bitmap) | |
81 , m_fontHeight(fontHeight) | |
82 , m_color(SK_ColorRED) | |
83 { | |
84 for (size_t i = 0; i < 128; ++i) | |
85 m_asciiToRectTable[i] = asciiToRectTable[i]; | |
86 } | |
87 | |
88 FontAtlas::~FontAtlas() | |
89 { | |
90 } | |
91 | |
92 void FontAtlas::drawText(SkCanvas* canvas, SkPaint* paint, const std::string& te
xt, const gfx::Point& destPosition, const gfx::Size& clip) const | |
93 { | |
94 ScopedFontColorFilter filter; | |
95 filter.init(paint, m_color); | |
96 | |
97 std::vector<std::string> lines; | |
98 base::SplitString(text, '\n', &lines); | |
99 | |
100 gfx::Point position = destPosition; | |
101 for (size_t i = 0; i < lines.size(); ++i) { | |
102 drawOneLineOfTextInternal(canvas, *paint, lines[i], position); | |
103 position.set_y(position.y() + m_fontHeight); | |
104 if (position.y() > clip.height()) | |
105 return; | |
106 } | |
107 } | |
108 | |
109 void FontAtlas::drawOneLineOfTextInternal(SkCanvas* canvas, const SkPaint& paint
, const std::string& textLine, const gfx::Point& destPosition) const | |
110 { | |
111 gfx::Point position = destPosition; | |
112 for (unsigned i = 0; i < textLine.length(); ++i) { | |
113 // If the ASCII code is out of bounds, then index 0 is used, which is ju
st a plain rectangle glyph. | |
114 unsigned asciiIndex = textLine[i]; | |
115 if (asciiIndex >= 128) | |
116 asciiIndex = 0; | |
117 gfx::Rect glyphBounds = m_asciiToRectTable[asciiIndex]; | |
118 SkIRect source = SkIRect::MakeXYWH(glyphBounds.x(), glyphBounds.y(), gly
phBounds.width(), glyphBounds.height()); | |
119 canvas->drawBitmapRect(m_atlas, &source, SkRect::MakeXYWH(position.x(),
position.y(), glyphBounds.width(), glyphBounds.height()), &paint); | |
120 position.set_x(position.x() + glyphBounds.width()); | |
121 } | |
122 } | |
123 | |
124 gfx::Size FontAtlas::textSize(const std::string& text) | |
125 { | |
126 int maxWidth = 0; | |
127 std::vector<std::string> lines; | |
128 base::SplitString(text, '\n', &lines); | |
129 | |
130 for (size_t i = 0; i < lines.size(); ++i) { | |
131 int lineWidth = 0; | |
132 for (size_t j = 0; j < lines[i].size(); ++j) { | |
133 unsigned asciiIndex = lines[i][j]; | |
134 if (asciiIndex >= 128) | |
135 asciiIndex = 0; | |
136 lineWidth += m_asciiToRectTable[asciiIndex].width(); | |
137 } | |
138 if (lineWidth > maxWidth) | |
139 maxWidth = lineWidth; | |
140 } | |
141 | |
142 return gfx::Size(maxWidth, m_fontHeight * lines.size()); | |
143 } | |
144 | |
145 void FontAtlas::drawDebugAtlas(SkCanvas* canvas, const gfx::Point& destPosition)
const | |
146 { | |
147 SkIRect source = SkIRect::MakeWH(m_atlas.width(), m_atlas.height()); | |
148 canvas->drawBitmapRect(m_atlas, &source, SkRect::MakeXYWH(destPosition.x(),
destPosition.y(), m_atlas.width(), m_atlas.height())); | |
149 } | |
150 | |
151 } // namespace cc | |
OLD | NEW |