LCOV - code coverage report
Current view: top level - sw/source/core/objectpositioning - ascharanchoredobjectposition.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 119 157 75.8 %
Date: 2015-06-13 12:38:46 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        7116 : 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        7116 :       mnLineAlignment ( 0 )
      57        7116 : {}
      58             : 
      59             : /** destructor */
      60        7116 : SwAsCharAnchoredObjectPosition::~SwAsCharAnchoredObjectPosition()
      61        7116 : {}
      62             : 
      63             : /** method to cast <SwAnchoredObjectPosition::GetAnchorFrm()> to needed type */
      64        7116 : const SwTextFrm& SwAsCharAnchoredObjectPosition::GetAnchorTextFrm() const
      65             : {
      66             :     OSL_ENSURE( GetAnchorFrm().ISA(SwTextFrm),
      67             :             "SwAsCharAnchoredObjectPosition::GetAnchorTextFrm() - wrong anchor frame type" );
      68             : 
      69        7116 :     return static_cast<const SwTextFrm&>(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        7116 : void SwAsCharAnchoredObjectPosition::CalcPosition()
      80             : {
      81        7116 :     const SwTextFrm& rAnchorFrm = GetAnchorTextFrm();
      82             :     // swap anchor frame, if swapped. Note: destructor takes care of the 'undo'
      83        7116 :     SwFrmSwapper aFrmSwapper( &rAnchorFrm, false );
      84             : 
      85        7116 :     SWRECTFN( ( &rAnchorFrm ) )
      86             : 
      87        7116 :     Point aAnchorPos( mrProposedAnchorPos );
      88             : 
      89        7116 :     const SwFrameFormat& rFrameFormat = GetFrameFormat();
      90             : 
      91        7116 :     SwRect aObjBoundRect( GetAnchoredObj().GetObjRect() );
      92        7116 :     SwTwips nObjWidth = (aObjBoundRect.*fnRect->fnGetWidth)();
      93             : 
      94             :     // determine spacing values considering layout-/text-direction
      95        7116 :     const SvxLRSpaceItem& rLRSpace = rFrameFormat.GetLRSpace();
      96        7116 :     const SvxULSpaceItem& rULSpace = rFrameFormat.GetULSpace();
      97             :     SwTwips nLRSpaceLeft, nLRSpaceRight, nULSpaceUpper, nULSpaceLower;
      98             :     {
      99        7116 :         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        7116 :             if ( rAnchorFrm.IsRightToLeft() )
     115             :             {
     116          15 :                 nLRSpaceLeft = rLRSpace.GetRight();
     117          15 :                 nLRSpaceRight = rLRSpace.GetLeft();
     118             :             }
     119             :             else
     120             :             {
     121        7101 :                 nLRSpaceLeft = rLRSpace.GetLeft();
     122        7101 :                 nLRSpaceRight = rLRSpace.GetRight();
     123             :             }
     124             : 
     125        7116 :             nULSpaceUpper = rULSpace.GetUpper();
     126        7116 :             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        7116 :     if( mnFlags & AS_CHAR_ULSPACE )
     133             :     {
     134        5083 :         aAnchorPos.X() += nLRSpaceLeft;
     135             :     }
     136        7116 :     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        7116 :     if( !IsObjFly() )
     142             :     {
     143        2501 :         SwRect aSnapRect = GetObject().GetSnapRect();
     144        2501 :         if ( rAnchorFrm.IsVertical() )
     145             :         {
     146           0 :             rAnchorFrm.SwitchVerticalToHorizontal( aSnapRect );
     147             :         }
     148             : 
     149        2501 :         if( mnFlags & AS_CHAR_ULSPACE )
     150             :         {
     151        1717 :             aAnchorPos.X() += aSnapRect.Left() - aObjBoundRect.Left();
     152             :         }
     153        2501 :         aAnchorPos.Y() += aSnapRect.Top() - aObjBoundRect.Top();
     154             :     }
     155             : 
     156             :     // enlarge bounding rectangle of object by its spacing.
     157        7116 :     aObjBoundRect.Left( aObjBoundRect.Left() - nLRSpaceLeft );
     158        7116 :     aObjBoundRect.Width( aObjBoundRect.Width() + nLRSpaceRight );
     159        7116 :     aObjBoundRect.Top( aObjBoundRect.Top() - nULSpaceUpper );
     160        7116 :     aObjBoundRect.Height( aObjBoundRect.Height() + nULSpaceLower );
     161             : 
     162             :     // calculate relative position to given base line.
     163        7116 :     const SwFormatVertOrient& rVert = rFrameFormat.GetVertOrient();
     164        7116 :     const SwTwips nObjBoundHeight = ( mnFlags & AS_CHAR_ROTATE )
     165             :                                     ? aObjBoundRect.Width()
     166        7116 :                                     : aObjBoundRect.Height();
     167        7116 :     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        7116 :     if( mnFlags & AS_CHAR_INIT && nRelPos < 0 && mnLineAscentInclObjs < -nRelPos )
     175             :     {
     176         786 :         if( mnFlags & AS_CHAR_ROTATE )
     177           0 :             aAnchorPos.X() -= mnLineAscentInclObjs + nRelPos;
     178             :         else
     179         786 :             aAnchorPos.Y() -= mnLineAscentInclObjs + nRelPos;
     180             :     }
     181             : 
     182             :     // consider BIDI-multiportion by adjusting proposed anchor position
     183        7116 :     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        7116 :     Point aRelPos;
     189             :     {
     190        7116 :         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        7116 :             aRelPos.Y() = nRelPos;
     202             :     }
     203             : 
     204        7116 :     if( !IsObjFly() )
     205             :     {
     206        2501 :         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        2494 :             const sal_Int16 eVertOrient = rVert.GetVertOrient();
     212        2494 :             if( rVert.GetPos() != nRelPos && eVertOrient != text::VertOrientation::NONE )
     213             :             {
     214          94 :                 SwFormatVertOrient aVert( rVert );
     215          94 :                 aVert.SetPos( nRelPos );
     216          94 :                 const_cast<SwFrameFormat&>(rFrameFormat).LockModify();
     217          94 :                 const_cast<SwFrameFormat&>(rFrameFormat).SetFormatAttr( aVert );
     218          94 :                 const_cast<SwFrameFormat&>(rFrameFormat).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        2494 :             Point aAbsAnchorPos( aAnchorPos );
     225        2494 :             if ( rAnchorFrm.IsRightToLeft() )
     226             :             {
     227           0 :                 rAnchorFrm.SwitchLTRtoRTL( aAbsAnchorPos );
     228           0 :                 aAbsAnchorPos.X() -= nObjWidth;
     229             :             }
     230        2494 :             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        2494 :             GetObject().SetAnchorPos( aAbsAnchorPos );
     237             : 
     238             :             // move drawing object to set its correct relative position.
     239             :             {
     240        2494 :                 SwRect aSnapRect = GetObject().GetSnapRect();
     241        2494 :                 if ( rAnchorFrm.IsVertical() )
     242           0 :                     rAnchorFrm.SwitchVerticalToHorizontal( aSnapRect );
     243             : 
     244        2494 :                 Point aDiff;
     245        2494 :                 if ( rAnchorFrm.IsRightToLeft() )
     246           0 :                     aDiff = aRelPos + aAbsAnchorPos - aSnapRect.TopLeft();
     247             :                 else
     248        2494 :                     aDiff = aRelPos + aAnchorPos - aSnapRect.TopLeft();
     249             : 
     250        2494 :                 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        2494 :                 GetObject().Move( Size( aDiff.X(), aDiff.Y() ) );
     256             :             }
     257             :         }
     258             : 
     259             :         // switch horizontal, LTR anchor position to absolute values.
     260        2501 :         if ( rAnchorFrm.IsRightToLeft() )
     261             :         {
     262           0 :             rAnchorFrm.SwitchLTRtoRTL( aAnchorPos );
     263           0 :             aAnchorPos.X() -= nObjWidth;
     264             :         }
     265        2501 :         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        2501 :                         static_cast<SwAnchoredDrawObject&>( GetAnchoredObj() );
     273        2501 :         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        4615 :         Point aRelAttr;
     281        4615 :         if ( rAnchorFrm.IsRightToLeft() )
     282             :         {
     283          15 :             rAnchorFrm.SwitchLTRtoRTL( aAnchorPos );
     284          15 :             aAnchorPos.X() -= nObjWidth;
     285             :         }
     286        4615 :         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        4615 :             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        4615 :                 static_cast<const SwFlyInCntFrm&>(GetAnchoredObj());
     300       16698 :         if ( !(mnFlags & AS_CHAR_QUICK) &&
     301       10881 :              ( aAnchorPos != rFlyInCntFrm.GetRefPoint() ||
     302       14086 :                aRelAttr != rFlyInCntFrm.GetCurrRelPos() ) )
     303             :         {
     304             :             // set new anchor position and relative position
     305        1443 :             SwFlyInCntFrm* pFlyInCntFrm = &(const_cast<SwFlyInCntFrm&>(rFlyInCntFrm));
     306        1443 :             pFlyInCntFrm->SetRefPoint( aAnchorPos, aRelAttr, aRelPos );
     307        1443 :             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        7116 :     maAnchorPos = aAnchorPos;
     323        7116 :     mnRelPos = nRelPos;
     324        7116 :     maObjBoundRect = aObjBoundRect;
     325        7116 : }
     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        7116 : SwTwips SwAsCharAnchoredObjectPosition::_GetRelPosToBase(
     336             :                                             const SwTwips _nObjBoundHeight,
     337             :                                             const SwFormatVertOrient& _rVert )
     338             : {
     339        7116 :     SwTwips nRelPosToBase = 0;
     340             : 
     341        7116 :     mnLineAlignment = 0;
     342             : 
     343        7116 :     const sal_Int16 eVertOrient = _rVert.GetVertOrient();
     344             : 
     345        7116 :     if ( eVertOrient == text::VertOrientation::NONE )
     346        2209 :         nRelPosToBase = _rVert.GetPos();
     347             :     else
     348             :     {
     349        4907 :         if ( eVertOrient == text::VertOrientation::CENTER )
     350         165 :             nRelPosToBase -= _nObjBoundHeight /  2;
     351        4742 :         else if ( eVertOrient == text::VertOrientation::TOP )
     352        3436 :             nRelPosToBase -= _nObjBoundHeight;
     353        1306 :         else if ( eVertOrient == text::VertOrientation::BOTTOM )
     354           0 :             nRelPosToBase = 0;
     355        1306 :         else if ( eVertOrient == text::VertOrientation::CHAR_CENTER )
     356        1108 :             nRelPosToBase -= ( _nObjBoundHeight + mnLineAscent - mnLineDescent ) / 2;
     357         198 :         else if ( eVertOrient == text::VertOrientation::CHAR_TOP )
     358           0 :             nRelPosToBase -= mnLineAscent;
     359         198 :         else if ( eVertOrient == text::VertOrientation::CHAR_BOTTOM )
     360           0 :             nRelPosToBase += mnLineDescent - _nObjBoundHeight;
     361             :         else
     362             :         {
     363         198 :             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         144 :                 nRelPosToBase -= mnLineAscentInclObjs;
     368         144 :                 if ( eVertOrient == text::VertOrientation::LINE_CENTER )
     369         103 :                     mnLineAlignment = 2;
     370          41 :                 else if ( eVertOrient == text::VertOrientation::LINE_TOP )
     371          41 :                     mnLineAlignment = 1;
     372           0 :                 else if ( eVertOrient == text::VertOrientation::LINE_BOTTOM )
     373           0 :                     mnLineAlignment = 3;
     374             :             }
     375          54 :             else if ( eVertOrient == text::VertOrientation::LINE_CENTER )
     376             :             {
     377          35 :                 nRelPosToBase -= ( _nObjBoundHeight + mnLineAscentInclObjs - mnLineDescentInclObjs ) / 2;
     378          35 :                 mnLineAlignment = 2;
     379             :             }
     380          19 :             else if ( eVertOrient == text::VertOrientation::LINE_TOP )
     381             :             {
     382          19 :                 nRelPosToBase -= mnLineAscentInclObjs;
     383          19 :                 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        7116 :     return nRelPosToBase;
     394         177 : }
     395             : 
     396             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11