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