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

Generated by: LCOV version 1.10