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 
26 #include <com/sun/star/rendering/FontRequest.hpp>
27 #include <com/sun/star/rendering/PanoseProportion.hpp>
28 #include <com/sun/star/rendering/XCanvasFont.hpp>
31 #include <rtl/math.hxx>
32 #include <tools/color.hxx>
33 #include <tools/diagnose_ex.h>
34 #include <tools/poly.hxx>
35 #include <vcl/canvastools.hxx>
36 #include <vcl/metric.hxx>
37 #include <vcl/sysdata.hxx>
38 #include <vcl/virdev.hxx>
39 
40 #include <canvas/canvastools.hxx>
41 
42 #include "dx_bitmap.hxx"
43 #include "dx_canvasfont.hxx"
44 #include "dx_impltools.hxx"
46 
47 using namespace ::com::sun::star;
48 
49 
50 namespace dxcanvas
51 {
53  const uno::Reference< rendering::XGraphicDevice >& xGraphicDevice ) :
54  mxGraphicDevice(xGraphicDevice)
55  {
56  }
57 
59  {
60  }
61 
63  const std::shared_ptr<Gdiplus::Graphics>& rGraphics,
64  const css::rendering::ViewState& rViewState,
65  const css::rendering::RenderState& rRenderState,
66  const ::basegfx::B2ISize& rOutputOffset,
67  const css::rendering::StringContext& rText,
68  const css::uno::Sequence< double >& rLogicalAdvancements,
69  const css::uno::Reference<
70  css::rendering::XCanvasFont >& rCanvasFont,
71  const css::geometry::Matrix2D& rFontMatrix,
72  bool bAlphaSurface,
73  bool bIsRTL)
74  {
75  HDC hdc = rGraphics->GetHDC();
76 
77  // issue a ReleaseHDC() when leaving the scope
78  const ::comphelper::ScopeGuard aGuard(
79  [&rGraphics, &hdc]() mutable { rGraphics->ReleaseHDC(hdc); } );
80 
81  SystemGraphicsData aSystemGraphicsData;
82  aSystemGraphicsData.nSize = sizeof(SystemGraphicsData);
83  aSystemGraphicsData.hDC = reinterpret_cast< ::HDC >(hdc);
84  ScopedVclPtrInstance<VirtualDevice> xVirtualDevice(aSystemGraphicsData, Size(1, 1), DeviceFormat::DEFAULT);
85 
86  // disable font antialiasing - GDI does not handle alpha
87  // surfaces properly.
88  if( bAlphaSurface )
89  xVirtualDevice->SetAntialiasing(AntialiasingFlags::DisableText);
90 
91  if(rText.Length)
92  {
93  bool test = mxGraphicDevice.is();
94  ENSURE_OR_THROW( test,
95  "TextLayoutDrawHelper::drawText(): Invalid GraphicDevice" );
96 
97  // set text color. Make sure to remove transparence part first.
98  Color aColor( COL_WHITE );
99 
100  if( rRenderState.DeviceColor.getLength() > 2 )
102  rRenderState.DeviceColor,
103  mxGraphicDevice->getDeviceColorSpace());
104  aColor.SetTransparency(0);
105  xVirtualDevice->SetTextColor(aColor);
106 
107  // create the font
108  const css::rendering::FontRequest& rFontRequest = rCanvasFont->getFontRequest();
109  vcl::Font aFont(
110  rFontRequest.FontDescription.FamilyName,
111  rFontRequest.FontDescription.StyleName,
112  Size( 0, ::basegfx::fround(rFontRequest.CellSize)));
113 
114  aFont.SetAlignment( ALIGN_BASELINE );
115  aFont.SetCharSet( (rFontRequest.FontDescription.IsSymbolFont==css::util::TriState_YES) ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE );
116  aFont.SetVertical( rFontRequest.FontDescription.IsVertical==css::util::TriState_YES );
117  aFont.SetWeight( static_cast<FontWeight>(rFontRequest.FontDescription.FontDescription.Weight) );
118  aFont.SetItalic( (rFontRequest.FontDescription.FontDescription.Letterform<=8) ? ITALIC_NONE : ITALIC_NORMAL );
119  aFont.SetPitch(
120  rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
122 
123  aFont.SetLanguage(LanguageTag::convertToLanguageType(rFontRequest.Locale));
124 
125  // setup font color
126  aFont.SetColor( aColor );
127  aFont.SetFillColor( aColor );
128 
130  if (pFont.is() && pFont->getEmphasisMark())
131  aFont.SetEmphasisMark(FontEmphasisMark(pFont->getEmphasisMark()));
132 
133  // adjust to stretched font
134  if(!::rtl::math::approxEqual(rFontMatrix.m00, rFontMatrix.m11))
135  {
136  const Size aSize = xVirtualDevice->GetFontMetric( aFont ).GetFontSize();
137  const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
138  double fStretch = rFontMatrix.m00 + rFontMatrix.m01;
139 
140  if( !::basegfx::fTools::equalZero( fDividend) )
141  fStretch /= fDividend;
142 
143  const sal_Int32 nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
144 
145  aFont.SetAverageFontWidth( nNewWidth );
146  }
147 
148  // set font
149  xVirtualDevice->SetFont(aFont);
150 
151  // create world transformation matrix
152  ::basegfx::B2DHomMatrix aWorldTransform;
153  ::canvas::tools::mergeViewAndRenderTransform(aWorldTransform, rViewState, rRenderState);
154 
155  if(!rOutputOffset.equalZero())
156  {
157  aWorldTransform.translate(rOutputOffset.getX(), rOutputOffset.getY());
158  }
159 
160  // set ViewState clipping
161  if(rViewState.Clip.is())
162  {
164  ::basegfx::B2DHomMatrix aMatrix;
165  ::basegfx::unotools::homMatrixFromAffineMatrix(aMatrix, rViewState.AffineTransform );
166 
167  if(!rOutputOffset.equalZero())
168  {
169  aMatrix.translate(rOutputOffset.getX(), rOutputOffset.getY());
170  }
171 
172  aClipPoly.transform(aMatrix);
173  const vcl::Region& rClipRegion = vcl::Region(::tools::PolyPolygon(aClipPoly));
174  xVirtualDevice->IntersectClipRegion(rClipRegion);
175  }
176 
177  if(rRenderState.Clip.is())
178  {
180  aClipPoly.transform(aWorldTransform);
181  const vcl::Region& rClipRegion = vcl::Region(::tools::PolyPolygon(aClipPoly));
182  xVirtualDevice->IntersectClipRegion(rClipRegion);
183  }
184 
185  // set world transform
186  XFORM aXForm;
187  aXForm.eM11 = static_cast<FLOAT>(aWorldTransform.get(0, 0));
188  aXForm.eM12 = static_cast<FLOAT>(aWorldTransform.get(1, 0));
189  aXForm.eM21 = static_cast<FLOAT>(aWorldTransform.get(0, 1));
190  aXForm.eM22 = static_cast<FLOAT>(aWorldTransform.get(1, 1));
191  aXForm.eDx = static_cast<FLOAT>(aWorldTransform.get(0, 2));
192  aXForm.eDy = static_cast<FLOAT>(aWorldTransform.get(1, 2));
193 
194  // TODO(F3): This is NOT supported on 95/98/ME!
195  SetGraphicsMode(hdc, GM_ADVANCED);
196  SetTextAlign(hdc, TA_BASELINE);
197  SetWorldTransform(hdc, &aXForm);
198 
199  // use an empty StartPosition for text rendering
200  const Point aEmptyPoint(0, 0);
201 
202  // create the String
203  const OUString aText(rText.Text);
204 
205  if( rLogicalAdvancements.getLength() )
206  {
207  // create the DXArray
208  const sal_Int32 nLen( rLogicalAdvancements.getLength() );
209  std::unique_ptr<sal_Int32[]> pDXArray( new sal_Int32[nLen] );
210  for( sal_Int32 i=0; i<nLen; ++i )
211  pDXArray[i] = basegfx::fround( rLogicalAdvancements[i] );
212 
213  // draw the String
214  xVirtualDevice->DrawTextArray( aEmptyPoint,
215  aText,
216  pDXArray.get(),
217  rText.StartPosition,
218  rText.Length,
219  bIsRTL ? SalLayoutFlags::BiDiRtl : SalLayoutFlags::NONE);
220  }
221  else
222  {
223  // draw the String
224  xVirtualDevice->DrawText( aEmptyPoint,
225  aText,
226  rText.StartPosition,
227  rText.Length );
228  }
229  }
230  }
231 
232  geometry::RealRectangle2D TextLayoutDrawHelper::queryTextBounds( const rendering::StringContext& rText,
233  const uno::Sequence< double >& rLogicalAdvancements,
234  const uno::Reference< rendering::XCanvasFont >& rCanvasFont,
235  const geometry::Matrix2D& rFontMatrix )
236  {
237  if(!(rText.Length))
238  return geometry::RealRectangle2D();
239 
240  // TODO(F1): Fetching default screen DC here, will yield wrong
241  // metrics when e.g. formatting for a printer!
242  SystemGraphicsData aSystemGraphicsData;
243  aSystemGraphicsData.nSize = sizeof(SystemGraphicsData);
244  aSystemGraphicsData.hDC = reinterpret_cast< ::HDC >(GetDC( nullptr ));
245  ScopedVclPtrInstance<VirtualDevice> xVirtualDevice(aSystemGraphicsData, Size(1, 1), DeviceFormat::DEFAULT);
246 
247  // create the font
248  const css::rendering::FontRequest& rFontRequest = rCanvasFont->getFontRequest();
249  vcl::Font aFont(
250  rFontRequest.FontDescription.FamilyName,
251  rFontRequest.FontDescription.StyleName,
252  Size( 0, ::basegfx::fround(rFontRequest.CellSize)));
253 
254  aFont.SetAlignment( ALIGN_BASELINE );
255  aFont.SetCharSet( (rFontRequest.FontDescription.IsSymbolFont==css::util::TriState_YES) ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE );
256  aFont.SetVertical( rFontRequest.FontDescription.IsVertical==css::util::TriState_YES );
257  aFont.SetWeight( static_cast<FontWeight>(rFontRequest.FontDescription.FontDescription.Weight) );
258  aFont.SetItalic( (rFontRequest.FontDescription.FontDescription.Letterform<=8) ? ITALIC_NONE : ITALIC_NORMAL );
259  aFont.SetPitch(
260  rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
262 
263  // adjust to stretched font
264  if(!::rtl::math::approxEqual(rFontMatrix.m00, rFontMatrix.m11))
265  {
266  const Size aSize = xVirtualDevice->GetFontMetric( aFont ).GetFontSize();
267  const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
268  double fStretch = rFontMatrix.m00 + rFontMatrix.m01;
269 
270  if( !::basegfx::fTools::equalZero( fDividend) )
271  fStretch /= fDividend;
272 
273  const sal_Int32 nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
274 
275  aFont.SetAverageFontWidth( nNewWidth );
276  }
277 
279  if (pFont.is() && pFont->getEmphasisMark())
280  aFont.SetEmphasisMark(FontEmphasisMark(pFont->getEmphasisMark()));
281 
282  // set font
283  xVirtualDevice->SetFont(aFont);
284 
285  // need metrics for Y offset, the XCanvas always renders
286  // relative to baseline
287  const ::FontMetric& aMetric( xVirtualDevice->GetFontMetric() );
288 
289  const sal_Int32 nAboveBaseline( -aMetric.GetInternalLeading() - aMetric.GetAscent() );
290  const sal_Int32 nBelowBaseline( aMetric.GetDescent() );
291 
292  if( rLogicalAdvancements.getLength() )
293  {
294  return geometry::RealRectangle2D( 0, nAboveBaseline,
295  rLogicalAdvancements[ rLogicalAdvancements.getLength()-1 ],
296  nBelowBaseline );
297  }
298  else
299  {
300  return geometry::RealRectangle2D( 0, nAboveBaseline,
301  xVirtualDevice->GetTextWidth(
302  rText.Text,
303  ::canvas::tools::numeric_cast<sal_uInt16>(rText.StartPosition),
304  ::canvas::tools::numeric_cast<sal_uInt16>(rText.Length) ),
305  nBelowBaseline );
306  }
307  }
308 }
309 
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
void SetFillColor(const Color &)
#define TA_BASELINE
void SetAverageFontWidth(long nWidth)
void SetTransparency(sal_uInt8 nTransparency)
::basegfx::B2DHomMatrix & mergeViewAndRenderTransform(::basegfx::B2DHomMatrix &combinedTransform, const rendering::ViewState &viewState, const rendering::RenderState &renderState)
static LanguageType convertToLanguageType(const css::lang::Locale &rLocale, bool bResolveSystem=true)
FontEmphasisMark
void SetWeight(FontWeight)
void SetCharSet(rtl_TextEncoding)
double get(sal_uInt16 nRow, sal_uInt16 nColumn) const
::basegfx::B2DPolyPolygon polyPolygonFromXPolyPolygon2D(const uno::Reference< rendering::XPolyPolygon2D > &xPoly)
PITCH_VARIABLE
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)
B2IRange fround(const B2DRange &rRange)
void SetLanguage(LanguageType)
void SetPitch(FontPitch ePitch)
void SetAlignment(FontAlign)
css::uno::Reference< css::rendering::XGraphicDevice > mxGraphicDevice
ALIGN_BASELINE
int i
TextLayoutDrawHelper(const css::uno::Reference< css::rendering::XGraphicDevice > &xGraphicDevice)
PITCH_FIXED
static bool equalZero(const double &rfVal)
Color doubleSequenceToColor(const uno::Sequence< double > &rColor, const uno::Reference< rendering::XColorSpace > &xColorSpace)
void transform(const basegfx::B2DHomMatrix &rMatrix)
#define GM_ADVANCED
ITALIC_NONE
void SetColor(const Color &)
#define ENSURE_OR_THROW(c, m)
void SetEmphasisMark(FontEmphasisMark)
CanvasFont::ImplRef canvasFontFromXFont(const uno::Reference< rendering::XCanvasFont > &xFont)
ITALIC_NORMAL
constexpr::Color COL_WHITE(0xFF, 0xFF, 0xFF)
void translate(double fX, double fY)
void SetVertical(bool bVertical)
void SetItalic(FontItalic)
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)