void paintSkiaText(GraphicsContext* context, HFONT hfont, int numGlyphs, const WORD* glyphs, const int* advances, const GOFFSET* offsets, const SkPoint* origin) { HDC dc = GetDC(0); HGDIOBJ oldFont = SelectObject(dc, hfont); PlatformContextSkia* platformContext = context->platformContext(); SkCanvas* canvas = platformContext->canvas(); TextDrawingModeFlags textMode = platformContext->getTextDrawingMode(); // If platformContext is GPU-backed make its GL context current. platformContext->makeGrContextCurrent(); // Filling (if necessary). This is the common case. SkPaint paint; platformContext->setupPaintForFilling(&paint); paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); setupPaintForFont(hfont, &paint, platformContext); bool didFill = false; if ((textMode & TextModeFill) && (SkColorGetA(paint.getColor()) || paint.getLooper())) { skiaDrawText(canvas, *origin, &paint, &glyphs[0], &advances[0], &offsets[0], numGlyphs); didFill = true; } // Stroking on top (if necessary). if ((textMode & TextModeStroke) && platformContext->getStrokeStyle() != NoStroke && platformContext->getStrokeThickness() > 0) { paint.reset(); platformContext->setupPaintForStroking(&paint, 0, 0); paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); setupPaintForFont(hfont, &paint, platformContext); if (didFill) { // If there is a shadow and we filled above, there will already be // a shadow. We don't want to draw it again or it will be too dark // and it will go on top of the fill. // // Note that this isn't strictly correct, since the stroke could be // very thick and the shadow wouldn't account for this. The "right" // thing would be to draw to a new layer and then draw that layer // with a shadow. But this is a lot of extra work for something // that isn't normally an issue. paint.setLooper(0); } skiaDrawText(canvas, *origin, &paint, &glyphs[0], &advances[0], &offsets[0], numGlyphs); } SelectObject(dc, oldFont); ReleaseDC(0, dc); }
void paintSkiaText(GraphicsContext* context, HFONT hfont, int numGlyphs, const WORD* glyphs, const int* advances, const GOFFSET* offsets, const SkPoint* origin) { PlatformContextSkia* platformContext = context->platformContext(); SkCanvas* canvas = platformContext->canvas(); TextDrawingModeFlags textMode = platformContext->getTextDrawingMode(); // Ensure font load for printing, because PDF device needs it. if (canvas->getTopDevice()->getDeviceCapabilities() & SkDevice::kVector_Capability) PlatformSupport::ensureFontLoaded(hfont); // Filling (if necessary). This is the common case. SkPaint paint; platformContext->setupPaintForFilling(&paint); paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); setupPaintForFont(hfont, &paint, platformContext); bool didFill = false; if ((textMode & TextModeFill) && (SkColorGetA(paint.getColor()) || paint.getLooper())) { skiaDrawText(canvas, *origin, &paint, &glyphs[0], &advances[0], &offsets[0], numGlyphs); didFill = true; } // Stroking on top (if necessary). if ((textMode & TextModeStroke) && platformContext->getStrokeStyle() != NoStroke && platformContext->getStrokeThickness() > 0) { paint.reset(); platformContext->setupPaintForStroking(&paint, 0, 0); paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); setupPaintForFont(hfont, &paint, platformContext); if (didFill) { // If there is a shadow and we filled above, there will already be // a shadow. We don't want to draw it again or it will be too dark // and it will go on top of the fill. // // Note that this isn't strictly correct, since the stroke could be // very thick and the shadow wouldn't account for this. The "right" // thing would be to draw to a new layer and then draw that layer // with a shadow. But this is a lot of extra work for something // that isn't normally an issue. paint.setLooper(0); } skiaDrawText(canvas, *origin, &paint, &glyphs[0], &advances[0], &offsets[0], numGlyphs); } }
void Font::drawGlyphs(GraphicsContext* gc, const SimpleFontData* font, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point) const { SkASSERT(sizeof(GlyphBufferGlyph) == sizeof(uint16_t)); // compile-time assert const GlyphBufferGlyph* glyphs = glyphBuffer.glyphs(from); SkScalar x = SkFloatToScalar(point.x()); SkScalar y = SkFloatToScalar(point.y()); // FIXME: text rendering speed: // Android has code in their WebCore fork to special case when the // GlyphBuffer has no advances other than the defaults. In that case the // text drawing can proceed faster. However, it's unclear when those // patches may be upstreamed to WebKit so we always use the slower path // here. const GlyphBufferAdvance* adv = glyphBuffer.advances(from); SkAutoSTMalloc<32, SkPoint> storage(numGlyphs), storage2(numGlyphs), storage3(numGlyphs); SkPoint* pos = storage.get(); SkPoint* vPosBegin = storage2.get(); SkPoint* vPosEnd = storage3.get(); bool isVertical = font->platformData().orientation() == Vertical; for (int i = 0; i < numGlyphs; i++) { SkScalar myWidth = SkFloatToScalar(adv[i].width()); pos[i].set(x, y); if (isVertical) { vPosBegin[i].set(x + myWidth, y); vPosEnd[i].set(x + myWidth, y - myWidth); } x += myWidth; y += SkFloatToScalar(adv[i].height()); } PlatformContextSkia* platformContext = gc->platformContext(); TextDrawingModeFlags textMode = gc->platformContext()->getTextDrawingMode(); // We draw text up to two times (once for fill, once for stroke). if (textMode & TextModeFill) { SkPaint paint; gc->platformContext()->setupPaintForFilling(&paint); font->platformData().setupPaint(&paint); gc->platformContext()->adjustTextRenderMode(&paint); paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); if (isVertical) { SkPath path; for (int i = 0; i < numGlyphs; ++i) { path.reset(); path.moveTo(vPosBegin[i]); path.lineTo(vPosEnd[i]); platformContext->drawTextOnPath(glyphs + i, 2, path, 0, paint); } } else platformContext->drawPosText(glyphs, numGlyphs << 1, pos, paint); } if ((textMode & TextModeStroke) && gc->platformContext()->getStrokeStyle() != NoStroke && gc->platformContext()->getStrokeThickness() > 0) { SkPaint paint; gc->platformContext()->setupPaintForStroking(&paint, 0, 0); font->platformData().setupPaint(&paint); gc->platformContext()->adjustTextRenderMode(&paint); paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); if (textMode & TextModeFill) { // If we also filled, we don't want to draw shadows twice. // See comment in FontChromiumWin.cpp::paintSkiaText() for more details. // Since we use the looper for shadows, we remove it (if any) now. paint.setLooper(0); } if (isVertical) { SkPath path; for (int i = 0; i < numGlyphs; ++i) { path.reset(); path.moveTo(vPosBegin[i]); path.lineTo(vPosEnd[i]); platformContext->drawTextOnPath(glyphs + i, 2, path, 0, paint); } } else platformContext->drawPosText(glyphs, numGlyphs << 1, pos, paint); } }