Test upload from mkearney.
[chromium-blink-merge.git] / cc / CCFontAtlas.cpp
blob44695e1285f3166905004f56735da821405f4032
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.
5 #include "config.h"
7 #if USE(ACCELERATED_COMPOSITING)
8 #include "CCFontAtlas.h"
10 #include "CCProxy.h"
11 #include "SkCanvas.h"
13 namespace WebCore {
15 using namespace std;
17 CCFontAtlas::CCFontAtlas(SkBitmap bitmap, IntRect asciiToRectTable[128], int fontHeight)
18 : m_atlas(bitmap)
19 , m_fontHeight(fontHeight)
21 for (size_t i = 0; i < 128; ++i)
22 m_asciiToRectTable[i] = asciiToRectTable[i];
25 CCFontAtlas::~CCFontAtlas()
29 void CCFontAtlas::drawText(SkCanvas* canvas, const SkPaint& paint, const String& text, const IntPoint& destPosition, const IntSize& clip) const
31 ASSERT(CCProxy::isImplThread());
33 Vector<String> lines;
34 text.split('\n', lines);
36 IntPoint position = destPosition;
37 for (size_t i = 0; i < lines.size(); ++i) {
38 drawOneLineOfTextInternal(canvas, paint, lines[i], position);
39 position.setY(position.y() + m_fontHeight);
40 if (position.y() > clip.height())
41 return;
45 void CCFontAtlas::drawOneLineOfTextInternal(SkCanvas* canvas, const SkPaint& paint, const String& textLine, const IntPoint& destPosition) const
47 ASSERT(CCProxy::isImplThread());
49 IntPoint position = destPosition;
50 for (unsigned i = 0; i < textLine.length(); ++i) {
51 // If the ASCII code is out of bounds, then index 0 is used, which is just a plain rectangle glyph.
52 int asciiIndex = (textLine[i] < 128) ? textLine[i] : 0;
53 IntRect glyphBounds = m_asciiToRectTable[asciiIndex];
54 SkIRect source = SkIRect::MakeXYWH(glyphBounds.x(), glyphBounds.y(), glyphBounds.width(), glyphBounds.height());
55 canvas->drawBitmapRect(m_atlas, &source, SkRect::MakeXYWH(position.x(), position.y(), glyphBounds.width(), glyphBounds.height()), &paint);
56 position.setX(position.x() + glyphBounds.width());
60 void CCFontAtlas::drawDebugAtlas(SkCanvas* canvas, const IntPoint& destPosition) const
62 ASSERT(CCProxy::isImplThread());
64 SkIRect source = SkIRect::MakeWH(m_atlas.width(), m_atlas.height());
65 canvas->drawBitmapRect(m_atlas, &source, SkRect::MakeXYWH(destPosition.x(), destPosition.y(), m_atlas.width(), m_atlas.height()));
68 } // namespace WebCore
70 #endif // USE(ACCELERATED_COMPOSITING)