diff options
Diffstat (limited to 'Source/WebCore/platform/graphics/chromium/cc/CCFontAtlas.cpp')
-rw-r--r-- | Source/WebCore/platform/graphics/chromium/cc/CCFontAtlas.cpp | 115 |
1 files changed, 15 insertions, 100 deletions
diff --git a/Source/WebCore/platform/graphics/chromium/cc/CCFontAtlas.cpp b/Source/WebCore/platform/graphics/chromium/cc/CCFontAtlas.cpp index ce7d7c70d..388d43fb9 100644 --- a/Source/WebCore/platform/graphics/chromium/cc/CCFontAtlas.cpp +++ b/Source/WebCore/platform/graphics/chromium/cc/CCFontAtlas.cpp @@ -27,147 +27,62 @@ #if USE(ACCELERATED_COMPOSITING) #include "CCFontAtlas.h" -#include "Font.h" -#include "FontCache.h" -#include "FontDescription.h" -#include "GraphicsContext.h" -#include "ImageBuffer.h" -#include "TextRun.h" +#include "SkCanvas.h" #include "cc/CCProxy.h" -#define ATLAS_SIZE 128 -#define FONT_HEIGHT 14 - namespace WebCore { using namespace std; - -CCFontAtlas::CCFontAtlas() - : m_fontHeight(FONT_HEIGHT) +CCFontAtlas::CCFontAtlas(SkBitmap bitmap, IntRect asciiToRectTable[128], int fontHeight) + : m_atlas(bitmap) + , m_fontHeight(fontHeight) { + for (size_t i = 0; i < 128; ++i) + m_asciiToRectTable[i] = asciiToRectTable[i]; } -static void wrapPositionIfNeeded(IntPoint& position, int textWidth, int textHeight) -{ - if (position.x() + textWidth > ATLAS_SIZE) - position = IntPoint(0, position.y() + textHeight); -} - -void CCFontAtlas::generateAtlasForFont(GraphicsContext* atlasContext, const FontDescription& fontDescription, const Color& fontColor, const IntPoint& startingPosition, IntRect asciiToAtlasTable[128]) -{ - ASSERT(CCProxy::isMainThread()); - ASSERT(m_atlas); - - FontCachePurgePreventer fontCachePurgePreventer; - - IntPoint position = startingPosition; - int textHeight = fontDescription.computedPixelSize(); - // This is a dirty little trick to account for overhang letters like g, p, j. - int inflation = textHeight / 3; - - Font font(fontDescription, 0, 0); - font.update(0); - - atlasContext->setStrokeColor(fontColor, ColorSpaceDeviceRGB); - atlasContext->setFillColor(fontColor, ColorSpaceDeviceRGB); - - // First, draw a generic rect that will be used for special and unknown characters that have nothing else to render. - { - int textWidth = textHeight / 2; - wrapPositionIfNeeded(position, textWidth, textHeight + inflation); - atlasContext->strokeRect(FloatRect(FloatPoint(position.x() + 1, position.y() - textHeight + 1 + inflation), FloatSize(textWidth - 2, textHeight - 2 - inflation)), 1); - - // Initialize the rect that would be copied when drawing this glyph from the atlas. - asciiToAtlasTable[0] = IntRect(IntPoint(position.x(), position.y() - textHeight), IntSize(textWidth, textHeight + inflation)); - - // Increment to the position where the next glyph will be placed. - position.setX(position.x() + textWidth); - } - - // Then, draw the ASCII characters. - for (LChar i = 1; i < 128; ++i) { - if (i < 32) { - // Special characters will simply use the the default glyph. - asciiToAtlasTable[i] = asciiToAtlasTable[0]; - continue; - } - - String str; - str.append(i); - TextRun text(str); - - int textWidth = round(font.width(text)); - wrapPositionIfNeeded(position, textWidth, textHeight + inflation); - atlasContext->drawText(font, text, position); - - // Initialize the rect that would be copied when drawing this glyph from the atlas. - asciiToAtlasTable[i] = IntRect(IntPoint(position.x(), position.y() - textHeight), IntSize(textWidth, textHeight + inflation)); - - // Increment to the position where the next glyph will be placed. - position.setX(position.x() + textWidth); - } -} - -void CCFontAtlas::initialize() +CCFontAtlas::~CCFontAtlas() { - ASSERT(CCProxy::isMainThread()); - - // We expect this function to be called only once when the atlas did not exist yet. We should be aware if that's not true. - ASSERT(!m_atlas); - - m_atlas = ImageBuffer::create(IntSize(ATLAS_SIZE, ATLAS_SIZE)); - GraphicsContext* atlasContext = m_atlas->context(); - - // Clear the entire texture atlas to transparent before drawing fonts. - atlasContext->setFillColor(Color(0, 0, 0, 0), ColorSpaceDeviceRGB); - atlasContext->fillRect(FloatRect(0, 0, ATLAS_SIZE, ATLAS_SIZE)); - - // FIXME: monospace font does not work as expected. - FontDescription fontDescription; - fontDescription.setGenericFamily(FontDescription::MonospaceFamily); - fontDescription.setComputedSize(m_fontHeight); - generateAtlasForFont(atlasContext, fontDescription, Color(255, 0, 0), IntPoint(0, fontDescription.computedPixelSize()), m_asciiToRectTable); } -void CCFontAtlas::drawText(GraphicsContext* targetContext, const String& text, const IntPoint& destPosition, const IntSize& clip) const +void CCFontAtlas::drawText(SkCanvas* canvas, const SkPaint& paint, const String& text, const IntPoint& destPosition, const IntSize& clip) const { ASSERT(CCProxy::isImplThread()); - ASSERT(m_atlas); Vector<String> lines; text.split('\n', lines); IntPoint position = destPosition; for (size_t i = 0; i < lines.size(); ++i) { - drawOneLineOfTextInternal(targetContext, lines[i], position); + drawOneLineOfTextInternal(canvas, paint, lines[i], position); position.setY(position.y() + m_fontHeight); if (position.y() > clip.height()) return; } } -void CCFontAtlas::drawOneLineOfTextInternal(GraphicsContext* targetContext, const String& textLine, const IntPoint& destPosition) const +void CCFontAtlas::drawOneLineOfTextInternal(SkCanvas* canvas, const SkPaint& paint, const String& textLine, const IntPoint& destPosition) const { ASSERT(CCProxy::isImplThread()); - ASSERT(m_atlas); IntPoint position = destPosition; for (unsigned i = 0; i < textLine.length(); ++i) { // If the ASCII code is out of bounds, then index 0 is used, which is just a plain rectangle glyph. int asciiIndex = (textLine[i] < 128) ? textLine[i] : 0; IntRect glyphBounds = m_asciiToRectTable[asciiIndex]; - targetContext->drawImageBuffer(m_atlas.get(), ColorSpaceDeviceRGB, position, glyphBounds); + SkIRect source = SkIRect::MakeXYWH(glyphBounds.x(), glyphBounds.y(), glyphBounds.width(), glyphBounds.height()); + canvas->drawBitmapRect(m_atlas, &source, SkRect::MakeXYWH(position.x(), position.y(), glyphBounds.width(), glyphBounds.height()), &paint); position.setX(position.x() + glyphBounds.width()); } } -void CCFontAtlas::drawDebugAtlas(GraphicsContext* targetContext, const IntPoint& destPosition) const +void CCFontAtlas::drawDebugAtlas(SkCanvas* canvas, const IntPoint& destPosition) const { ASSERT(CCProxy::isImplThread()); - ASSERT(m_atlas); - targetContext->drawImageBuffer(m_atlas.get(), ColorSpaceDeviceRGB, destPosition, IntRect(IntPoint::zero(), IntSize(ATLAS_SIZE, ATLAS_SIZE))); + SkIRect source = SkIRect::MakeWH(m_atlas.width(), m_atlas.height()); + canvas->drawBitmapRect(m_atlas, &source, SkRect::MakeXYWH(destPosition.x(), destPosition.y(), m_atlas.width(), m_atlas.height())); } } // namespace WebCore |