[Libreoffice-commits] core.git: Branch 'libreoffice-5-1' - config_host/config_graphite.h.in vcl/win

Tim Eves tim_eves at sil.org
Mon Mar 14 17:53:05 UTC 2016


 config_host/config_graphite.h.in |    2 
 vcl/win/source/gdi/salgdi3.cxx   |    4 
 vcl/win/source/gdi/winlayout.cxx |  837 ++++++++++++++++++++++++++++++---------
 3 files changed, 649 insertions(+), 194 deletions(-)

New commits:
commit 620a1351a7627246f139a54a8cc3b35fa7ef8434
Author: Tim Eves <tim_eves at sil.org>
Date:   Tue Mar 8 14:46:49 2016 +0700

    tdf#97171: Use DirectWrite for OpenGL glyph caching
    
    A squash of several commits. Most important points mentioned below:
    
    DirectWrite rendering for Graphite to work around ExtTextOut bug where
    it incorrectly positions certain diacritics, using DirectWrite and
    Direct2D fixes this. Implemented on-demand loading of the DLL so the
    old ExTextOut based renderer will be used when drwite and d2d1 cannot
    be found allowing this work on Windows XP (where this bug doesn't seem
    to occur)
    
    Make the new D2D Graphite rendering work also in the OpenGL case It
    used to render just black boxes.
    
    The change in DrawTextImpl() semantics from
    61085083e4a5060ba7e2135818264d63c6da13c2 was not properly implemented
    in the new Graphite code. The return value is not some kind of
    "success" indicator, but tells the caller whether to continue the loop
    at that level.
    
    We do need to call FillRect() to fill the requested rectangle of the
    HDC with white. On the other hand, the call to Clear() is not needed
    and in fact makes no text show up.
    
    (I now see that that same code snippet that calls FillRect() is used
    in all the DrawTextImpl() implementations, so it should be factored
    out to the call site in WinLayout::DrawText().)
    
    Factor out the calls to FillRect in the DrawTextImpl()
    implementations.  They were all in fact doing exactly the same, so do
    it at the call site instead.
    
    Fix what seems to have been off-by-one errors in glyph bounds
    calculation. For some reason the error had a visible impact (as far as
    I an see, anyway) only for Graphite fonts. The bottommost pixels were
    cut off. (Also leftmost, but that was not so easily visible.)
    
    <tools/gen.hxx> Rectangle type, I love you.
    
    Refactor some previously private methods into public ones and
    reimplement parts of D2DWriteTextOutRenderer to user them. Also apply
    them to rendering the OpenGL glyph atlas, in an effort to workaround
    some bugs in legacy Windows text rendering APIs.
    
    I assume we want to initialise the rectangle with zero
    left/right/top/bottom and not using the default constructor, which
    sets the right and bottom coordinates to the magic value -32767. That
    made the 'bound' rectangle end up with rather amusing boundaries, like
    left=-32766, top=-16, right=-32576, bottom=6.
    
    Try calculating a chunks ascent & height from the inkboxes rather than
    using the font metrics which might not alway be correct when glyphs
    inkboxes are tall than the ascent or lower than the descent.
    
    Mark the mnAscent in the chunk bitmap debug output.
    
    Fix several miscalulations in positioning glyph to be rendered into
    that atlas.
    
    Fix vertical alignment problems. Inkboxes are returned with all
    co-ordinates relative to the glyphs not the fonts ascent.  Therefor
    bounds.Top() is not the vertical overhang but the -ve height of the
    inkbox above the baseline.  This fixes the calulation of the per Chunk
    ascent.
    
    Fix horizontal occsional alingment issues in OpenGL cached glyphs. The
    left edge of the src location rectangle for the first glyph in a cache
    chunk would set to extraspace and not zero, but all other rectangles
    in the chunk would be set from the aEnds array. This produced a bug
    where only certain letters would be mispositioned, proportional to the
    fonts point size.
    
    Rename OpenGLGlyphChunk::mnAscent to mnBaselineOffset to reflect curr
    use.  Changed at Tor's stuggestion to better describe to it's use as
    it's value would be per chunk and based on the maximum ink box bounds
    of the glyphs in the chunk, rather than having anything to do with the
    font's real ascent value.
    
    Change-Id: I92ca17609022ae4af187e9a9f452a694fdd976ad
    Reviewed-on: https://gerrit.libreoffice.org/23246
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Michael Meeks <michael.meeks at collabora.com>

diff --git a/config_host/config_graphite.h.in b/config_host/config_graphite.h.in
index 46e624d..953c006 100644
--- a/config_host/config_graphite.h.in
+++ b/config_host/config_graphite.h.in
@@ -3,4 +3,6 @@
 
 #define ENABLE_GRAPHITE 0
 
+#define ENABLE_GRAPHITE_DWRITE 1
+
 #endif
diff --git a/vcl/win/source/gdi/salgdi3.cxx b/vcl/win/source/gdi/salgdi3.cxx
index dfeb0e0..499c0e5 100644
--- a/vcl/win/source/gdi/salgdi3.cxx
+++ b/vcl/win/source/gdi/salgdi3.cxx
@@ -2160,9 +2160,9 @@ bool WinSalGraphics::GetGlyphBoundRect( sal_GlyphId aGlyphId, Rectangle& rRect )
     rRect = Rectangle( Point( +aGM.gmptGlyphOrigin.x, -aGM.gmptGlyphOrigin.y ),
         Size( aGM.gmBlackBoxX, aGM.gmBlackBoxY ) );
     rRect.Left()    = static_cast<int>( mfCurrentFontScale * rRect.Left() );
-    rRect.Right()   = static_cast<int>( mfCurrentFontScale * rRect.Right() );
+    rRect.Right()   = static_cast<int>( mfCurrentFontScale * rRect.Right() ) + 1;
     rRect.Top()     = static_cast<int>( mfCurrentFontScale * rRect.Top() );
-    rRect.Bottom()  = static_cast<int>( mfCurrentFontScale * rRect.Bottom() );
+    rRect.Bottom()  = static_cast<int>( mfCurrentFontScale * rRect.Bottom() ) + 1;
     return true;
 }
 
diff --git a/vcl/win/source/gdi/winlayout.cxx b/vcl/win/source/gdi/winlayout.cxx
index a37b198..58d0855 100644
--- a/vcl/win/source/gdi/winlayout.cxx
+++ b/vcl/win/source/gdi/winlayout.cxx
@@ -33,12 +33,12 @@
 
 #include "sft.hxx"
 #include "sallayout.hxx"
-#include "glyphy/demo.hxx"
 
 #include <cstdio>
 #include <cstdlib>
 
 #include <sal/alloca.h>
+#include <rtl/character.hxx>
 
 #include <algorithm>
 
