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 <math.h>
21 :
22 : #include <canvas/debug.hxx>
23 : #include <canvas/verbosetrace.hxx>
24 : #include <cppuhelper/supportsservice.hxx>
25 : #include <tools/diagnose_ex.h>
26 :
27 : #include <vcl/metric.hxx>
28 : #include <vcl/virdev.hxx>
29 :
30 : #ifdef WNT
31 : #ifdef max
32 : #undef max
33 : #endif
34 : #ifdef min
35 : #undef min
36 : #endif
37 : #endif
38 : #include <vcl/sysdata.hxx>
39 :
40 : #include <basegfx/matrix/b2dhommatrix.hxx>
41 : #include <basegfx/numeric/ftools.hxx>
42 :
43 : #include <boost/scoped_array.hpp>
44 :
45 : #include "cairo_textlayout.hxx"
46 : #include "cairo_spritecanvas.hxx"
47 :
48 : #ifdef CAIRO_HAS_QUARTZ_SURFACE
49 : # include "cairo_quartz_cairo.hxx"
50 : #elif defined CAIRO_HAS_WIN32_SURFACE
51 : # include "cairo_win32_cairo.hxx"
52 : # include <cairo-win32.h>
53 : #elif defined CAIRO_HAS_XLIB_SURFACE
54 : # include "cairo_xlib_cairo.hxx"
55 : # include <cairo-ft.h>
56 : #else
57 : # error Native API needed.
58 : #endif
59 :
60 : #ifdef IOS
61 : #include <CoreText/CoreText.h>
62 : #endif
63 :
64 : using namespace ::cairo;
65 : using namespace ::com::sun::star;
66 :
67 : namespace cairocanvas
68 : {
69 : namespace
70 : {
71 0 : void setupLayoutMode( OutputDevice& rOutDev,
72 : sal_Int8 nTextDirection )
73 : {
74 : // TODO(P3): avoid if already correctly set
75 : sal_uLong nLayoutMode;
76 0 : switch( nTextDirection )
77 : {
78 : default:
79 0 : nLayoutMode = 0;
80 0 : break;
81 : case rendering::TextDirection::WEAK_LEFT_TO_RIGHT:
82 0 : nLayoutMode = TEXT_LAYOUT_BIDI_LTR;
83 0 : break;
84 : case rendering::TextDirection::STRONG_LEFT_TO_RIGHT:
85 0 : nLayoutMode = TEXT_LAYOUT_BIDI_LTR | TEXT_LAYOUT_BIDI_STRONG;
86 0 : break;
87 : case rendering::TextDirection::WEAK_RIGHT_TO_LEFT:
88 0 : nLayoutMode = TEXT_LAYOUT_BIDI_RTL;
89 0 : break;
90 : case rendering::TextDirection::STRONG_RIGHT_TO_LEFT:
91 0 : nLayoutMode = TEXT_LAYOUT_BIDI_RTL | TEXT_LAYOUT_BIDI_STRONG;
92 0 : break;
93 : }
94 :
95 : // set calculated layout mode. Origin is always the left edge,
96 : // as required at the API spec
97 0 : rOutDev.SetLayoutMode( nLayoutMode | TEXT_LAYOUT_TEXTORIGIN_LEFT );
98 0 : }
99 :
100 0 : bool compareFallbacks(const SystemGlyphData&rA, const SystemGlyphData &rB)
101 : {
102 0 : return rA.fallbacklevel < rB.fallbacklevel;
103 : }
104 : }
105 :
106 0 : TextLayout::TextLayout( const rendering::StringContext& aText,
107 : sal_Int8 nDirection,
108 : sal_Int64 /*nRandomSeed*/,
109 : const CanvasFont::Reference& rFont,
110 : const SurfaceProviderRef& rRefDevice ) :
111 : TextLayout_Base( m_aMutex ),
112 : maText( aText ),
113 : maLogicalAdvancements(),
114 : mpFont( rFont ),
115 : mpRefDevice( rRefDevice ),
116 0 : mnTextDirection( nDirection )
117 : {
118 0 : }
119 :
120 0 : TextLayout::~TextLayout()
121 : {
122 0 : }
123 :
124 0 : void SAL_CALL TextLayout::disposing()
125 : {
126 0 : ::osl::MutexGuard aGuard( m_aMutex );
127 :
128 0 : mpFont.reset();
129 0 : mpRefDevice.clear();
130 0 : }
131 :
132 : // XTextLayout
133 0 : uno::Sequence< uno::Reference< rendering::XPolyPolygon2D > > SAL_CALL TextLayout::queryTextShapes( ) throw (uno::RuntimeException, std::exception)
134 : {
135 0 : ::osl::MutexGuard aGuard( m_aMutex );
136 :
137 : // TODO
138 0 : return uno::Sequence< uno::Reference< rendering::XPolyPolygon2D > >();
139 : }
140 :
141 0 : uno::Sequence< geometry::RealRectangle2D > SAL_CALL TextLayout::queryInkMeasures( ) throw (uno::RuntimeException, std::exception)
142 : {
143 0 : ::osl::MutexGuard aGuard( m_aMutex );
144 :
145 : // TODO
146 0 : return uno::Sequence< geometry::RealRectangle2D >();
147 : }
148 :
149 0 : uno::Sequence< geometry::RealRectangle2D > SAL_CALL TextLayout::queryMeasures( ) throw (uno::RuntimeException, std::exception)
150 : {
151 0 : ::osl::MutexGuard aGuard( m_aMutex );
152 :
153 : // TODO
154 0 : return uno::Sequence< geometry::RealRectangle2D >();
155 : }
156 :
157 0 : uno::Sequence< double > SAL_CALL TextLayout::queryLogicalAdvancements( ) throw (uno::RuntimeException, std::exception)
158 : {
159 0 : ::osl::MutexGuard aGuard( m_aMutex );
160 :
161 0 : return maLogicalAdvancements;
162 : }
163 :
164 0 : void SAL_CALL TextLayout::applyLogicalAdvancements( const uno::Sequence< double >& aAdvancements ) throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
165 : {
166 0 : ::osl::MutexGuard aGuard( m_aMutex );
167 :
168 0 : if( aAdvancements.getLength() != maText.Length )
169 : {
170 : OSL_TRACE( "TextLayout::applyLogicalAdvancements(): mismatching number of advancements" );
171 0 : throw lang::IllegalArgumentException();
172 : }
173 :
174 0 : maLogicalAdvancements = aAdvancements;
175 0 : }
176 :
177 0 : geometry::RealRectangle2D SAL_CALL TextLayout::queryTextBounds( ) throw (uno::RuntimeException, std::exception)
178 : {
179 0 : ::osl::MutexGuard aGuard( m_aMutex );
180 :
181 0 : OutputDevice* pOutDev = mpRefDevice->getOutputDevice();
182 0 : if( !pOutDev )
183 0 : return geometry::RealRectangle2D();
184 :
185 0 : VirtualDevice aVDev( *pOutDev );
186 0 : aVDev.SetFont( mpFont->getVCLFont() );
187 :
188 : // need metrics for Y offset, the XCanvas always renders
189 : // relative to baseline
190 0 : const ::FontMetric& aMetric( aVDev.GetFontMetric() );
191 :
192 0 : setupLayoutMode( aVDev, mnTextDirection );
193 :
194 0 : const sal_Int32 nAboveBaseline( -aMetric.GetIntLeading() - aMetric.GetAscent() );
195 0 : const sal_Int32 nBelowBaseline( aMetric.GetDescent() );
196 :
197 0 : if( maLogicalAdvancements.getLength() )
198 : {
199 : return geometry::RealRectangle2D( 0, nAboveBaseline,
200 0 : maLogicalAdvancements[ maLogicalAdvancements.getLength()-1 ],
201 0 : nBelowBaseline );
202 : }
203 : else
204 : {
205 : return geometry::RealRectangle2D( 0, nAboveBaseline,
206 : aVDev.GetTextWidth(
207 : maText.Text,
208 0 : ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
209 0 : ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) ),
210 0 : nBelowBaseline );
211 0 : }
212 : }
213 :
214 0 : double SAL_CALL TextLayout::justify( double /*nSize*/ ) throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
215 : {
216 0 : ::osl::MutexGuard aGuard( m_aMutex );
217 :
218 : // TODO
219 0 : return 0.0;
220 : }
221 :
222 0 : double SAL_CALL TextLayout::combinedJustify( const uno::Sequence< uno::Reference< rendering::XTextLayout > >& /*aNextLayouts*/,
223 : double /*nSize*/ ) throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
224 : {
225 0 : ::osl::MutexGuard aGuard( m_aMutex );
226 :
227 : // TODO
228 0 : return 0.0;
229 : }
230 :
231 0 : rendering::TextHit SAL_CALL TextLayout::getTextHit( const geometry::RealPoint2D& /*aHitPoint*/ ) throw (uno::RuntimeException, std::exception)
232 : {
233 0 : ::osl::MutexGuard aGuard( m_aMutex );
234 :
235 : // TODO
236 0 : return rendering::TextHit();
237 : }
238 :
239 0 : rendering::Caret SAL_CALL TextLayout::getCaret( sal_Int32 /*nInsertionIndex*/,
240 : sal_Bool /*bExcludeLigatures*/ ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
241 : {
242 0 : ::osl::MutexGuard aGuard( m_aMutex );
243 :
244 : // TODO
245 0 : return rendering::Caret();
246 : }
247 :
248 0 : sal_Int32 SAL_CALL TextLayout::getNextInsertionIndex( sal_Int32 /*nStartIndex*/,
249 : sal_Int32 /*nCaretAdvancement*/,
250 : sal_Bool /*bExcludeLigatures*/ ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
251 : {
252 0 : ::osl::MutexGuard aGuard( m_aMutex );
253 :
254 : // TODO
255 0 : return 0;
256 : }
257 :
258 0 : uno::Reference< rendering::XPolyPolygon2D > SAL_CALL TextLayout::queryVisualHighlighting( sal_Int32 /*nStartIndex*/,
259 : sal_Int32 /*nEndIndex*/ ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
260 : {
261 0 : ::osl::MutexGuard aGuard( m_aMutex );
262 :
263 : // TODO
264 0 : return uno::Reference< rendering::XPolyPolygon2D >();
265 : }
266 :
267 0 : uno::Reference< rendering::XPolyPolygon2D > SAL_CALL TextLayout::queryLogicalHighlighting( sal_Int32 /*nStartIndex*/,
268 : sal_Int32 /*nEndIndex*/ ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
269 : {
270 0 : ::osl::MutexGuard aGuard( m_aMutex );
271 :
272 : // TODO
273 0 : return uno::Reference< rendering::XPolyPolygon2D >();
274 : }
275 :
276 0 : double SAL_CALL TextLayout::getBaselineOffset( ) throw (uno::RuntimeException, std::exception)
277 : {
278 0 : ::osl::MutexGuard aGuard( m_aMutex );
279 :
280 : // TODO
281 0 : return 0.0;
282 : }
283 :
284 0 : sal_Int8 SAL_CALL TextLayout::getMainTextDirection( ) throw (uno::RuntimeException, std::exception)
285 : {
286 0 : ::osl::MutexGuard aGuard( m_aMutex );
287 :
288 0 : return mnTextDirection;
289 : }
290 :
291 0 : uno::Reference< rendering::XCanvasFont > SAL_CALL TextLayout::getFont( ) throw (uno::RuntimeException, std::exception)
292 : {
293 0 : ::osl::MutexGuard aGuard( m_aMutex );
294 :
295 0 : return mpFont.getRef();
296 : }
297 :
298 0 : rendering::StringContext SAL_CALL TextLayout::getText( ) throw (uno::RuntimeException, std::exception)
299 : {
300 0 : ::osl::MutexGuard aGuard( m_aMutex );
301 :
302 0 : return maText;
303 : }
304 :
305 : /**
306 : * TextLayout::isCairoRenderable
307 : *
308 : * Features currenly not supported by Cairo (VCL rendering is used as fallback):
309 : * - vertical glyphs
310 : *
311 : * @return true, if text/font can be rendered with cairo
312 : **/
313 0 : bool TextLayout::isCairoRenderable(SystemFontData aSysFontData) const
314 : {
315 : #if defined UNX && !defined MACOSX && !defined IOS
316 : // is font usable?
317 0 : if (!aSysFontData.nFontId)
318 0 : return false;
319 : #endif
320 :
321 : // vertical glyph rendering is not supported in cairo for now
322 0 : if (aSysFontData.bVerticalCharacterType)
323 : {
324 : OSL_TRACE(":cairocanvas::TextLayout::isCairoRenderable(): ***************** VERTICAL CHARACTER STYLE!!! ****************");
325 0 : return false;
326 : }
327 :
328 0 : return true;
329 : }
330 :
331 : /**
332 : * TextLayout::draw
333 : *
334 : * Cairo-based text rendering. Draw text directly on the cairo surface with cairo fonts.
335 : * Avoid using VCL VirtualDevices for that, bypassing VCL DrawText functions, when possible
336 : *
337 : * Note: some text effects are not rendered due to lacking generic canvas or cairo canvas
338 : * implementation. See issues 92657, 92658, 92659, 92660, 97529
339 : *
340 : * @return true, if successful
341 : **/
342 0 : bool TextLayout::draw( SurfaceSharedPtr& pSurface,
343 : OutputDevice& rOutDev,
344 : const Point& rOutpos,
345 : const rendering::ViewState& viewState,
346 : const rendering::RenderState& renderState ) const
347 : {
348 0 : ::osl::MutexGuard aGuard( m_aMutex );
349 0 : SystemTextLayoutData aSysLayoutData;
350 : #if (defined CAIRO_HAS_WIN32_SURFACE) && (OSL_DEBUG_LEVEL > 1)
351 : LOGFONTW logfont;
352 : #endif
353 0 : setupLayoutMode( rOutDev, mnTextDirection );
354 :
355 : // TODO(P2): cache that
356 0 : ::boost::scoped_array< sal_Int32 > aOffsets(new sal_Int32[maLogicalAdvancements.getLength()]);
357 :
358 0 : if( maLogicalAdvancements.getLength() )
359 : {
360 0 : setupTextOffsets( aOffsets.get(), maLogicalAdvancements, viewState, renderState );
361 :
362 : // TODO(F3): ensure correct length and termination for DX
363 : // array (last entry _must_ contain the overall width)
364 : }
365 :
366 0 : aSysLayoutData = rOutDev.GetSysTextLayoutData(rOutpos, maText.Text,
367 0 : ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
368 0 : ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length),
369 0 : maLogicalAdvancements.getLength() ? aOffsets.get() : NULL);
370 :
371 : // Sort them so that all glyphs on the same glyph fallback level are consecutive
372 0 : std::sort(aSysLayoutData.rGlyphData.begin(), aSysLayoutData.rGlyphData.end(), compareFallbacks);
373 0 : bool bCairoRenderable = true;
374 :
375 : //Pull all the fonts we need to render the text
376 : typedef std::pair<SystemFontData,int> FontLevel;
377 : typedef std::vector<FontLevel> FontLevelVector;
378 0 : FontLevelVector aFontData;
379 0 : SystemGlyphDataVector::const_iterator aGlyphIter=aSysLayoutData.rGlyphData.begin();
380 0 : const SystemGlyphDataVector::const_iterator aGlyphEnd=aSysLayoutData.rGlyphData.end();
381 0 : for( ; aGlyphIter != aGlyphEnd; ++aGlyphIter )
382 : {
383 0 : if( aFontData.empty() || aGlyphIter->fallbacklevel != aFontData.back().second )
384 : {
385 0 : aFontData.push_back(FontLevel(rOutDev.GetSysFontData(aGlyphIter->fallbacklevel),
386 0 : aGlyphIter->fallbacklevel));
387 0 : if( !isCairoRenderable(aFontData.back().first) )
388 : {
389 0 : bCairoRenderable = false;
390 : OSL_TRACE(":cairocanvas::TextLayout::draw(S,O,p,v,r): VCL FALLBACK %s%s%s%s - %s",
391 : maLogicalAdvancements.getLength() ? "ADV " : "",
392 : aFontData.back().first.bAntialias ? "AA " : "",
393 : aFontData.back().first.bFakeBold ? "FB " : "",
394 : aFontData.back().first.bFakeItalic ? "FI " : "",
395 : OUStringToOString( maText.Text.copy( maText.StartPosition, maText.Length ),
396 : RTL_TEXTENCODING_UTF8 ).getStr());
397 0 : break;
398 : }
399 : }
400 : }
401 :
402 : // The ::GetSysTextLayoutData(), i.e. layouting of text to glyphs can change the font being used.
403 : // The fallback checks need to be done after final font is known.
404 0 : if (!bCairoRenderable) // VCL FALLBACKS
405 : {
406 0 : if (maLogicalAdvancements.getLength()) // VCL FALLBACK - with glyph advances
407 : {
408 0 : rOutDev.DrawTextArray( rOutpos, maText.Text, aOffsets.get(),
409 0 : ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
410 0 : ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) );
411 0 : return true;
412 : }
413 : else // VCL FALLBACK - without advances
414 : {
415 : rOutDev.DrawText( rOutpos, maText.Text,
416 0 : ::canvas::tools::numeric_cast<sal_uInt16>(maText.StartPosition),
417 0 : ::canvas::tools::numeric_cast<sal_uInt16>(maText.Length) );
418 0 : return true;
419 : }
420 : }
421 :
422 0 : if (aSysLayoutData.rGlyphData.empty())
423 0 : return false; //??? false?
424 :
425 : /**
426 : * Setup platform independent glyph vector into cairo-based glyphs vector.
427 : **/
428 :
429 : // Loop through the fonts used and render the matching glyphs for each
430 0 : FontLevelVector::const_iterator aFontDataIter = aFontData.begin();
431 0 : const FontLevelVector::const_iterator aFontDataEnd = aFontData.end();
432 0 : for( ; aFontDataIter != aFontDataEnd; ++aFontDataIter )
433 : {
434 0 : const SystemFontData &rSysFontData = aFontDataIter->first;
435 :
436 : // setup glyphs
437 0 : std::vector<cairo_glyph_t> cairo_glyphs;
438 0 : cairo_glyphs.reserve( 256 );
439 :
440 0 : aGlyphIter=aSysLayoutData.rGlyphData.begin();
441 0 : for( ; aGlyphIter != aGlyphEnd; ++aGlyphIter )
442 : {
443 0 : SystemGlyphData systemGlyph = *aGlyphIter;
444 0 : if( systemGlyph.fallbacklevel != aFontDataIter->second )
445 0 : continue;
446 :
447 : cairo_glyph_t aGlyph;
448 0 : aGlyph.index = systemGlyph.index;
449 : #ifdef CAIRO_HAS_WIN32_SURFACE
450 : // Cairo requires standard glyph indexes (ETO_GLYPH_INDEX), while vcl/win/* uses ucs4 chars.
451 : // Convert to standard indexes
452 : aGlyph.index = cairo::ucs4toindex((unsigned int) aGlyph.index, rSysFontData.hFont);
453 : #endif
454 0 : aGlyph.x = systemGlyph.x;
455 0 : aGlyph.y = systemGlyph.y;
456 0 : cairo_glyphs.push_back(aGlyph);
457 : }
458 :
459 0 : if (cairo_glyphs.empty())
460 0 : continue;
461 :
462 : /**
463 : * Setup font
464 : **/
465 0 : cairo_font_face_t* font_face = NULL;
466 :
467 : #ifdef CAIRO_HAS_QUARTZ_SURFACE
468 : # ifdef MACOSX
469 : // TODO: use cairo_quartz_font_face_create_for_cgfont(cgFont)
470 : // when CGFont (Mac OS X 10.5 API) is provided by the AQUA VCL backend.
471 : font_face = cairo_quartz_font_face_create_for_atsu_font_id((ATSUFontID) rSysFontData.aATSUFontID);
472 : # else // iOS
473 : font_face = cairo_quartz_font_face_create_for_cgfont( CTFontCopyGraphicsFont( rSysFontData.rCTFont, NULL ) );
474 : # endif
475 :
476 : #elif defined CAIRO_HAS_WIN32_SURFACE
477 : # if (OSL_DEBUG_LEVEL > 1)
478 : GetObjectW( rSysFontData.hFont, sizeof(logfont), &logfont );
479 : # endif
480 : // Note: cairo library uses logfont fallbacks when lfEscapement, lfOrientation and lfWidth are not zero.
481 : // VCL always has non-zero value for lfWidth
482 : font_face = cairo_win32_font_face_create_for_hfont(rSysFontData.hFont);
483 :
484 : #elif defined CAIRO_HAS_XLIB_SURFACE
485 : font_face = cairo_ft_font_face_create_for_ft_face((FT_Face)rSysFontData.nFontId,
486 0 : rSysFontData.nFontFlags);
487 : #else
488 : # error Native API needed.
489 : #endif
490 :
491 0 : CairoSharedPtr pSCairo = pSurface->getCairo();
492 :
493 0 : cairo_set_font_face( pSCairo.get(), font_face);
494 :
495 : // create default font options. cairo_get_font_options() does not retrieve the surface defaults,
496 : // only what has been set before with cairo_set_font_options()
497 0 : cairo_font_options_t* options = cairo_font_options_create();
498 0 : if (rSysFontData.bAntialias)
499 : {
500 : // CAIRO_ANTIALIAS_GRAY provides more similar result to VCL Canvas,
501 : // so we're not using CAIRO_ANTIALIAS_SUBPIXEL
502 0 : cairo_font_options_set_antialias(options, CAIRO_ANTIALIAS_GRAY);
503 : }
504 0 : cairo_set_font_options( pSCairo.get(), options);
505 :
506 : // Font color
507 0 : Color mTextColor = rOutDev.GetTextColor();
508 : cairo_set_source_rgb(pSCairo.get(),
509 0 : mTextColor.GetRed()/255.0,
510 0 : mTextColor.GetGreen()/255.0,
511 0 : mTextColor.GetBlue()/255.0);
512 :
513 : // Font rotation and scaling
514 : cairo_matrix_t m;
515 0 : Font aFont = rOutDev.GetFont();
516 :
517 0 : cairo_matrix_init_identity(&m);
518 :
519 0 : if (aSysLayoutData.orientation)
520 0 : cairo_matrix_rotate(&m, (3600 - aSysLayoutData.orientation) * M_PI / 1800.0);
521 :
522 0 : long nWidth = aFont.GetWidth();
523 0 : long nHeight = aFont.GetHeight();
524 0 : if (nWidth == 0)
525 0 : nWidth = nHeight;
526 0 : cairo_matrix_scale(&m, nWidth, nHeight);
527 :
528 : //faux italics
529 0 : if (rSysFontData.bFakeItalic)
530 0 : m.xy = -m.xx * 0x6000L / 0x10000L;
531 :
532 0 : cairo_set_font_matrix(pSCairo.get(), &m);
533 :
534 : #if (defined CAIRO_HAS_WIN32_SURFACE) && (OSL_DEBUG_LEVEL > 1)
535 : # define TEMP_TRACE_FONT OUStringToOString( reinterpret_cast<const sal_Unicode*> (logfont.lfFaceName), RTL_TEXTENCODING_UTF8 ).getStr()
536 : #else
537 : # define TEMP_TRACE_FONT OUStringToOString( aFont.GetName(), RTL_TEXTENCODING_UTF8 ).getStr()
538 : #endif
539 : OSL_TRACE("\r\n:cairocanvas::TextLayout::draw(S,O,p,v,r): Size:(%d,%d), Pos (%d,%d), G(%d,%d,%d) %s%s%s%s || Name:%s - %s",
540 : aFont.GetWidth(),
541 : aFont.GetHeight(),
542 : (int) rOutpos.X(),
543 : (int) rOutpos.Y(),
544 : cairo_glyphs.size() > 0 ? cairo_glyphs[0].index : -1,
545 : cairo_glyphs.size() > 1 ? cairo_glyphs[1].index : -1,
546 : cairo_glyphs.size() > 2 ? cairo_glyphs[2].index : -1,
547 : maLogicalAdvancements.getLength() ? "ADV " : "",
548 : rSysFontData.bAntialias ? "AA " : "",
549 : rSysFontData.bFakeBold ? "FB " : "",
550 : rSysFontData.bFakeItalic ? "FI " : "",
551 : TEMP_TRACE_FONT,
552 : OUStringToOString( maText.Text.copy( maText.StartPosition, maText.Length ),
553 : RTL_TEXTENCODING_UTF8 ).getStr()
554 : );
555 : #undef TEMP_TRACE_FONT
556 :
557 0 : cairo_show_glyphs(pSCairo.get(), &cairo_glyphs[0], cairo_glyphs.size());
558 :
559 : //faux bold
560 0 : if (rSysFontData.bFakeBold)
561 : {
562 0 : double bold_dx = 0.5 * sqrt( 0.7 * aFont.GetHeight() );
563 0 : int total_steps = 1 * ((int) (bold_dx + 0.5));
564 :
565 : // loop to draw the text for every half pixel of displacement
566 0 : for (int nSteps = 0; nSteps < total_steps; nSteps++)
567 : {
568 0 : for(int nGlyphIdx = 0; nGlyphIdx < (int) cairo_glyphs.size(); nGlyphIdx++)
569 : {
570 0 : cairo_glyphs[nGlyphIdx].x += (bold_dx * nSteps / total_steps) / 4;
571 0 : cairo_glyphs[nGlyphIdx].y -= (bold_dx * nSteps / total_steps) / 4;
572 : }
573 0 : cairo_show_glyphs(pSCairo.get(), &cairo_glyphs[0], cairo_glyphs.size());
574 : }
575 : OSL_TRACE(":cairocanvas::TextLayout::draw(S,O,p,v,r): FAKEBOLD - dx:%d", (int) bold_dx);
576 : }
577 :
578 0 : cairo_restore( pSCairo.get() );
579 0 : cairo_font_face_destroy(font_face);
580 0 : cairo_font_options_destroy(options);
581 0 : }
582 0 : return true;
583 : }
584 :
585 :
586 : namespace
587 : {
588 0 : class OffsetTransformer
589 : {
590 : public:
591 0 : OffsetTransformer( const ::basegfx::B2DHomMatrix& rMat ) :
592 0 : maMatrix( rMat )
593 : {
594 0 : }
595 :
596 0 : sal_Int32 operator()( const double& rOffset )
597 : {
598 : // This is an optimization of the normal rMat*[x,0]
599 : // transformation of the advancement vector (in x
600 : // direction), followed by a length calculation of the
601 : // resulting vector: advancement' =
602 : // ||rMat*[x,0]||. Since advancements are vectors, we
603 : // can ignore translational components, thus if [x,0],
604 : // it follows that rMat*[x,0]=[x',0] holds. Thus, we
605 : // just have to calc the transformation of the x
606 : // component.
607 :
608 : // TODO(F2): Handle non-horizontal advancements!
609 0 : return ::basegfx::fround( hypot(maMatrix.get(0,0)*rOffset,
610 0 : maMatrix.get(1,0)*rOffset) );
611 : }
612 :
613 : private:
614 : ::basegfx::B2DHomMatrix maMatrix;
615 : };
616 : }
617 :
618 0 : void TextLayout::setupTextOffsets( sal_Int32* outputOffsets,
619 : const uno::Sequence< double >& inputOffsets,
620 : const rendering::ViewState& viewState,
621 : const rendering::RenderState& renderState ) const
622 : {
623 0 : ENSURE_OR_THROW( outputOffsets!=NULL,
624 : "TextLayout::setupTextOffsets offsets NULL" );
625 :
626 0 : ::basegfx::B2DHomMatrix aMatrix;
627 :
628 : ::canvas::tools::mergeViewAndRenderTransform(aMatrix,
629 : viewState,
630 0 : renderState);
631 :
632 : // fill integer offsets
633 : ::std::transform( const_cast< uno::Sequence< double >& >(inputOffsets).getConstArray(),
634 0 : const_cast< uno::Sequence< double >& >(inputOffsets).getConstArray()+inputOffsets.getLength(),
635 : outputOffsets,
636 0 : OffsetTransformer( aMatrix ) );
637 0 : }
638 :
639 0 : OUString SAL_CALL TextLayout::getImplementationName() throw( uno::RuntimeException, std::exception )
640 : {
641 0 : return OUString( "CairoCanvas::TextLayout" );
642 : }
643 :
644 0 : sal_Bool SAL_CALL TextLayout::supportsService( const OUString& ServiceName ) throw( uno::RuntimeException, std::exception )
645 : {
646 0 : return cppu::supportsService( this, ServiceName );
647 : }
648 :
649 0 : uno::Sequence< OUString > SAL_CALL TextLayout::getSupportedServiceNames() throw( uno::RuntimeException, std::exception )
650 : {
651 0 : uno::Sequence< OUString > aRet(1);
652 0 : aRet[0] = "com.sun.star.rendering.TextLayout";
653 :
654 0 : return aRet;
655 : }
656 0 : }
657 :
658 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|