LibreOffice Module canvas (master)  1
cairo_textlayout.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 #include <sal/log.hxx>
22 
23 #include <math.h>
24 #include <memory>
25 
26 #include <com/sun/star/rendering/TextDirection.hpp>
27 #include <canvas/canvastools.hxx>
31 #include <tools/diagnose_ex.h>
32 #include <vcl/metric.hxx>
33 #include <vcl/virdev.hxx>
34 
35 
36 #include "cairo_textlayout.hxx"
37 
38 using namespace ::cairo;
39 using namespace ::com::sun::star;
40 
41 namespace cairocanvas
42 {
43  namespace
44  {
45  void setupLayoutMode( OutputDevice& rOutDev,
46  sal_Int8 nTextDirection )
47  {
48  // TODO(P3): avoid if already correctly set
49  ComplexTextLayoutFlags nLayoutMode = ComplexTextLayoutFlags::Default;
50  switch( nTextDirection )
51  {
52  case rendering::TextDirection::WEAK_LEFT_TO_RIGHT:
53  break;
54  case rendering::TextDirection::STRONG_LEFT_TO_RIGHT:
55  nLayoutMode = ComplexTextLayoutFlags::BiDiStrong;
56  break;
57  case rendering::TextDirection::WEAK_RIGHT_TO_LEFT:
58  nLayoutMode = ComplexTextLayoutFlags::BiDiRtl;
59  break;
60  case rendering::TextDirection::STRONG_RIGHT_TO_LEFT:
61  nLayoutMode = ComplexTextLayoutFlags::BiDiRtl | ComplexTextLayoutFlags::BiDiStrong;
62  break;
63  default:
64  break;
65  }
66 
67  // set calculated layout mode. Origin is always the left edge,
68  // as required at the API spec
69  rOutDev.SetLayoutMode( nLayoutMode | ComplexTextLayoutFlags::TextOriginLeft );
70  }
71  }
72 
73  TextLayout::TextLayout( const rendering::StringContext& aText,
74  sal_Int8 nDirection,
75  sal_Int64 /*nRandomSeed*/,
76  const CanvasFont::Reference& rFont,
77  const SurfaceProviderRef& rRefDevice ) :
79  maText( aText ),
80  maLogicalAdvancements(),
81  mpFont( rFont ),
82  mpRefDevice( rRefDevice ),
83  mnTextDirection( nDirection )
84  {
85  }
86 
87  TextLayout::~TextLayout()
88  {
89  }
90 
91  void SAL_CALL TextLayout::disposing()
92  {
93  ::osl::MutexGuard aGuard( m_aMutex );
94 
95  mpFont.clear();
96  mpRefDevice.clear();
97  }
98 
99  // XTextLayout
100  uno::Sequence< uno::Reference< rendering::XPolyPolygon2D > > SAL_CALL TextLayout::queryTextShapes( )
101  {
102  // TODO
103  return uno::Sequence< uno::Reference< rendering::XPolyPolygon2D > >();
104  }
105 
106  uno::Sequence< geometry::RealRectangle2D > SAL_CALL TextLayout::queryInkMeasures( )
107  {
108  // TODO
109  return uno::Sequence< geometry::RealRectangle2D >();
110  }
111 
112  uno::Sequence< geometry::RealRectangle2D > SAL_CALL TextLayout::queryMeasures( )
113  {
114  // TODO
115  return uno::Sequence< geometry::RealRectangle2D >();
116  }
117 
118  uno::Sequence< double > SAL_CALL TextLayout::queryLogicalAdvancements( )
119  {
120  ::osl::MutexGuard aGuard( m_aMutex );
121 
122  return maLogicalAdvancements;
123  }
124 
125  void SAL_CALL TextLayout::applyLogicalAdvancements( const uno::Sequence< double >& aAdvancements )
126  {
127  ::osl::MutexGuard aGuard( m_aMutex );
128 
129  if( aAdvancements.getLength() != maText.Length )
130  {
131  SAL_WARN("canvas.cairo", "TextLayout::applyLogicalAdvancements(): mismatching number of advancements" );
132  throw lang::IllegalArgumentException();
133  }
134 
135  maLogicalAdvancements = aAdvancements;
136  }
137 
138  geometry::RealRectangle2D SAL_CALL TextLayout::queryTextBounds( )
139  {
140  ::osl::MutexGuard aGuard( m_aMutex );
141 
142  OutputDevice* pOutDev = mpRefDevice->getOutputDevice();
143  if( !pOutDev )
144  return geometry::RealRectangle2D();
145 
146  ScopedVclPtrInstance< VirtualDevice > pVDev( *pOutDev );
147  pVDev->SetFont( mpFont->getVCLFont() );
148 
149  // need metrics for Y offset, the XCanvas always renders
150  // relative to baseline
151  const ::FontMetric& aMetric( pVDev->GetFontMetric() );
152 
153  setupLayoutMode( *pVDev, mnTextDirection );
154 
155  const sal_Int32 nAboveBaseline( -aMetric.GetAscent() );
156  const sal_Int32 nBelowBaseline( aMetric.GetDescent() );
157 
158  if( maLogicalAdvancements.hasElements() )
159  {
160  return geometry::RealRectangle2D( 0, nAboveBaseline,
161  maLogicalAdvancements[ maLogicalAdvancements.getLength()-1 ],
162  nBelowBaseline );
163  }
164  else
165  {
166  return geometry::RealRectangle2D( 0, nAboveBaseline,
167  pVDev->GetTextWidth(
168  maText.Text,
169  ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
170  ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) ),
171  nBelowBaseline );
172  }
173  }
174 
175  double SAL_CALL TextLayout::justify( double /*nSize*/ )
176  {
177  // TODO
178  return 0.0;
179  }
180 
181  double SAL_CALL TextLayout::combinedJustify( const uno::Sequence< uno::Reference< rendering::XTextLayout > >& /*aNextLayouts*/,
182  double /*nSize*/ )
183  {
184  // TODO
185  return 0.0;
186  }
187 
188  rendering::TextHit SAL_CALL TextLayout::getTextHit( const geometry::RealPoint2D& /*aHitPoint*/ )
189  {
190  // TODO
191  return rendering::TextHit();
192  }
193 
194  rendering::Caret SAL_CALL TextLayout::getCaret( sal_Int32 /*nInsertionIndex*/,
195  sal_Bool /*bExcludeLigatures*/ )
196  {
197  // TODO
198  return rendering::Caret();
199  }
200 
201  sal_Int32 SAL_CALL TextLayout::getNextInsertionIndex( sal_Int32 /*nStartIndex*/,
202  sal_Int32 /*nCaretAdvancement*/,
203  sal_Bool /*bExcludeLigatures*/ )
204  {
205  // TODO
206  return 0;
207  }
208 
209  uno::Reference< rendering::XPolyPolygon2D > SAL_CALL TextLayout::queryVisualHighlighting( sal_Int32 /*nStartIndex*/,
210  sal_Int32 /*nEndIndex*/ )
211  {
212  // TODO
213  return uno::Reference< rendering::XPolyPolygon2D >();
214  }
215 
216  uno::Reference< rendering::XPolyPolygon2D > SAL_CALL TextLayout::queryLogicalHighlighting( sal_Int32 /*nStartIndex*/,
217  sal_Int32 /*nEndIndex*/ )
218  {
219  // TODO
220  return uno::Reference< rendering::XPolyPolygon2D >();
221  }
222 
223  double SAL_CALL TextLayout::getBaselineOffset( )
224  {
225  // TODO
226  return 0.0;
227  }
228 
229  sal_Int8 SAL_CALL TextLayout::getMainTextDirection( )
230  {
231  ::osl::MutexGuard aGuard( m_aMutex );
232 
233  return mnTextDirection;
234  }
235 
236  uno::Reference< rendering::XCanvasFont > SAL_CALL TextLayout::getFont( )
237  {
238  ::osl::MutexGuard aGuard( m_aMutex );
239 
240  return mpFont.get();
241  }
242 
243  rendering::StringContext SAL_CALL TextLayout::getText( )
244  {
245  ::osl::MutexGuard aGuard( m_aMutex );
246 
247  return maText;
248  }
249 
259  void TextLayout::draw( OutputDevice& rOutDev,
260  const Point& rOutpos,
261  const rendering::ViewState& viewState,
262  const rendering::RenderState& renderState ) const
263  {
264  ::osl::MutexGuard aGuard( m_aMutex );
265  setupLayoutMode( rOutDev, mnTextDirection );
266 
267  std::unique_ptr< tools::Long []> aOffsets(new tools::Long[maLogicalAdvancements.getLength()]);
268 
269  if( maLogicalAdvancements.hasElements() )
270  setupTextOffsets( aOffsets.get(), maLogicalAdvancements, viewState, renderState );
271 
272  if (maLogicalAdvancements.hasElements())
273  {
274  rOutDev.DrawTextArray( rOutpos, maText.Text, aOffsets.get(),
275  ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
276  ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) );
277  }
278  else
279  {
280  rOutDev.DrawText( rOutpos, maText.Text,
281  ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
282  ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) );
283  }
284  }
285 
286  namespace
287  {
288  class OffsetTransformer
289  {
290  public:
291  explicit OffsetTransformer( const ::basegfx::B2DHomMatrix& rMat ) :
292  maMatrix( rMat )
293  {
294  }
295 
296  sal_Int32 operator()( const double& rOffset )
297  {
298  // This is an optimization of the normal rMat*[x,0]
299  // transformation of the advancement vector (in x
300  // direction), followed by a length calculation of the
301  // resulting vector: advancement' =
302  // ||rMat*[x,0]||. Since advancements are vectors, we
303  // can ignore translational components, thus if [x,0],
304  // it follows that rMat*[x,0]=[x',0] holds. Thus, we
305  // just have to calc the transformation of the x
306  // component.
307 
308  // TODO(F2): Handle non-horizontal advancements!
309  return ::basegfx::fround( hypot(maMatrix.get(0,0)*rOffset,
310  maMatrix.get(1,0)*rOffset) );
311  }
312 
313  private:
315  };
316  }
317 
318  void TextLayout::setupTextOffsets( tools::Long* outputOffsets,
319  const uno::Sequence< double >& inputOffsets,
320  const rendering::ViewState& viewState,
321  const rendering::RenderState& renderState ) const
322  {
323  ENSURE_OR_THROW( outputOffsets!=nullptr,
324  "TextLayout::setupTextOffsets offsets NULL" );
325 
326  ::basegfx::B2DHomMatrix aMatrix;
327 
329  viewState,
330  renderState);
331 
332  // fill integer offsets
333  std::transform( inputOffsets.begin(),
334  inputOffsets.end(),
335  outputOffsets,
336  OffsetTransformer( aMatrix ) );
337  }
338 
339  OUString SAL_CALL TextLayout::getImplementationName()
340  {
341  return "CairoCanvas::TextLayout";
342  }
343 
344  sal_Bool SAL_CALL TextLayout::supportsService( const OUString& ServiceName )
345  {
346  return cppu::supportsService( this, ServiceName );
347  }
348 
349  uno::Sequence< OUString > SAL_CALL TextLayout::getSupportedServiceNames()
350  {
351  return { "com.sun.star.rendering.TextLayout" };
352  }
353 }
354 
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void DrawText(const Point &rStartPt, const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, MetricVector *pVector=nullptr, OUString *pDisplayText=nullptr, const SalLayoutGlyphs *pLayoutCache=nullptr)
TextLayout(const TextLayout &)=delete
make noncopyable
::basegfx::B2DHomMatrix & mergeViewAndRenderTransform(::basegfx::B2DHomMatrix &combinedTransform, const rendering::ViewState &viewState, const rendering::RenderState &renderState)
signed char sal_Int8
long Long
void SetLayoutMode(ComplexTextLayoutFlags nTextLayoutMode)
void DrawTextArray(const Point &rStartPt, const OUString &rStr, const tools::Long *pDXAry, sal_Int32 nIndex=0, sal_Int32 nLen=-1, SalLayoutFlags flags=SalLayoutFlags::NONE, const SalLayoutGlyphs *pLayoutCache=nullptr)
double get(sal_uInt16 nRow, sal_uInt16 nColumn) const
rtl::Reference< CanvasFont > Reference
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
::cppu::WeakComponentImplHelper< css::rendering::XTextLayout, css::lang::XServiceInfo > TextLayout_Base
ComplexTextLayoutFlags
Target numeric_cast(Source arg)
Cast numeric value into another (numeric) data type.
unsigned char sal_Bool
std::unique_ptr< vcl::Font > mpFont
#define ENSURE_OR_THROW(c, m)
Text maText
::basegfx::B2DHomMatrix maMatrix
::rtl::Reference< SurfaceProvider > SurfaceProviderRef
#define SAL_WARN(area, stream)
osl::Mutex m_aMutex