@@ -54,8 +54,16 @@ typedef std::unordered_map<int,int> IntMap;
 #if ENABLE_GRAPHITE
 #include <i18nlangtag/languagetag.hxx>
 #include <graphite_features.hxx>
+#if ENABLE_GRAPHITE_DWRITE
+#include <d2d1.h>
+#include <dwrite.h>
+#endif
 #endif
 
+// This needs to come after any includes for d2d1.h, otherwise we get lots of errors
+#include "glyphy/demo.hxx"
+
+
 #define DROPPED_OUTGLYPH 0xFFFF
 
 namespace
@@ -71,8 +79,9 @@ struct OpenGLGlyphCacheChunk
     WORD mnFirstGlyph;
     int mnGlyphCount;
     std::vector<Rectangle> maLocation;
+    std::vector<int> maLeftOverhangs;
     std::shared_ptr<OpenGLTexture> mpTexture;
-    int mnAscent;
+    int mnBaselineOffset;
     int mnHeight;
     bool mbVertical;
     bool mbRealGlyphIndices;
@@ -140,6 +149,104 @@ private:
 
 GLuint ImplWinFontEntry::mnGLyphyProgram = 0;
 
+class TextOutRenderer
+{
+protected:
+    explicit TextOutRenderer() = default;
+    TextOutRenderer(const TextOutRenderer &) = delete;
+    TextOutRenderer & operator = (const TextOutRenderer &) = delete;
+
+public:
+    static TextOutRenderer & get();
+
+    virtual ~TextOutRenderer() = default;
+
+    virtual bool operator ()(WinLayout const &rLayout, HDC hDC,
+        const Rectangle* pRectToErase,
+        Point* pPos, int* pGetNextGlypInfo) = 0;
+};
+
+class ExTextOutRenderer : public TextOutRenderer
+{
+    ExTextOutRenderer(const ExTextOutRenderer &) = delete;
+    ExTextOutRenderer & operator = (const ExTextOutRenderer &) = delete;
+
+public:
+    explicit ExTextOutRenderer() = default;
+    virtual ~ExTextOutRenderer() override = default;
+
+    bool operator ()(WinLayout const &rLayout, HDC hDC,
+        const Rectangle* pRectToErase,
+        Point* pPos, int* pGetNextGlypInfo) override;
+};
+
+#if ENABLE_GRAPHITE_DWRITE
+
+class D2DWriteTextOutRenderer : public TextOutRenderer
+{
+    typedef HRESULT(WINAPI *pD2D1CreateFactory_t)(D2D1_FACTORY_TYPE,
+        REFIID, const D2D1_FACTORY_OPTIONS *, void **);
+    typedef HRESULT(WINAPI *pDWriteCreateFactory_t)(DWRITE_FACTORY_TYPE,
+        REFIID, IUnknown **);
+
+    static HINSTANCE mmD2d1, mmDWrite;
+    static pD2D1CreateFactory_t     D2D1CreateFactory;
+    static pDWriteCreateFactory_t   DWriteCreateFactory;
+
+public:
+    static bool InitModules();
+
+    explicit D2DWriteTextOutRenderer();
+    virtual ~D2DWriteTextOutRenderer() override;
+
+    bool operator ()(WinLayout const &rLayout, HDC hDC,
+        const Rectangle* pRectToErase,
+        Point* pPos, int* pGetNextGlypInfo) override;
+
+    inline bool BindDC(HDC hDC, Rectangle const & rRect = Rectangle(0, 0, 0, 0)) {
+        RECT const rc = { rRect.Left(), rRect.Top(), rRect.Right(), rRect.Bottom() };
+        return SUCCEEDED(mpRT->BindDC(hDC, &rc));
+    }
+
+    bool BindFont(HDC hDC) /*override*/;
+    bool ReleaseFont() /*override*/;
+
+    std::vector<Rectangle>  GetGlyphInkBoxes(uint16_t * pGid, uint16_t * pGidEnd) const /*override*/;
+    ID2D1RenderTarget * GetRenderTarget() const { return mpRT; }
+    IDWriteFontFace   * GetFontFace() const { return mpFontFace; }
+    float               GetEmHeight() const { return mlfEmHeight; }
+
+    inline HRESULT CreateRenderTarget() {
+        if (mpRT) mpRT->Release(); mpRT = nullptr;
+        return mpD2DFactory->CreateDCRenderTarget(&mRTProps, &mpRT);
+    }
+
+    inline bool Ready() const { return mpGdiInterop && mpRT; }
+
+private:
+    static void CleanupModules();
+
+    // This is a singleton object disable copy ctor and assignemnt operator
+    D2DWriteTextOutRenderer(const D2DWriteTextOutRenderer &) = delete;
+    D2DWriteTextOutRenderer & operator = (const D2DWriteTextOutRenderer &) = delete;
+
+    bool GetDWriteFaceFromHDC(HDC hDC, IDWriteFontFace ** ppFontFace, float * lfSize) const;
+    bool GetDWriteInkBox(IDWriteFontFace & rFontFace, WinLayout const &rLayout, float const lfEmHeight, Rectangle &) const;
+    bool DrawGlyphs(const Point & origin, uint16_t * pGid, uint16_t * pGidEnd,
+        float * pAdvances, Point * pOffsets) /*override*/;
+
+    ID2D1Factory        * mpD2DFactory;
+    IDWriteFactory      * mpDWriteFactory;
+    IDWriteGdiInterop   * mpGdiInterop;
+    ID2D1DCRenderTarget * mpRT;
+    const D2D1_RENDER_TARGET_PROPERTIES mRTProps;
+
+    IDWriteFontFace * mpFontFace;
+    float             mlfEmHeight;
+    HDC               mhDC;
+};
+
+
 #ifdef SAL_LOG_INFO
 
 namespace {
@@ -154,7 +261,7 @@ char ColorFor(COLORREF aColor)
     return '0' + (10*(GetRValue(aColor) + GetGValue(aColor) + GetBValue(aColor))) / (0xFF*3);
 }
 
