LibreOffice Module canvas (master)  1
cairo_canvashelper_text.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 <com/sun/star/rendering/TextDirection.hpp>
23 
24 #include <rtl/math.hxx>
26 #include <tools/diagnose_ex.h>
27 #include <vcl/canvastools.hxx>
28 #include <vcl/metric.hxx>
29 #include <vcl/virdev.hxx>
30 
31 #include <canvas/canvastools.hxx>
32 #include <verifyinput.hxx>
33 
34 #include "cairo_canvasfont.hxx"
35 #include "cairo_canvashelper.hxx"
36 #include "cairo_textlayout.hxx"
37 
38 using namespace ::cairo;
39 using namespace ::com::sun::star;
40 
41 namespace cairocanvas
42 {
43  uno::Reference< rendering::XCanvasFont > CanvasHelper::createFont( const rendering::XCanvas* ,
44  const rendering::FontRequest& fontRequest,
45  const uno::Sequence< beans::PropertyValue >& extraFontProperties,
46  const geometry::Matrix2D& fontMatrix )
47  {
48  return uno::Reference< rendering::XCanvasFont >( new CanvasFont( fontRequest, extraFontProperties, fontMatrix, mpSurfaceProvider ));
49  }
50 
51  uno::Sequence< rendering::FontInfo > CanvasHelper::queryAvailableFonts( const rendering::XCanvas* ,
52  const rendering::FontInfo& /*aFilter*/,
53  const uno::Sequence< beans::PropertyValue >& /*aFontProperties*/ )
54  {
55  // TODO
56  return uno::Sequence< rendering::FontInfo >();
57  }
58 
59  static bool
60  setupFontTransform( ::OutputDevice const & rOutDev,
61  ::Point& o_rPoint,
62  vcl::Font& io_rVCLFont,
63  const rendering::ViewState& rViewState,
64  const rendering::RenderState& rRenderState )
65  {
67 
69  rViewState,
70  rRenderState);
71 
72  ::basegfx::B2DTuple aScale;
73  ::basegfx::B2DTuple aTranslate;
74  double nRotate, nShearX;
75 
76  aMatrix.decompose( aScale, aTranslate, nRotate, nShearX );
77 
78  // query font metric _before_ tampering with width and height
79  if( !::rtl::math::approxEqual(aScale.getX(), aScale.getY()) )
80  {
81  // retrieve true font width
82  const sal_Int32 nFontWidth( rOutDev.GetFontMetric( io_rVCLFont ).GetAverageFontWidth() );
83 
84  const sal_Int32 nScaledFontWidth( ::basegfx::fround(nFontWidth * aScale.getX()) );
85 
86  if( !nScaledFontWidth )
87  {
88  // scale is smaller than one pixel - disable text
89  // output altogether
90  return false;
91  }
92 
93  io_rVCLFont.SetAverageFontWidth( nScaledFontWidth );
94  }
95 
96  if( !::rtl::math::approxEqual(aScale.getY(), 1.0) )
97  {
98  const sal_Int32 nFontHeight( io_rVCLFont.GetFontHeight() );
99  io_rVCLFont.SetFontHeight( ::basegfx::fround(nFontHeight * aScale.getY()) );
100  }
101 
102  io_rVCLFont.SetOrientation( Degree10(static_cast< sal_Int16 >( ::basegfx::fround(-fmod(nRotate, 2*M_PI)*(1800.0/M_PI)) )) );
103 
104  // TODO(F2): Missing functionality in VCL: shearing
105  o_rPoint.setX( ::basegfx::fround(aTranslate.getX()) );
106  o_rPoint.setY( ::basegfx::fround(aTranslate.getY()) );
107 
108  return true;
109  }
110 
111  static int
113  const rendering::XCanvas* pOwner,
114  const rendering::ViewState& viewState,
115  const rendering::RenderState& renderState )
116  {
117  ::canvas::tools::verifyInput( renderState,
118  __func__,
119  const_cast<rendering::XCanvas*>(pOwner), // only for refcount
120  2,
121  3 /* text */ );
122 
123  int nTransparency(0);
124 
125  // TODO(P2): Don't change clipping all the time, maintain current clip
126  // state and change only when update is necessary
127  ::canvas::tools::clipOutDev(viewState, renderState, rOutDev);
128 
129  Color aColor( COL_WHITE );
130 
131  if( renderState.DeviceColor.getLength() > 2 )
132  {
133  aColor = vcl::unotools::stdColorSpaceSequenceToColor( renderState.DeviceColor );
134  }
135 
136  // extract alpha, and make color opaque
137  // afterwards. Otherwise, OutputDevice won't draw anything
138  nTransparency = aColor.GetTransparency();
139  aColor.SetTransparency(0);
140 
141  rOutDev.SetTextColor( aColor );
142 
143  return nTransparency;
144  }
145 
146  namespace {
147 
148  class DeviceSettingsGuard
149  {
150  private:
153  public:
154  DeviceSettingsGuard(OutputDevice *pVirtualDevice)
155  : mpVirtualDevice(pVirtualDevice)
156  , mbMappingWasEnabled(mpVirtualDevice->IsMapModeEnabled())
157  {
158  mpVirtualDevice->Push();
159  mpVirtualDevice->EnableMapMode(false);
160  }
161 
162  ~DeviceSettingsGuard()
163  {
164  mpVirtualDevice->EnableMapMode(mbMappingWasEnabled);
165  mpVirtualDevice->Pop();
166  }
167  };
168 
169  }
170 
171  static bool setupTextOutput( OutputDevice& rOutDev,
172  const rendering::XCanvas* pOwner,
173  ::Point& o_rOutPos,
174  const rendering::ViewState& viewState,
175  const rendering::RenderState& renderState,
176  const uno::Reference< rendering::XCanvasFont >& xFont )
177  {
178  setupOutDevState( rOutDev, pOwner, viewState, renderState );
179 
180  CanvasFont* pFont = dynamic_cast< CanvasFont* >( xFont.get() );
181 
182  ENSURE_ARG_OR_THROW( pFont,
183  "CanvasHelper::setupTextOutput(): Font not compatible with this canvas" );
184 
185  vcl::Font aVCLFont = pFont->getVCLFont();
186 
187  Color aColor( COL_BLACK );
188 
189  if( renderState.DeviceColor.getLength() > 2 )
190  {
191  aColor = vcl::unotools::stdColorSpaceSequenceToColor(renderState.DeviceColor );
192  }
193 
194  // setup font color
195  aVCLFont.SetColor( aColor );
196  aVCLFont.SetFillColor( aColor );
197 
198  // no need to replicate this for mp2ndOutDev, we're modifying only aVCLFont here.
199  if( !setupFontTransform( rOutDev, o_rOutPos, aVCLFont, viewState, renderState ) )
200  return false;
201 
202  rOutDev.SetFont( aVCLFont );
203 
204  return true;
205  }
206 
207  uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawText( const rendering::XCanvas* pOwner,
208  const rendering::StringContext& text,
209  const uno::Reference< rendering::XCanvasFont >& xFont,
210  const rendering::ViewState& viewState,
211  const rendering::RenderState& renderState,
212  sal_Int8 textDirection )
213  {
214 #ifdef CAIRO_CANVAS_PERF_TRACE
215  struct timespec aTimer;
216  mxDevice->startPerfTrace( &aTimer );
217 #endif
218 
219  ENSURE_ARG_OR_THROW( xFont.is(),
220  "CanvasHelper::drawText(): font is NULL");
221 
222  if( !mpVirtualDevice )
223  mpVirtualDevice = mpSurface->createVirtualDevice();
224 
225  if( mpVirtualDevice )
226  {
227  DeviceSettingsGuard aGuard(mpVirtualDevice.get());
228 
229  ::Point aOutpos;
230  if( !setupTextOutput( *mpVirtualDevice, pOwner, aOutpos, viewState, renderState, xFont ) )
231  return uno::Reference< rendering::XCachedPrimitive >(nullptr); // no output necessary
232 
233  // change text direction and layout mode
234  ComplexTextLayoutFlags nLayoutMode(ComplexTextLayoutFlags::Default);
235  switch( textDirection )
236  {
237  case rendering::TextDirection::WEAK_LEFT_TO_RIGHT:
238  case rendering::TextDirection::STRONG_LEFT_TO_RIGHT:
239  nLayoutMode |= ComplexTextLayoutFlags::BiDiStrong;
240  nLayoutMode |= ComplexTextLayoutFlags::TextOriginLeft;
241  break;
242 
243  case rendering::TextDirection::WEAK_RIGHT_TO_LEFT:
244  nLayoutMode |= ComplexTextLayoutFlags::BiDiRtl;
245  [[fallthrough]];
246  case rendering::TextDirection::STRONG_RIGHT_TO_LEFT:
247  nLayoutMode |= ComplexTextLayoutFlags::BiDiRtl | ComplexTextLayoutFlags::BiDiStrong;
248  nLayoutMode |= ComplexTextLayoutFlags::TextOriginRight;
249  break;
250  }
251 
252  // TODO(F2): alpha
253  mpVirtualDevice->SetLayoutMode( nLayoutMode );
254 
255  rtl::Reference pTextLayout( new TextLayout(text, textDirection, 0, CanvasFont::Reference(dynamic_cast< CanvasFont* >( xFont.get() )), mpSurfaceProvider) );
256  pTextLayout->draw(*mpVirtualDevice, aOutpos, viewState, renderState);
257  }
258 
259  return uno::Reference< rendering::XCachedPrimitive >(nullptr);
260  }
261 
262  uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawTextLayout( const rendering::XCanvas* pOwner,
263  const uno::Reference< rendering::XTextLayout >& xLayoutedText,
264  const rendering::ViewState& viewState,
265  const rendering::RenderState& renderState )
266  {
267  ENSURE_ARG_OR_THROW( xLayoutedText.is(),
268  "CanvasHelper::drawTextLayout(): layout is NULL");
269 
270  TextLayout* pTextLayout = dynamic_cast< TextLayout* >( xLayoutedText.get() );
271 
272  if( pTextLayout )
273  {
274  if( !mpVirtualDevice )
275  mpVirtualDevice = mpSurface->createVirtualDevice();
276 
277  if( mpVirtualDevice )
278  {
279  DeviceSettingsGuard aGuard(mpVirtualDevice.get());
280 
281  // TODO(T3): Race condition. We're taking the font
282  // from xLayoutedText, and then calling draw() at it,
283  // without exclusive access. Move setupTextOutput(),
284  // e.g. to impltools?
285 
286  ::Point aOutpos;
287  if( !setupTextOutput( *mpVirtualDevice, pOwner, aOutpos, viewState, renderState, xLayoutedText->getFont() ) )
288  return uno::Reference< rendering::XCachedPrimitive >(nullptr); // no output necessary
289 
290  // TODO(F2): What about the offset scalings?
291  pTextLayout->draw(*mpVirtualDevice, aOutpos, viewState, renderState);
292  }
293  }
294  else
295  {
296  ENSURE_ARG_OR_THROW( false,
297  "CanvasHelper::drawTextLayout(): TextLayout not compatible with this canvas" );
298  }
299 
300  return uno::Reference< rendering::XCachedPrimitive >(nullptr);
301  }
302 
303 }
304 
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool mbMappingWasEnabled
void SetFillColor(const Color &)
void SetTransparency(sal_uInt8 nTransparency)
void SetAverageFontWidth(tools::Long nWidth)
::basegfx::B2DHomMatrix & mergeViewAndRenderTransform(::basegfx::B2DHomMatrix &combinedTransform, const rendering::ViewState &viewState, const rendering::RenderState &renderState)
signed char sal_Int8
vcl::Font const & getVCLFont() const
sal_uInt8 GetTransparency() const
void verifyInput(const geometry::RealPoint2D &rPoint, const char *pStr, const uno::Reference< uno::XInterface > &xIf,::sal_Int16 nArgPos)
Definition: verifyinput.cxx:51
double getX() const
double getY() const
void SetLayoutMode(ComplexTextLayoutFlags nTextLayoutMode)
void EnableMapMode(bool bEnable=true)
Color stdColorSpaceSequenceToColor(const uno::Sequence< double > &rColor)
tools::Long GetFontHeight() const
css::uno::Reference< css::rendering::XCachedPrimitive > drawTextLayout(const css::rendering::XCanvas *pCanvas, const css::uno::Reference< css::rendering::XTextLayout > &laidOutText, const css::rendering::ViewState &viewState, const css::rendering::RenderState &renderState)
rtl::Reference< CanvasFont > Reference
VclPtr< OutputDevice > mpVirtualDevice
FontMetric GetFontMetric() const
B2IRange fround(const B2DRange &rRange)
css::uno::Sequence< css::rendering::FontInfo > queryAvailableFonts(const css::rendering::XCanvas *pCanvas, const css::rendering::FontInfo &aFilter, const css::uno::Sequence< css::beans::PropertyValue > &aFontProperties)
void clipOutDev(const rendering::ViewState &viewState, const rendering::RenderState &renderState, OutputDevice &rOutDev, OutputDevice *p2ndOutDev)
bool decompose(B2DTuple &rScale, B2DTuple &rTranslate, double &rRotate, double &rShearX) const
ComplexTextLayoutFlags
void SetOrientation(Degree10 nLineOrientation)
void SetTextColor(const Color &rColor)
css::uno::Reference< css::rendering::XCanvasFont > createFont(const css::rendering::XCanvas *pCanvas, const css::rendering::FontRequest &fontRequest, const css::uno::Sequence< css::beans::PropertyValue > &extraFontProperties, const css::geometry::Matrix2D &fontMatrix)
static int setupOutDevState(OutputDevice &rOutDev, const rendering::XCanvas *pOwner, const rendering::ViewState &viewState, const rendering::RenderState &renderState)
#define ENSURE_ARG_OR_THROW(c, m)
void SetColor(const Color &)
::cairo::SurfaceSharedPtr mpSurface
SurfaceProvider * mpSurfaceProvider
Surface provider.
static bool setupFontTransform(::OutputDevice const &rOutDev,::Point &o_rPoint, vcl::Font &io_rVCLFont, const rendering::ViewState &rViewState, const rendering::RenderState &rRenderState)
void SetFont(const vcl::Font &rNewFont)
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_WHITE
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_BLACK
static bool setupTextOutput(OutputDevice &rOutDev, const rendering::XCanvas *pOwner,::Point &o_rOutPos, const rendering::ViewState &viewState, const rendering::RenderState &renderState, const uno::Reference< rendering::XCanvasFont > &xFont)
void SetFontHeight(tools::Long nHeight)
OutputDevice * get() const
void Push(PushFlags nFlags=PushFlags::ALL)
tools::Long GetAverageFontWidth() const
css::uno::Reference< css::rendering::XCachedPrimitive > drawText(const css::rendering::XCanvas *pCanvas, const css::rendering::StringContext &text, const css::uno::Reference< css::rendering::XCanvasFont > &xFont, const css::rendering::ViewState &viewState, const css::rendering::RenderState &renderState, sal_Int8 textDirection)