LibreOffice Module canvas (master) 1
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
21#include <sal/config.h>
22
27#include <com/sun/star/rendering/CompositeOperation.hpp>
28#include <com/sun/star/rendering/RenderState.hpp>
29#include <com/sun/star/rendering/TextDirection.hpp>
30#include <com/sun/star/rendering/ViewState.hpp>
33#include <utility>
34#include <vcl/kernarray.hxx>
35#include <vcl/metric.hxx>
36#include <vcl/virdev.hxx>
37
39
40#include "textlayout.hxx"
41
42#include <memory>
43
44using namespace ::com::sun::star;
45
46namespace vclcanvas
47{
48 namespace
49 {
50 void setupLayoutMode( OutputDevice& rOutDev,
51 sal_Int8 nTextDirection )
52 {
53 // TODO(P3): avoid if already correctly set
55 switch( nTextDirection )
56 {
57 case rendering::TextDirection::WEAK_LEFT_TO_RIGHT:
58 break;
59 case rendering::TextDirection::STRONG_LEFT_TO_RIGHT:
61 break;
62 case rendering::TextDirection::WEAK_RIGHT_TO_LEFT:
64 break;
65 case rendering::TextDirection::STRONG_RIGHT_TO_LEFT:
67 break;
68 default:
69 break;
70 }
71
72 // set calculated layout mode. Origin is always the left edge,
73 // as required at the API spec
75 }
76 }
77
78 TextLayout::TextLayout( rendering::StringContext aText,
79 sal_Int8 nDirection,
81 uno::Reference<rendering::XGraphicDevice> xDevice,
82 OutDevProviderSharedPtr xOutDev ) :
83 maText(std::move( aText )),
84 mpFont(std::move( rFont )),
85 mxDevice(std::move( xDevice )),
86 mpOutDevProvider(std::move( xOutDev )),
87 mnTextDirection( nDirection )
88 {}
89
90 void TextLayout::disposing(std::unique_lock<std::mutex>& rGuard)
91 {
92 rGuard.unlock();
93 {
94 SolarMutexGuard aGuard;
95 mpOutDevProvider.reset();
96 mxDevice.clear();
97 mpFont.clear();
98 }
99 rGuard.lock();
100 }
101
102 // XTextLayout
103 uno::Sequence< uno::Reference< rendering::XPolyPolygon2D > > SAL_CALL TextLayout::queryTextShapes( )
104 {
105 SolarMutexGuard aGuard;
106
107 OutputDevice& rOutDev = mpOutDevProvider->getOutDev();
109 pVDev->SetFont( mpFont->getVCLFont() );
110
111 setupLayoutMode( *pVDev, mnTextDirection );
112
113 const rendering::ViewState aViewState(
114 geometry::AffineMatrix2D(1,0,0, 0,1,0),
115 nullptr);
116
117 rendering::RenderState aRenderState (
118 geometry::AffineMatrix2D(1,0,0,0,1,0),
119 nullptr,
120 uno::Sequence<double>(4),
121 rendering::CompositeOperation::SOURCE);
122
123 KernArray aOffsets(setupTextOffsets(maLogicalAdvancements, aViewState, aRenderState));
124 o3tl::span<const sal_Bool> aKashidaArray(maKashidaPositions.getArray(), maKashidaPositions.getLength());
125
126 std::vector< uno::Reference< rendering::XPolyPolygon2D> > aOutlineSequence;
128 if (pVDev->GetTextOutlines(
129 aOutlines,
130 maText.Text,
131 maText.StartPosition,
132 maText.StartPosition,
133 maText.Length,
134 0,
135 aOffsets,
136 aKashidaArray))
137 {
138 aOutlineSequence.reserve(aOutlines.size());
139 sal_Int32 nIndex (0);
140 for (auto const& outline : aOutlines)
141 {
142 aOutlineSequence[nIndex++] = ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon(
143 mxDevice,
144 outline);
145 }
146 }
147
148 return comphelper::containerToSequence(aOutlineSequence);
149 }
150
151 uno::Sequence< geometry::RealRectangle2D > SAL_CALL TextLayout::queryInkMeasures( )
152 {
153 SolarMutexGuard aGuard;
154
155
156 OutputDevice& rOutDev = mpOutDevProvider->getOutDev();
158 pVDev->SetFont( mpFont->getVCLFont() );
159
160 setupLayoutMode( *pVDev, mnTextDirection );
161
162 const rendering::ViewState aViewState(
163 geometry::AffineMatrix2D(1,0,0, 0,1,0),
164 nullptr);
165
166 rendering::RenderState aRenderState (
167 geometry::AffineMatrix2D(1,0,0,0,1,0),
168 nullptr,
169 uno::Sequence<double>(4),
170 rendering::CompositeOperation::SOURCE);
171
172 KernArray aOffsets(setupTextOffsets(maLogicalAdvancements, aViewState, aRenderState));
173
174 std::vector< ::tools::Rectangle > aMetricVector;
175 uno::Sequence<geometry::RealRectangle2D> aBoundingBoxes;
176 if (pVDev->GetGlyphBoundRects(
177 Point(0,0),
178 maText.Text,
179 ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
180 ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length),
181 aMetricVector))
182 {
183 aBoundingBoxes.realloc(aMetricVector.size());
184 auto pBoundingBoxes = aBoundingBoxes.getArray();
185 sal_Int32 nIndex (0);
186 for (auto const& metric : aMetricVector)
187 {
188 pBoundingBoxes[nIndex++] = geometry::RealRectangle2D(
189 metric.Left(),
190 metric.Top(),
191 metric.Right(),
192 metric.Bottom());
193 }
194 }
195 return aBoundingBoxes;
196 }
197
198 uno::Sequence< geometry::RealRectangle2D > SAL_CALL TextLayout::queryMeasures( )
199 {
200 // TODO(F1)
201 return uno::Sequence< geometry::RealRectangle2D >();
202 }
203
204 uno::Sequence< double > SAL_CALL TextLayout::queryLogicalAdvancements( )
205 {
206 SolarMutexGuard aGuard;
207
208 return maLogicalAdvancements;
209 }
210
211 void SAL_CALL TextLayout::applyLogicalAdvancements( const uno::Sequence< double >& aAdvancements )
212 {
213 SolarMutexGuard aGuard;
214
215 ENSURE_ARG_OR_THROW( aAdvancements.getLength() == maText.Length,
216 "TextLayout::applyLogicalAdvancements(): mismatching number of advancements" );
217
218 maLogicalAdvancements = aAdvancements;
219 }
220
221 uno::Sequence< sal_Bool > SAL_CALL TextLayout::queryKashidaPositions( )
222 {
223 SolarMutexGuard aGuard;
224
225 return maKashidaPositions;
226 }
227
228 void SAL_CALL TextLayout::applyKashidaPositions( const uno::Sequence< sal_Bool >& aPositions )
229 {
230 SolarMutexGuard aGuard;
231
232 ENSURE_ARG_OR_THROW( !aPositions.hasElements() || aPositions.getLength() == maText.Length,
233 "TextLayout::applyKashidaPositions(): mismatching number of positions" );
234
235 maKashidaPositions = aPositions;
236 }
237
238 geometry::RealRectangle2D SAL_CALL TextLayout::queryTextBounds( )
239 {
240 SolarMutexGuard aGuard;
241
242 if( !mpOutDevProvider )
243 return geometry::RealRectangle2D();
244
245 OutputDevice& rOutDev = mpOutDevProvider->getOutDev();
246
248 pVDev->SetFont( mpFont->getVCLFont() );
249
250 // need metrics for Y offset, the XCanvas always renders
251 // relative to baseline
252 const ::FontMetric& aMetric( pVDev->GetFontMetric() );
253
254 setupLayoutMode( *pVDev, mnTextDirection );
255
256 const sal_Int32 nAboveBaseline( -aMetric.GetAscent() );
257 const sal_Int32 nBelowBaseline( aMetric.GetDescent() );
258
259 if( maLogicalAdvancements.hasElements() )
260 {
261 return geometry::RealRectangle2D( 0, nAboveBaseline,
262 maLogicalAdvancements[ maLogicalAdvancements.getLength()-1 ],
263 nBelowBaseline );
264 }
265 else
266 {
267 return geometry::RealRectangle2D( 0, nAboveBaseline,
268 pVDev->GetTextWidth(
269 maText.Text,
270 ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
271 ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) ),
272 nBelowBaseline );
273 }
274 }
275
276 double SAL_CALL TextLayout::justify( double )
277 {
278 // TODO(F1)
279 return 0.0;
280 }
281
282 double SAL_CALL TextLayout::combinedJustify( const uno::Sequence< uno::Reference< rendering::XTextLayout > >&,
283 double )
284 {
285 // TODO(F1)
286 return 0.0;
287 }
288
289 rendering::TextHit SAL_CALL TextLayout::getTextHit( const geometry::RealPoint2D& )
290 {
291 // TODO(F1)
292 return rendering::TextHit();
293 }
294
295 rendering::Caret SAL_CALL TextLayout::getCaret( sal_Int32, sal_Bool )
296 {
297 // TODO(F1)
298 return rendering::Caret();
299 }
300
301 sal_Int32 SAL_CALL TextLayout::getNextInsertionIndex( sal_Int32, sal_Int32, sal_Bool )
302 {
303 // TODO(F1)
304 return 0;
305 }
306
307 uno::Reference< rendering::XPolyPolygon2D > SAL_CALL TextLayout::queryVisualHighlighting( sal_Int32, sal_Int32 )
308 {
309 // TODO(F1)
310 return uno::Reference< rendering::XPolyPolygon2D >();
311 }
312
313 uno::Reference< rendering::XPolyPolygon2D > SAL_CALL TextLayout::queryLogicalHighlighting( sal_Int32, sal_Int32 )
314 {
315 // TODO(F1)
316 return uno::Reference< rendering::XPolyPolygon2D >();
317 }
318
319 double SAL_CALL TextLayout::getBaselineOffset( )
320 {
321 // TODO(F1)
322 return 0.0;
323 }
324
325 sal_Int8 SAL_CALL TextLayout::getMainTextDirection( )
326 {
327 SolarMutexGuard aGuard;
328
329 return mnTextDirection;
330 }
331
332 uno::Reference< rendering::XCanvasFont > SAL_CALL TextLayout::getFont( )
333 {
334 SolarMutexGuard aGuard;
335
336 return mpFont;
337 }
338
339 rendering::StringContext SAL_CALL TextLayout::getText( )
340 {
341 SolarMutexGuard aGuard;
342
343 return maText;
344 }
345
346 void TextLayout::draw( OutputDevice& rOutDev,
347 const Point& rOutpos,
348 const rendering::ViewState& viewState,
349 const rendering::RenderState& renderState ) const
350 {
351 SolarMutexGuard aGuard;
352
353#ifdef _WIN32
354 // tdf#147999
355 // On Windows we get the wrong font width for fallback fonts unless we setup again here.
356 vcl::Font aFont(rOutDev.GetFont());
357 tools::setupFontWidth(mpFont->getFontMatrix(), aFont, rOutDev);
358 rOutDev.SetFont(aFont);
359#endif
360
361 setupLayoutMode( rOutDev, mnTextDirection );
362
363 if( maLogicalAdvancements.hasElements() )
364 {
365 // TODO(P2): cache that
366 KernArray aOffsets(setupTextOffsets(maLogicalAdvancements, viewState, renderState));
367 o3tl::span<const sal_Bool> aKashidaArray(maKashidaPositions.getConstArray(), maKashidaPositions.getLength());
368
369 // TODO(F3): ensure correct length and termination for DX
370 // array (last entry _must_ contain the overall width)
371
372 rOutDev.DrawTextArray( rOutpos,
373 maText.Text,
374 aOffsets,
375 aKashidaArray,
376 ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
377 ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) );
378 }
379 else
380 {
381 rOutDev.DrawText( rOutpos,
382 maText.Text,
383 ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
384 ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) );
385 }
386 }
387
388 namespace
389 {
390 class OffsetTransformer
391 {
392 public:
393 explicit OffsetTransformer( ::basegfx::B2DHomMatrix aMat ) :
394 maMatrix(std::move( aMat ))
395 {
396 }
397
398 sal_Int32 operator()( const double& rOffset )
399 {
400 // This is an optimization of the normal rMat*[x,0]
401 // transformation of the advancement vector (in x
402 // direction), followed by a length calculation of the
403 // resulting vector: advancement' =
404 // ||rMat*[x,0]||. Since advancements are vectors, we
405 // can ignore translational components, thus if [x,0],
406 // it follows that rMat*[x,0]=[x',0] holds. Thus, we
407 // just have to calc the transformation of the x
408 // component.
409
410 // TODO(F2): Handle non-horizontal advancements!
411 return ::basegfx::fround( hypot(maMatrix.get(0,0)*rOffset,
412 maMatrix.get(1,0)*rOffset) );
413 }
414
415 private:
417 };
418 }
419
420 KernArray TextLayout::setupTextOffsets(
421 const uno::Sequence< double >& inputOffsets,
422 const rendering::ViewState& viewState,
423 const rendering::RenderState& renderState ) const
424 {
426
428 viewState,
429 renderState);
430
431 // fill integer offsets
432 KernArray outputOffsets;
433 OffsetTransformer aTransform(aMatrix);
434 std::for_each(inputOffsets.begin(), inputOffsets.end(),
435 [&outputOffsets, &aTransform](double n) {outputOffsets.push_back(aTransform(n)); } );
436 return outputOffsets;
437 }
438
439 OUString SAL_CALL TextLayout::getImplementationName()
440 {
441 return "VCLCanvas::TextLayout";
442 }
443
444 sal_Bool SAL_CALL TextLayout::supportsService( const OUString& ServiceName )
445 {
446 return cppu::supportsService( this, ServiceName );
447 }
448
449 uno::Sequence< OUString > SAL_CALL TextLayout::getSupportedServiceNames()
450 {
451 return { "com.sun.star.rendering.TextLayout" };
452 }
453}
454
455/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Text maText
const vcl::Font & GetFont() const
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 SetFont(const vcl::Font &rNewFont)
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
Definition: canvasfont.hxx:49
TextLayout(const TextLayout &)=delete
make noncopyable
#define ENSURE_ARG_OR_THROW(c, m)
sal_Int32 nIndex
sal_Int64 n
::std::vector< B2DPolyPolygon > B2DPolyPolygonVector
::basegfx::B2DHomMatrix & mergeViewAndRenderTransform(::basegfx::B2DHomMatrix &combinedTransform, const rendering::ViewState &viewState, const rendering::RenderState &renderState)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
ComplexTextLayoutFlags
void setupFontWidth(const css::geometry::Matrix2D &rFontMatrix, vcl::Font &rFont, OutputDevice &rOutDev)
Definition: impltools.cxx:138
std::shared_ptr< OutDevProvider > OutDevProviderSharedPtr
std::optional< vcl::Font > mpFont
::basegfx::B2DHomMatrix maMatrix
Definition: textlayout.cxx:416
unsigned char sal_Bool
signed char sal_Int8