-void DumpGlyphBitmap(HDC hDC)
+void DumpGlyphBitmap(HDC hDC, const OpenGLGlyphCacheChunk& rChunk)
 {
     HBITMAP hBitmap = static_cast<HBITMAP>(GetCurrentObject(hDC, OBJ_BITMAP));
     if (hBitmap == NULL)
@@ -172,10 +279,14 @@ void DumpGlyphBitmap(HDC hDC)
 
     SAL_INFO("vcl.gdi.opengl", "Bitmap " << hBitmap << ": " << aBitmap.bmWidth << "x" << aBitmap.bmHeight << ":");
 
-    std::ostringstream sLine("\n");
+    std::ostringstream sLine("\n", std::ios_base::ate);
     for (long y = 0; y < aBitmap.bmHeight; y++)
     {
-        for (long x = 0; x < std::min(75l, aBitmap.bmWidth); x++)
+        if (y == rChunk.mnBaselineOffset + rChunk.getExtraOffset())
+            sLine << "-";
+        else
+            sLine << ColorFor(GetPixel(hDC, 0, y));
+        for (long x = 1; x < std::min(75l, aBitmap.bmWidth); x++)
             sLine << ColorFor(GetPixel(hDC, x, y));
         if (y < aBitmap.bmHeight - 1)
             sLine << "\n";
@@ -290,196 +401,155 @@ bool ImplWinFontEntry::AddChunkOfGlyphs(bool bRealGlyphIndices, int nGlyphIndex,
         return false;
     }
 
-    SIZE aSize;
+    // For now we assume DWrite is present and we won't bother with fallback paths.
+    D2DWriteTextOutRenderer * pTxt = dynamic_cast<D2DWriteTextOutRenderer *>(&TextOutRenderer::get());
+    if (!pTxt)
+        return false;
+
+    pTxt->BindFont(hDC);
 
-    std::vector<ABC> aABC(nCount);
-    if (bRealGlyphIndices)
+    // Bail for vertical text.
     {
-        if (!GetTextExtentExPointI(hDC, aGlyphIndices.data(), nCount, 0, NULL, NULL, &aSize))
-        {
-            SAL_WARN("vcl.gdi", "GetTextExtentExPointI failed: " << WindowsErrorString(GetLastError()));
-            SelectObject(hDC, hOrigFont);
-            DeleteDC(hDC);
-            return false;
-        }
-        if (!GetCharABCWidthsI(hDC, 0, nCount, aGlyphIndices.data(), aABC.data()))
+        wchar_t sFaceName[200];
+        int nFaceNameLen = GetTextFaceW(hDC, SAL_N_ELEMENTS(sFaceName), sFaceName);
+        SelectObject(hDC, hOrigFont);
+        DeleteDC(hDC);
+
+        if (!nFaceNameLen)
+            SAL_WARN("vcl.gdi", "GetTextFace failed: " << WindowsErrorString(GetLastError()));
+
+        if (sFaceName[0] == '@')
         {
-            SAL_WARN("vcl.gdi", "GetCharABCWidthsI failed: " << WindowsErrorString(GetLastError()));
-            SelectObject(hDC, hOrigFont);
-            DeleteDC(hDC);
+            pTxt->ReleaseFont();
             return false;
         }
     }
-    else
+    // Fetch the ink boxes and calculate the size of the atlas.
+    if (!bRealGlyphIndices)
     {
-        if (!GetTextExtentExPointW(hDC, aGlyphIndices.data(), nCount, 0, NULL, NULL, &aSize))
+        // FIXME First convert from UTF16 to utf32
+        std::vector<uint32_t> aCodePoints(aGlyphIndices.begin(), aGlyphIndices.end());
+        aGlyphIndices.resize(aCodePoints.size());
+            if (!SUCCEEDED(pTxt->GetFontFace()->GetGlyphIndices(aCodePoints.data(), aCodePoints.size(), aGlyphIndices.data())))
         {
-            SAL_WARN("vcl.gdi", "GetTextExtentExPoint failed: " << WindowsErrorString(GetLastError()));
-            SelectObject(hDC, hOrigFont);
-            DeleteDC(hDC);
-            return false;
-        }
-        if (!GetCharABCWidthsW(hDC, nGlyphIndex, nGlyphIndex+nCount-1, aABC.data()))
-        {
-            SAL_WARN("vcl.gdi", "GetCharABCWidths failed: " << WindowsErrorString(GetLastError()));
-            SelectObject(hDC, hOrigFont);
-            DeleteDC(hDC);
+            pTxt->ReleaseFont();
             return false;
         }
     }
-
-    std::ostringstream sLine;
-    for (int i = 0; i < nCount; i++)
-        sLine << aABC[i].abcA << ":" << aABC[i].abcB << ":" << aABC[i].abcC << " ";
-    SAL_INFO("vcl.gdi.opengl", "ABC widths: " << sLine.str());
-
-    TEXTMETRICW aTextMetric;
-    if (!GetTextMetricsW(hDC, &aTextMetric))
-    {
-        SAL_WARN("vcl.gdi", "GetTextMetrics failed: " << WindowsErrorString(GetLastError()));
-        SelectObject(hDC, hOrigFont);
-        DeleteDC(hDC);
-        return false;
-    }
-    aChunk.mnAscent = aTextMetric.tmAscent;
-    aChunk.mnHeight = aTextMetric.tmHeight;
+    Rectangle bounds(0, 0, 0, 0);
+    auto aInkBoxes = pTxt->GetGlyphInkBoxes(aGlyphIndices.data(), aGlyphIndices.data() + nCount);
+    for (auto &box : aInkBoxes)
+        bounds.Union(box + Point(bounds.Right(), 0));
+
+    // bounds.Top() is the offset from the baseline at (0,0) to the top of the
+    // inkbox.
+    aChunk.mnBaselineOffset = -bounds.Top();
+    aChunk.mnHeight = bounds.getHeight();
+    aChunk.mbVertical = false;
+
+    aChunk.maLeftOverhangs.resize(nCount);
+    aChunk.maLocation.resize(nCount);
 
     // Try hard to avoid overlap as we want to be able to use
     // individual rectangles for each glyph. The ABC widths don't
     // take anti-aliasing into consideration. Let's hope that leaving
-    // "extra" space inbetween glyphs will help.
-    std::vector<int> aDX(nCount);
-    int totWidth = 0;
-    for (int i = 0; i < nCount; i++)
+    // "extra" space between glyphs will help.
+    std::vector<float> aGlyphAdv(nCount);   // offsets between glyphs
+    std::vector<DWRITE_GLYPH_OFFSET> aGlyphOffset(nCount, DWRITE_GLYPH_OFFSET{0.0f,0.0f});
+    std::vector<int> aEnds(nCount); // end of each glyph box
+    float totWidth = 0;
+    for (int i = 0; i < nCount; ++i)
     {
-        aDX[i] = aABC[i].abcB + std::abs(aABC[i].abcC);
-        if (i == 0)
-            aDX[0] += std::abs(aABC[0].abcA);
-        if (i < nCount-1)
-            aDX[i] += std::abs(aABC[i+1].abcA);
-        aDX[i] += aChunk.getExtraSpace();
-        totWidth += aDX[i];
-    }
+        int overhang = aInkBoxes[i].Left();
+        int blackWidth = aInkBoxes[i].getWidth(); // width of non-AA pixels
+        aChunk.maLeftOverhangs[i] = overhang;
 
-    LOGFONTW aLogfont;
-    if (!GetObjectW(rLayout.mhFont, sizeof(aLogfont), &aLogfont))
-    {
-        SAL_WARN("vcl.gdi", "GetObject failed: " << WindowsErrorString(GetLastError()));
-        SelectObject(hDC, hOrigFont);
-        DeleteDC(hDC);
-        return false;
-    }
+        aGlyphAdv[i] = blackWidth + aChunk.getExtraSpace();
+        aGlyphOffset[i].advanceOffset = -overhang;
 
-    wchar_t sFaceName[200];
-    int nFaceNameLen = GetTextFaceW(hDC, SAL_N_ELEMENTS(sFaceName), sFaceName);
-    if (!nFaceNameLen)
-    {
-        SAL_WARN("vcl.gdi", "GetTextFace failed: " << WindowsErrorString(GetLastError()));
-        SelectObject(hDC, hOrigFont);
-        DeleteDC(hDC);
-        return false;
+        totWidth += aGlyphAdv[i];
+        aEnds[i] = totWidth;
     }
 
-    SAL_INFO("vcl.gdi.opengl", OUString(sFaceName, nFaceNameLen) <<
-             ": Escapement=" << aLogfont.lfEscapement <<
-             " Orientation=" << aLogfont.lfOrientation <<
-             " Ascent=" << aTextMetric.tmAscent <<
-             " InternalLeading=" << aTextMetric.tmInternalLeading <<
-             " Size=(" << aSize.cx << "," << aSize.cy << ") totWidth=" << totWidth);
-
-    if (SelectObject(hDC, hOrigFont) == NULL)
-        SAL_WARN("vcl.gdi", "SelectObject failed: " << WindowsErrorString(GetLastError()));
-    if (!DeleteDC(hDC))
-        SAL_WARN("vcl.gdi", "DeleteDC failed: " << WindowsErrorString(GetLastError()));
-
     // Leave extra space also at top and bottom
-    int nBitmapWidth, nBitmapHeight;
-    if (sFaceName[0] == '@')
-    {
-        nBitmapWidth = aSize.cy + aChunk.getExtraSpace();
-        nBitmapHeight = totWidth;
-        aChunk.mbVertical = true;
-    }
-    else
+    int nBitmapWidth = totWidth,
+        nBitmapHeight = bounds.getHeight() + aChunk.getExtraSpace();
+
+    aChunk.maLocation.resize(nCount);
+    UINT nPos = 0;
+    for (int i = 0; i < nCount; i++)
     {
-        nBitmapWidth = totWidth;
-        nBitmapHeight = aSize.cy + aChunk.getExtraSpace();
-        aChunk.mbVertical = false;
+        // FIXME: really I don't get why 'vertical' makes any difference [!] what does it mean !?
+        if (aChunk.mbVertical)
+        {
+            aChunk.maLocation[i].Left() = 0;
+            aChunk.maLocation[i].Right() = nBitmapWidth;
+            aChunk.maLocation[i].Top() = nPos;
+            aChunk.maLocation[i].Bottom() = nPos + aGlyphAdv[i] + aChunk.maLeftOverhangs[i];
+        }
+        else
+        {
+            aChunk.maLocation[i].Left() = nPos;
+            aChunk.maLocation[i].Right() = aEnds[i];
+            aChunk.maLocation[i].Top() = 0;
+            aChunk.maLocation[i].Bottom() = bounds.getHeight() + aChunk.getExtraSpace();
+        }
+        nPos = aEnds[i];
     }
 
-    // Don't even try to handle non-horizontal text
-    if (aChunk.mbVertical || aLogfont.lfEscapement != 0)
-        return false;
-
     OpenGLCompatibleDC aDC(rGraphics, 0, 0, nBitmapWidth, nBitmapHeight);
 
     HFONT hNonAntialiasedFont = NULL;
 
-#ifdef DBG_UTIL
-    static bool bNoAntialias = (std::getenv("VCL_GLYPH_CACHING_HACK_NO_ANTIALIAS") != NULL);
-    if (bNoAntialias)
-    {
-        aLogfont.lfQuality = NONANTIALIASED_QUALITY;
-        hNonAntialiasedFont = CreateFontIndirectW(&aLogfont);
-        if (hNonAntialiasedFont == NULL)
-        {
-            SAL_WARN("vcl.gdi", "CreateFontIndirect failed: " << WindowsErrorString(GetLastError()));
-            return false;
-        }
-    }
-#endif
+    SetTextColor(aDC.getCompatibleHDC(), RGB(0, 0, 0));
+    SetBkColor(aDC.getCompatibleHDC(), RGB(255, 255, 255));
 
-    hOrigFont = SelectFont(aDC.getCompatibleHDC(), hNonAntialiasedFont != NULL ? hNonAntialiasedFont : rLayout.mhFont);
-    if (hOrigFont == NULL)
+    aDC.fill(MAKE_SALCOLOR(0xff, 0xff, 0xff));
+
+    pTxt->BindDC(aDC.getCompatibleHDC(), Rectangle(0, 0, nBitmapWidth, nBitmapHeight));
+    auto pRT = pTxt->GetRenderTarget();
+
+    ID2D1SolidColorBrush* pBrush = nullptr;
+    if (!SUCCEEDED(pRT->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &pBrush)))
     {
-        SAL_WARN("vcl.gdi", "SelectObject failed: " << WindowsErrorString(GetLastError()));
+        pTxt->ReleaseFont();
         return false;
     }
 
-    SetTextColor(aDC.getCompatibleHDC(), RGB(0, 0, 0));
-    SetBkColor(aDC.getCompatibleHDC(), RGB(255, 255, 255));
+    D2D1_POINT_2F baseline = { aChunk.getExtraOffset(), aChunk.getExtraOffset() + aChunk.mnBaselineOffset };
+    DWRITE_GLYPH_RUN glyphs = {
+        pTxt->GetFontFace(),
+        pTxt->GetEmHeight(),
+        nCount,
+        aGlyphIndices.data(),
+        aGlyphAdv.data(),
+        aGlyphOffset.data(),
+        false,
+        0
+    };
 
-    aDC.fill(MAKE_SALCOLOR(0xff, 0xff, 0xff));
+    pRT->BeginDraw();
+    pRT->DrawGlyphRun(baseline, &glyphs, pBrush);
+    HRESULT hr = pRT->EndDraw();
+
+    pBrush->Release();
 
-    int nY =  aChunk.getExtraOffset();
-    int nX =  nY;
-    if (aChunk.mbVertical)
-        nX += aDX[0];
-    if (!ExtTextOutW(aDC.getCompatibleHDC(),
-                     nX, nY,
-                     bRealGlyphIndices ? ETO_GLYPH_INDEX : 0,
-                     NULL,
-                     aGlyphIndices.data(), nCount,
-                     aDX.data()))
-    {
-        SAL_WARN("vcl.gdi", "ExtTextOutW failed: " << WindowsErrorString(GetLastError()));
+    switch (hr)
+    {
+    case S_OK:
+        break;
+    case D2DERR_RECREATE_TARGET:
+        pTxt->CreateRenderTarget();
+        break;
+    default:
+        SAL_WARN("vcl.gdi", "DrawGlyphRun-EndDraw failed: " << WindowsErrorString(GetLastError()));
         SelectFont(aDC.getCompatibleHDC(), hOrigFont);
         if (hNonAntialiasedFont != NULL)
             DeleteObject(hNonAntialiasedFont);
         return false;
     }
 
-    aChunk.maLocation.resize(nCount);
-    UINT nPos = 0;
-    for (int i = 0; i < nCount; i++)
-    {
-        if (aChunk.mbVertical)
-        {
-            aChunk.maLocation[i].Left() = 0;
-            aChunk.maLocation[i].Right() = nBitmapWidth;
-            aChunk.maLocation[i].Top() = nPos;
-            aChunk.maLocation[i].Bottom() = nPos + aDX[i];
-            nPos = aChunk.maLocation[i].Bottom();
-        }
-        else
-        {
-            aChunk.maLocation[i].Left() = nPos;
-            aChunk.maLocation[i].Right() = nPos + aDX[i];
-            nPos = aChunk.maLocation[i].Right();
-            aChunk.maLocation[i].Top() = 0;
-            aChunk.maLocation[i].Bottom() = aSize.cy + aChunk.getExtraSpace();
-        }
-    }
+    pTxt->ReleaseFont();
 
     aChunk.mpTexture = std::unique_ptr<OpenGLTexture>(aDC.getTexture());
 
@@ -491,7 +561,14 @@ bool ImplWinFontEntry::AddChunkOfGlyphs(bool bRealGlyphIndices, int nGlyphIndex,
 
 #ifdef SAL_LOG_INFO
     SAL_INFO("vcl.gdi.opengl", "this=" << this << " now: " << maOpenGLGlyphCache);
-    DumpGlyphBitmap(aDC.getCompatibleHDC());
+    DumpGlyphBitmap(aDC.getCompatibleHDC(), aChunk);
+
+    {
+        std::ostringstream sLine;
+        for (int i = 0; i < nCount; i++)
+            sLine << aGlyphAdv[i] << ":" << aChunk.maLeftOverhangs[i] << " ";
+        SAL_INFO("vcl.gdi.opengl", "DX:offset : " << sLine.str());
+    }
 #endif
 
     return true;
@@ -840,16 +917,10 @@ int SimpleWinLayout::GetNextGlyphs( int nLen, sal_GlyphId* pGlyphIds, Point& rPo
 }
 
 bool SimpleWinLayout::DrawTextImpl(HDC hDC,
-                                   const Rectangle* pRectToErase,
+                                   const Rectangle* /* pRectToErase */,
                                    Point* /* pPos */,
                                    int* /* pGetNextGlypInfo */) const
 {
-    if (pRectToErase)
-    {
-        RECT aRect = { pRectToErase->Left(), pRectToErase->Top(), pRectToErase->Left()+pRectToErase->GetWidth(), pRectToErase->Top()+pRectToErase->GetHeight() };
-        FillRect(hDC, &aRect, static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)));
-    }
-
     if( mnGlyphCount <= 0 )
         return false;
 
@@ -1378,6 +1449,9 @@ void WinLayout::DrawText(SalGraphics& rGraphics) const
                 // we are making changes to the DC, make sure we got a new one
                 assert(aDC.getCompatibleHDC() != hDC);
 
+                RECT aWinRect = { aRect.Left(), aRect.Top(), aRect.Left() + aRect.GetWidth(), aRect.Top() + aRect.GetHeight() };
+                FillRect(aDC.getCompatibleHDC(), &aWinRect, static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)));
+
                 // setup the hidden DC with black color and white background, we will
                 // use the result of the text drawing later as a mask only
                 HFONT hOrigFont = SelectFont(aDC.getCompatibleHDC(), mhFont);
