LibreOffice Module canvas (master) 1
dx_textlayout_drawhelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/config.h>
21
22#include <memory>
23
27#include <com/sun/star/rendering/FontRequest.hpp>
28#include <com/sun/star/rendering/PanoseProportion.hpp>
29#include <com/sun/star/rendering/XCanvasFont.hpp>
32#include <rtl/math.hxx>
33#include <tools/color.hxx>
35#include <tools/poly.hxx>
36#include <vcl/canvastools.hxx>
37#include <vcl/kernarray.hxx>
38#include <vcl/metric.hxx>
39#include <vcl/sysdata.hxx>
40#include <vcl/virdev.hxx>
41
43
44#include "dx_bitmap.hxx"
45#include "dx_canvasfont.hxx"
46#include "dx_impltools.hxx"
48
49using namespace ::com::sun::star;
50
51
52namespace dxcanvas
53{
55 const uno::Reference< rendering::XGraphicDevice >& xGraphicDevice ) :
56 mxGraphicDevice(xGraphicDevice)
57 {
58 }
59
61 {
62 }
63
65 const std::shared_ptr<Gdiplus::Graphics>& rGraphics,
66 const css::rendering::ViewState& rViewState,
67 const css::rendering::RenderState& rRenderState,
68 const ::basegfx::B2ISize& rOutputOffset,
69 const css::rendering::StringContext& rText,
70 const css::uno::Sequence< double >& rLogicalAdvancements,
71 const css::uno::Sequence< sal_Bool >& rKashidaPositions,
72 const css::uno::Reference<
73 css::rendering::XCanvasFont >& rCanvasFont,
74 const css::geometry::Matrix2D& rFontMatrix,
75 bool bAlphaSurface,
76 bool bIsRTL)
77 {
78 HDC hdc = rGraphics->GetHDC();
79
80 // issue a ReleaseHDC() when leaving the scope
81 const ::comphelper::ScopeGuard aGuard(
82 [&rGraphics, &hdc]() mutable { rGraphics->ReleaseHDC(hdc); } );
83
84 SystemGraphicsData aSystemGraphicsData;
85 aSystemGraphicsData.nSize = sizeof(SystemGraphicsData);
86 aSystemGraphicsData.hDC = reinterpret_cast< ::HDC >(hdc);
87 ScopedVclPtrInstance<VirtualDevice> xVirtualDevice(aSystemGraphicsData, Size(1, 1), DeviceFormat::WITHOUT_ALPHA);
88
89 // disable font antialiasing - GDI does not handle alpha
90 // surfaces properly.
91 if( bAlphaSurface )
92 xVirtualDevice->SetAntialiasing(AntialiasingFlags::DisableText);
93
94 if(rText.Length)
95 {
96 bool test = mxGraphicDevice.is();
98 "TextLayoutDrawHelper::drawText(): Invalid GraphicDevice" );
99
100 // set text color. Make sure to remove transparence part first.
101 Color aColor( COL_WHITE );
102
103 if( rRenderState.DeviceColor.getLength() > 2 )
105 rRenderState.DeviceColor,
106 mxGraphicDevice->getDeviceColorSpace());
107 aColor.SetAlpha(255);
108 xVirtualDevice->SetTextColor(aColor);
109
110 // create the font
111 const css::rendering::FontRequest& rFontRequest = rCanvasFont->getFontRequest();
112 vcl::Font aFont(
113 rFontRequest.FontDescription.FamilyName,
114 rFontRequest.FontDescription.StyleName,
115 Size( 0, ::basegfx::fround(rFontRequest.CellSize)));
116
118 aFont.SetCharSet( (rFontRequest.FontDescription.IsSymbolFont==css::util::TriState_YES) ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE );
119 aFont.SetVertical( rFontRequest.FontDescription.IsVertical==css::util::TriState_YES );
120 aFont.SetWeight( static_cast<FontWeight>(rFontRequest.FontDescription.FontDescription.Weight) );
121 aFont.SetItalic( (rFontRequest.FontDescription.FontDescription.Letterform<=8) ? ITALIC_NONE : ITALIC_NORMAL );
122 aFont.SetPitch(
123 rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
125
126 aFont.SetLanguage(LanguageTag::convertToLanguageType(rFontRequest.Locale));
127
128 // setup font color
129 aFont.SetColor( aColor );
130 aFont.SetFillColor( aColor );
131
133 if (pFont.is() && pFont->getEmphasisMark())
134 aFont.SetEmphasisMark(FontEmphasisMark(pFont->getEmphasisMark()));
135
136 // adjust to stretched font
137 if(!::rtl::math::approxEqual(rFontMatrix.m00, rFontMatrix.m11))
138 {
139 const Size aSize = xVirtualDevice->GetFontMetric( aFont ).GetFontSize();
140 const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
141 double fStretch = rFontMatrix.m00 + rFontMatrix.m01;
142
143 if( !::basegfx::fTools::equalZero( fDividend) )
144 fStretch /= fDividend;
145
146 const sal_Int32 nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
147
148 aFont.SetAverageFontWidth( nNewWidth );
149 }
150
151 // set font
152 xVirtualDevice->SetFont(aFont);
153
154 // create world transformation matrix
155 ::basegfx::B2DHomMatrix aWorldTransform;
156 ::canvas::tools::mergeViewAndRenderTransform(aWorldTransform, rViewState, rRenderState);
157
158 if(!rOutputOffset.equalZero())
159 {
160 aWorldTransform.translate(rOutputOffset.getWidth(), rOutputOffset.getHeight());
161 }
162
163 // set ViewState clipping
164 if(rViewState.Clip.is())
165 {
168 ::basegfx::unotools::homMatrixFromAffineMatrix(aMatrix, rViewState.AffineTransform );
169
170 if(!rOutputOffset.equalZero())
171 {
172 aMatrix.translate(rOutputOffset.getWidth(), rOutputOffset.getHeight());
173 }
174
175 aClipPoly.transform(aMatrix);
176 const vcl::Region& rClipRegion = vcl::Region(::tools::PolyPolygon(aClipPoly));
177 xVirtualDevice->IntersectClipRegion(rClipRegion);
178 }
179
180 if(rRenderState.Clip.is())
181 {
183 aClipPoly.transform(aWorldTransform);
184 const vcl::Region& rClipRegion = vcl::Region(::tools::PolyPolygon(aClipPoly));
185 xVirtualDevice->IntersectClipRegion(rClipRegion);
186 }
187
188 // set world transform
189 XFORM aXForm;
190 aXForm.eM11 = static_cast<FLOAT>(aWorldTransform.get(0, 0));
191 aXForm.eM12 = static_cast<FLOAT>(aWorldTransform.get(1, 0));
192 aXForm.eM21 = static_cast<FLOAT>(aWorldTransform.get(0, 1));
193 aXForm.eM22 = static_cast<FLOAT>(aWorldTransform.get(1, 1));
194 aXForm.eDx = static_cast<FLOAT>(aWorldTransform.get(0, 2));
195 aXForm.eDy = static_cast<FLOAT>(aWorldTransform.get(1, 2));
196
197 // TODO(F3): This is NOT supported on 95/98/ME!
198 SetGraphicsMode(hdc, GM_ADVANCED);
199 SetTextAlign(hdc, TA_BASELINE);
200 SetWorldTransform(hdc, &aXForm);
201
202 // use an empty StartPosition for text rendering
203 const Point aEmptyPoint(0, 0);
204
205 // create the String
206 const OUString aText(rText.Text);
207
208 if( rLogicalAdvancements.getLength() )
209 {
210 // create the DXArray
211 const sal_Int32 nLen( rLogicalAdvancements.getLength() );
212 KernArray DXArray;
213 DXArray.reserve(nLen);
214 for( sal_Int32 i=0; i<nLen; ++i )
215 DXArray.push_back(basegfx::fround(rLogicalAdvancements[i]));
216
217 o3tl::span<const sal_Bool> aKashidaArray(rKashidaPositions.getConstArray(), rKashidaPositions.getLength());
218
219 // draw the String
220 xVirtualDevice->DrawTextArray( aEmptyPoint,
221 aText,
222 DXArray,
223 aKashidaArray,
224 rText.StartPosition,
225 rText.Length,
226 bIsRTL ? SalLayoutFlags::BiDiRtl : SalLayoutFlags::NONE);
227 }
228 else
229 {
230 // draw the String
231 xVirtualDevice->DrawText( aEmptyPoint,
232 aText,
233 rText.StartPosition,
234 rText.Length );
235 }
236 }
237 }
238
239 geometry::RealRectangle2D TextLayoutDrawHelper::queryTextBounds( const rendering::StringContext& rText,
240 const uno::Sequence< double >& rLogicalAdvancements,
241 const uno::Reference< rendering::XCanvasFont >& rCanvasFont,
242 const geometry::Matrix2D& rFontMatrix )
243 {
244 if(!(rText.Length))
245 return geometry::RealRectangle2D();
246
247 // TODO(F1): Fetching default screen DC here, will yield wrong
248 // metrics when e.g. formatting for a printer!
249 SystemGraphicsData aSystemGraphicsData;
250 aSystemGraphicsData.nSize = sizeof(SystemGraphicsData);
251 aSystemGraphicsData.hDC = reinterpret_cast< ::HDC >(GetDC( nullptr ));
252 ScopedVclPtrInstance<VirtualDevice> xVirtualDevice(aSystemGraphicsData, Size(1, 1), DeviceFormat::WITHOUT_ALPHA);
253
254 // create the font
255 const css::rendering::FontRequest& rFontRequest = rCanvasFont->getFontRequest();
256 vcl::Font aFont(
257 rFontRequest.FontDescription.FamilyName,
258 rFontRequest.FontDescription.StyleName,
259 Size( 0, ::basegfx::fround(rFontRequest.CellSize)));
260
262 aFont.SetCharSet( (rFontRequest.FontDescription.IsSymbolFont==css::util::TriState_YES) ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE );
263 aFont.SetVertical( rFontRequest.FontDescription.IsVertical==css::util::TriState_YES );
264 aFont.SetWeight( static_cast<FontWeight>(rFontRequest.FontDescription.FontDescription.Weight) );
265 aFont.SetItalic( (rFontRequest.FontDescription.FontDescription.Letterform<=8) ? ITALIC_NONE : ITALIC_NORMAL );
266 aFont.SetPitch(
267 rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
269
270 // adjust to stretched font
271 if(!::rtl::math::approxEqual(rFontMatrix.m00, rFontMatrix.m11))
272 {
273 const Size aSize = xVirtualDevice->GetFontMetric( aFont ).GetFontSize();
274 const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
275 double fStretch = rFontMatrix.m00 + rFontMatrix.m01;
276
277 if( !::basegfx::fTools::equalZero( fDividend) )
278 fStretch /= fDividend;
279
280 const sal_Int32 nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
281
282 aFont.SetAverageFontWidth( nNewWidth );
283 }
284
286 if (pFont.is() && pFont->getEmphasisMark())
287 aFont.SetEmphasisMark(FontEmphasisMark(pFont->getEmphasisMark()));
288
289 // set font
290 xVirtualDevice->SetFont(aFont);
291
292 // need metrics for Y offset, the XCanvas always renders
293 // relative to baseline
294 const ::FontMetric& aMetric( xVirtualDevice->GetFontMetric() );
295
296 const sal_Int32 nAboveBaseline( -aMetric.GetInternalLeading() - aMetric.GetAscent() );
297 const sal_Int32 nBelowBaseline( aMetric.GetDescent() );
298
299 if( rLogicalAdvancements.getLength() )
300 {
301 return geometry::RealRectangle2D( 0, nAboveBaseline,
302 rLogicalAdvancements[ rLogicalAdvancements.getLength()-1 ],
303 nBelowBaseline );
304 }
305 else
306 {
307 return geometry::RealRectangle2D( 0, nAboveBaseline,
308 xVirtualDevice->GetTextWidth(
309 rText.Text,
310 ::canvas::tools::numeric_cast<sal_uInt16>(rText.StartPosition),
311 ::canvas::tools::numeric_cast<sal_uInt16>(rText.Length) ),
312 nBelowBaseline );
313 }
314 }
315}
316
317/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetAlpha(sal_uInt8 nAlpha)
void reserve(size_t nCapacity)
void push_back(sal_Int32 nUnit)
static LanguageType convertToLanguageType(const css::lang::Locale &rLocale, bool bResolveSystem=true)
constexpr tools::Long Width() const
void translate(double fX, double fY)
double get(sal_uInt16 nRow, sal_uInt16 nColumn) const
void transform(const basegfx::B2DHomMatrix &rMatrix)
css::uno::Reference< css::rendering::XGraphicDevice > mxGraphicDevice
css::geometry::RealRectangle2D queryTextBounds(const css::rendering::StringContext &rText, const css::uno::Sequence< double > &rLogicalAdvancements, const css::uno::Reference< css::rendering::XCanvasFont > &rCanvasFont, const css::geometry::Matrix2D &rFontMatrix)
TextLayoutDrawHelper(const css::uno::Reference< css::rendering::XGraphicDevice > &xGraphicDevice)
void 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::Sequence< sal_Bool > &rKashidaPositions, const css::uno::Reference< css::rendering::XCanvasFont > &rCanvasFont, const css::geometry::Matrix2D &rFontMatrix, bool bAlphaSurface, bool bIsRTL)
void SetVertical(bool bVertical)
void SetAverageFontWidth(tools::Long nWidth)
void SetPitch(FontPitch ePitch)
void SetFillColor(const Color &)
void SetColor(const Color &)
void SetItalic(FontItalic)
void SetWeight(FontWeight)
void SetCharSet(rtl_TextEncoding)
void SetAlignment(TextAlign)
void SetLanguage(LanguageType)
void SetEmphasisMark(FontEmphasisMark)
constexpr ::Color COL_WHITE(0xFF, 0xFF, 0xFF)
#define ENSURE_OR_THROW(c, m)
PITCH_VARIABLE
PITCH_FIXED
ITALIC_NORMAL
ITALIC_NONE
FontEmphasisMark
ALIGN_BASELINE
B2IRange fround(const B2DRange &rRange)
::basegfx::B2DHomMatrix & mergeViewAndRenderTransform(::basegfx::B2DHomMatrix &combinedTransform, const rendering::ViewState &viewState, const rendering::RenderState &renderState)
CanvasFont::ImplRef canvasFontFromXFont(const uno::Reference< rendering::XCanvasFont > &xFont)
::basegfx::B2DPolyPolygon polyPolygonFromXPolyPolygon2D(const uno::Reference< rendering::XPolyPolygon2D > &xPoly)
TA_BASELINE
int i
FontWeight
Color doubleSequenceToColor(const uno::Sequence< double > &rColor, const uno::Reference< rendering::XColorSpace > &xColorSpace)