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.
7 #if USE(ACCELERATED_COMPOSITING)
8 #include "CCFontAtlas.h"
10 #include "base/string_split.h"
19 CCFontAtlas::CCFontAtlas(SkBitmap bitmap
, IntRect asciiToRectTable
[128], int fontHeight
)
21 , m_fontHeight(fontHeight
)
23 for (size_t i
= 0; i
< 128; ++i
)
24 m_asciiToRectTable
[i
] = asciiToRectTable
[i
];
27 CCFontAtlas::~CCFontAtlas()
31 void CCFontAtlas::drawText(SkCanvas
* canvas
, const SkPaint
& paint
, const std::string
& text
, const IntPoint
& destPosition
, const IntSize
& clip
) const
33 ASSERT(CCProxy::isImplThread());
35 std::vector
<std::string
> lines
;
36 base::SplitString(text
, '\n', &lines
);
38 IntPoint position
= destPosition
;
39 for (size_t i
= 0; i
< lines
.size(); ++i
) {
40 drawOneLineOfTextInternal(canvas
, paint
, lines
[i
], position
);
41 position
.setY(position
.y() + m_fontHeight
);
42 if (position
.y() > clip
.height())
47 void CCFontAtlas::drawOneLineOfTextInternal(SkCanvas
* canvas
, const SkPaint
& paint
, const std::string
& textLine
, const IntPoint
& destPosition
) const
49 ASSERT(CCProxy::isImplThread());
51 IntPoint position
= destPosition
;
52 for (unsigned i
= 0; i
< textLine
.length(); ++i
) {
53 // If the ASCII code is out of bounds, then index 0 is used, which is just a plain rectangle glyph.
54 int asciiIndex
= (textLine
[i
] < 128) ? textLine
[i
] : 0;
55 IntRect glyphBounds
= m_asciiToRectTable
[asciiIndex
];
56 SkIRect source
= SkIRect::MakeXYWH(glyphBounds
.x(), glyphBounds
.y(), glyphBounds
.width(), glyphBounds
.height());
57 canvas
->drawBitmapRect(m_atlas
, &source
, SkRect::MakeXYWH(position
.x(), position
.y(), glyphBounds
.width(), glyphBounds
.height()), &paint
);
58 position
.setX(position
.x() + glyphBounds
.width());
62 void CCFontAtlas::drawDebugAtlas(SkCanvas
* canvas
, const IntPoint
& destPosition
) const
64 ASSERT(CCProxy::isImplThread());
66 SkIRect source
= SkIRect::MakeWH(m_atlas
.width(), m_atlas
.height());
67 canvas
->drawBitmapRect(m_atlas
, &source
, SkRect::MakeXYWH(destPosition
.x(), destPosition
.y(), m_atlas
.width(), m_atlas
.height()));
70 } // namespace WebCore
72 #endif // USE(ACCELERATED_COMPOSITING)