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
125 std::vector< uno::Reference< rendering::XPolyPolygon2D> > aOutlineSequence;
127 if (pVDev->GetTextOutlines(
128 aOutlines,
129 maText.Text,
130 maText.StartPosition,
131 maText.StartPosition,
132 maText.Length,
133 0,
134 aOffsets))
135 {
136 aOutlineSequence.reserve(aOutlines.size());
137 sal_Int32 nIndex (0);
138 for (auto const& outline : aOutlines)
139 {
140 aOutlineSequence[nIndex++] = ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon(
141 mxDevice,
142 outline);
143 }
144 }
145
146 return comphelper::containerToSequence(aOutlineSequence);
147 }
148
149 uno::Sequence< geometry::RealRectangle2D > SAL_CALL TextLayout::queryInkMeasures( )
150 {
151 SolarMutexGuard aGuard;
152
153
154 OutputDevice& rOutDev = mpOutDevProvider->getOutDev();
156 pVDev->SetFont( mpFont->getVCLFont() );
157
158 setupLayoutMode( *pVDev, mnTextDirection );
159
160 const rendering::ViewState aViewState(
161 geometry::AffineMatrix2D(1,0,0, 0,1,0),
162 nullptr);
163
164 rendering::RenderState aRenderState (
165 geometry::AffineMatrix2D(1,0,0,0,1,0),
166 nullptr,
167 uno::Sequence<double>(4),
168 rendering::CompositeOperation::SOURCE);
169
170 KernArray aOffsets(setupTextOffsets(maLogicalAdvancements, aViewState, aRenderState));
171
172 std::vector< ::tools::Rectangle > aMetricVector;
173 uno::Sequence<geometry::RealRectangle2D> aBoundingBoxes;
174 if (pVDev->GetGlyphBoundRects(
175 Point(0,0),
176 maText.Text,
177 ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
178 ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length),
179 aMetricVector))
180 {
181 aBoundingBoxes.realloc(aMetricVector.size());
182 auto pBoundingBoxes = aBoundingBoxes.getArray();
183 sal_Int32 nIndex (0);
184 for (auto const& metric : aMetricVector)
185 {
186 pBoundingBoxes[nIndex++] = geometry::RealRectangle2D(
187 metric.Left(),
188 metric.Top(),
189 metric.Right(),
190 metric.Bottom());
191 }
192 }
193 return aBoundingBoxes;
194 }
195
196 uno::Sequence< geometry::RealRectangle2D > SAL_CALL TextLayout::queryMeasures( )
197 {
198 // TODO(F1)
199 return uno::Sequence< geometry::RealRectangle2D >();
200 }
201
202 uno::Sequence< double > SAL_CALL TextLayout::queryLogicalAdvancements( )
203 {
204 SolarMutexGuard aGuard;
205
206 return maLogicalAdvancements;
207 }
208
209 void SAL_CALL TextLayout::applyLogicalAdvancements( const uno::Sequence< double >& aAdvancements )
210 {
211 SolarMutexGuard aGuard;
212
213 ENSURE_ARG_OR_THROW( aAdvancements.getLength() == maText.Length,
214 "TextLayout::applyLogicalAdvancements(): mismatching number of advancements" );
215
216 maLogicalAdvancements = aAdvancements;
217 }
218
219 geometry::RealRectangle2D SAL_CALL TextLayout::queryTextBounds( )
220 {
221 SolarMutexGuard aGuard;
222
223 if( !mpOutDevProvider )
224 return geometry::RealRectangle2D();
225
226 OutputDevice& rOutDev = mpOutDevProvider->getOutDev();
227
229 pVDev->SetFont( mpFont->getVCLFont() );
230
231 // need metrics for Y offset, the XCanvas always renders
232 // relative to baseline
233 const ::FontMetric& aMetric( pVDev->GetFontMetric() );
234
235 setupLayoutMode( *pVDev, mnTextDirection );
236
237 const sal_Int32 nAboveBaseline( -aMetric.GetAscent() );
238 const sal_Int32 nBelowBaseline( aMetric.GetDescent() );
239
240 if( maLogicalAdvancements.hasElements() )
241 {
242 return geometry::RealRectangle2D( 0, nAboveBaseline,
243 maLogicalAdvancements[ maLogicalAdvancements.getLength()-1 ],
244 nBelowBaseline );
245 }
246 else
247 {
248 return geometry::RealRectangle2D( 0, nAboveBaseline,
249 pVDev->GetTextWidth(
250 maText.Text,
251 ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
252 ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) ),
253 nBelowBaseline );
254 }
255 }
256
257 double SAL_CALL TextLayout::justify( double )
258 {
259 // TODO(F1)
260 return 0.0;
261 }
262
263 double SAL_CALL TextLayout::combinedJustify( const uno::Sequence< uno::Reference< rendering::XTextLayout > >&,
264 double )
265 {
266 // TODO(F1)
267 return 0.0;
268 }
269
270 rendering::TextHit SAL_CALL TextLayout::getTextHit( const geometry::RealPoint2D& )
271 {
272 // TODO(F1)
273 return rendering::TextHit();
274 }
275
276 rendering::Caret SAL_CALL TextLayout::getCaret( sal_Int32, sal_Bool )
277 {
278 // TODO(F1)
279 return rendering::Caret();
280 }
281
282 sal_Int32 SAL_CALL TextLayout::getNextInsertionIndex( sal_Int32, sal_Int32, sal_Bool )
283 {
284 // TODO(F1)
285 return 0;
286 }
287
288 uno::Reference< rendering::XPolyPolygon2D > SAL_CALL TextLayout::queryVisualHighlighting( sal_Int32, sal_Int32 )
289 {
290 // TODO(F1)
291 return uno::Reference< rendering::XPolyPolygon2D >();
292 }
293
294 uno::Reference< rendering::XPolyPolygon2D > SAL_CALL TextLayout::queryLogicalHighlighting( sal_Int32, sal_Int32 )
295 {
296 // TODO(F1)
297 return uno::Reference< rendering::XPolyPolygon2D >();
298 }
299
300 double SAL_CALL TextLayout::getBaselineOffset( )
301 {
302 // TODO(F1)
303 return 0.0;
304 }
305
306 sal_Int8 SAL_CALL TextLayout::getMainTextDirection( )
307 {
308 SolarMutexGuard aGuard;
309
310 return mnTextDirection;
311 }
312
313 uno::Reference< rendering::XCanvasFont > SAL_CALL TextLayout::getFont( )
314 {
315 SolarMutexGuard aGuard;
316
317 return mpFont;
318 }
319
320 rendering::StringContext SAL_CALL TextLayout::getText( )
321 {
322 SolarMutexGuard aGuard;
323
324 return maText;
325 }
326
327 void TextLayout::draw( OutputDevice& rOutDev,
328 const Point& rOutpos,
329 const rendering::ViewState& viewState,
330 const rendering::RenderState& renderState ) const
331 {
332 SolarMutexGuard aGuard;
333
334 setupLayoutMode( rOutDev, mnTextDirection );
335
336 if( maLogicalAdvancements.hasElements() )
337 {
338 // TODO(P2): cache that
339 KernArray aOffsets(setupTextOffsets(maLogicalAdvancements, viewState, renderState));
340
341 // TODO(F3): ensure correct length and termination for DX
342 // array (last entry _must_ contain the overall width)
343
344 rOutDev.DrawTextArray( rOutpos,
345 maText.Text,
346 aOffsets,
347 {},
348 ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
349 ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) );
350 }
351 else
352 {
353 rOutDev.DrawText( rOutpos,
354 maText.Text,
355 ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
356 ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) );
357 }
358 }
359
360 namespace
361 {
362 class OffsetTransformer
363 {
364 public:
365 explicit OffsetTransformer( ::basegfx::B2DHomMatrix aMat ) :
366 maMatrix(std::move( aMat ))
367 {
368 }
369
370 sal_Int32 operator()( const double& rOffset )
371 {
372 // This is an optimization of the normal rMat*[x,0]
373 // transformation of the advancement vector (in x
374 // direction), followed by a length calculation of the
375 // resulting vector: advancement' =
376 // ||rMat*[x,0]||. Since advancements are vectors, we
377 // can ignore translational components, thus if [x,0],
378 // it follows that rMat*[x,0]=[x',0] holds. Thus, we
379 // just have to calc the transformation of the x
380 // component.
381
382 // TODO(F2): Handle non-horizontal advancements!
383 return ::basegfx::fround( hypot(maMatrix.get(0,0)*rOffset,
384 maMatrix.get(1,0)*rOffset) );
385 }
386
387 private:
389 };
390 }
391
392 KernArray TextLayout::setupTextOffsets(
393 const uno::Sequence< double >& inputOffsets,
394 const rendering::ViewState& viewState,
395 const rendering::RenderState& renderState ) const
396 {
398
400 viewState,
401 renderState);
402
403 // fill integer offsets
404 KernArray outputOffsets;
405 OffsetTransformer aTransform(aMatrix);
406 std::for_each(inputOffsets.begin(), inputOffsets.end(),
407 [&outputOffsets, &aTransform](double n) {outputOffsets.push_back(aTransform(n)); } );
408 return outputOffsets;
409 }
410
411 OUString SAL_CALL TextLayout::getImplementationName()
412 {
413 return "VCLCanvas::TextLayout";
414 }
415
416 sal_Bool SAL_CALL TextLayout::supportsService( const OUString& ServiceName )
417 {
418 return cppu::supportsService( this, ServiceName );
419 }
420
421 uno::Sequence< OUString > SAL_CALL TextLayout::getSupportedServiceNames()
422 {
423 return { "com.sun.star.rendering.TextLayout" };
424 }
425}
426
427/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Text maText
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
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
std::shared_ptr< OutDevProvider > OutDevProviderSharedPtr
std::optional< vcl::Font > mpFont
::basegfx::B2DHomMatrix maMatrix
Definition: textlayout.cxx:388
unsigned char sal_Bool
signed char sal_Int8