@@ -1462,7 +1536,8 @@ bool SimpleWinLayout::DrawCachedGlyphs(SalGraphics& rGraphics) const
 
         SalTwoRect a2Rects(rChunk.maLocation[n].Left(), rChunk.maLocation[n].Top(),
                            rChunk.maLocation[n].getWidth(), rChunk.maLocation[n].getHeight(),
-                           nAdvance + aPos.X() - rChunk.getExtraOffset(), aPos.Y() - rChunk.mnAscent - rChunk.getExtraOffset(),
+                           nAdvance + aPos.X() - rChunk.getExtraOffset() + rChunk.maLeftOverhangs[n],
+                           aPos.Y() - rChunk.mnBaselineOffset - rChunk.getExtraOffset(),
                            rChunk.maLocation[n].getWidth(), rChunk.maLocation[n].getHeight()); // ???
         pImpl->DrawMask(*rChunk.mpTexture, salColor, a2Rects);
 
@@ -2598,16 +2673,10 @@ void UniscribeLayout::Simplify( bool /*bIsBase*/ )
 }
 
 bool UniscribeLayout::DrawTextImpl(HDC hDC,
-                                   const Rectangle* pRectToErase,
+                                   const Rectangle* /* pRectToErase */,
                                    Point* /* pPos */,
                                    int* /* pGetNextGlypInfo */) const
 {
-    if (pRectToErase)
-    {
-        RECT aRect = { pRectToErase->Left(), pRectToErase->Top(), pRectToErase->Left()+pRectToErase->GetWidth(), pRectToErase->Top()+pRectToErase->GetHeight() };
-        FillRect(hDC, &aRect, static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)));
-    }
-
     HFONT hOrigFont = DisableFontScaling();
 
     int nBaseClusterOffset = 0;
