| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 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 "config.h" | 5 #include "config.h" |
| 6 | 6 |
| 7 #include "cc/font_atlas.h" | 7 #include "cc/font_atlas.h" |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/string_split.h" | 11 #include "base/string_split.h" |
| 12 #include "cc/proxy.h" | 12 #include "cc/proxy.h" |
| 13 #include "third_party/skia/include/core/SkCanvas.h" | 13 #include "third_party/skia/include/core/SkCanvas.h" |
| 14 #include "ui/gfx/point.h" | |
| 15 | 14 |
| 16 namespace cc { | 15 namespace cc { |
| 17 | 16 |
| 18 using namespace std; | 17 using namespace std; |
| 19 | 18 |
| 20 FontAtlas::FontAtlas(SkBitmap bitmap, IntRect asciiToRectTable[128], int fontHei
ght) | 19 FontAtlas::FontAtlas(SkBitmap bitmap, gfx::Rect asciiToRectTable[128], int fontH
eight) |
| 21 : m_atlas(bitmap) | 20 : m_atlas(bitmap) |
| 22 , m_fontHeight(fontHeight) | 21 , m_fontHeight(fontHeight) |
| 23 { | 22 { |
| 24 for (size_t i = 0; i < 128; ++i) | 23 for (size_t i = 0; i < 128; ++i) |
| 25 m_asciiToRectTable[i] = asciiToRectTable[i]; | 24 m_asciiToRectTable[i] = asciiToRectTable[i]; |
| 26 } | 25 } |
| 27 | 26 |
| 28 FontAtlas::~FontAtlas() | 27 FontAtlas::~FontAtlas() |
| 29 { | 28 { |
| 30 } | 29 } |
| 31 | 30 |
| 32 void FontAtlas::drawText(SkCanvas* canvas, const SkPaint& paint, const std::stri
ng& text, const gfx::Point& destPosition, const IntSize& clip) const | 31 void FontAtlas::drawText(SkCanvas* canvas, const SkPaint& paint, const std::stri
ng& text, const gfx::Point& destPosition, const gfx::Size& clip) const |
| 33 { | 32 { |
| 34 DCHECK(Proxy::isImplThread()); | 33 DCHECK(Proxy::isImplThread()); |
| 35 | 34 |
| 36 std::vector<std::string> lines; | 35 std::vector<std::string> lines; |
| 37 base::SplitString(text, '\n', &lines); | 36 base::SplitString(text, '\n', &lines); |
| 38 | 37 |
| 39 gfx::Point position = destPosition; | 38 gfx::Point position = destPosition; |
| 40 for (size_t i = 0; i < lines.size(); ++i) { | 39 for (size_t i = 0; i < lines.size(); ++i) { |
| 41 drawOneLineOfTextInternal(canvas, paint, lines[i], position); | 40 drawOneLineOfTextInternal(canvas, paint, lines[i], position); |
| 42 position.set_y(position.y() + m_fontHeight); | 41 position.set_y(position.y() + m_fontHeight); |
| 43 if (position.y() > clip.height()) | 42 if (position.y() > clip.height()) |
| 44 return; | 43 return; |
| 45 } | 44 } |
| 46 } | 45 } |
| 47 | 46 |
| 48 void FontAtlas::drawOneLineOfTextInternal(SkCanvas* canvas, const SkPaint& paint
, const std::string& textLine, const gfx::Point& destPosition) const | 47 void FontAtlas::drawOneLineOfTextInternal(SkCanvas* canvas, const SkPaint& paint
, const std::string& textLine, const gfx::Point& destPosition) const |
| 49 { | 48 { |
| 50 DCHECK(Proxy::isImplThread()); | 49 DCHECK(Proxy::isImplThread()); |
| 51 | 50 |
| 52 gfx::Point position = destPosition; | 51 gfx::Point position = destPosition; |
| 53 for (unsigned i = 0; i < textLine.length(); ++i) { | 52 for (unsigned i = 0; i < textLine.length(); ++i) { |
| 54 // If the ASCII code is out of bounds, then index 0 is used, which is ju
st a plain rectangle glyph. | 53 // If the ASCII code is out of bounds, then index 0 is used, which is ju
st a plain rectangle glyph. |
| 55 int asciiIndex = (textLine[i] < 128) ? textLine[i] : 0; | 54 int asciiIndex = (textLine[i] < 128) ? textLine[i] : 0; |
| 56 IntRect glyphBounds = m_asciiToRectTable[asciiIndex]; | 55 gfx::Rect glyphBounds = m_asciiToRectTable[asciiIndex]; |
| 57 SkIRect source = SkIRect::MakeXYWH(glyphBounds.x(), glyphBounds.y(), gly
phBounds.width(), glyphBounds.height()); | 56 SkIRect source = SkIRect::MakeXYWH(glyphBounds.x(), glyphBounds.y(), gly
phBounds.width(), glyphBounds.height()); |
| 58 canvas->drawBitmapRect(m_atlas, &source, SkRect::MakeXYWH(position.x(),
position.y(), glyphBounds.width(), glyphBounds.height()), &paint); | 57 canvas->drawBitmapRect(m_atlas, &source, SkRect::MakeXYWH(position.x(),
position.y(), glyphBounds.width(), glyphBounds.height()), &paint); |
| 59 position.set_x(position.x() + glyphBounds.width()); | 58 position.set_x(position.x() + glyphBounds.width()); |
| 60 } | 59 } |
| 61 } | 60 } |
| 62 | 61 |
| 63 void FontAtlas::drawDebugAtlas(SkCanvas* canvas, const gfx::Point& destPosition)
const | 62 void FontAtlas::drawDebugAtlas(SkCanvas* canvas, const gfx::Point& destPosition)
const |
| 64 { | 63 { |
| 65 DCHECK(Proxy::isImplThread()); | 64 DCHECK(Proxy::isImplThread()); |
| 66 | 65 |
| 67 SkIRect source = SkIRect::MakeWH(m_atlas.width(), m_atlas.height()); | 66 SkIRect source = SkIRect::MakeWH(m_atlas.width(), m_atlas.height()); |
| 68 canvas->drawBitmapRect(m_atlas, &source, SkRect::MakeXYWH(destPosition.x(),
destPosition.y(), m_atlas.width(), m_atlas.height())); | 67 canvas->drawBitmapRect(m_atlas, &source, SkRect::MakeXYWH(destPosition.x(),
destPosition.y(), m_atlas.width(), m_atlas.height())); |
| 69 } | 68 } |
| 70 | 69 |
| 71 } // namespace cc | 70 } // namespace cc |
| OLD | NEW |