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