@@ -2958,7 +3027,7 @@ bool UniscribeLayout::DrawCachedGlyphsUsingTextures(SalGraphics& rGraphics) cons
             {
                 SalTwoRect a2Rects(rChunk.maLocation[n].Left(), rChunk.maLocation[n].Top(),
                                    rChunk.maLocation[n].getWidth(), rChunk.maLocation[n].getHeight(),
-                                   aPos.X(), nAdvance + aPos.Y(),
+                                   aPos.X() + rChunk.maLeftOverhangs[n], nAdvance + aPos.Y(),
                                    rChunk.maLocation[n].getWidth(), rChunk.maLocation[n].getHeight()); // ???
                 pImpl->DrawMask(*rChunk.mpTexture, salColor, a2Rects);
             }
@@ -2966,7 +3035,8 @@ bool UniscribeLayout::DrawCachedGlyphsUsingTextures(SalGraphics& rGraphics) cons
             {
                 SalTwoRect a2Rects(rChunk.maLocation[n].Left(), rChunk.maLocation[n].Top(),
                                    rChunk.maLocation[n].getWidth(), rChunk.maLocation[n].getHeight(),
-                                   nAdvance + aPos.X() + mpGlyphOffsets[i].du - rChunk.getExtraOffset(), aPos.Y() + mpGlyphOffsets[i].dv - rChunk.mnAscent - rChunk.getExtraOffset(),
+                                   nAdvance + aPos.X() + mpGlyphOffsets[i].du - rChunk.getExtraOffset() + rChunk.maLeftOverhangs[n],
+                                   aPos.Y() + mpGlyphOffsets[i].dv - rChunk.mnBaselineOffset - rChunk.getExtraOffset(),
                                    rChunk.maLocation[n].getWidth(), rChunk.maLocation[n].getHeight()); // ???
                 pImpl->DrawMask(*rChunk.mpTexture, salColor, a2Rects);
             }
