1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <win/salgdi.h>
#include <win/saldata.hxx>
#include <ImplOutDevData.hxx>
#include <win/DWriteTextRenderer.hxx>
#include <sft.hxx>
#include <sallayout.hxx>
#include <shlwapi.h>
#include <winver.h>
#include <comphelper/windowserrorstring.hxx>
#include <o3tl/safeint.hxx>
#include <sal/log.hxx>
namespace
{
D2D1_TEXT_ANTIALIAS_MODE lclGetSystemTextAntiAliasType()
{
UINT t;
if (Application::GetSettings().GetStyleSettings().GetUseSubpixelAA()
&& SystemParametersInfoW(SPI_GETFONTSMOOTHINGTYPE, 0, &t, 0)
&& t == FE_FONTSMOOTHINGCLEARTYPE)
return D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE;
return D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE;
}
IDWriteRenderingParams* lclSetRenderingMode(DWRITE_RENDERING_MODE eRenderingMode)
{
IDWriteFactory* pDWriteFactory = WinSalGraphics::getDWriteFactory();
IDWriteRenderingParams* pDefaultParameters = nullptr;
pDWriteFactory->CreateRenderingParams(&pDefaultParameters);
IDWriteRenderingParams* pParameters = nullptr;
pDWriteFactory->CreateCustomRenderingParams(
pDefaultParameters->GetGamma(),
pDefaultParameters->GetEnhancedContrast(),
pDefaultParameters->GetClearTypeLevel(),
pDefaultParameters->GetPixelGeometry(),
eRenderingMode,
&pParameters);
return pParameters;
}
#ifdef SAL_LOG_WARN
HRESULT checkResult(HRESULT hr, const char* location)
{
SAL_DETAIL_LOG_STREAM(SAL_DETAIL_ENABLE_LOG_WARN && FAILED(hr), ::SAL_DETAIL_LOG_LEVEL_WARN,
"vcl.gdi", location,
"HRESULT failed with: 0x" << OUString::number(hr, 16) << ": " << WindowsErrorStringFromHRESULT(hr));
return hr;
}
#define CHECKHR(funct) checkResult(funct, SAL_WHERE)
#else
#define CHECKHR(funct) (funct)
#endif
// Sets and unsets the needed DirectWrite transform to support the font's rotation.
class WinFontTransformGuard
{
public:
WinFontTransformGuard(ID2D1RenderTarget* pRenderTarget, float hscale,
const GenericSalLayout& rLayout, const D2D1_POINT_2F& rBaseline,
bool bIsVertical);
~WinFontTransformGuard();
private:
ID2D1RenderTarget* mpRenderTarget;
std::optional<D2D1::Matrix3x2F> moTransform;
};
} // end anonymous namespace
// static
D2DWriteTextOutRenderer::MODE D2DWriteTextOutRenderer::GetMode(bool bRenderingModeNatural,
bool bAntiAlias)
{
D2D1_TEXT_ANTIALIAS_MODE eTextMode;
if (!Application::GetSettings().GetStyleSettings().GetUseFontAAFromSystem())
// Currently only for file output - see GraphicExporter::filter
eTextMode = bAntiAlias ? D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE : D2D1_TEXT_ANTIALIAS_MODE_ALIASED;
else if (BOOL bSmoothing; SystemParametersInfoW(SPI_GETFONTSMOOTHING, 0, &bSmoothing, 0))
eTextMode = bSmoothing ? lclGetSystemTextAntiAliasType() : D2D1_TEXT_ANTIALIAS_MODE_ALIASED;
else
eTextMode = D2D1_TEXT_ANTIALIAS_MODE_DEFAULT;
DWRITE_RENDERING_MODE eRenderingMode;
if (eTextMode == D2D1_TEXT_ANTIALIAS_MODE_ALIASED)
eRenderingMode = DWRITE_RENDERING_MODE_ALIASED; // no way to use bRenderingModeNatural
else if (bRenderingModeNatural)
eRenderingMode = DWRITE_RENDERING_MODE_NATURAL;
else if (eTextMode == D2D1_TEXT_ANTIALIAS_MODE_DEFAULT)
eRenderingMode = DWRITE_RENDERING_MODE_DEFAULT;
else // D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE || D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE
eRenderingMode = DWRITE_RENDERING_MODE_GDI_CLASSIC;
return { eTextMode, eRenderingMode };
}
D2DWriteTextOutRenderer::D2DWriteTextOutRenderer(MODE mode)
: mpD2DFactory(nullptr),
mpRT(nullptr),
mRTProps(D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED),
0, 0)),
maRenderingMode(mode)
{
HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory), nullptr, IID_PPV_ARGS_Helper(&mpD2DFactory));
if (SUCCEEDED(hr))
hr = CreateRenderTarget();
}
HRESULT D2DWriteTextOutRenderer::CreateRenderTarget()
{
HRESULT hr = CHECKHR(mpD2DFactory->CreateDCRenderTarget(&mRTProps, &mpRT));
if (SUCCEEDED(hr))
{
mpRT->SetTextRenderingParams(lclSetRenderingMode(maRenderingMode.second));
mpRT->SetTextAntialiasMode(maRenderingMode.first);
}
return hr;
}
bool D2DWriteTextOutRenderer::Ready() const
{
return mpRT;
}
HRESULT D2DWriteTextOutRenderer::BindDC(HDC hDC, tools::Rectangle const & rRect)
{
RECT const rc = {
o3tl::narrowing<LONG>(rRect.Left()), o3tl::narrowing<LONG>(rRect.Top()),
o3tl::narrowing<LONG>(rRect.Right()), o3tl::narrowing<LONG>(rRect.Bottom()) };
return CHECKHR(mpRT->BindDC(hDC, &rc));
}
bool D2DWriteTextOutRenderer::operator()(GenericSalLayout const & rLayout, SalGraphics& rGraphics, HDC hDC)
{
bool bRetry = false;
bool bResult = false;
int nCount = 0;
do
{
bRetry = false;
bResult = performRender(rLayout, rGraphics, hDC, bRetry);
nCount++;
} while (bRetry && nCount < 3);
return bResult;
}
bool D2DWriteTextOutRenderer::performRender(GenericSalLayout const & rLayout, SalGraphics& rGraphics, HDC hDC, bool& bRetry)
{
if (!Ready())
return false;
HRESULT hr = BindDC(hDC);
if (hr == D2DERR_RECREATE_TARGET)
{
CreateRenderTarget();
bRetry = true;
return false;
}
if (FAILED(hr))
{
// If for any reason we can't bind fallback to legacy APIs.
return ExTextOutRenderer()(rLayout, rGraphics, hDC);
}
const WinFontInstance& rWinFont = static_cast<const WinFontInstance&>(rLayout.GetFont());
float lfEmHeight = 0;
IDWriteFontFace* pFontFace = GetDWriteFace(rWinFont, &lfEmHeight);
if (!pFontFace)
return false;
auto [succeeded, bounds] = [&rLayout]()
{
basegfx::B2DRectangle r;
bool result = rLayout.GetBoundRect(r);
if (result)
r.grow(1); // plus 1 pixel to the tight range
return std::make_pair(result, SalLayout::BoundRect2Rectangle(r));
}();
if (succeeded)
{
hr = BindDC(hDC, bounds); // Update the bounding rect.
succeeded = SUCCEEDED(hr);
}
sal::systools::COMReference<ID2D1SolidColorBrush> pBrush;
if (succeeded)
{
COLORREF bgrTextColor = GetTextColor(hDC);
D2D1::ColorF aD2DColor(GetRValue(bgrTextColor) / 255.0f, GetGValue(bgrTextColor) / 255.0f, GetBValue(bgrTextColor) / 255.0f);
succeeded = SUCCEEDED(CHECKHR(mpRT->CreateSolidColorBrush(aD2DColor, &pBrush)));
}
if (succeeded)
{
mpRT->BeginDraw();
const float hscale = rWinFont.getHScale();
int nStart = 0;
basegfx::B2DPoint aPos;
const GlyphItem* pGlyph;
while (rLayout.GetNextGlyph(&pGlyph, aPos, nStart))
{
UINT16 glyphIndices[] = { static_cast<UINT16>(pGlyph->glyphId()) };
FLOAT glyphAdvances[] = { static_cast<FLOAT>(pGlyph->newWidth()) / hscale };
DWRITE_GLYPH_OFFSET glyphOffsets[] = { { 0.0f, 0.0f }, };
D2D1_POINT_2F baseline = { static_cast<FLOAT>(aPos.getX() - bounds.Left()) / hscale,
static_cast<FLOAT>(aPos.getY() - bounds.Top()) };
WinFontTransformGuard aTransformGuard(mpRT, hscale, rLayout, baseline, pGlyph->IsVertical());
DWRITE_GLYPH_RUN glyphs = {
pFontFace,
lfEmHeight,
1,
glyphIndices,
glyphAdvances,
glyphOffsets,
false,
0
};
mpRT->DrawGlyphRun(baseline, &glyphs, pBrush);
}
hr = CHECKHR(mpRT->EndDraw());
}
if (hr == D2DERR_RECREATE_TARGET)
{
CreateRenderTarget();
bRetry = true;
}
return succeeded;
}
IDWriteFontFace* D2DWriteTextOutRenderer::GetDWriteFace(const WinFontInstance& rWinFont,
float* lfSize) const
{
auto pFontFace = rWinFont.GetDWFontFace();
if (pFontFace)
{
LOGFONTW aLogFont;
HFONT hFont = rWinFont.GetHFONT();
GetObjectW(hFont, sizeof(LOGFONTW), &aLogFont);
float dpix, dpiy;
mpRT->GetDpi(&dpix, &dpiy);
*lfSize = aLogFont.lfHeight * 96.0f / dpiy;
assert(*lfSize < 0);
*lfSize *= -1;
}
return pFontFace;
}
WinFontTransformGuard::WinFontTransformGuard(ID2D1RenderTarget* pRenderTarget, float hscale,
const GenericSalLayout& rLayout,
const D2D1_POINT_2F& rBaseline,
bool bIsVertical)
: mpRenderTarget(pRenderTarget)
{
Degree10 angle = rLayout.GetOrientation();
if (bIsVertical)
angle += 900_deg10;
if (hscale != 1.0f || angle)
{
D2D1::Matrix3x2F aTransform;
pRenderTarget->GetTransform(&aTransform);
moTransform = aTransform;
if (hscale != 1.0f) // basegfx::fTools::equal is useless with float
aTransform = aTransform * D2D1::Matrix3x2F::Scale(hscale, 1.0f, { 0, 0 });
// DWrite angle is in clockwise degrees, our orientation is in counter-clockwise 10th
// degrees.
if (angle)
aTransform = aTransform * D2D1::Matrix3x2F::Rotation(-toDegrees(angle), rBaseline);
mpRenderTarget->SetTransform(aTransform);
}
}
WinFontTransformGuard::~WinFontTransformGuard()
{
if (moTransform)
mpRenderTarget->SetTransform(*moTransform);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|