From 98d653e97b85dbdf934dac5cab71f2f3dc94aa48 Mon Sep 17 00:00:00 2001 From: Bas Schouten Date: Tue, 15 May 2012 16:57:51 +0200 Subject: [PATCH] Bug 717393 - Part 1: Add helper for creating DWriteGlyphRuns. r=jrmuizel --- gfx/2d/DrawTargetD2D.cpp | 29 ++------------- gfx/2d/HelpersD2D.h | 75 +++++++++++++++++++++++++++++++++++++++ gfx/2d/ScaledFontDWrite.h | 3 -- 3 files changed, 78 insertions(+), 29 deletions(-) diff --git a/gfx/2d/DrawTargetD2D.cpp b/gfx/2d/DrawTargetD2D.cpp index 6564b36752a..a8ffb7744da 100644 --- a/gfx/2d/DrawTargetD2D.cpp +++ b/gfx/2d/DrawTargetD2D.cpp @@ -910,34 +910,11 @@ DrawTargetD2D::FillGlyphs(ScaledFont *aFont, RefPtr brush = CreateBrushForPattern(aPattern, aOptions.mAlpha); - DWRITE_GLYPH_RUN glyphRun; - - glyphRun.bidiLevel = 0; - glyphRun.fontEmSize = font->mSize; - glyphRun.isSideways = FALSE; - glyphRun.fontFace = font->mFontFace; - glyphRun.glyphCount = aBuffer.mNumGlyphs; - - std::vector indices; - std::vector advances; - std::vector offsets; - indices.resize(aBuffer.mNumGlyphs); - advances.resize(aBuffer.mNumGlyphs); - offsets.resize(aBuffer.mNumGlyphs); - - memset(&advances.front(), 0, sizeof(FLOAT) * aBuffer.mNumGlyphs); - for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) { - indices[i] = aBuffer.mGlyphs[i].mIndex; - offsets[i].advanceOffset = aBuffer.mGlyphs[i].mPosition.x; - offsets[i].ascenderOffset = -aBuffer.mGlyphs[i].mPosition.y; - } - - glyphRun.glyphAdvances = &advances.front(); - glyphRun.glyphIndices = &indices.front(); - glyphRun.glyphOffsets = &offsets.front(); + AutoDWriteGlyphRun autoRun; + DWriteGlyphRunFromGlyphs(aBuffer, font, &autoRun); if (brush) { - rt->DrawGlyphRun(D2D1::Point2F(), &glyphRun, brush); + rt->DrawGlyphRun(D2D1::Point2F(), &autoRun, brush); } FinalizeRTForOperation(aOptions.mCompositionOp, aPattern, Rect(0, 0, (Float)mSize.width, (Float)mSize.height)); diff --git a/gfx/2d/HelpersD2D.h b/gfx/2d/HelpersD2D.h index d631d96017a..7c0e2ea947d 100644 --- a/gfx/2d/HelpersD2D.h +++ b/gfx/2d/HelpersD2D.h @@ -39,8 +39,11 @@ #define MOZILLA_GFX_HELPERSD2D_H_ #include +#include #include "2D.h" +#include "ScaledFontDWrite.h" + namespace mozilla { namespace gfx { @@ -212,6 +215,78 @@ struct ShaderConstantRectD3D10 operator float* () { return &mX; } }; +static DWRITE_MATRIX +DWriteMatrixFromMatrix(Matrix &aMatrix) +{ + DWRITE_MATRIX mat; + mat.m11 = aMatrix._11; + mat.m12 = aMatrix._12; + mat.m21 = aMatrix._21; + mat.m22 = aMatrix._22; + mat.dx = aMatrix._31; + mat.dy = aMatrix._32; + return mat; +} + +class AutoDWriteGlyphRun : public DWRITE_GLYPH_RUN +{ + static const int kNumAutoGlyphs = 256; + +public: + AutoDWriteGlyphRun() { + glyphCount = 0; + } + + ~AutoDWriteGlyphRun() { + if (glyphCount > kNumAutoGlyphs) { + delete[] glyphIndices; + delete[] glyphAdvances; + delete[] glyphOffsets; + } + } + + void allocate(int aNumGlyphs) { + glyphCount = aNumGlyphs; + if (aNumGlyphs <= kNumAutoGlyphs) { + glyphIndices = &mAutoIndices[0]; + glyphAdvances = &mAutoAdvances[0]; + glyphOffsets = &mAutoOffsets[0]; + } else { + glyphIndices = new UINT16[aNumGlyphs]; + glyphAdvances = new FLOAT[aNumGlyphs]; + glyphOffsets = new DWRITE_GLYPH_OFFSET[aNumGlyphs]; + } + } + +private: + DWRITE_GLYPH_OFFSET mAutoOffsets[kNumAutoGlyphs]; + FLOAT mAutoAdvances[kNumAutoGlyphs]; + UINT16 mAutoIndices[kNumAutoGlyphs]; +}; + +static void +DWriteGlyphRunFromGlyphs(const GlyphBuffer &aGlyphs, ScaledFontDWrite *aFont, AutoDWriteGlyphRun *run) +{ + run->allocate(aGlyphs.mNumGlyphs); + + FLOAT *advances = const_cast(run->glyphAdvances); + UINT16 *indices = const_cast(run->glyphIndices); + DWRITE_GLYPH_OFFSET *offsets = const_cast(run->glyphOffsets); + + memset(advances, 0, sizeof(FLOAT) * aGlyphs.mNumGlyphs); + for (unsigned int i = 0; i < aGlyphs.mNumGlyphs; i++) { + indices[i] = aGlyphs.mGlyphs[i].mIndex; + offsets[i].advanceOffset = aGlyphs.mGlyphs[i].mPosition.x; + offsets[i].ascenderOffset = -aGlyphs.mGlyphs[i].mPosition.y; + } + + run->bidiLevel = 0; + run->fontFace = aFont->mFontFace; + run->fontEmSize = aFont->mSize; + run->glyphCount = aGlyphs.mNumGlyphs; + run->isSideways = FALSE; +} + } } diff --git a/gfx/2d/ScaledFontDWrite.h b/gfx/2d/ScaledFontDWrite.h index 05d76b4aeae..c22c58c2730 100644 --- a/gfx/2d/ScaledFontDWrite.h +++ b/gfx/2d/ScaledFontDWrite.h @@ -59,9 +59,6 @@ public: virtual TemporaryRef GetPathForGlyphs(const GlyphBuffer &aBuffer, const DrawTarget *aTarget); virtual void CopyGlyphsToBuilder(const GlyphBuffer &aBuffer, PathBuilder *aBuilder); -private: - friend class DrawTargetD2D; - void CopyGlyphsToSink(const GlyphBuffer &aBuffer, ID2D1GeometrySink *aSink); RefPtr mFontFace;