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::Reference<
72 css::rendering::XCanvasFont >& rCanvasFont,
73 const css::geometry::Matrix2D& rFontMatrix,
74 bool bAlphaSurface,
75 bool bIsRTL)
76 {
77 HDC hdc = rGraphics->GetHDC();
78
79 // issue a ReleaseHDC() when leaving the scope
80 const ::comphelper::ScopeGuard aGuard(
81 [&rGraphics, &hdc]() mutable { rGraphics->ReleaseHDC(hdc); } );
82
83 SystemGraphicsData aSystemGraphicsData;
84 aSystemGraphicsData.nSize = sizeof(SystemGraphicsData);
85 aSystemGraphicsData.hDC = reinterpret_cast< ::HDC >(hdc);
86 ScopedVclPtrInstance<VirtualDevice> xVirtualDevice(aSystemGraphicsData, Size(1, 1), DeviceFormat::WITHOUT_ALPHA);
87
88 // disable font antialiasing - GDI does not handle alpha
89 // surfaces properly.
90 if( bAlphaSurface )
91 xVirtualDevice->SetAntialiasing(AntialiasingFlags::DisableText);
92
93 if(rText.Length)
94 {
95 bool test = mxGraphicDevice.is();
97 "TextLayoutDrawHelper::drawText(): Invalid GraphicDevice" );
98
99 // set text color. Make sure to remove transparence part first.
100 Color aColor( COL_WHITE );
101
102 if( rRenderState.DeviceColor.getLength() > 2 )
104 rRenderState.DeviceColor,
105 mxGraphicDevice->getDeviceColorSpace());
106 aColor.SetAlpha(255);
107 xVirtualDevice->SetTextColor(aColor);
108
109 // create the font
110 const css::rendering::FontRequest& rFontRequest = rCanvasFont->getFontRequest();
111 vcl::Font aFont(
112 rFontRequest.FontDescription.FamilyName,
113 rFontRequest.FontDescription.StyleName,
114 Size( 0, ::basegfx::fround(rFontRequest.CellSize)));
115
117 aFont.SetCharSet( (rFontRequest.FontDescription.IsSymbolFont==css::util::TriState_YES) ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE );
118 aFont.SetVertical( rFontRequest.FontDescription.IsVertical==css::util::TriState_YES );
119 aFont.SetWeight( static_cast<FontWeight>(rFontRequest.FontDescription.FontDescription.Weight) );
120 aFont.SetItalic( (rFontRequest.FontDescription.FontDescription.Letterform<=8) ? ITALIC_NONE : ITALIC_NORMAL );
121 aFont.SetPitch(
122 rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
124
125 aFont.SetLanguage(LanguageTag::convertToLanguageType(rFontRequest.Locale));
126
127 // setup font color
128 aFont.SetColor( aColor );
129 aFont.SetFillColor( aColor );
130
132 if (pFont.is() && pFont->getEmphasisMark())
133 aFont.SetEmphasisMark(FontEmphasisMark(pFont->getEmphasisMark()));
134
135 // adjust to stretched font
136 if(!::rtl::math::approxEqual(rFontMatrix.m00, rFontMatrix.m11))
137 {
138 const Size aSize = xVirtualDevice->GetFontMetric( aFont ).GetFontSize();
139 const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
140 double fStretch = rFontMatrix.m00 + rFontMatrix.m01;
141
142 if( !::basegfx::fTools::equalZero( fDividend) )
143 fStretch /= fDividend;
144
145 const sal_Int32 nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
146
147 aFont.SetAverageFontWidth( nNewWidth );
148 }
149
150 // set font
151 xVirtualDevice->SetFont(aFont);
152
153 // create world transformation matrix
154 ::basegfx::B2DHomMatrix aWorldTransform;
155 ::canvas::tools::mergeViewAndRenderTransform(aWorldTransform, rViewState, rRenderState);
156
157 if(!rOutputOffset.equalZero())
158 {
159 aWorldTransform.translate(rOutputOffset.getX(), rOutputOffset.getY());
160 }
161
162 // set ViewState clipping
163 if(rViewState.Clip.is())
164 {
167 ::basegfx::unotools::homMatrixFromAffineMatrix(aMatrix, rViewState.AffineTransform );
168
169 if(!rOutputOffset.equalZero())
170 {
171 aMatrix.translate(rOutputOffset.getX(), rOutputOffset.getY());
172 }
173
174 aClipPoly.transform(aMatrix);
175 const vcl::Region& rClipRegion = vcl::Region(::tools::PolyPolygon(aClipPoly));
176 xVirtualDevice->IntersectClipRegion(rClipRegion);
177 }
178
179 if(rRenderState.Clip.is())
180 {
182 aClipPoly.transform(aWorldTransform);
183 const vcl::Region& rClipRegion = vcl::Region(::tools::PolyPolygon(aClipPoly));
184 xVirtualDevice->IntersectClipRegion(rClipRegion);
185 }
186
187 // set world transform
188 XFORM aXForm;
189 aXForm.eM11 = static_cast<FLOAT>(aWorldTransform.get(0, 0));
190 aXForm.eM12 = static_cast<FLOAT>(aWorldTransform.get(1, 0));
191 aXForm.eM21 = static_cast<FLOAT>(aWorldTransform.get(0, 1));
192 aXForm.eM22 = static_cast<FLOAT>(aWorldTransform.get(1, 1));
193 aXForm.eDx = static_cast<FLOAT>(aWorldTransform.get(0, 2));
194 aXForm.eDy = static_cast<FLOAT>(aWorldTransform.get(1, 2));
195
196 // TODO(F3): This is NOT supported on 95/98/ME!
197 SetGraphicsMode(hdc, GM_ADVANCED);
198 SetTextAlign(hdc, TA_BASELINE);
199 SetWorldTransform(hdc, &aXForm);
200
201 // use an empty StartPosition for text rendering
202 const Point aEmptyPoint(0, 0);
203
204 // create the String
205 const OUString aText(rText.Text);
206
207 if( rLogicalAdvancements.getLength() )
208 {
209 // create the DXArray
210 const sal_Int32 nLen( rLogicalAdvancements.getLength() );
211 KernArray DXArray;
212 DXArray.reserve(nLen);
213 for( sal_Int32 i=0; i<nLen; ++i )
214 DXArray.push_back(basegfx::fround(rLogicalAdvancements[i]));
215
216 // draw the String
217 xVirtualDevice->DrawTextArray( aEmptyPoint,
218 aText,
219 DXArray,
220 {},
221 rText.StartPosition,
222 rText.Length,
223 bIsRTL ? SalLayoutFlags::BiDiRtl : SalLayoutFlags::NONE);
224 }
225 else
226 {
227 // draw the String
228 xVirtualDevice->DrawText( aEmptyPoint,
229 aText,
230 rText.StartPosition,
231 rText.Length );
232 }
233 }
234 }
235
236 geometry::RealRectangle2D TextLayoutDrawHelper::queryTextBounds( const rendering::StringContext& rText,
237 const uno::Sequence< double >& rLogicalAdvancements,
238 const uno::Reference< rendering::XCanvasFont >& rCanvasFont,
239 const geometry::Matrix2D& rFontMatrix )
240 {
241 if(!(rText.Length))
242 return geometry::RealRectangle2D();
243
244 // TODO(F1): Fetching default screen DC here, will yield wrong
245 // metrics when e.g. formatting for a printer!
246 SystemGraphicsData aSystemGraphicsData;
247 aSystemGraphicsData.nSize = sizeof(SystemGraphicsData);
248 aSystemGraphicsData.hDC = reinterpret_cast< ::HDC >(GetDC( nullptr ));
249 ScopedVclPtrInstance<VirtualDevice> xVirtualDevice(aSystemGraphicsData, Size(1, 1), DeviceFormat::WITHOUT_ALPHA);
250
251 // create the font
252 const css::rendering::FontRequest& rFontRequest = rCanvasFont->getFontRequest();
253 vcl::Font aFont(
254 rFontRequest.FontDescription.FamilyName,
255 rFontRequest.FontDescription.StyleName,
256 Size( 0, ::basegfx::fround(rFontRequest.CellSize)));
257
259 aFont.SetCharSet( (rFontRequest.FontDescription.IsSymbolFont==css::util::TriState_YES) ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE );
260 aFont.SetVertical( rFontRequest.FontDescription.IsVertical==css::util::TriState_YES );
261 aFont.SetWeight( static_cast<FontWeight>(rFontRequest.FontDescription.FontDescription.Weight) );
262 aFont.SetItalic( (rFontRequest.FontDescription.FontDescription.Letterform<=8) ? ITALIC_NONE : ITALIC_NORMAL );
263 aFont.SetPitch(
264 rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
266
267 // adjust to stretched font
268 if(!::rtl::math::approxEqual(rFontMatrix.m00, rFontMatrix.m11))
269 {
270 const Size aSize = xVirtualDevice->GetFontMetric( aFont ).GetFontSize();
271 const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
272 double fStretch = rFontMatrix.m00 + rFontMatrix.m01;
273
274 if( !::basegfx::fTools::equalZero( fDividend) )
275 fStretch /= fDividend;
276
277 const sal_Int32 nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
278
279 aFont.SetAverageFontWidth( nNewWidth );
280 }
281
283 if (pFont.is() && pFont->getEmphasisMark())
284 aFont.SetEmphasisMark(FontEmphasisMark(pFont->getEmphasisMark()));
285
286 // set font
287 xVirtualDevice->SetFont(aFont);
288
289 // need metrics for Y offset, the XCanvas always renders
290 // relative to baseline
291 const ::FontMetric& aMetric( xVirtualDevice->GetFontMetric() );
292
293 const sal_Int32 nAboveBaseline( -aMetric.GetInternalLeading() - aMetric.GetAscent() );
294 const sal_Int32 nBelowBaseline( aMetric.GetDescent() );
295
296 if( rLogicalAdvancements.getLength() )
297 {
298 return geometry::RealRectangle2D( 0, nAboveBaseline,
299 rLogicalAdvancements[ rLogicalAdvancements.getLength()-1 ],
300 nBelowBaseline );
301 }
302 else
303 {
304 return geometry::RealRectangle2D( 0, nAboveBaseline,
305 xVirtualDevice->GetTextWidth(
306 rText.Text,
307 ::canvas::tools::numeric_cast<sal_uInt16>(rText.StartPosition),
308 ::canvas::tools::numeric_cast<sal_uInt16>(rText.Length) ),
309 nBelowBaseline );
310 }
311 }
312}
313
314/* 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::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)