LCOV - code coverage report
Current view: top level - sw/source/core/objectpositioning - ascharanchoredobjectposition.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 119 157 75.8 %
Date: 2014-11-03 Functions: 7 8 87.5 %
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 <ascharanchoredobjectposition.hxx>
      21             : #include <frame.hxx>
      22             : #include <txtfrm.hxx>
      23             : #include <flyfrms.hxx>
      24             : #include <svx/svdobj.hxx>
      25             : #include <dcontact.hxx>
      26             : #include <frmfmt.hxx>
      27             : #include <frmatr.hxx>
      28             : #include <editeng/lrspitem.hxx>
      29             : #include <editeng/ulspitem.hxx>
      30             : #include <fmtornt.hxx>
      31             : 
      32             : #include <com/sun/star/text/HoriOrientation.hpp>
      33             : 
      34             : using namespace ::com::sun::star;
      35             : using namespace objectpositioning;
      36             : 
      37             : /** constructor */
      38       13361 : SwAsCharAnchoredObjectPosition::SwAsCharAnchoredObjectPosition(
      39             :                                     SdrObject& _rDrawObj,
      40             :                                     const Point&    _rProposedAnchorPos,
      41             :                                     const AsCharFlags _nFlags,
      42             :                                     const SwTwips     _nLineAscent,
      43             :                                     const SwTwips     _nLineDescent,
      44             :                                     const SwTwips     _nLineAscentInclObjs,
      45             :                                     const SwTwips     _nLineDescentInclObjs )
      46             :     : SwAnchoredObjectPosition( _rDrawObj ),
      47             :       mrProposedAnchorPos( _rProposedAnchorPos ),
      48             :       mnFlags( _nFlags ),
      49             :       mnLineAscent( _nLineAscent ),
      50             :       mnLineDescent( _nLineDescent ),
      51             :       mnLineAscentInclObjs( _nLineAscentInclObjs ),
      52             :       mnLineDescentInclObjs( _nLineDescentInclObjs ),
      53             :       maAnchorPos ( Point() ),
      54             :       mnRelPos ( 0 ),
      55             :       maObjBoundRect ( SwRect() ),
      56       13361 :       mnLineAlignment ( 0 )
      57       13361 : {}
      58             : 
      59             : /** destructor */
      60       13361 : SwAsCharAnchoredObjectPosition::~SwAsCharAnchoredObjectPosition()
      61       13361 : {}
      62             : 
      63             : /** method to cast <SwAnchoredObjectPosition::GetAnchorFrm()> to needed type */
      64       13361 : const SwTxtFrm& SwAsCharAnchoredObjectPosition::GetAnchorTxtFrm() const
      65             : {
      66             :     OSL_ENSURE( GetAnchorFrm().ISA(SwTxtFrm),
      67             :             "SwAsCharAnchoredObjectPosition::GetAnchorTxtFrm() - wrong anchor frame type" );
      68             : 
      69       13361 :     return static_cast<const SwTxtFrm&>(GetAnchorFrm());
      70             : }
      71             : 
      72             : /** calculate position for object
      73             : 
      74             :     OD 30.07.2003 #110978#
      75             :     members <maAnchorPos>, <mnRelPos>, <maObjBoundRect> and
      76             :     <mnLineAlignment> are calculated.
      77             :     calculated position is set at the given object.
      78             : */
      79       13361 : void SwAsCharAnchoredObjectPosition::CalcPosition()
      80             : {
      81       13361 :     const SwTxtFrm& rAnchorFrm = GetAnchorTxtFrm();
      82             :     // swap anchor frame, if swapped. Note: destructor takes care of the 'undo'
      83       13361 :     SwFrmSwapper aFrmSwapper( &rAnchorFrm, false );
      84             : 
      85       13361 :     SWRECTFN( ( &rAnchorFrm ) )
      86             : 
      87       13361 :     Point aAnchorPos( mrProposedAnchorPos );
      88             : 
      89       13361 :     const SwFrmFmt& rFrmFmt = GetFrmFmt();
      90             : 
      91       13361 :     SwRect aObjBoundRect( GetAnchoredObj().GetObjRect() );
      92       13361 :     SwTwips nObjWidth = (aObjBoundRect.*fnRect->fnGetWidth)();
      93             : 
      94             :     // determine spacing values considering layout-/text-direction
      95       13361 :     const SvxLRSpaceItem& rLRSpace = rFrmFmt.GetLRSpace();
      96       13361 :     const SvxULSpaceItem& rULSpace = rFrmFmt.GetULSpace();
      97             :     SwTwips nLRSpaceLeft, nLRSpaceRight, nULSpaceUpper, nULSpaceLower;
      98             :     {
      99       13361 :         if ( rAnchorFrm.IsVertical() )
     100             :         {
     101             :             // Seems to be easier to do it all the horizontal way
     102             :             // So, from now on think horizontal.
     103           0 :             rAnchorFrm.SwitchVerticalToHorizontal( aObjBoundRect );
     104           0 :             rAnchorFrm.SwitchVerticalToHorizontal( aAnchorPos );
     105             : 
     106             :             // convert the spacing values
     107           0 :             nLRSpaceLeft = rULSpace.GetUpper();
     108           0 :             nLRSpaceRight = rULSpace.GetLower();
     109           0 :             nULSpaceUpper = rLRSpace.GetRight();
     110           0 :             nULSpaceLower = rLRSpace.GetLeft();
     111             :         }
     112             :         else
     113             :         {
     114       13361 :             if ( rAnchorFrm.IsRightToLeft() )
     115             :             {
     116          30 :                 nLRSpaceLeft = rLRSpace.GetRight();
     117          30 :                 nLRSpaceRight = rLRSpace.GetLeft();
     118             :             }
     119             :             else
     120             :             {
     121       13331 :                 nLRSpaceLeft = rLRSpace.GetLeft();
     122       13331 :                 nLRSpaceRight = rLRSpace.GetRight();
     123             :             }
     124             : 
     125       13361 :             nULSpaceUpper = rULSpace.GetUpper();
     126       13361 :             nULSpaceLower = rULSpace.GetLower();
     127             :         }
     128             :     }
     129             : 
     130             :     // consider left and upper spacing by adjusting anchor position.
     131             :     // left spacing is only considered, if requested.
     132       13361 :     if( mnFlags & AS_CHAR_ULSPACE )
     133             :     {
     134        9497 :         aAnchorPos.X() += nLRSpaceLeft;
     135             :     }
     136       13361 :     aAnchorPos.Y() += nULSpaceUpper;
     137             : 
     138             :     // for drawing objects: consider difference between its bounding rectangle
     139             :     // and its snapping rectangle by adjusting anchor position.
     140             :     // left difference is only considered, if requested.
     141       13361 :     if( !IsObjFly() )
     142             :     {
     143        4660 :         SwRect aSnapRect = GetObject().GetSnapRect();
     144        4660 :         if ( rAnchorFrm.IsVertical() )
     145             :         {
     146           0 :             rAnchorFrm.SwitchVerticalToHorizontal( aSnapRect );
     147             :         }
     148             : 
     149        4660 :         if( mnFlags & AS_CHAR_ULSPACE )
     150             :         {
     151        3190 :             aAnchorPos.X() += aSnapRect.Left() - aObjBoundRect.Left();
     152             :         }
     153        4660 :         aAnchorPos.Y() += aSnapRect.Top() - aObjBoundRect.Top();
     154             :     }
     155             : 
     156             :     // enlarge bounding rectangle of object by its spacing.
     157       13361 :     aObjBoundRect.Left( aObjBoundRect.Left() - nLRSpaceLeft );
     158       13361 :     aObjBoundRect.Width( aObjBoundRect.Width() + nLRSpaceRight );
     159       13361 :     aObjBoundRect.Top( aObjBoundRect.Top() - nULSpaceUpper );
     160       13361 :     aObjBoundRect.Height( aObjBoundRect.Height() + nULSpaceLower );
     161             : 
     162             :     // calculate relative position to given base line.
     163       13361 :     const SwFmtVertOrient& rVert = rFrmFmt.GetVertOrient();
     164       13361 :     const SwTwips nObjBoundHeight = ( mnFlags & AS_CHAR_ROTATE )
     165             :                                     ? aObjBoundRect.Width()
     166       13361 :                                     : aObjBoundRect.Height();
     167       13361 :     const SwTwips nRelPos = _GetRelPosToBase( nObjBoundHeight, rVert );
     168             : 
     169             :     // for initial positioning:
     170             :     // adjust the proposed anchor position by difference between
     171             :     // calculated relative position to base line and current maximal line ascent.
     172             :     // Note: In the following line formatting the base line will be adjusted
     173             :     //       by the same difference.
     174       13361 :     if( mnFlags & AS_CHAR_INIT && nRelPos < 0 && mnLineAscentInclObjs < -nRelPos )
     175             :     {
     176        1281 :         if( mnFlags & AS_CHAR_ROTATE )
     177           0 :             aAnchorPos.X() -= mnLineAscentInclObjs + nRelPos;
     178             :         else
     179        1281 :             aAnchorPos.Y() -= mnLineAscentInclObjs + nRelPos;
     180             :     }
     181             : 
     182             :     // consider BIDI-multiportion by adjusting proposed anchor position
     183       13361 :     if( mnFlags & AS_CHAR_BIDI )
     184           0 :         aAnchorPos.X() -= aObjBoundRect.Width();
     185             : 
     186             :     // calculate relative position considering rotation and inside rotation
     187             :     // reverse direction.
     188       13361 :     Point aRelPos;
     189             :     {
     190       13361 :         if( mnFlags & AS_CHAR_ROTATE )
     191             :         {
     192           0 :             if( mnFlags & AS_CHAR_REVERSE )
     193           0 :                 aRelPos.X() = -nRelPos - aObjBoundRect.Width();
     194             :             else
     195             :             {
     196           0 :                 aRelPos.X() = nRelPos;
     197           0 :                 aRelPos.Y() = -aObjBoundRect.Height();
     198             :             }
     199             :         }
     200             :         else
     201       13361 :             aRelPos.Y() = nRelPos;
     202             :     }
     203             : 
     204       13361 :     if( !IsObjFly() )
     205             :     {
     206        4660 :         if( !( mnFlags & AS_CHAR_QUICK ) )
     207             :         {
     208             :             // save calculated Y-position value for 'automatic' vertical positioning,
     209             :             // in order to avoid a switch to 'manual' vertical positioning in
     210             :             // <SwDrawContact::_Changed(..)>.
     211        4660 :             const sal_Int16 eVertOrient = rVert.GetVertOrient();
     212        4660 :             if( rVert.GetPos() != nRelPos && eVertOrient != text::VertOrientation::NONE )
     213             :             {
     214          88 :                 SwFmtVertOrient aVert( rVert );
     215          88 :                 aVert.SetPos( nRelPos );
     216          88 :                 const_cast<SwFrmFmt&>(rFrmFmt).LockModify();
     217          88 :                 const_cast<SwFrmFmt&>(rFrmFmt).SetFmtAttr( aVert );
     218          88 :                 const_cast<SwFrmFmt&>(rFrmFmt).UnlockModify();
     219             :             }
     220             : 
     221             :             // determine absolute anchor position considering layout directions.
     222             :             // Note: Use copy of <aAnchorPos>, because it's needed for
     223             :             //       setting relative position.
     224        4660 :             Point aAbsAnchorPos( aAnchorPos );
     225        4660 :             if ( rAnchorFrm.IsRightToLeft() )
     226             :             {
     227           0 :                 rAnchorFrm.SwitchLTRtoRTL( aAbsAnchorPos );
     228           0 :                 aAbsAnchorPos.X() -= nObjWidth;
     229             :             }
     230        4660 :             if ( rAnchorFrm.IsVertical() )
     231           0 :                 rAnchorFrm.SwitchHorizontalToVertical( aAbsAnchorPos );
     232             : 
     233             :             // set proposed anchor position at the drawing object.
     234             :             // OD 2004-04-06 #i26791# - distinction between 'master' drawing
     235             :             // object and 'virtual' drawing object no longer needed.
     236        4660 :             GetObject().SetAnchorPos( aAbsAnchorPos );
     237             : 
     238             :             // move drawing object to set its correct relative position.
     239             :             {
     240        4660 :                 SwRect aSnapRect = GetObject().GetSnapRect();
     241        4660 :                 if ( rAnchorFrm.IsVertical() )
     242           0 :                     rAnchorFrm.SwitchVerticalToHorizontal( aSnapRect );
     243             : 
     244        4660 :                 Point aDiff;
     245        4660 :                 if ( rAnchorFrm.IsRightToLeft() )
     246           0 :                     aDiff = aRelPos + aAbsAnchorPos - aSnapRect.TopLeft();
     247             :                 else
     248        4660 :                     aDiff = aRelPos + aAnchorPos - aSnapRect.TopLeft();
     249             : 
     250        4660 :                 if ( rAnchorFrm.IsVertical() )
     251           0 :                     aDiff = Point( -aDiff.Y(), aDiff.X() );
     252             : 
     253             :                 // OD 2004-04-06 #i26791# - distinction between 'master' drawing
     254             :                 // object and 'virtual' drawing object no longer needed.
     255        4660 :                 GetObject().Move( Size( aDiff.X(), aDiff.Y() ) );
     256             :             }
     257             :         }
     258             : 
     259             :         // switch horizontal, LTR anchor position to absolute values.
     260        4660 :         if ( rAnchorFrm.IsRightToLeft() )
     261             :         {
     262           0 :             rAnchorFrm.SwitchLTRtoRTL( aAnchorPos );
     263           0 :             aAnchorPos.X() -= nObjWidth;
     264             :         }
     265        4660 :         if ( rAnchorFrm.IsVertical() )
     266           0 :             rAnchorFrm.SwitchHorizontalToVertical( aAnchorPos );
     267             : 
     268             :         // #i44347# - keep last object rectangle at anchored object
     269             :        OSL_ENSURE( GetAnchoredObj().ISA(SwAnchoredDrawObject),
     270             :                  "<SwAsCharAnchoredObjectPosition::CalcPosition()> - wrong type of anchored object." );
     271             :         SwAnchoredDrawObject& rAnchoredDrawObj =
     272        4660 :                         static_cast<SwAnchoredDrawObject&>( GetAnchoredObj() );
     273        4660 :         rAnchoredDrawObj.SetLastObjRect( rAnchoredDrawObj.GetObjRect().SVRect() );
     274             :     }
     275             :     else
     276             :     {
     277             :         // determine absolute anchor position and calculate corresponding
     278             :         // relative position and its relative position attribute.
     279             :         // Note: The relative position contains the spacing values.
     280        8701 :         Point aRelAttr;
     281        8701 :         if ( rAnchorFrm.IsRightToLeft() )
     282             :         {
     283          30 :             rAnchorFrm.SwitchLTRtoRTL( aAnchorPos );
     284          30 :             aAnchorPos.X() -= nObjWidth;
     285             :         }
     286        8701 :         if ( rAnchorFrm.IsVertical() )
     287             :         {
     288           0 :             rAnchorFrm.SwitchHorizontalToVertical( aAnchorPos );
     289           0 :             aRelAttr = Point( -nRelPos, 0 );
     290           0 :             aRelPos = Point( -aRelPos.Y(), aRelPos.X() );
     291             :         }
     292             :         else
     293        8701 :             aRelAttr = Point( 0, nRelPos );
     294             : 
     295             :         // OD 2004-03-23 #i26791#
     296             :         OSL_ENSURE( GetAnchoredObj().ISA(SwFlyInCntFrm),
     297             :                 "<SwAsCharAnchoredObjectPosition::CalcPosition()> - wrong anchored object." );
     298             :         const SwFlyInCntFrm& rFlyInCntFrm =
     299        8701 :                 static_cast<const SwFlyInCntFrm&>(GetAnchoredObj());
     300       31811 :         if ( !(mnFlags & AS_CHAR_QUICK) &&
     301       20301 :              ( aAnchorPos != rFlyInCntFrm.GetRefPoint() ||
     302       26182 :                aRelAttr != rFlyInCntFrm.GetCurrRelPos() ) )
     303             :         {
     304             :             // set new anchor position and relative position
     305        2888 :             SwFlyInCntFrm* pFlyInCntFrm = &(const_cast<SwFlyInCntFrm&>(rFlyInCntFrm));
     306        2888 :             pFlyInCntFrm->SetRefPoint( aAnchorPos, aRelAttr, aRelPos );
     307        2888 :             if( nObjWidth != (pFlyInCntFrm->Frm().*fnRect->fnGetWidth)() )
     308             :             {
     309             :                 // recalculate object bound rectangle, if object width has changed.
     310           0 :                 aObjBoundRect = GetAnchoredObj().GetObjRect();
     311           0 :                 aObjBoundRect.Left( aObjBoundRect.Left() - rLRSpace.GetLeft() );
     312           0 :                 aObjBoundRect.Width( aObjBoundRect.Width() + rLRSpace.GetRight() );
     313           0 :                 aObjBoundRect.Top( aObjBoundRect.Top() - rULSpace.GetUpper() );
     314           0 :                 aObjBoundRect.Height( aObjBoundRect.Height() + rULSpace.GetLower() );
     315             :             }
     316             :         }
     317             :         OSL_ENSURE( (rFlyInCntFrm.Frm().*fnRect->fnGetHeight)(),
     318             :             "SwAnchoredObjectPosition::CalcPosition(..) - fly frame has an invalid height" );
     319             :     }
     320             : 
     321             :     // keep calculated values
     322       13361 :     maAnchorPos = aAnchorPos;
     323       13361 :     mnRelPos = nRelPos;
     324       13361 :     maObjBoundRect = aObjBoundRect;
     325       13361 : }
     326             : 
     327             : /** determine the relative position to base line for object position type AS_CHAR
     328             : 
     329             :     OD 29.07.2003 #110978#
     330             :     Note about values set at member <mnLineAlignment> -
     331             :     value gives feedback for the line formatting.
     332             :     0 - no feedback; 1|2|3 - proposed formatting of characters
     333             :     at top|at center|at bottom of line.
     334             : */
     335       13361 : SwTwips SwAsCharAnchoredObjectPosition::_GetRelPosToBase(
     336             :                                             const SwTwips _nObjBoundHeight,
     337             :                                             const SwFmtVertOrient& _rVert )
     338             : {
     339       13361 :     SwTwips nRelPosToBase = 0;
     340             : 
     341       13361 :     mnLineAlignment = 0;
     342             : 
     343       13361 :     const sal_Int16 eVertOrient = _rVert.GetVertOrient();
     344             : 
     345       13361 :     if ( eVertOrient == text::VertOrientation::NONE )
     346        4732 :         nRelPosToBase = _rVert.GetPos();
     347             :     else
     348             :     {
     349        8629 :         if ( eVertOrient == text::VertOrientation::CENTER )
     350         230 :             nRelPosToBase -= _nObjBoundHeight /  2;
     351        8399 :         else if ( eVertOrient == text::VertOrientation::TOP )
     352        5769 :             nRelPosToBase -= _nObjBoundHeight;
     353        2630 :         else if ( eVertOrient == text::VertOrientation::BOTTOM )
     354           0 :             nRelPosToBase = 0;
     355        2630 :         else if ( eVertOrient == text::VertOrientation::CHAR_CENTER )
     356        2270 :             nRelPosToBase -= ( _nObjBoundHeight + mnLineAscent - mnLineDescent ) / 2;
     357         360 :         else if ( eVertOrient == text::VertOrientation::CHAR_TOP )
     358           0 :             nRelPosToBase -= mnLineAscent;
     359         360 :         else if ( eVertOrient == text::VertOrientation::CHAR_BOTTOM )
     360           0 :             nRelPosToBase += mnLineDescent - _nObjBoundHeight;
     361             :         else
     362             :         {
     363         360 :             if( _nObjBoundHeight >= mnLineAscentInclObjs + mnLineDescentInclObjs )
     364             :             {
     365             :                 // object is at least as high as the line. Thus, no more is
     366             :                 // positioning necessary. Also, the max. ascent isn't changed.
     367         244 :                 nRelPosToBase -= mnLineAscentInclObjs;
     368         244 :                 if ( eVertOrient == text::VertOrientation::LINE_CENTER )
     369         136 :                     mnLineAlignment = 2;
     370         108 :                 else if ( eVertOrient == text::VertOrientation::LINE_TOP )
     371         108 :                     mnLineAlignment = 1;
     372           0 :                 else if ( eVertOrient == text::VertOrientation::LINE_BOTTOM )
     373           0 :                     mnLineAlignment = 3;
     374             :             }
     375         116 :             else if ( eVertOrient == text::VertOrientation::LINE_CENTER )
     376             :             {
     377          68 :                 nRelPosToBase -= ( _nObjBoundHeight + mnLineAscentInclObjs - mnLineDescentInclObjs ) / 2;
     378          68 :                 mnLineAlignment = 2;
     379             :             }
     380          48 :             else if ( eVertOrient == text::VertOrientation::LINE_TOP )
     381             :             {
     382          48 :                 nRelPosToBase -= mnLineAscentInclObjs;
     383          48 :                 mnLineAlignment = 1;
     384             :             }
     385           0 :             else if ( eVertOrient == text::VertOrientation::LINE_BOTTOM )
     386             :             {
     387           0 :                 nRelPosToBase += mnLineDescentInclObjs - _nObjBoundHeight;
     388           0 :                 mnLineAlignment = 3;
     389             :             }
     390             :         }
     391             :     }
     392             : 
     393       13361 :     return nRelPosToBase;
     394         270 : }
     395             : 
     396             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10