@@ -3531,6 +3601,404 @@ sal_GlyphId GraphiteLayoutWinImpl::getKashidaGlyph(int & rWidth)
     return mrFont.GetMinKashidaGlyph();
 }
 
+HINSTANCE D2DWriteTextOutRenderer::mmD2d1 = nullptr,
+          D2DWriteTextOutRenderer::mmDWrite = nullptr;
+D2DWriteTextOutRenderer::pD2D1CreateFactory_t D2DWriteTextOutRenderer::D2D1CreateFactory = nullptr;
+D2DWriteTextOutRenderer::pDWriteCreateFactory_t D2DWriteTextOutRenderer::DWriteCreateFactory = nullptr;
+
+bool D2DWriteTextOutRenderer::InitModules()
+{
+    mmD2d1 = LoadLibrary("D2d1.dll");
+    mmDWrite = LoadLibrary("dwrite.dll");
+    if (mmD2d1 && mmDWrite)
+    {
+        D2D1CreateFactory = pD2D1CreateFactory_t(GetProcAddress(mmD2d1, "D2D1CreateFactory"));
+        DWriteCreateFactory = pDWriteCreateFactory_t(GetProcAddress(mmDWrite, "DWriteCreateFactory"));
+    }
+
+    if (!D2D1CreateFactory || !DWriteCreateFactory)
+    {
+        CleanupModules();
+        return false;
+    }
+
+    return true;
+}
+
+void D2DWriteTextOutRenderer::CleanupModules()
+{
+    if (mmD2d1)
+        FreeLibrary(mmD2d1);
+    if (mmDWrite)
+        FreeLibrary(mmDWrite);
+
+    mmD2d1 = nullptr;
+    mmDWrite = nullptr;
+    D2D1CreateFactory = nullptr;
+    DWriteCreateFactory = nullptr;
+}
+#endif // ENABLE_GRAPHITE_DWRITE
+
+TextOutRenderer & TextOutRenderer::get()
+{
+#if ENABLE_GRAPHITE_DWRITE
+    static std::unique_ptr<TextOutRenderer> _impl(D2DWriteTextOutRenderer::InitModules()
+        ? static_cast<TextOutRenderer*>(new D2DWriteTextOutRenderer())
+        : static_cast<TextOutRenderer*>(new ExTextOutRenderer()));
+#else // ENABLE_GRAPHITE_DWRITE
+    static std::unique_ptr<TextOutRenderer> _impl(static_cast<TextOutRenderer*>(new ExTextOutRenderer()));
+#endif // ENABLE_GRAPHITE_DWRITE
+
+    return *_impl;
+}
+
+
+bool ExTextOutRenderer::operator ()(WinLayout const &rLayout, HDC hDC,
+    const Rectangle* pRectToErase,
+    Point* pPos, int* pGetNextGlypInfo)
+{
+    const int MAX_GLYPHS = 2;
+    sal_GlyphId glyphIntStr[MAX_GLYPHS];
+    int nGlyphs = 0;
+    WORD glyphWStr[MAX_GLYPHS];
+    do
+    {
+        nGlyphs = rLayout.GetNextGlyphs(1, glyphIntStr, *pPos, *pGetNextGlypInfo);
+        if (nGlyphs < 1)
+            break;
+
+        std::copy_n(glyphIntStr, nGlyphs, glyphWStr);
+        ExtTextOutW(hDC, pPos->X(), pPos->Y(), ETO_GLYPH_INDEX, NULL, LPCWSTR(&glyphWStr), nGlyphs, NULL);
+    } while (!pRectToErase);
+
+    return (pRectToErase && nGlyphs >= 1);
+}
+
+#if ENABLE_GRAPHITE_DWRITE
+
+D2DWriteTextOutRenderer::D2DWriteTextOutRenderer()
+    : mpD2DFactory(nullptr),
+    mpDWriteFactory(nullptr),
+    mpGdiInterop(nullptr),
+    mpRT(nullptr),
+    mRTProps(D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT,
+    D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED),
+    0, 0, D2D1_RENDER_TARGET_USAGE_NONE, D2D1_FEATURE_LEVEL_DEFAULT)),
+    mpFontFace(nullptr),
+    mlfEmHeight(0.0f),
+    mhDC(0)
+{
+    HRESULT hr = S_OK;
+    hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory), NULL, reinterpret_cast<void **>(&mpD2DFactory));
+    hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown**>(&mpDWriteFactory));
+    if (SUCCEEDED(hr))
+    {
+        hr = mpDWriteFactory->GetGdiInterop(&mpGdiInterop);
+        hr = CreateRenderTarget();
+    }
+}
+
+D2DWriteTextOutRenderer::~D2DWriteTextOutRenderer()
+{
+    if (mpRT)
+        mpRT->Release();
+    if (mpGdiInterop)
+        mpGdiInterop->Release();
+    if (mpDWriteFactory)
+        mpDWriteFactory->Release();
+    if (mpD2DFactory)
+        mpD2DFactory->Release();
+
+    CleanupModules();
+}
+
+bool D2DWriteTextOutRenderer::operator ()(WinLayout const &rLayout, HDC hDC,
+    const Rectangle* pRectToErase,
+    Point* pPos, int* pGetNextGlypInfo)
+{
+    if (!Ready())
+        return false;
+
+    if (!BindFont(hDC))
+        return false;
+#if 0
+    // Gather glyph positioning data.
+    std::vector<uint16_t> indices;
+    std::vector<float> advances;
+    std::vector<Point> offsets;
+    sal_GlyphId nLGlyph;
+    DeviceCoordinate nAdv;
+    while (rLayout.GetNextGlyphs(1, &nLGlyph, *pPos, *pGetNextGlypInfo, &nAdv) == 1)
+    {
+        indices.push_back(nLGlyph);
+        advances.push_back(nAdv);
+        offsets.push_back(Point(0,pPos->Y()));
+    }
+
+    // Find the bounds
+    DrawGlyphs(rLayout.DrawBase(), indices.data(), indices.data()+indices.size(), advances.data(), offsets.data());
+
+    ReleaseFont();
+
+    return false;
+#else
+    Rectangle bounds;
+    bool succeeded = GetDWriteInkBox(*mpFontFace, rLayout, mlfEmHeight, bounds);
+    if (pRectToErase)
+    {
+        // Isn't this equivalent to simply doing: bounds = *pRectToErase ?
+        bounds.Intersection(*pRectToErase);
+        bounds.Union(*pRectToErase);
+    }
+    succeeded &= BindDC(hDC, bounds);   // Update the bounding rect.
+
+    ID2D1SolidColorBrush* pBlackBrush = NULL;
+    succeeded &= SUCCEEDED(mpRT->CreateSolidColorBrush(D2D1::ColorF(GetTextColor(hDC)), &pBlackBrush));
+
+    HRESULT hr = S_OK;
+    int nGlyphs = 0;
+    if (succeeded)
+    {
+        const int MAX_GLYPHS = 2;
+        sal_GlyphId glyphIntStr[MAX_GLYPHS];
+        UINT16 glyphIndices[MAX_GLYPHS];
+        long   glyphIntAdv[MAX_GLYPHS];
+        FLOAT  glyphAdvances[MAX_GLYPHS];
+        DWRITE_GLYPH_OFFSET glyphOffsets[MAX_GLYPHS] = { { 0.0f, 0.0f }, };
+
+        mpRT->BeginDraw();
+        do
+        {
+            nGlyphs = rLayout.GetNextGlyphs(1, glyphIntStr, *pPos, *pGetNextGlypInfo, glyphIntAdv);
+            if (nGlyphs < 1)
+                break;
+
+            std::copy_n(glyphIntStr, nGlyphs, glyphIndices);
+            std::copy_n(glyphIntAdv, nGlyphs, glyphAdvances);
+
+            D2D1_POINT_2F baseline = { pPos->X() - bounds.Left(), pPos->Y() - bounds.Top() };
+            DWRITE_GLYPH_RUN glyphs = {
+                mpFontFace,
+                mlfEmHeight,
+                nGlyphs,
+                glyphIndices,
+                glyphAdvances,
+                glyphOffsets,
+                false,
+                0
+            };
+
+            mpRT->DrawGlyphRun(baseline, &glyphs, pBlackBrush);
+        } while (!pRectToErase);
+
+        hr = mpRT->EndDraw();
+    }
+
+    if (pBlackBrush)
+        pBlackBrush->Release();
+
+    ReleaseFont();
+
+    if (hr == D2DERR_RECREATE_TARGET)
+        CreateRenderTarget();
+
+    return (succeeded && nGlyphs >= 1 && pRectToErase);
+#endif
+}
+
+bool D2DWriteTextOutRenderer::BindFont(HDC hDC)
+{
+    // A TextOutRender can only be bound to one font at a time, so the
+    assert(mpFontFace == nullptr);
+    if (mpFontFace)
+    {
+        ReleaseFont();
+        return false;
+    }
+
+    // Initially bind to an empty rectangle to get access to the font face,
+    //  we'll update it once we've calculated a bounding rect in DrawGlyphs
+    if (!BindDC(mhDC = hDC))
+        return false;
+
+    mlfEmHeight = 0;
+    return GetDWriteFaceFromHDC(hDC, &mpFontFace, &mlfEmHeight);
+}
+
+bool D2DWriteTextOutRenderer::ReleaseFont()
+{
+    mpFontFace->Release();
+    mpFontFace = nullptr;
+    mhDC = 0;
+
+    return true;
+}
+
+// GetGlyphInkBoxes
+// The inkboxes returned have their origin on the baseline, to a -ve value
+// of Top() means the glyph extends abs(Top()) many pixels above the
+// baseline, and +ve means the ink starts that many pixels below.
+std::vector<Rectangle> D2DWriteTextOutRenderer::GetGlyphInkBoxes(uint16_t * pGid, uint16_t * pGidEnd) const
+{
+    Rectangle   aExtent;
+    Point aPos;
+    ptrdiff_t nGlyphs = pGidEnd - pGid;
+    if (nGlyphs < 0) return std::vector<Rectangle>();
+
+    DWRITE_FONT_METRICS aFontMetrics;
+    mpFontFace->GetMetrics(&aFontMetrics);
+
+    std::vector<DWRITE_GLYPH_METRICS> metrics(nGlyphs);
+    if (!SUCCEEDED(mpFontFace->GetDesignGlyphMetrics(pGid, nGlyphs, metrics.data(), false)))
+        return std::vector<Rectangle>();
+
+    std::vector<Rectangle> aOut(nGlyphs);
+    auto pOut = aOut.begin();
+    for (auto &m : metrics)
+    {
+        const long left  = m.leftSideBearing,
+                   top   = m.topSideBearing - m.verticalOriginY,
+                   right = m.advanceWidth - m.rightSideBearing,
+                   bottom = INT32(m.advanceHeight) - m.verticalOriginY - m.bottomSideBearing;
+
+        // Scale to screen space.
+        pOut->Left()   = std::lround(left * mlfEmHeight / aFontMetrics.designUnitsPerEm);
+        pOut->Top()    = std::lround(top * mlfEmHeight / aFontMetrics.designUnitsPerEm);
+        pOut->Right()  = std::lround(right * mlfEmHeight / aFontMetrics.designUnitsPerEm);
+        pOut->Bottom() = std::lround(bottom * mlfEmHeight / aFontMetrics.designUnitsPerEm);
+
+        ++pOut;
+    }
+
+    return aOut;
+}
+
+bool D2DWriteTextOutRenderer::DrawGlyphs(const Point & origin, uint16_t * pGid, uint16_t * pGidEnd,
+    float * pAdvances, Point * pOffsets)
+{
+    Rectangle bounds;
+    //bool succeeded = GetDWriteInkBox(*mpFontFace, rLayout, mlfEmHeight, bounds);
+
+    Point aPos = origin;
+    auto aInks = GetGlyphInkBoxes(pGid, pGidEnd);
+    if (aInks.empty()) return false;
+
+    // Calculate the bounding rectangle.
+    auto adv = pAdvances;
+    auto ofs = pOffsets;
+    for (auto &b:aInks)
+    {
+        aPos += *ofs++;
+        b += aPos;
+        aPos.X() += *adv++;
+        bounds.Union(b);
+    }
+    bool succeeded = BindDC(mhDC, bounds);   // Update the bounding rect.
+
+    ID2D1SolidColorBrush* pBrush = NULL;
+    succeeded &= SUCCEEDED(mpRT->CreateSolidColorBrush(D2D1::ColorF(GetTextColor(mhDC)), &pBrush));
+
+    HRESULT hr = S_OK;
+    if (succeeded)
+    {
+        const int MAX_GLYPHS = 20;
+        UINT16 glyphIndices[MAX_GLYPHS];
+        FLOAT  glyphAdvances[MAX_GLYPHS];
+        DWRITE_GLYPH_OFFSET glyphOffsets[MAX_GLYPHS] = { { 0.0f, 0.0f }, };
+
+        mpRT->BeginDraw();
+        for (auto nGlyphs = std::min(pGidEnd - pGid, MAX_GLYPHS); pGid != pGidEnd;
+                    pGid += nGlyphs, nGlyphs = std::min(pGidEnd - pGid, MAX_GLYPHS))
+        {
+            std::copy(pGid, pGidEnd, glyphIndices);
+            std::copy_n(pAdvances, nGlyphs, glyphAdvances);
+            std::transform(pOffsets, pOffsets + nGlyphs, glyphOffsets, [](Point &o) { return DWRITE_GLYPH_OFFSET{ o.X(), o.Y() }; });
+            D2D1_POINT_2F baseline = { origin.X() - bounds.Left(), origin.Y() - bounds.Top() };
+            DWRITE_GLYPH_RUN glyphs = {
+                mpFontFace,
+                mlfEmHeight,
+                nGlyphs,
+                glyphIndices,
+                glyphAdvances,
+                glyphOffsets,
+                false,
+                0
+            };
+
+            mpRT->DrawGlyphRun(baseline, &glyphs, pBrush);
+        }
+
+        hr = mpRT->EndDraw();
+    }
+
+    if (pBrush)
+        pBrush->Release();
+
+    if (hr == D2DERR_RECREATE_TARGET)
+    {
+        ReleaseFont();
+        hr = CreateRenderTarget();
+    }
+
+    return (SUCCEEDED(hr) && succeeded);
+}
+
+
+bool D2DWriteTextOutRenderer::GetDWriteFaceFromHDC(HDC hDC, IDWriteFontFace ** ppFontFace, float * lfSize) const
+{
+    bool const succeeded = SUCCEEDED(mpGdiInterop->CreateFontFaceFromHdc(hDC, ppFontFace));
+
+    if (succeeded)
+    {
+        LOGFONTW aLogFont;
+        HFONT hFont = (HFONT)::GetCurrentObject(hDC, OBJ_FONT);
+
+        GetObjectW(hFont, sizeof(LOGFONTW), &aLogFont);
+        float dpix, dpiy;
+        mpRT->GetDpi(&dpix, &dpiy);
+        *lfSize = aLogFont.lfHeight * 96.0f / dpiy;
+
+        assert(*lfSize < 0);
+        *lfSize *= -1;
+    }
+
+    return succeeded;
+}
+
+bool D2DWriteTextOutRenderer::GetDWriteInkBox(IDWriteFontFace & rFontFace, WinLayout const &rLayout, float const /*lfEmHeight*/, Rectangle & rOut) const
+{
+    rOut.SetEmpty();
+
+    DWRITE_FONT_METRICS aFontMetrics;
+    rFontFace.GetMetrics(&aFontMetrics);
+
+    Point aPos;
+    sal_GlyphId nLGlyph;
+    std::vector<uint16_t> indices;
+    std::vector<Point>  positions;
+    int nStart = 0;
+    while (rLayout.GetNextGlyphs(1, &nLGlyph, aPos, nStart) == 1)
+    {
+        positions.push_back(aPos);
+        indices.push_back(nLGlyph);
+    }
+
+    auto aBoxes = GetGlyphInkBoxes(indices.data(), indices.data() + indices.size());
+    if (aBoxes.empty())
+        return false;
+
+    auto p = positions.begin();
+    for (auto &b:aBoxes)
+    {
+        b += *p++;
+        rOut.Union(b);
+    }
+
+    return true;
+}
+
+#endif // ENABLE_GRAPHITE_DWRITE
+
 float gr_fontAdvance(const void* appFontHandle, gr_uint16 glyphId)
 {
     HDC hDC = reinterpret_cast<HDC>(const_cast<void*>(appFontHandle));
@@ -3577,7 +4045,7 @@ GraphiteWinLayout::~GraphiteWinLayout()
     gr_font_destroy(maImpl.GetFont());
 }
 
