xref: /AOO41X/main/canvas/source/cairo/cairo_canvashelper_text.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_canvas.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <canvas/debug.hxx>
32*cdf0e10cSrcweir #include <canvas/canvastools.hxx>
33*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <vcl/virdev.hxx>
36*cdf0e10cSrcweir #include <vcl/metric.hxx>
37*cdf0e10cSrcweir #include <vcl/canvastools.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygon.hxx>
40*cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include "cairo_canvasfont.hxx"
43*cdf0e10cSrcweir #include "cairo_textlayout.hxx"
44*cdf0e10cSrcweir #include "cairo_canvashelper.hxx"
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir using namespace ::cairo;
47*cdf0e10cSrcweir using namespace ::com::sun::star;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace cairocanvas
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir 	enum ColorType
52*cdf0e10cSrcweir 	{
53*cdf0e10cSrcweir 		LINE_COLOR, FILL_COLOR, TEXT_COLOR, IGNORE_COLOR
54*cdf0e10cSrcweir 	};
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir     uno::Reference< rendering::XCanvasFont > CanvasHelper::createFont( const rendering::XCanvas* 					,
57*cdf0e10cSrcweir                                                                        const rendering::FontRequest& 				fontRequest,
58*cdf0e10cSrcweir                                                                        const uno::Sequence< beans::PropertyValue >& extraFontProperties,
59*cdf0e10cSrcweir                                                                        const geometry::Matrix2D& 					fontMatrix )
60*cdf0e10cSrcweir     {
61*cdf0e10cSrcweir         return uno::Reference< rendering::XCanvasFont >( new CanvasFont( fontRequest, extraFontProperties, fontMatrix, mpSurfaceProvider ));
62*cdf0e10cSrcweir     }
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     uno::Sequence< rendering::FontInfo > CanvasHelper::queryAvailableFonts( const rendering::XCanvas* 						,
65*cdf0e10cSrcweir                                                                             const rendering::FontInfo& 						/*aFilter*/,
66*cdf0e10cSrcweir                                                                             const uno::Sequence< beans::PropertyValue >& 	/*aFontProperties*/ )
67*cdf0e10cSrcweir     {
68*cdf0e10cSrcweir         // TODO
69*cdf0e10cSrcweir         return uno::Sequence< rendering::FontInfo >();
70*cdf0e10cSrcweir     }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 	static bool
73*cdf0e10cSrcweir 	setupFontTransform( ::OutputDevice&				    rOutDev,
74*cdf0e10cSrcweir 						::Point&						o_rPoint,
75*cdf0e10cSrcweir 						::Font& 						io_rVCLFont,
76*cdf0e10cSrcweir 						const rendering::ViewState& 	rViewState,
77*cdf0e10cSrcweir 						const rendering::RenderState& 	rRenderState )
78*cdf0e10cSrcweir 	{
79*cdf0e10cSrcweir 		::basegfx::B2DHomMatrix aMatrix;
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 		::canvas::tools::mergeViewAndRenderTransform(aMatrix,
82*cdf0e10cSrcweir 													 rViewState,
83*cdf0e10cSrcweir 													 rRenderState);
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 		::basegfx::B2DTuple aScale;
86*cdf0e10cSrcweir 		::basegfx::B2DTuple aTranslate;
87*cdf0e10cSrcweir 		double nRotate, nShearX;
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 		aMatrix.decompose( aScale, aTranslate, nRotate, nShearX );
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 		// query font metric _before_ tampering with width and height
92*cdf0e10cSrcweir 		if( !::rtl::math::approxEqual(aScale.getX(), aScale.getY()) )
93*cdf0e10cSrcweir         {
94*cdf0e10cSrcweir 			// retrieve true font width
95*cdf0e10cSrcweir 			const sal_Int32 nFontWidth( rOutDev.GetFontMetric( io_rVCLFont ).GetWidth() );
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 			const sal_Int32 nScaledFontWidth( ::basegfx::fround(nFontWidth * aScale.getX()) );
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 			if( !nScaledFontWidth )
100*cdf0e10cSrcweir 			{
101*cdf0e10cSrcweir 				// scale is smaller than one pixel - disable text
102*cdf0e10cSrcweir 				// output altogether
103*cdf0e10cSrcweir 				return false;
104*cdf0e10cSrcweir 			}
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 			io_rVCLFont.SetWidth( nScaledFontWidth );
107*cdf0e10cSrcweir 		}
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 		if( !::rtl::math::approxEqual(aScale.getY(), 1.0) )
110*cdf0e10cSrcweir         {
111*cdf0e10cSrcweir 			const sal_Int32 nFontHeight( io_rVCLFont.GetHeight() );
112*cdf0e10cSrcweir 			io_rVCLFont.SetHeight( ::basegfx::fround(nFontHeight * aScale.getY()) );
113*cdf0e10cSrcweir 		}
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 		io_rVCLFont.SetOrientation( static_cast< short >( ::basegfx::fround(-fmod(nRotate, 2*M_PI)*(1800.0/M_PI)) ) );
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 		// TODO(F2): Missing functionality in VCL: shearing
118*cdf0e10cSrcweir 		o_rPoint.X() = ::basegfx::fround(aTranslate.getX());
119*cdf0e10cSrcweir 		o_rPoint.Y() = ::basegfx::fround(aTranslate.getY());
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 		return true;
122*cdf0e10cSrcweir 	}
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     static int
125*cdf0e10cSrcweir 	setupOutDevState( OutputDevice&                 rOutDev,
126*cdf0e10cSrcweir                       const rendering::XCanvas*     pOwner,
127*cdf0e10cSrcweir                       const rendering::ViewState& 	viewState,
128*cdf0e10cSrcweir                       const rendering::RenderState& renderState,
129*cdf0e10cSrcweir                       ColorType						eColorType )
130*cdf0e10cSrcweir     {
131*cdf0e10cSrcweir         ::canvas::tools::verifyInput( renderState,
132*cdf0e10cSrcweir                                       BOOST_CURRENT_FUNCTION,
133*cdf0e10cSrcweir                                       const_cast<rendering::XCanvas*>(pOwner), // only for refcount
134*cdf0e10cSrcweir                                       2,
135*cdf0e10cSrcweir                                       eColorType == IGNORE_COLOR ? 0 : 3 );
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir         int nTransparency(0);
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir         // TODO(P2): Don't change clipping all the time, maintain current clip
140*cdf0e10cSrcweir         // state and change only when update is necessary
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir         // accumulate non-empty clips into one region
143*cdf0e10cSrcweir         // ==========================================
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir         Region aClipRegion;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         if( viewState.Clip.is() )
148*cdf0e10cSrcweir         {
149*cdf0e10cSrcweir             ::basegfx::B2DPolyPolygon aClipPoly(
150*cdf0e10cSrcweir                 ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(
151*cdf0e10cSrcweir                     viewState.Clip) );
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir             if( aClipPoly.count() )
154*cdf0e10cSrcweir             {
155*cdf0e10cSrcweir                 // setup non-empty clipping
156*cdf0e10cSrcweir                 ::basegfx::B2DHomMatrix aMatrix;
157*cdf0e10cSrcweir                 aClipPoly.transform(
158*cdf0e10cSrcweir                     ::basegfx::unotools::homMatrixFromAffineMatrix( aMatrix,
159*cdf0e10cSrcweir                                                                     viewState.AffineTransform ) );
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir                 aClipRegion = Region::GetRegionFromPolyPolygon( ::PolyPolygon( aClipPoly ) );
162*cdf0e10cSrcweir             }
163*cdf0e10cSrcweir         }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir         if( renderState.Clip.is() )
166*cdf0e10cSrcweir         {
167*cdf0e10cSrcweir             ::basegfx::B2DPolyPolygon aClipPoly(
168*cdf0e10cSrcweir                 ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(
169*cdf0e10cSrcweir                     renderState.Clip) );
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir             ::basegfx::B2DHomMatrix aMatrix;
172*cdf0e10cSrcweir             aClipPoly.transform(
173*cdf0e10cSrcweir                 ::canvas::tools::mergeViewAndRenderTransform( aMatrix,
174*cdf0e10cSrcweir                                                               viewState,
175*cdf0e10cSrcweir                                                               renderState ) );
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir             if( aClipPoly.count() )
178*cdf0e10cSrcweir             {
179*cdf0e10cSrcweir                 // setup non-empty clipping
180*cdf0e10cSrcweir                 Region aRegion = Region::GetRegionFromPolyPolygon( ::PolyPolygon( aClipPoly ) );
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir                 if( aClipRegion.IsEmpty() )
183*cdf0e10cSrcweir                     aClipRegion = aRegion;
184*cdf0e10cSrcweir                 else
185*cdf0e10cSrcweir                     aClipRegion.Intersect( aRegion );
186*cdf0e10cSrcweir             }
187*cdf0e10cSrcweir             else
188*cdf0e10cSrcweir             {
189*cdf0e10cSrcweir                 // clip polygon is empty
190*cdf0e10cSrcweir                 aClipRegion.SetEmpty();
191*cdf0e10cSrcweir             }
192*cdf0e10cSrcweir         }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir         // setup accumulated clip region. Note that setting an
195*cdf0e10cSrcweir         // empty clip region denotes "clip everything" on the
196*cdf0e10cSrcweir         // OutputDevice (which is why we translate that into
197*cdf0e10cSrcweir         // SetClipRegion() here). When both view and render clip
198*cdf0e10cSrcweir         // are empty, aClipRegion remains default-constructed,
199*cdf0e10cSrcweir         // i.e. empty, too.
200*cdf0e10cSrcweir         if( aClipRegion.IsEmpty() )
201*cdf0e10cSrcweir         {
202*cdf0e10cSrcweir             rOutDev.SetClipRegion();
203*cdf0e10cSrcweir         }
204*cdf0e10cSrcweir         else
205*cdf0e10cSrcweir         {
206*cdf0e10cSrcweir             rOutDev.SetClipRegion( aClipRegion );
207*cdf0e10cSrcweir         }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir         if( eColorType != IGNORE_COLOR )
210*cdf0e10cSrcweir         {
211*cdf0e10cSrcweir             Color aColor( COL_WHITE );
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir             if( renderState.DeviceColor.getLength() > 2 )
214*cdf0e10cSrcweir             {
215*cdf0e10cSrcweir                 aColor = ::vcl::unotools::stdColorSpaceSequenceToColor( renderState.DeviceColor );
216*cdf0e10cSrcweir             }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir             // extract alpha, and make color opaque
219*cdf0e10cSrcweir             // afterwards. Otherwise, OutputDevice won't draw anything
220*cdf0e10cSrcweir             nTransparency = aColor.GetTransparency();
221*cdf0e10cSrcweir             aColor.SetTransparency(0);
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir             switch( eColorType )
224*cdf0e10cSrcweir             {
225*cdf0e10cSrcweir                 case LINE_COLOR:
226*cdf0e10cSrcweir                     rOutDev.SetLineColor( aColor );
227*cdf0e10cSrcweir                     rOutDev.SetFillColor();
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir                     break;
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir                 case FILL_COLOR:
232*cdf0e10cSrcweir                     rOutDev.SetFillColor( aColor );
233*cdf0e10cSrcweir                     rOutDev.SetLineColor();
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir                     break;
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir                 case TEXT_COLOR:
238*cdf0e10cSrcweir                     rOutDev.SetTextColor( aColor );
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir                     break;
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir                 default:
243*cdf0e10cSrcweir                     ENSURE_OR_THROW( false,
244*cdf0e10cSrcweir                                       "CanvasHelper::setupOutDevState(): Unexpected color type");
245*cdf0e10cSrcweir                     break;
246*cdf0e10cSrcweir             }
247*cdf0e10cSrcweir         }
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir         return nTransparency;
250*cdf0e10cSrcweir     }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir     bool setupTextOutput( OutputDevice&                                     rOutDev,
253*cdf0e10cSrcweir                           const rendering::XCanvas*                         pOwner,
254*cdf0e10cSrcweir 						  ::Point&										    o_rOutPos,
255*cdf0e10cSrcweir 						  const rendering::ViewState& 					    viewState,
256*cdf0e10cSrcweir 						  const rendering::RenderState& 					renderState,
257*cdf0e10cSrcweir 						  const uno::Reference< rendering::XCanvasFont >&	xFont	)
258*cdf0e10cSrcweir     {
259*cdf0e10cSrcweir         setupOutDevState( rOutDev, pOwner, viewState, renderState, TEXT_COLOR );
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir         ::Font aVCLFont;
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir         CanvasFont* pFont = dynamic_cast< CanvasFont* >( xFont.get() );
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( pFont,
266*cdf0e10cSrcweir                          "CanvasHelper::setupTextOutput(): Font not compatible with this canvas" );
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir         aVCLFont = pFont->getVCLFont();
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir         Color aColor( COL_BLACK );
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir         if( renderState.DeviceColor.getLength() > 2 )
273*cdf0e10cSrcweir         {
274*cdf0e10cSrcweir             aColor = ::vcl::unotools::stdColorSpaceSequenceToColor(renderState.DeviceColor );
275*cdf0e10cSrcweir         }
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir         // setup font color
278*cdf0e10cSrcweir         aVCLFont.SetColor( aColor );
279*cdf0e10cSrcweir         aVCLFont.SetFillColor( aColor );
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir         // no need to replicate this for mp2ndOutDev, we're modifying only aVCLFont here.
282*cdf0e10cSrcweir         if( !setupFontTransform( rOutDev, o_rOutPos, aVCLFont, viewState, renderState ) )
283*cdf0e10cSrcweir             return false;
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir         rOutDev.SetFont( aVCLFont );
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir         return true;
289*cdf0e10cSrcweir     }
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawText( const rendering::XCanvas* 						pOwner,
292*cdf0e10cSrcweir                                                                           const rendering::StringContext& 					text,
293*cdf0e10cSrcweir                                                                           const uno::Reference< rendering::XCanvasFont >& 	xFont,
294*cdf0e10cSrcweir                                                                           const rendering::ViewState& 						viewState,
295*cdf0e10cSrcweir                                                                           const rendering::RenderState& 					renderState,
296*cdf0e10cSrcweir                                                                           sal_Int8				 							textDirection )
297*cdf0e10cSrcweir     {
298*cdf0e10cSrcweir #ifdef CAIRO_CANVAS_PERF_TRACE
299*cdf0e10cSrcweir 		struct timespec aTimer;
300*cdf0e10cSrcweir 		mxDevice->startPerfTrace( &aTimer );
301*cdf0e10cSrcweir #endif
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( xFont.is(),
304*cdf0e10cSrcweir                          "CanvasHelper::drawText(): font is NULL");
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir 		if( !mpVirtualDevice )
307*cdf0e10cSrcweir 			mpVirtualDevice = mpSurface->createVirtualDevice();
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir         if( mpVirtualDevice )
310*cdf0e10cSrcweir 		{
311*cdf0e10cSrcweir #if defined CAIRO_HAS_WIN32_SURFACE
312*cdf0e10cSrcweir             // FIXME: Some kind of work-araound...
313*cdf0e10cSrcweir             cairo_rectangle (mpSurface->getCairo().get(), 0, 0, 0, 0);
314*cdf0e10cSrcweir             cairo_fill(mpSurface->getCairo().get());
315*cdf0e10cSrcweir #endif
316*cdf0e10cSrcweir             ::Point aOutpos;
317*cdf0e10cSrcweir 			if( !setupTextOutput( *mpVirtualDevice, pOwner, aOutpos, viewState, renderState, xFont ) )
318*cdf0e10cSrcweir 				return uno::Reference< rendering::XCachedPrimitive >(NULL); // no output necessary
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir 				// change text direction and layout mode
321*cdf0e10cSrcweir 			sal_uLong nLayoutMode(0);
322*cdf0e10cSrcweir 			switch( textDirection )
323*cdf0e10cSrcweir 				{
324*cdf0e10cSrcweir 				case rendering::TextDirection::WEAK_LEFT_TO_RIGHT:
325*cdf0e10cSrcweir 					nLayoutMode |= TEXT_LAYOUT_BIDI_LTR;
326*cdf0e10cSrcweir 					// FALLTHROUGH intended
327*cdf0e10cSrcweir 				case rendering::TextDirection::STRONG_LEFT_TO_RIGHT:
328*cdf0e10cSrcweir 					nLayoutMode |= TEXT_LAYOUT_BIDI_LTR | TEXT_LAYOUT_BIDI_STRONG;
329*cdf0e10cSrcweir 					nLayoutMode |= TEXT_LAYOUT_TEXTORIGIN_LEFT;
330*cdf0e10cSrcweir 					break;
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir 				case rendering::TextDirection::WEAK_RIGHT_TO_LEFT:
333*cdf0e10cSrcweir 					nLayoutMode |= TEXT_LAYOUT_BIDI_RTL;
334*cdf0e10cSrcweir 					// FALLTHROUGH intended
335*cdf0e10cSrcweir 				case rendering::TextDirection::STRONG_RIGHT_TO_LEFT:
336*cdf0e10cSrcweir 					nLayoutMode |= TEXT_LAYOUT_BIDI_RTL | TEXT_LAYOUT_BIDI_STRONG;
337*cdf0e10cSrcweir 					nLayoutMode |= TEXT_LAYOUT_TEXTORIGIN_RIGHT;
338*cdf0e10cSrcweir 					break;
339*cdf0e10cSrcweir 				}
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir 			// TODO(F2): alpha
342*cdf0e10cSrcweir 			mpVirtualDevice->SetLayoutMode( nLayoutMode );
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir             OSL_TRACE(":cairocanvas::CanvasHelper::drawText(O,t,f,v,r,d): %s", ::rtl::OUStringToOString( text.Text.copy( text.StartPosition, text.Length ),
345*cdf0e10cSrcweir                                                                                                          RTL_TEXTENCODING_UTF8 ).getStr());
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir             TextLayout* pTextLayout = new TextLayout(text, textDirection, 0, CanvasFont::Reference(dynamic_cast< CanvasFont* >( xFont.get() )), mpSurfaceProvider);
348*cdf0e10cSrcweir             pTextLayout->draw( mpSurface, *mpVirtualDevice, aOutpos, viewState, renderState );
349*cdf0e10cSrcweir 		}
350*cdf0e10cSrcweir 
351*cdf0e10cSrcweir         return uno::Reference< rendering::XCachedPrimitive >(NULL);
352*cdf0e10cSrcweir     }
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawTextLayout( const rendering::XCanvas* 						pOwner,
355*cdf0e10cSrcweir                                                                                 const uno::Reference< rendering::XTextLayout >& xLayoutedText,
356*cdf0e10cSrcweir                                                                                 const rendering::ViewState& 					viewState,
357*cdf0e10cSrcweir                                                                                 const rendering::RenderState& 					renderState )
358*cdf0e10cSrcweir     {
359*cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( xLayoutedText.is(),
360*cdf0e10cSrcweir                          "CanvasHelper::drawTextLayout(): layout is NULL");
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir         TextLayout* pTextLayout = dynamic_cast< TextLayout* >( xLayoutedText.get() );
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir         if( pTextLayout )
365*cdf0e10cSrcweir         {
366*cdf0e10cSrcweir 			if( !mpVirtualDevice )
367*cdf0e10cSrcweir 				mpVirtualDevice = mpSurface->createVirtualDevice();
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir             if( mpVirtualDevice )
370*cdf0e10cSrcweir             {
371*cdf0e10cSrcweir #if defined CAIRO_HAS_WIN32_SURFACE
372*cdf0e10cSrcweir                 // FIXME: Some kind of work-araound...
373*cdf0e10cSrcweir                 cairo_rectangle( mpSurface->getCairo().get(), 0, 0, 0, 0);
374*cdf0e10cSrcweir                 cairo_fill(mpSurface->getCairo().get());
375*cdf0e10cSrcweir #endif
376*cdf0e10cSrcweir                 // TODO(T3): Race condition. We're taking the font
377*cdf0e10cSrcweir                 // from xLayoutedText, and then calling draw() at it,
378*cdf0e10cSrcweir                 // without exclusive access. Move setupTextOutput(),
379*cdf0e10cSrcweir                 // e.g. to impltools?
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir                 ::Point aOutpos;
382*cdf0e10cSrcweir                 if( !setupTextOutput( *mpVirtualDevice, pOwner, aOutpos, viewState, renderState, xLayoutedText->getFont() ) )
383*cdf0e10cSrcweir                     return uno::Reference< rendering::XCachedPrimitive >(NULL); // no output necessary
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir                 // TODO(F2): What about the offset scalings?
386*cdf0e10cSrcweir                 pTextLayout->draw( mpSurface, *mpVirtualDevice, aOutpos, viewState, renderState );
387*cdf0e10cSrcweir             }
388*cdf0e10cSrcweir         }
389*cdf0e10cSrcweir         else
390*cdf0e10cSrcweir         {
391*cdf0e10cSrcweir             ENSURE_ARG_OR_THROW( false,
392*cdf0e10cSrcweir                              "CanvasHelper::drawTextLayout(): TextLayout not compatible with this canvas" );
393*cdf0e10cSrcweir         }
394*cdf0e10cSrcweir 
395*cdf0e10cSrcweir         return uno::Reference< rendering::XCachedPrimitive >(NULL);
396*cdf0e10cSrcweir     }
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir }
399