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", static_cast<cppu::OWeakObject*>(this), 1);
129 }
130
131 maLogicalAdvancements = aAdvancements;
132 }
133
134 geometry::RealRectangle2D SAL_CALL TextLayout::queryTextBounds( )
135 {
136 std::unique_lock aGuard( m_aMutex );
137
138 OutputDevice* pOutDev = mpRefDevice->getOutputDevice();
139 if( !pOutDev )
140 return geometry::RealRectangle2D();
141
143 pVDev->SetFont( mpFont->getVCLFont() );
144
145 // need metrics for Y offset, the XCanvas always renders
146 // relative to baseline
147 const ::FontMetric& aMetric( pVDev->GetFontMetric() );
148
149 setupLayoutMode( *pVDev, mnTextDirection );
150
151 const sal_Int32 nAboveBaseline( -aMetric.GetAscent() );
152 const sal_Int32 nBelowBaseline( aMetric.GetDescent() );
153
154 if( maLogicalAdvancements.hasElements() )
155 {
156 return geometry::RealRectangle2D( 0, nAboveBaseline,
157 maLogicalAdvancements[ maLogicalAdvancements.getLength()-1 ],
158 nBelowBaseline );
159 }
160 else
161 {
162 return geometry::RealRectangle2D( 0, nAboveBaseline,
163 pVDev->GetTextWidth(
164 maText.Text,
165 ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
166 ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) ),
167 nBelowBaseline );
168 }
169 }
170
171 double SAL_CALL TextLayout::justify( double /*nSize*/ )
172 {
173 // TODO
174 return 0.0;
175 }
176
177 double SAL_CALL TextLayout::combinedJustify( const uno::Sequence< uno::Reference< rendering::XTextLayout > >& /*aNextLayouts*/,
178 double /*nSize*/ )
179 {
180 // TODO
181 return 0.0;
182 }
183
184 rendering::TextHit SAL_CALL TextLayout::getTextHit( const geometry::RealPoint2D& /*aHitPoint*/ )
185 {
186 // TODO
187 return rendering::TextHit();
188 }
189
190 rendering::Caret SAL_CALL TextLayout::getCaret( sal_Int32 /*nInsertionIndex*/,
191 sal_Bool /*bExcludeLigatures*/ )
192 {
193 // TODO
194 return rendering::Caret();
195 }
196
197 sal_Int32 SAL_CALL TextLayout::getNextInsertionIndex( sal_Int32 /*nStartIndex*/,
198 sal_Int32 /*nCaretAdvancement*/,
199 sal_Bool /*bExcludeLigatures*/ )
200 {
201 // TODO
202 return 0;
203 }
204
205 uno::Reference< rendering::XPolyPolygon2D > SAL_CALL TextLayout::queryVisualHighlighting( sal_Int32 /*nStartIndex*/,
206 sal_Int32 /*nEndIndex*/ )
207 {
208 // TODO
209 return uno::Reference< rendering::XPolyPolygon2D >();
210 }
211
212 uno::Reference< rendering::XPolyPolygon2D > SAL_CALL TextLayout::queryLogicalHighlighting( sal_Int32 /*nStartIndex*/,
213 sal_Int32 /*nEndIndex*/ )
214 {
215 // TODO
216 return uno::Reference< rendering::XPolyPolygon2D >();
217 }
218
219 double SAL_CALL TextLayout::getBaselineOffset( )
220 {
221 // TODO
222 return 0.0;
223 }
224
225 sal_Int8 SAL_CALL TextLayout::getMainTextDirection( )
226 {
227 std::unique_lock aGuard( m_aMutex );
228
229 return mnTextDirection;
230 }
231
232 uno::Reference< rendering::XCanvasFont > SAL_CALL TextLayout::getFont( )
233 {
234 std::unique_lock aGuard( m_aMutex );
235
236 return mpFont;
237 }
238
239 rendering::StringContext SAL_CALL TextLayout::getText( )
240 {
241 std::unique_lock aGuard( m_aMutex );
242
243 return maText;
244 }
245
255 void TextLayout::draw( OutputDevice& rOutDev,
256 const Point& rOutpos,
257 const rendering::ViewState& viewState,
258 const rendering::RenderState& renderState ) const
259 {
260 std::unique_lock aGuard( m_aMutex );
261 setupLayoutMode( rOutDev, mnTextDirection );
262
263 if (maLogicalAdvancements.hasElements())
264 {
265 KernArray aOffsets(setupTextOffsets(maLogicalAdvancements, viewState, renderState));
266
267 rOutDev.DrawTextArray( rOutpos, maText.Text, aOffsets, {},
268 ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
269 ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) );
270 }
271 else
272 {
273 rOutDev.DrawText( rOutpos, maText.Text,
274 ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
275 ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) );
276 }
277 }
278
279 namespace
280 {
281 class OffsetTransformer
282 {
283 public:
284 explicit OffsetTransformer( ::basegfx::B2DHomMatrix aMat ) :
285 maMatrix(std::move( aMat ))
286 {
287 }
288
289 sal_Int32 operator()( const double& rOffset )
290 {
291 // This is an optimization of the normal rMat*[x,0]
292 // transformation of the advancement vector (in x
293 // direction), followed by a length calculation of the
294 // resulting vector: advancement' =
295 // ||rMat*[x,0]||. Since advancements are vectors, we
296 // can ignore translational components, thus if [x,0],
297 // it follows that rMat*[x,0]=[x',0] holds. Thus, we
298 // just have to calc the transformation of the x
299 // component.
300
301 // TODO(F2): Handle non-horizontal advancements!
302 return ::basegfx::fround( hypot(maMatrix.get(0,0)*rOffset,
303 maMatrix.get(1,0)*rOffset) );
304 }
305
306 private:
308 };
309 }
310
311 KernArray TextLayout::setupTextOffsets(
312 const uno::Sequence< double >& inputOffsets,
313 const rendering::ViewState& viewState,
314 const rendering::RenderState& renderState ) const
315 {
317
319 viewState,
320 renderState);
321
322 // fill integer offsets
323 KernArray outputOffsets;
324 OffsetTransformer aTransform(aMatrix);
325 std::for_each(inputOffsets.begin(), inputOffsets.end(),
326 [&outputOffsets, &aTransform](double n) {outputOffsets.push_back(aTransform(n)); } );
327 return outputOffsets;
328 }
329
330 OUString SAL_CALL TextLayout::getImplementationName()
331 {
332 return "CairoCanvas::TextLayout";
333 }
334
335 sal_Bool SAL_CALL TextLayout::supportsService( const OUString& ServiceName )
336 {
337 return cppu::supportsService( this, ServiceName );
338 }
339
340 uno::Sequence< OUString > SAL_CALL TextLayout::getSupportedServiceNames()
341 {
342 return { "com.sun.star.rendering.TextLayout" };
343 }
344}
345
346/* 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