-bool GraphiteWinLayout::LayoutText( ImplLayoutArgs & args)
+bool GraphiteWinLayout::LayoutText(ImplLayoutArgs & args)
 {
     HFONT hUnRotatedFont = 0;
     if (args.mnOrientation)
@@ -3619,30 +4087,15 @@ bool GraphiteWinLayout::DrawTextImpl(HDC hDC,
                                      Point* pPos,
                                      int* pGetNextGlypInfo) const
 {
-    if (pRectToErase)
-    {
-        RECT aRect = { pRectToErase->Left(), pRectToErase->Top(), pRectToErase->Left()+pRectToErase->GetWidth(), pRectToErase->Top()+pRectToErase->GetHeight() };
-        FillRect(hDC, &aRect, static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)));
-    }
-
     HFONT hOrigFont = DisableFontScaling();
     maImpl.DrawBase() = WinLayout::maDrawBase;
     maImpl.DrawOffset() = WinLayout::maDrawOffset;
-    const int MAX_GLYPHS = 2;
-    sal_GlyphId glyphIntStr[MAX_GLYPHS];
-    WORD glyphWStr[MAX_GLYPHS];
-    int nGlyphs = 0;
-    do
-    {
-        nGlyphs = maImpl.GetNextGlyphs(1, glyphIntStr, *pPos, *pGetNextGlypInfo);
-        if (nGlyphs < 1)
-            break;
-        std::copy(glyphIntStr, glyphIntStr + nGlyphs, glyphWStr);
-        ExtTextOutW(hDC, pPos->X(), pPos->Y(), ETO_GLYPH_INDEX, NULL, (LPCWSTR)&(glyphWStr), nGlyphs, NULL);
-    } while (!pRectToErase);
+
+    TextOutRenderer & render = TextOutRenderer::get();
+    bool const ok = render(*this, hDC, pRectToErase, pPos, pGetNextGlypInfo);
     if( hOrigFont )
         DeleteFont(SelectFont(hDC, hOrigFont));
-    return (pRectToErase && nGlyphs >= 1);
+    return ok;
 }
 
 bool GraphiteWinLayout::CacheGlyphs(SalGraphics& /*rGraphics*/) const


More information about the Libreoffice-commits mailing list