summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Eves <tim_eves@sil.org>2016-02-23 16:45:21 +0000
committerTor Lillqvist <tml@collabora.com>2016-03-14 12:50:59 +0200
commite45d80f20ff6a05888f92ae942abed67609d5b20 (patch)
treefef594425e80e321d4b6466e228fb4a85c97cac7
parent2a1b70c4b2d5debf235c088a1e3c71a3457c1af7 (diff)
tdf#97171: Use DirectWrite for OpenGL glyph caching
This is a squash of several separate intermediate commits, the most relevant log messages of which are repeated here: 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.
-rw-r--r--vcl/win/gdi/winlayout.cxx698
1 files changed, 415 insertions, 283 deletions
diff --git a/vcl/win/gdi/winlayout.cxx b/vcl/win/gdi/winlayout.cxx
index ab04e70c97fe..c8856dc400dc 100644
--- a/vcl/win/gdi/winlayout.cxx
+++ b/vcl/win/gdi/winlayout.cxx
@@ -38,6 +38,7 @@
#include <cstdlib>
#include <sal/alloca.h>
+#include <rtl/character.hxx>
#include <algorithm>
@@ -78,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;
@@ -147,6 +149,104 @@ private:
GLuint WinFontInstance::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 {
@@ -161,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)
@@ -182,7 +282,11 @@ void DumpGlyphBitmap(HDC hDC)
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";
@@ -297,196 +401,155 @@ bool WinFontInstance::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, reinterpret_cast<wchar_t *>(aGlyphIndices.data()), nCount, 0, NULL, NULL, &aSize))
- {
- SAL_WARN("vcl.gdi", "GetTextExtentExPoint failed: " << WindowsErrorString(GetLastError()));
- SelectObject(hDC, hOrigFont);
- DeleteDC(hDC);
- return false;
- }
- if (!GetCharABCWidthsW(hDC, nGlyphIndex, nGlyphIndex+nCount-1, aABC.data()))
+ // 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", "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 between glyphs will help.
- std::vector<int> aDX(nCount);
- int totWidth = 0;
- for (int i = 0; i < nCount; i++)
+ 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,
- reinterpret_cast<wchar_t *>(aGlyphIndices.data()), nCount,
- aDX.data()))
+ switch (hr)
{
- SAL_WARN("vcl.gdi", "ExtTextOutW failed: " << WindowsErrorString(GetLastError()));
+ 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());
@@ -498,7 +561,14 @@ bool WinFontInstance::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;
@@ -1465,7 +1535,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);
@@ -2987,7 +3058,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);
}
@@ -2995,7 +3066,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);
}
@@ -3560,93 +3632,6 @@ sal_GlyphId GraphiteLayoutWinImpl::getKashidaGlyph(int & rWidth)
return mrFont.GetMinKashidaGlyph();
}
-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;
-
-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;
-
- inline
- HRESULT CreateRenderTarget() {
- if (mpRT) mpRT->Release(); mpRT = NULL;
- return mpD2DFactory->CreateDCRenderTarget(&mRTProps, &mpRT);
- }
-
- inline
- bool Ready() const { return mpGdiInterop && mpRT; }
-
- 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));
- }
-
- ID2D1Factory * mpD2DFactory;
- IDWriteFactory * mpDWriteFactory;
- IDWriteGdiInterop * mpGdiInterop;
- ID2D1DCRenderTarget * mpRT;
- const D2D1_RENDER_TARGET_PROPERTIES mRTProps;
-};
-
HINSTANCE D2DWriteTextOutRenderer::mmD2d1 = nullptr,
D2DWriteTextOutRenderer::mmDWrite = nullptr;
D2DWriteTextOutRenderer::pD2D1CreateFactory_t D2DWriteTextOutRenderer::D2D1CreateFactory = nullptr;
@@ -3723,13 +3708,16 @@ bool ExTextOutRenderer::operator ()(WinLayout const &rLayout, HDC hDC,
#if ENABLE_GRAPHITE_DWRITE
D2DWriteTextOutRenderer::D2DWriteTextOutRenderer()
- : mpD2DFactory(NULL),
- mpDWriteFactory(NULL),
- mpGdiInterop(NULL),
- mpRT(NULL),
+ : 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))
+ 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));
@@ -3762,18 +3750,31 @@ bool D2DWriteTextOutRenderer::operator ()(WinLayout const &rLayout, HDC hDC,
if (!Ready())
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.
- if (!BindDC(hDC))
+ 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()));
+ }
- IDWriteFontFace * pFontFace = NULL;
- float lfEmHeight = 0;
- if (!GetDWriteFaceFromHDC(hDC, &pFontFace, &lfEmHeight))
- return false;
+ // 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(*pFontFace, rLayout, lfEmHeight, bounds);
+ bool succeeded = GetDWriteInkBox(*mpFontFace, rLayout, mlfEmHeight, bounds);
if (pRectToErase)
{
// Isn't this equivalent to simply doing: bounds = *pRectToErase ?
@@ -3808,8 +3809,8 @@ bool D2DWriteTextOutRenderer::operator ()(WinLayout const &rLayout, HDC hDC,
D2D1_POINT_2F baseline = { pPos->X() - bounds.Left(), pPos->Y() - bounds.Top() };
DWRITE_GLYPH_RUN glyphs = {
- pFontFace,
- lfEmHeight,
+ mpFontFace,
+ mlfEmHeight,
nGlyphs,
glyphIndices,
glyphAdvances,
@@ -3827,15 +3828,153 @@ bool D2DWriteTextOutRenderer::operator ()(WinLayout const &rLayout, HDC hDC,
if (pBlackBrush)
pBlackBrush->Release();
- if (pFontFace)
- pFontFace->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));
@@ -3857,7 +3996,7 @@ bool D2DWriteTextOutRenderer::GetDWriteFaceFromHDC(HDC hDC, IDWriteFontFace ** p
return succeeded;
}
-bool D2DWriteTextOutRenderer::GetDWriteInkBox(IDWriteFontFace & rFontFace, WinLayout const &rLayout, float const lfEmHeight, Rectangle & rOut) const
+bool D2DWriteTextOutRenderer::GetDWriteInkBox(IDWriteFontFace & rFontFace, WinLayout const &rLayout, float const /*lfEmHeight*/, Rectangle & rOut) const
{
rOut.SetEmpty();
@@ -3865,34 +4004,27 @@ bool D2DWriteTextOutRenderer::GetDWriteInkBox(IDWriteFontFace & rFontFace, WinLa
rFontFace.GetMetrics(&aFontMetrics);
Point aPos;
- for (int nStart = 0;;)
+ sal_GlyphId nLGlyph;
+ std::vector<uint16_t> indices;
+ std::vector<Point> positions;
+ int nStart = 0;
+ while (rLayout.GetNextGlyphs(1, &nLGlyph, aPos, nStart) == 1)
{
- sal_GlyphId nLGlyph;
- if (rLayout.GetNextGlyphs(1, &nLGlyph, aPos, nStart) < 1)
- break;
+ positions.push_back(aPos);
+ indices.push_back(nLGlyph);
+ }
- // get bounding rectangle of individual glyph
- UINT16 indices[1] = { nLGlyph };
- DWRITE_GLYPH_METRICS metrics[1];
- if (!SUCCEEDED(rFontFace.GetDesignGlyphMetrics(indices, 1, metrics, false)))
- return false;
+ auto aBoxes = GetGlyphInkBoxes(indices.data(), indices.data() + indices.size());
+ if (aBoxes.empty())
+ return false;
- // We accumulate glyph boxes design unit space
- Rectangle aGlyphInkBox(
- metrics->leftSideBearing,
- metrics->topSideBearing - metrics->verticalOriginY,
- metrics->advanceWidth - metrics->rightSideBearing,
- INT32(metrics->advanceHeight) - metrics->verticalOriginY - metrics->bottomSideBearing);
- aGlyphInkBox += aPos * aFontMetrics.designUnitsPerEm / lfEmHeight;
- rOut.Union(aGlyphInkBox);
+ auto p = positions.begin();
+ for (auto &b:aBoxes)
+ {
+ b += *p++;
+ rOut.Union(b);
}
- // Scale to screen space.
- rOut.Left() = std::lround(rOut.Left()*lfEmHeight / aFontMetrics.designUnitsPerEm);
- rOut.Top() = std::lround(rOut.Top()*lfEmHeight / aFontMetrics.designUnitsPerEm);
- rOut.Right() = 1+std::lround(rOut.Right()*lfEmHeight / aFontMetrics.designUnitsPerEm);
- rOut.Bottom() = 1+std::lround(rOut.Bottom()*lfEmHeight / aFontMetrics.designUnitsPerEm);
-
return true;
}
@@ -3944,7 +4076,7 @@ GraphiteWinLayout::~GraphiteWinLayout()
gr_font_destroy(maImpl.GetFont());
}
-bool GraphiteWinLayout::LayoutText( ImplLayoutArgs & args)
+bool GraphiteWinLayout::LayoutText(ImplLayoutArgs & args)
{
HFONT hUnRotatedFont = 0;
if (args.mnOrientation)