LCOV - code coverage report
Current view: top level - canvas/source/cairo - cairo_canvashelper_text.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 1 115 0.9 %
Date: 2015-06-13 12:38:46 Functions: 2 12 16.7 %
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 <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             :                         vcl::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           0 :         ::canvas::tools::clipOutDev(viewState, renderState, rOutDev);
     131             : 
     132           0 :         if( eColorType != IGNORE_COLOR )
     133             :         {
     134           0 :             Color aColor( COL_WHITE );
     135             : 
     136           0 :             if( renderState.DeviceColor.getLength() > 2 )
     137             :             {
     138           0 :                 aColor = vcl::unotools::stdColorSpaceSequenceToColor( renderState.DeviceColor );
     139             :             }
     140             : 
     141             :             // extract alpha, and make color opaque
     142             :             // afterwards. Otherwise, OutputDevice won't draw anything
     143           0 :             nTransparency = aColor.GetTransparency();
     144           0 :             aColor.SetTransparency(0);
     145             : 
     146           0 :             switch( eColorType )
     147             :             {
     148             :                 case LINE_COLOR:
     149           0 :                     rOutDev.SetLineColor( aColor );
     150           0 :                     rOutDev.SetFillColor();
     151             : 
     152           0 :                     break;
     153             : 
     154             :                 case FILL_COLOR:
     155           0 :                     rOutDev.SetFillColor( aColor );
     156           0 :                     rOutDev.SetLineColor();
     157             : 
     158           0 :                     break;
     159             : 
     160             :                 case TEXT_COLOR:
     161           0 :                     rOutDev.SetTextColor( aColor );
     162             : 
     163           0 :                     break;
     164             : 
     165             :                 default:
     166           0 :                     ENSURE_OR_THROW( false,
     167             :                                       "CanvasHelper::setupOutDevState(): Unexpected color type");
     168             :                     break;
     169             :             }
     170             :         }
     171             : 
     172           0 :         return nTransparency;
     173             :     }
     174             : 
     175             :     class DeviceSettingsGuard
     176             :     {
     177             :     private:
     178             :         VclPtr<OutputDevice> mpVirtualDevice;
     179             :         cairo_t *mpCairo;
     180             :         bool mbMappingWasEnabled;
     181             :     public:
     182           0 :         DeviceSettingsGuard(OutputDevice *pVirtualDevice, cairo_t *pCairo)
     183             :             : mpVirtualDevice(pVirtualDevice)
     184             :             , mpCairo(pCairo)
     185           0 :             , mbMappingWasEnabled(mpVirtualDevice->IsMapModeEnabled())
     186             :         {
     187           0 :             cairo_save(mpCairo);
     188           0 :             mpVirtualDevice->Push();
     189           0 :             mpVirtualDevice->EnableMapMode(false);
     190           0 :         }
     191             : 
     192           0 :         ~DeviceSettingsGuard()
     193           0 :         {
     194           0 :             mpVirtualDevice->EnableMapMode(mbMappingWasEnabled);
     195           0 :             mpVirtualDevice->Pop();
     196           0 :             cairo_restore(mpCairo);
     197           0 :         }
     198             :     };
     199             : 
     200           0 :     bool setupTextOutput( OutputDevice&                                     rOutDev,
     201             :                           const rendering::XCanvas*                         pOwner,
     202             :                           ::Point&                                          o_rOutPos,
     203             :                           const rendering::ViewState&                       viewState,
     204             :                           const rendering::RenderState&                     renderState,
     205             :                           const uno::Reference< rendering::XCanvasFont >&   xFont   )
     206             :     {
     207           0 :         setupOutDevState( rOutDev, pOwner, viewState, renderState, TEXT_COLOR );
     208             : 
     209           0 :         CanvasFont* pFont = dynamic_cast< CanvasFont* >( xFont.get() );
     210             : 
     211           0 :         ENSURE_ARG_OR_THROW( pFont,
     212             :                          "CanvasHelper::setupTextOutput(): Font not compatible with this canvas" );
     213             : 
     214           0 :         vcl::Font aVCLFont = pFont->getVCLFont();
     215             : 
     216           0 :         Color aColor( COL_BLACK );
     217             : 
     218           0 :         if( renderState.DeviceColor.getLength() > 2 )
     219             :         {
     220           0 :             aColor = vcl::unotools::stdColorSpaceSequenceToColor(renderState.DeviceColor );
     221             :         }
     222             : 
     223             :         // setup font color
     224           0 :         aVCLFont.SetColor( aColor );
     225           0 :         aVCLFont.SetFillColor( aColor );
     226             : 
     227             :         // no need to replicate this for mp2ndOutDev, we're modifying only aVCLFont here.
     228           0 :         if( !setupFontTransform( rOutDev, o_rOutPos, aVCLFont, viewState, renderState ) )
     229           0 :             return false;
     230             : 
     231           0 :         rOutDev.SetFont( aVCLFont );
     232             : 
     233           0 :         return true;
     234             :     }
     235             : 
     236             :     //set the clip of the rOutDev to the cairo surface
     237           0 :     void CanvasHelper::clip_cairo_from_dev(::OutputDevice& rOutDev)
     238             :     {
     239           0 :         vcl::Region aRegion(rOutDev.GetClipRegion());
     240           0 :         if (!aRegion.IsEmpty() && !aRegion.IsNull())
     241             :         {
     242             :             doPolyPolygonImplementation(aRegion.GetAsB2DPolyPolygon(), Clip, mpCairo.get(),
     243           0 :                                         NULL, mpSurfaceProvider, rendering::FillRule_EVEN_ODD);
     244           0 :         }
     245           0 :     }
     246             : 
     247           0 :     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawText( const rendering::XCanvas*                         pOwner,
     248             :                                                                           const rendering::StringContext&                   text,
     249             :                                                                           const uno::Reference< rendering::XCanvasFont >&   xFont,
     250             :                                                                           const rendering::ViewState&                       viewState,
     251             :                                                                           const rendering::RenderState&                     renderState,
     252             :                                                                           sal_Int8                                          textDirection )
     253             :     {
     254             : #ifdef CAIRO_CANVAS_PERF_TRACE
     255             :         struct timespec aTimer;
     256             :         mxDevice->startPerfTrace( &aTimer );
     257             : #endif
     258             : 
     259           0 :         ENSURE_ARG_OR_THROW( xFont.is(),
     260             :                          "CanvasHelper::drawText(): font is NULL");
     261             : 
     262           0 :         if( !mpVirtualDevice )
     263           0 :             mpVirtualDevice = mpSurface->createVirtualDevice();
     264             : 
     265           0 :         if( mpVirtualDevice )
     266             :         {
     267           0 :             DeviceSettingsGuard aGuard(mpVirtualDevice.get(), mpCairo.get());
     268             : 
     269             : #if defined CAIRO_HAS_WIN32_SURFACE
     270             :             // FIXME: Some kind of work-araound...
     271             :             cairo_rectangle (mpCairo.get(), 0, 0, 0, 0);
     272             :             cairo_fill(mpCairo.get());
     273             : #endif
     274           0 :             ::Point aOutpos;
     275           0 :             if( !setupTextOutput( *mpVirtualDevice.get(), pOwner, aOutpos, viewState, renderState, xFont ) )
     276           0 :                 return uno::Reference< rendering::XCachedPrimitive >(NULL); // no output necessary
     277             : 
     278             :                 // change text direction and layout mode
     279           0 :             ComplexTextLayoutMode nLayoutMode(TEXT_LAYOUT_DEFAULT);
     280           0 :             switch( textDirection )
     281             :             {
     282             :                 case rendering::TextDirection::WEAK_LEFT_TO_RIGHT:
     283             :                     // FALLTHROUGH intended
     284             :                 case rendering::TextDirection::STRONG_LEFT_TO_RIGHT:
     285           0 :                     nLayoutMode |= TEXT_LAYOUT_BIDI_STRONG;
     286           0 :                     nLayoutMode |= TEXT_LAYOUT_TEXTORIGIN_LEFT;
     287           0 :                     break;
     288             : 
     289             :                 case rendering::TextDirection::WEAK_RIGHT_TO_LEFT:
     290           0 :                     nLayoutMode |= TEXT_LAYOUT_BIDI_RTL;
     291             :                     // FALLTHROUGH intended
     292             :                 case rendering::TextDirection::STRONG_RIGHT_TO_LEFT:
     293           0 :                     nLayoutMode |= TEXT_LAYOUT_BIDI_RTL | TEXT_LAYOUT_BIDI_STRONG;
     294           0 :                     nLayoutMode |= TEXT_LAYOUT_TEXTORIGIN_RIGHT;
     295           0 :                     break;
     296             :             }
     297             : 
     298             :             // TODO(F2): alpha
     299           0 :             mpVirtualDevice->SetLayoutMode( nLayoutMode );
     300             : 
     301           0 :             clip_cairo_from_dev(*mpVirtualDevice);
     302             : 
     303             :             OSL_TRACE(":cairocanvas::CanvasHelper::drawText(O,t,f,v,r,d): %s", OUStringToOString( text.Text.copy( text.StartPosition, text.Length ),
     304             :                                                                                                          RTL_TEXTENCODING_UTF8 ).getStr());
     305             : 
     306           0 :             rtl::Reference< TextLayout > pTextLayout( new TextLayout(text, textDirection, 0, CanvasFont::Reference(dynamic_cast< CanvasFont* >( xFont.get() )), mpSurfaceProvider) );
     307           0 :             pTextLayout->draw(mpCairo, *mpVirtualDevice, aOutpos, viewState, renderState);
     308             :         }
     309             : 
     310           0 :         return uno::Reference< rendering::XCachedPrimitive >(NULL);
     311             :     }
     312             : 
     313           0 :     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawTextLayout( const rendering::XCanvas*                       pOwner,
     314             :                                                                                 const uno::Reference< rendering::XTextLayout >& xLayoutedText,
     315             :                                                                                 const rendering::ViewState&                     viewState,
     316             :                                                                                 const rendering::RenderState&                   renderState )
     317             :     {
     318           0 :         ENSURE_ARG_OR_THROW( xLayoutedText.is(),
     319             :                          "CanvasHelper::drawTextLayout(): layout is NULL");
     320             : 
     321           0 :         TextLayout* pTextLayout = dynamic_cast< TextLayout* >( xLayoutedText.get() );
     322             : 
     323           0 :         if( pTextLayout )
     324             :         {
     325           0 :             if( !mpVirtualDevice )
     326           0 :                 mpVirtualDevice = mpSurface->createVirtualDevice();
     327             : 
     328           0 :             if( mpVirtualDevice )
     329             :             {
     330           0 :                 DeviceSettingsGuard aGuard(mpVirtualDevice.get(), mpCairo.get());
     331             : 
     332             : #if defined CAIRO_HAS_WIN32_SURFACE
     333             :                 // FIXME: Some kind of work-araound...
     334             :                 cairo_rectangle(mpCairo.get(), 0, 0, 0, 0);
     335             :                 cairo_fill(mpCairo.get());
     336             : #endif
     337             :                 // TODO(T3): Race condition. We're taking the font
     338             :                 // from xLayoutedText, and then calling draw() at it,
     339             :                 // without exclusive access. Move setupTextOutput(),
     340             :                 // e.g. to impltools?
     341             : 
     342           0 :                 ::Point aOutpos;
     343           0 :                 if( !setupTextOutput( *mpVirtualDevice, pOwner, aOutpos, viewState, renderState, xLayoutedText->getFont() ) )
     344           0 :                     return uno::Reference< rendering::XCachedPrimitive >(NULL); // no output necessary
     345             : 
     346           0 :                 clip_cairo_from_dev(*mpVirtualDevice);
     347             : 
     348             :                 // TODO(F2): What about the offset scalings?
     349           0 :                 pTextLayout->draw(mpCairo, *mpVirtualDevice, aOutpos, viewState, renderState);
     350             :             }
     351             :         }
     352             :         else
     353             :         {
     354           0 :             ENSURE_ARG_OR_THROW( false,
     355             :                              "CanvasHelper::drawTextLayout(): TextLayout not compatible with this canvas" );
     356             :         }
     357             : 
     358           0 :         return uno::Reference< rendering::XCachedPrimitive >(NULL);
     359             :     }
     360             : 
     361           6 : }
     362             : 
     363             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11