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