LCOV - code coverage report
Current view: top level - canvas/source/cairo - cairo_textlayout.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 212 0.5 %
Date: 2014-11-03 Functions: 2 34 5.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10