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
|
/* -*- 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 <memory>
#include <basegfx/polygon/b2dpolypolygon.hxx>
#include <basegfx/utils/canvastools.hxx>
#include <com/sun/star/rendering/FontRequest.hpp>
#include <com/sun/star/rendering/PanoseProportion.hpp>
#include <com/sun/star/rendering/XCanvasFont.hpp>
#include <comphelper/scopeguard.hxx>
#include <i18nlangtag/languagetag.hxx>
#include <rtl/math.hxx>
#include <tools/color.hxx>
#include <tools/diagnose_ex.h>
#include <tools/poly.hxx>
#include <vcl/canvastools.hxx>
#include <vcl/metric.hxx>
#include <vcl/sysdata.hxx>
#include <vcl/virdev.hxx>
#include <canvas/canvastools.hxx>
#include "dx_bitmap.hxx"
#include "dx_canvasfont.hxx"
#include "dx_impltools.hxx"
#include "dx_textlayout_drawhelper.hxx"
using namespace ::com::sun::star;
namespace dxcanvas
{
TextLayoutDrawHelper::TextLayoutDrawHelper(
const uno::Reference< rendering::XGraphicDevice >& xGraphicDevice ) :
mxGraphicDevice(xGraphicDevice)
{
}
TextLayoutDrawHelper::~TextLayoutDrawHelper()
{
}
void TextLayoutDrawHelper::drawText(
const std::shared_ptr<Gdiplus::Graphics>& rGraphics,
const css::rendering::ViewState& rViewState,
const css::rendering::RenderState& rRenderState,
const ::basegfx::B2ISize& rOutputOffset,
const css::rendering::StringContext& rText,
const css::uno::Sequence< double >& rLogicalAdvancements,
const css::uno::Reference<
css::rendering::XCanvasFont >& rCanvasFont,
const css::geometry::Matrix2D& rFontMatrix,
bool bAlphaSurface,
bool bIsRTL)
{
HDC hdc = rGraphics->GetHDC();
// issue a ReleaseHDC() when leaving the scope
const ::comphelper::ScopeGuard aGuard(
[&rGraphics, &hdc]() mutable { rGraphics->ReleaseHDC(hdc); } );
SystemGraphicsData aSystemGraphicsData;
aSystemGraphicsData.nSize = sizeof(SystemGraphicsData);
aSystemGraphicsData.hDC = reinterpret_cast< ::HDC >(hdc);
ScopedVclPtrInstance<VirtualDevice> xVirtualDevice(aSystemGraphicsData, Size(1, 1), DeviceFormat::DEFAULT);
// disable font antialiasing - GDI does not handle alpha
// surfaces properly.
if( bAlphaSurface )
xVirtualDevice->SetAntialiasing(AntialiasingFlags::DisableText);
if(rText.Length)
{
bool test = mxGraphicDevice.is();
ENSURE_OR_THROW( test,
"TextLayoutDrawHelper::drawText(): Invalid GraphicDevice" );
// set text color. Make sure to remove transparence part first.
Color aColor( COL_WHITE );
if( rRenderState.DeviceColor.getLength() > 2 )
aColor = vcl::unotools::doubleSequenceToColor(
rRenderState.DeviceColor,
mxGraphicDevice->getDeviceColorSpace());
aColor.SetTransparency(0);
xVirtualDevice->SetTextColor(aColor);
// create the font
const css::rendering::FontRequest& rFontRequest = rCanvasFont->getFontRequest();
vcl::Font aFont(
rFontRequest.FontDescription.FamilyName,
rFontRequest.FontDescription.StyleName,
Size( 0, ::basegfx::fround(rFontRequest.CellSize)));
aFont.SetAlignment( ALIGN_BASELINE );
aFont.SetCharSet( (rFontRequest.FontDescription.IsSymbolFont==css::util::TriState_YES) ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE );
aFont.SetVertical( rFontRequest.FontDescription.IsVertical==css::util::TriState_YES );
aFont.SetWeight( static_cast<FontWeight>(rFontRequest.FontDescription.FontDescription.Weight) );
aFont.SetItalic( (rFontRequest.FontDescription.FontDescription.Letterform<=8) ? ITALIC_NONE : ITALIC_NORMAL );
aFont.SetPitch(
rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
? PITCH_FIXED : PITCH_VARIABLE);
aFont.SetLanguage(LanguageTag::convertToLanguageType(rFontRequest.Locale));
// setup font color
aFont.SetColor( aColor );
aFont.SetFillColor( aColor );
CanvasFont::ImplRef pFont(tools::canvasFontFromXFont(rCanvasFont));
if (pFont.is() && pFont->getEmphasisMark())
aFont.SetEmphasisMark(FontEmphasisMark(pFont->getEmphasisMark()));
// adjust to stretched font
if(!::rtl::math::approxEqual(rFontMatrix.m00, rFontMatrix.m11))
{
const Size aSize = xVirtualDevice->GetFontMetric( aFont ).GetFontSize();
const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
double fStretch = rFontMatrix.m00 + rFontMatrix.m01;
if( !::basegfx::fTools::equalZero( fDividend) )
fStretch /= fDividend;
const sal_Int32 nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
aFont.SetAverageFontWidth( nNewWidth );
}
// set font
xVirtualDevice->SetFont(aFont);
// create world transformation matrix
::basegfx::B2DHomMatrix aWorldTransform;
::canvas::tools::mergeViewAndRenderTransform(aWorldTransform, rViewState, rRenderState);
if(!rOutputOffset.equalZero())
{
aWorldTransform.translate(rOutputOffset.getX(), rOutputOffset.getY());
}
// set ViewState clipping
if(rViewState.Clip.is())
{
::basegfx::B2DPolyPolygon aClipPoly(dxcanvas::tools::polyPolygonFromXPolyPolygon2D(rViewState.Clip));
::basegfx::B2DHomMatrix aMatrix;
::basegfx::unotools::homMatrixFromAffineMatrix(aMatrix, rViewState.AffineTransform );
if(!rOutputOffset.equalZero())
{
aMatrix.translate(rOutputOffset.getX(), rOutputOffset.getY());
}
aClipPoly.transform(aMatrix);
const vcl::Region& rClipRegion = vcl::Region(::tools::PolyPolygon(aClipPoly));
xVirtualDevice->IntersectClipRegion(rClipRegion);
}
if(rRenderState.Clip.is())
{
::basegfx::B2DPolyPolygon aClipPoly(dxcanvas::tools::polyPolygonFromXPolyPolygon2D(rRenderState.Clip));
aClipPoly.transform(aWorldTransform);
const vcl::Region& rClipRegion = vcl::Region(::tools::PolyPolygon(aClipPoly));
xVirtualDevice->IntersectClipRegion(rClipRegion);
}
// set world transform
XFORM aXForm;
aXForm.eM11 = static_cast<FLOAT>(aWorldTransform.get(0, 0));
aXForm.eM12 = static_cast<FLOAT>(aWorldTransform.get(1, 0));
aXForm.eM21 = static_cast<FLOAT>(aWorldTransform.get(0, 1));
aXForm.eM22 = static_cast<FLOAT>(aWorldTransform.get(1, 1));
aXForm.eDx = static_cast<FLOAT>(aWorldTransform.get(0, 2));
aXForm.eDy = static_cast<FLOAT>(aWorldTransform.get(1, 2));
// TODO(F3): This is NOT supported on 95/98/ME!
SetGraphicsMode(hdc, GM_ADVANCED);
SetTextAlign(hdc, TA_BASELINE);
SetWorldTransform(hdc, &aXForm);
// use an empty StartPosition for text rendering
const Point aEmptyPoint(0, 0);
// create the String
const OUString aText(rText.Text);
if( rLogicalAdvancements.getLength() )
{
// create the DXArray
const sal_Int32 nLen( rLogicalAdvancements.getLength() );
std::unique_ptr<sal_Int32[]> pDXArray( new sal_Int32[nLen] );
for( sal_Int32 i=0; i<nLen; ++i )
pDXArray[i] = basegfx::fround( rLogicalAdvancements[i] );
// draw the String
xVirtualDevice->DrawTextArray( aEmptyPoint,
aText,
pDXArray.get(),
rText.StartPosition,
rText.Length,
bIsRTL ? SalLayoutFlags::BiDiRtl : SalLayoutFlags::NONE);
}
else
{
// draw the String
xVirtualDevice->DrawText( aEmptyPoint,
aText,
rText.StartPosition,
rText.Length );
}
}
}
geometry::RealRectangle2D TextLayoutDrawHelper::queryTextBounds( const rendering::StringContext& rText,
const uno::Sequence< double >& rLogicalAdvancements,
const uno::Reference< rendering::XCanvasFont >& rCanvasFont,
const geometry::Matrix2D& rFontMatrix )
{
if(!(rText.Length))
return geometry::RealRectangle2D();
// TODO(F1): Fetching default screen DC here, will yield wrong
// metrics when e.g. formatting for a printer!
SystemGraphicsData aSystemGraphicsData;
aSystemGraphicsData.nSize = sizeof(SystemGraphicsData);
aSystemGraphicsData.hDC = reinterpret_cast< ::HDC >(GetDC( nullptr ));
ScopedVclPtrInstance<VirtualDevice> xVirtualDevice(aSystemGraphicsData, Size(1, 1), DeviceFormat::DEFAULT);
// create the font
const css::rendering::FontRequest& rFontRequest = rCanvasFont->getFontRequest();
vcl::Font aFont(
rFontRequest.FontDescription.FamilyName,
rFontRequest.FontDescription.StyleName,
Size( 0, ::basegfx::fround(rFontRequest.CellSize)));
aFont.SetAlignment( ALIGN_BASELINE );
aFont.SetCharSet( (rFontRequest.FontDescription.IsSymbolFont==css::util::TriState_YES) ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE );
aFont.SetVertical( rFontRequest.FontDescription.IsVertical==css::util::TriState_YES );
aFont.SetWeight( static_cast<FontWeight>(rFontRequest.FontDescription.FontDescription.Weight) );
aFont.SetItalic( (rFontRequest.FontDescription.FontDescription.Letterform<=8) ? ITALIC_NONE : ITALIC_NORMAL );
aFont.SetPitch(
rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
? PITCH_FIXED : PITCH_VARIABLE);
// adjust to stretched font
if(!::rtl::math::approxEqual(rFontMatrix.m00, rFontMatrix.m11))
{
const Size aSize = xVirtualDevice->GetFontMetric( aFont ).GetFontSize();
const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
double fStretch = rFontMatrix.m00 + rFontMatrix.m01;
if( !::basegfx::fTools::equalZero( fDividend) )
fStretch /= fDividend;
const sal_Int32 nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
aFont.SetAverageFontWidth( nNewWidth );
}
CanvasFont::ImplRef pFont(tools::canvasFontFromXFont(rCanvasFont));
if (pFont.is() && pFont->getEmphasisMark())
aFont.SetEmphasisMark(FontEmphasisMark(pFont->getEmphasisMark()));
// set font
xVirtualDevice->SetFont(aFont);
// need metrics for Y offset, the XCanvas always renders
// relative to baseline
const ::FontMetric& aMetric( xVirtualDevice->GetFontMetric() );
const sal_Int32 nAboveBaseline( -aMetric.GetInternalLeading() - aMetric.GetAscent() );
const sal_Int32 nBelowBaseline( aMetric.GetDescent() );
if( rLogicalAdvancements.getLength() )
{
return geometry::RealRectangle2D( 0, nAboveBaseline,
rLogicalAdvancements[ rLogicalAdvancements.getLength()-1 ],
nBelowBaseline );
}
else
{
return geometry::RealRectangle2D( 0, nAboveBaseline,
xVirtualDevice->GetTextWidth(
rText.Text,
::canvas::tools::numeric_cast<sal_uInt16>(rText.StartPosition),
::canvas::tools::numeric_cast<sal_uInt16>(rText.Length) ),
nBelowBaseline );
}
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|