LCOV - code coverage report
Current view: top level - vcl/source/gdi - textlayout.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 80 105 76.2 %
Date: 2015-06-13 12:38:46 Functions: 19 24 79.2 %
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 "vcl/ctrl.hxx"
      21             : #include "vcl/outdev.hxx"
      22             : 
      23             : #include "outfont.hxx"
      24             : #include "textlayout.hxx"
      25             : 
      26             : #include <com/sun/star/i18n/ScriptDirection.hpp>
      27             : 
      28             : #include <tools/diagnose_ex.h>
      29             : #include <tools/fract.hxx>
      30             : 
      31             : #if OSL_DEBUG_LEVEL > 1
      32             : #include <rtl/strbuf.hxx>
      33             : #endif
      34             : 
      35             : #include <boost/scoped_array.hpp>
      36             : 
      37             : namespace vcl
      38             : {
      39             : 
      40             :     using ::com::sun::star::uno::Reference;
      41             :     using ::com::sun::star::uno::Exception;
      42             :     namespace ScriptDirection = ::com::sun::star::i18n::ScriptDirection;
      43             : 
      44       22170 :     DefaultTextLayout::~DefaultTextLayout()
      45             :     {
      46       22170 :     }
      47             : 
      48       11570 :     long DefaultTextLayout::GetTextWidth( const OUString& _rText, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
      49             :     {
      50       11570 :         return m_rTargetDevice.GetTextWidth( _rText, _nStartIndex, _nLength );
      51             :     }
      52             : 
      53        5662 :     void DefaultTextLayout::DrawText( const Point& _rStartPoint, const OUString& _rText, sal_Int32 _nStartIndex,
      54             :         sal_Int32 _nLength, MetricVector* _pVector, OUString* _pDisplayText )
      55             :     {
      56        5662 :         m_rTargetDevice.DrawText( _rStartPoint, _rText, _nStartIndex, _nLength, _pVector, _pDisplayText );
      57        5662 :     }
      58             : 
      59          58 :     bool DefaultTextLayout::GetCaretPositions( const OUString& _rText, long* _pCaretXArray,
      60             :         sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
      61             :     {
      62          58 :         return m_rTargetDevice.GetCaretPositions( _rText, _pCaretXArray, _nStartIndex, _nLength );
      63             :     }
      64             : 
      65       11322 :     sal_Int32 DefaultTextLayout::GetTextBreak( const OUString& _rText, long _nMaxTextWidth, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
      66             :     {
      67       11322 :         return m_rTargetDevice.GetTextBreak( _rText, _nMaxTextWidth, _nStartIndex, _nLength );
      68             :     }
      69             : 
      70           0 :     bool DefaultTextLayout::DecomposeTextRectAction() const
      71             :     {
      72           0 :         return false;
      73             :     }
      74             : 
      75             :     class ReferenceDeviceTextLayout : public ITextLayout
      76             :     {
      77             :     public:
      78             :         ReferenceDeviceTextLayout( const Control& _rControl, OutputDevice& _rTargetDevice, OutputDevice& _rReferenceDevice );
      79             :         virtual ~ReferenceDeviceTextLayout();
      80             : 
      81             :         // ITextLayout
      82             :         virtual long        GetTextWidth( const OUString& rStr, sal_Int32 nIndex, sal_Int32 nLen ) const SAL_OVERRIDE;
      83             :         virtual void        DrawText( const Point& _rStartPoint, const OUString& _rText, sal_Int32 _nStartIndex, sal_Int32 _nLength, MetricVector* _pVector, OUString* _pDisplayText ) SAL_OVERRIDE;
      84             :         virtual bool        GetCaretPositions( const OUString& _rText, long* _pCaretXArray, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const SAL_OVERRIDE;
      85             :         virtual sal_Int32   GetTextBreak(const OUString& _rText, long _nMaxTextWidth, sal_Int32 _nStartIndex, sal_Int32 _nLength) const SAL_OVERRIDE;
      86             :         virtual bool        DecomposeTextRectAction() const SAL_OVERRIDE;
      87             : 
      88             :     public:
      89             :         // equivalents to the respective OutputDevice methods, which take the reference device into account
      90             :         long        GetTextArray( const OUString& _rText, long* _pDXAry, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const;
      91             :         Rectangle   DrawText( const Rectangle& _rRect, const OUString& _rText, DrawTextFlags _nStyle, MetricVector* _pVector, OUString* _pDisplayText );
      92             : 
      93             :     protected:
      94         451 :         void onBeginDrawText()
      95             :         {
      96         451 :             m_aCompleteTextRect.SetEmpty();
      97         451 :         }
      98         451 :         Rectangle onEndDrawText()
      99             :         {
     100         451 :             return m_aCompleteTextRect;
     101             :         }
     102             : 
     103             :     private:
     104             :         OutputDevice&   m_rTargetDevice;
     105             :         OutputDevice&   m_rReferenceDevice;
     106             :         Font            m_aUnzoomedPointFont;
     107             :         const Fraction  m_aZoom;
     108             :         const bool      m_bRTLEnabled;
     109             : 
     110             :         Rectangle       m_aCompleteTextRect;
     111             :     };
     112             : 
     113         456 :     ReferenceDeviceTextLayout::ReferenceDeviceTextLayout( const Control& _rControl, OutputDevice& _rTargetDevice,
     114             :         OutputDevice& _rReferenceDevice )
     115             :         :m_rTargetDevice( _rTargetDevice )
     116             :         ,m_rReferenceDevice( _rReferenceDevice )
     117             :         ,m_aUnzoomedPointFont( _rControl.GetUnzoomedControlPointFont() )
     118         456 :         ,m_aZoom( _rControl.GetZoom() )
     119         912 :         ,m_bRTLEnabled( _rControl.IsRTLEnabled() )
     120             :     {
     121         456 :         m_rTargetDevice.Push( PushFlags::MAPMODE | PushFlags::FONT | PushFlags::TEXTLAYOUTMODE );
     122             : 
     123         456 :         MapMode aTargetMapMode( m_rTargetDevice.GetMapMode() );
     124             :         OSL_ENSURE( aTargetMapMode.GetOrigin() == Point(), "ReferenceDeviceTextLayout::ReferenceDeviceTextLayout: uhm, the code below won't work here ..." );
     125             : 
     126             :         // normally, controls simulate "zoom" by "zooming" the font. This is responsible for (part of) the discrepancies
     127             :         // between text in Writer and text in controls in Writer, though both have the same font.
     128             :         // So, if we have a zoom set at the control, then we do not scale the font, but instead modify the map mode
     129             :         // to accommodate for the zoom.
     130         456 :         aTargetMapMode.SetScaleX( m_aZoom );    // TODO: shouldn't this be "current_scale * zoom"?
     131         456 :         aTargetMapMode.SetScaleY( m_aZoom );
     132             : 
     133             :         // also, use a higher-resolution map unit than "pixels", which should save us some rounding errors when
     134             :         // translating coordinates between the reference device and the target device.
     135             :         OSL_ENSURE( aTargetMapMode.GetMapUnit() == MAP_PIXEL,
     136             :             "ReferenceDeviceTextLayout::ReferenceDeviceTextLayout: this class is not expected to work with such target devices!" );
     137             :             // we *could* adjust all the code in this class to handle this case, but at the moment, it's not necessary
     138         456 :         const MapUnit eTargetMapUnit = m_rReferenceDevice.GetMapMode().GetMapUnit();
     139         456 :         aTargetMapMode.SetMapUnit( eTargetMapUnit );
     140             :         OSL_ENSURE( aTargetMapMode.GetMapUnit() != MAP_PIXEL,
     141             :             "ReferenceDeviceTextLayout::ReferenceDeviceTextLayout: a reference device which has map mode PIXEL?!" );
     142             : 
     143         456 :         m_rTargetDevice.SetMapMode( aTargetMapMode );
     144             : 
     145             :         // now that the Zoom is part of the map mode, reset the target device's font to the "unzoomed" version
     146         912 :         Font aDrawFont( m_aUnzoomedPointFont );
     147         456 :         aDrawFont.SetSize( OutputDevice::LogicToLogic( aDrawFont.GetSize(), MAP_POINT, eTargetMapUnit ) );
     148         456 :         _rTargetDevice.SetFont( aDrawFont );
     149             : 
     150             :         // transfer font to the reference device
     151         456 :         m_rReferenceDevice.Push( PushFlags::FONT | PushFlags::TEXTLAYOUTMODE );
     152         912 :         Font aRefFont( m_aUnzoomedPointFont );
     153             :         aRefFont.SetSize( OutputDevice::LogicToLogic(
     154         456 :             aRefFont.GetSize(), MAP_POINT, m_rReferenceDevice.GetMapMode().GetMapUnit() ) );
     155         912 :         m_rReferenceDevice.SetFont( aRefFont );
     156         456 :     }
     157             : 
     158        1368 :     ReferenceDeviceTextLayout::~ReferenceDeviceTextLayout()
     159             :     {
     160         456 :         m_rReferenceDevice.Pop();
     161         456 :         m_rTargetDevice.Pop();
     162         912 :     }
     163             : 
     164             :     namespace
     165             :     {
     166        1353 :         bool lcl_normalizeLength( const OUString& _rText, const sal_Int32 _nStartIndex, sal_Int32& _io_nLength )
     167             :         {
     168        1353 :             sal_Int32 nTextLength = _rText.getLength();
     169        1353 :             if ( _nStartIndex > nTextLength )
     170           0 :                 return false;
     171        1353 :             if ( _nStartIndex + _io_nLength > nTextLength )
     172           0 :                 _io_nLength = nTextLength - _nStartIndex;
     173        1353 :             return true;
     174             :         }
     175             :     }
     176             : 
     177         902 :     long ReferenceDeviceTextLayout::GetTextArray( const OUString& _rText, long* _pDXAry, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
     178             :     {
     179         902 :         if ( !lcl_normalizeLength( _rText, _nStartIndex, _nLength ) )
     180           0 :             return 0;
     181             : 
     182             :         // retrieve the character widths from the reference device
     183         902 :         long nTextWidth = m_rReferenceDevice.GetTextArray( _rText, _pDXAry, _nStartIndex, _nLength );
     184             : #if OSL_DEBUG_LEVEL > 1
     185             :         if ( _pDXAry )
     186             :         {
     187             :             OStringBuffer aTrace;
     188             :             aTrace.append( "ReferenceDeviceTextLayout::GetTextArray( " );
     189             :             aTrace.append( OUStringToOString( _rText, RTL_TEXTENCODING_UTF8 ) );
     190             :             aTrace.append( " ): " );
     191             :             aTrace.append( nTextWidth );
     192             :             aTrace.append( " = ( " );
     193             :             for ( sal_Int32 i=0; i<_nLength; )
     194             :             {
     195             :                 aTrace.append( _pDXAry[i] );
     196             :                 if ( ++i < _nLength )
     197             :                     aTrace.append( ", " );
     198             :             }
     199             :             aTrace.append( ")" );
     200             :             OSL_TRACE( "%s", aTrace.makeStringAndClear().getStr() );
     201             :         }
     202             : #endif
     203         902 :         return nTextWidth;
     204             :     }
     205             : 
     206         451 :     long ReferenceDeviceTextLayout::GetTextWidth( const OUString& _rText, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
     207             :     {
     208         451 :         return GetTextArray( _rText, NULL, _nStartIndex, _nLength );
     209             :     }
     210             : 
     211         451 :     void ReferenceDeviceTextLayout::DrawText( const Point& _rStartPoint, const OUString& _rText, sal_Int32 _nStartIndex, sal_Int32 _nLength, MetricVector* _pVector, OUString* _pDisplayText )
     212             :     {
     213         451 :         if ( !lcl_normalizeLength( _rText, _nStartIndex, _nLength ) )
     214           0 :             return;
     215             : 
     216         451 :         if ( _pVector && _pDisplayText )
     217             :         {
     218           0 :             MetricVector aGlyphBounds;
     219           0 :             m_rReferenceDevice.GetGlyphBoundRects( _rStartPoint, _rText, _nStartIndex, _nLength, _nStartIndex, aGlyphBounds );
     220             :             ::std::copy(
     221             :                 aGlyphBounds.begin(), aGlyphBounds.end(),
     222           0 :                 ::std::insert_iterator< MetricVector > ( *_pVector, _pVector->end() ) );
     223           0 :             *_pDisplayText += _rText.copy( _nStartIndex, _nLength );
     224           0 :             return;
     225             :         }
     226             : 
     227         451 :         boost::scoped_array<long> pCharWidths(new long[ _nLength ]);
     228         451 :         long nTextWidth = GetTextArray( _rText, pCharWidths.get(), _nStartIndex, _nLength );
     229         451 :         m_rTargetDevice.DrawTextArray( _rStartPoint, _rText, pCharWidths.get(), _nStartIndex, _nLength );
     230         451 :         pCharWidths.reset();
     231             : 
     232         451 :         m_aCompleteTextRect.Union( Rectangle( _rStartPoint, Size( nTextWidth, m_rTargetDevice.GetTextHeight() ) ) );
     233             :     }
     234             : 
     235           0 :     bool ReferenceDeviceTextLayout::GetCaretPositions( const OUString& _rText, long* _pCaretXArray,
     236             :                                                        sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
     237             :     {
     238           0 :         if ( !lcl_normalizeLength( _rText, _nStartIndex, _nLength ) )
     239           0 :             return false;
     240             : 
     241             :         // retrieve the caret positions from the reference device
     242           0 :         if ( !m_rReferenceDevice.GetCaretPositions( _rText, _pCaretXArray, _nStartIndex, _nLength ) )
     243           0 :             return false;
     244             : 
     245           0 :         return true;
     246             :     }
     247             : 
     248           0 :     sal_Int32 ReferenceDeviceTextLayout::GetTextBreak( const OUString& _rText, long _nMaxTextWidth, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
     249             :     {
     250           0 :         if ( !lcl_normalizeLength( _rText, _nStartIndex, _nLength ) )
     251           0 :             return 0;
     252             : 
     253           0 :         return m_rReferenceDevice.GetTextBreak( _rText, _nMaxTextWidth, _nStartIndex, _nLength );
     254             :     }
     255             : 
     256         451 :     bool ReferenceDeviceTextLayout::DecomposeTextRectAction() const
     257             :     {
     258         451 :         return true;
     259             :     }
     260             : 
     261         456 :     Rectangle ReferenceDeviceTextLayout::DrawText( const Rectangle& _rRect, const OUString& _rText, DrawTextFlags _nStyle, MetricVector* _pVector, OUString* _pDisplayText )
     262             :     {
     263         456 :         if ( _rText.isEmpty() )
     264           5 :             return Rectangle();
     265             : 
     266             :         // determine text layout mode from the RTL-ness of the control whose text we render
     267         451 :         ComplexTextLayoutMode nTextLayoutMode = m_bRTLEnabled ? TEXT_LAYOUT_BIDI_RTL : TEXT_LAYOUT_DEFAULT;
     268         451 :         m_rReferenceDevice.SetLayoutMode( nTextLayoutMode );
     269         451 :         m_rTargetDevice.SetLayoutMode( nTextLayoutMode | TEXT_LAYOUT_TEXTORIGIN_LEFT );
     270             : 
     271             :         // TEXT_LAYOUT_TEXTORIGIN_LEFT is because when we do actually draw the text (in DrawText( Point, ... )), then
     272             :         // our caller gives us the left border of the draw position, regardless of script type, text layout,
     273             :         // and the like in our ctor, we set the map mode of the target device from pixel to twip, but our caller doesn't know this,
     274             :         // but passed pixel coordinates. So, adjust the rect.
     275         451 :         Rectangle aRect( m_rTargetDevice.PixelToLogic( _rRect ) );
     276             : 
     277         451 :         onBeginDrawText();
     278         451 :         m_rTargetDevice.DrawText( aRect, _rText, _nStyle, _pVector, _pDisplayText, this );
     279         451 :         Rectangle aTextRect = onEndDrawText();
     280             : 
     281         451 :         if ( aTextRect.IsEmpty() && !aRect.IsEmpty() )
     282             :         {
     283             :             // this happens for instance if we're in a PaintToDevice call, where only a MetaFile is recorded,
     284             :             // but no actual painting happens, so our "DrawText( Point, ... )" is never called
     285             :             // In this case, calculate the rect from what OutputDevice::GetTextRect would give us. This has
     286             :             // the disadvantage of less accuracy, compared with the approach to calculate the rect from the
     287             :             // single "DrawText( Point, ... )" calls, since more intermediate arithmetics will translate
     288             :             // from ref- to target-units.
     289           0 :             aTextRect = m_rTargetDevice.GetTextRect( aRect, _rText, _nStyle, NULL, this );
     290             :         }
     291             : 
     292             :         // similar to above, the text rect now contains TWIPs (or whatever unit the ref device has), but the caller
     293             :         // expects pixel coordinates
     294         451 :         aTextRect = m_rTargetDevice.LogicToPixel( aTextRect );
     295             : 
     296             :         // convert the metric vector
     297         451 :         if ( _pVector )
     298             :         {
     299           0 :             for (   MetricVector::iterator charRect = _pVector->begin();
     300           0 :                     charRect != _pVector->end();
     301             :                     ++charRect
     302             :                 )
     303             :             {
     304           0 :                 *charRect = m_rTargetDevice.LogicToPixel( *charRect );
     305             :             }
     306             :         }
     307             : 
     308         451 :         return aTextRect;
     309             :     }
     310             : 
     311         456 :     ControlTextRenderer::ControlTextRenderer( const Control& _rControl, OutputDevice& _rTargetDevice, OutputDevice& _rReferenceDevice )
     312         456 :         :m_pImpl( new ReferenceDeviceTextLayout( _rControl, _rTargetDevice, _rReferenceDevice ) )
     313             :     {
     314         456 :     }
     315             : 
     316         456 :     ControlTextRenderer::~ControlTextRenderer()
     317             :     {
     318         456 :     }
     319             : 
     320         456 :     Rectangle ControlTextRenderer::DrawText( const Rectangle& _rRect, const OUString& _rText, DrawTextFlags _nStyle,
     321             :         MetricVector* _pVector, OUString* _pDisplayText )
     322             :     {
     323         456 :         return m_pImpl->DrawText( _rRect, _rText, _nStyle, _pVector, _pDisplayText );
     324             :     }
     325             : 
     326             : } // namespace vcl
     327             : 
     328             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11