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

Generated by: LCOV version 1.10