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