1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <editeng/txtrange.hxx>
23 #include <tools/poly.hxx>
24 #include <tools/debug.hxx>
25 #include <tools/solar.h>
26 #include <basegfx/polygon/b2dpolygon.hxx>
27 #include <basegfx/polygon/b2dpolygontools.hxx>
31 TextRanger::TextRanger( const basegfx::B2DPolyPolygon
& rPolyPolygon
,
32 const basegfx::B2DPolyPolygon
* pLinePolyPolygon
,
33 sal_uInt16 nCacheSz
, sal_uInt16 nLft
, sal_uInt16 nRght
,
34 bool bSimpl
, bool bInnr
, bool bVert
) :
36 nCacheSize( nCacheSz
),
46 sal_uInt32
nCount(rPolyPolygon
.count());
47 mpPolyPolygon
= new tools::PolyPolygon( (sal_uInt16
)nCount
);
49 for(sal_uInt32
i(0L); i
< nCount
; i
++)
51 const basegfx::B2DPolygon
aCandidate(rPolyPolygon
.getB2DPolygon(i
).getDefaultAdaptiveSubdivision());
52 nPointCount
+= aCandidate
.count();
53 mpPolyPolygon
->Insert( Polygon(aCandidate
), (sal_uInt16
)i
);
56 if( pLinePolyPolygon
)
58 nCount
= pLinePolyPolygon
->count();
59 mpLinePolyPolygon
= new tools::PolyPolygon();
61 for(sal_uInt32
i(0L); i
< nCount
; i
++)
63 const basegfx::B2DPolygon
aCandidate(pLinePolyPolygon
->getB2DPolygon(i
).getDefaultAdaptiveSubdivision());
64 nPointCount
+= aCandidate
.count();
65 mpLinePolyPolygon
->Insert( Polygon(aCandidate
), (sal_uInt16
)i
);
69 mpLinePolyPolygon
= NULL
;
73 TextRanger::~TextRanger()
77 delete mpLinePolyPolygon
;
81 /* TextRanger::SetVertical(..)
82 If there's is a change in the writing direction,
83 the cache has to be cleared.
85 void TextRanger::SetVertical( bool bNew
)
87 if( IsVertical() != bNew
)
94 //! SvxBoundArgs is used to perform temporary calculations on a range array.
95 //! Temporary instances are created in TextRanger::GetTextRanges()
98 std::vector
<bool> aBoolArr
;
100 TextRanger
*pTextRanger
;
121 void NoteRange( bool bToggle
);
122 long Cut( long nY
, const Point
& rPt1
, const Point
& rPt2
);
124 void _NoteFarPoint( long nPx
, long nPyDiff
, long nDiff
);
125 void NoteFarPoint( long nPx
, long nPyDiff
, long nDiff
)
126 { if( nDiff
) _NoteFarPoint( nPx
, nPyDiff
, nDiff
); }
127 long CalcMax( const Point
& rPt1
, const Point
& rPt2
, long nRange
, long nFar
);
128 void CheckCut( const Point
& rLst
, const Point
& rNxt
);
129 inline long A( const Point
& rP
) const { return bRotate
? rP
.Y() : rP
.X(); }
130 inline long B( const Point
& rP
) const { return bRotate
? rP
.X() : rP
.Y(); }
132 SvxBoundArgs( TextRanger
* pRanger
, LongDqPtr pLong
, const Range
& rRange
);
133 void NotePoint( const long nA
) { NoteMargin( nA
- nStart
, nA
+ nEnd
); }
134 void NoteMargin( const long nL
, const long nR
)
135 { if( nMin
> nL
) nMin
= nL
; if( nMax
< nR
) nMax
= nR
; }
136 sal_uInt16
Area( const Point
& rPt
);
137 void NoteUpLow( long nA
, const sal_uInt8 nArea
);
138 void Calc( const tools::PolyPolygon
& rPoly
);
139 void Concat( const tools::PolyPolygon
* pPoly
);
141 void NoteLast() { if( bMultiple
) NoteRange( nAct
== nFirst
); }
142 void SetClosed( const bool bNew
){ bClosed
= bNew
; }
143 bool IsClosed() const { return bClosed
; }
144 void SetConcat( const bool bNew
){ bConcat
= bNew
; }
145 bool IsConcat() const { return bConcat
; }
148 SvxBoundArgs::SvxBoundArgs( TextRanger
* pRanger
, LongDqPtr pLong
,
149 const Range
& rRange
)
151 , pTextRanger(pRanger
)
155 , nBottom(rRange
.Max())
162 , bInner(pRanger
->IsInner())
163 , bMultiple(bInner
|| !pRanger
->IsSimple())
165 , bRotate(pRanger
->IsVertical())
169 nStart
= pRanger
->GetUpper();
170 nEnd
= pRanger
->GetLower();
171 nLowDiff
= pRanger
->GetLeft();
172 nUpDiff
= pRanger
->GetRight();
176 nStart
= pRanger
->GetLeft();
177 nEnd
= pRanger
->GetRight();
178 nLowDiff
= pRanger
->GetUpper();
179 nUpDiff
= pRanger
->GetLower();
181 nUpper
= nTop
- nUpDiff
;
182 nLower
= nBottom
+ nLowDiff
;
186 long SvxBoundArgs::CalcMax( const Point
& rPt1
, const Point
& rPt2
,
187 long nRange
, long nFarRange
)
189 double nDa
= Cut( nRange
, rPt1
, rPt2
) - Cut( nFarRange
, rPt1
, rPt2
);
200 nB
= nRange
+ nDa
* ( nFarRange
- nRange
) / sqrt( nB
);
204 bNote
= nB
> B(rPt1
);
206 bNote
= nB
< B(rPt1
);
208 return( long( nB
) );
212 void SvxBoundArgs::CheckCut( const Point
& rLst
, const Point
& rNxt
)
215 NotePoint( Cut( nBottom
, rLst
, rNxt
) );
217 NotePoint( Cut( nTop
, rLst
, rNxt
) );
218 if( rLst
.X() != rNxt
.X() && rLst
.Y() != rNxt
.Y() )
221 if( nLowDiff
&& ( ( nCut
& 1 ) || nLast
== 1 || nNext
== 1 ) )
223 nYps
= CalcMax( rLst
, rNxt
, nBottom
, nLower
);
225 _NoteFarPoint( Cut( nYps
, rLst
, rNxt
), nLower
-nYps
, nLowDiff
);
227 if( nUpDiff
&& ( ( nCut
& 2 ) || nLast
== 2 || nNext
== 2 ) )
229 nYps
= CalcMax( rLst
, rNxt
, nTop
, nUpper
);
231 _NoteFarPoint( Cut( nYps
, rLst
, rNxt
), nYps
-nUpper
, nUpDiff
);
236 void SvxBoundArgs::_NoteFarPoint( long nPa
, long nPbDiff
, long nDiff
)
239 double nQuot
= 2 * nDiff
- nPbDiff
;
241 nQuot
= sqrt( nQuot
);
243 nTmpA
= nPa
- long( nStart
* nQuot
);
244 nPbDiff
= nPa
+ long( nEnd
* nQuot
);
245 NoteMargin( nTmpA
, nPbDiff
);
248 void SvxBoundArgs::NoteRange( bool bToggle
)
250 DBG_ASSERT( nMax
>= nMin
|| bInner
, "NoteRange: Min > Max?");
256 sal_uInt16 nCount
= pLongArr
->size();
257 DBG_ASSERT( nCount
== 2 * aBoolArr
.size(), "NoteRange: Incompatible Sizes" );
258 while( nIdx
< nCount
&& (*pLongArr
)[ nIdx
] < nMin
)
260 bool bOdd
= (nIdx
% 2) != 0;
261 // No overlap with existing intervals?
262 if( nIdx
== nCount
|| ( !bOdd
&& nMax
< (*pLongArr
)[ nIdx
] ) )
263 { // Then a new one is inserted ...
264 pLongArr
->insert( pLongArr
->begin() + nIdx
, nMin
);
265 pLongArr
->insert( pLongArr
->begin() + nIdx
+ 1, nMax
);
266 aBoolArr
.insert( aBoolArr
.begin() + (nIdx
/2), bToggle
);
269 { // expand an existing interval ...
270 sal_uInt16 nMaxIdx
= nIdx
;
271 // If we end up on a left interval boundary, it must be reduced to nMin.
275 (*pLongArr
)[ nIdx
] = nMin
;
276 while( nMaxIdx
< nCount
&& (*pLongArr
)[ nMaxIdx
] < nMax
)
278 DBG_ASSERT( nMaxIdx
> nIdx
|| nMin
== nMax
, "NoteRange: Funny Situation." );
283 // If we end up on a right interval boundary, it must be raised to nMax.
285 (*pLongArr
)[ nMaxIdx
-- ] = nMax
;
286 // Possible merge of intervals.
287 sal_uInt16 nDiff
= nMaxIdx
- nIdx
;
288 nMaxIdx
= nIdx
/ 2; // From here on is nMaxIdx the Index in BoolArray.
291 pLongArr
->erase( pLongArr
->begin() + nIdx
+ 1, pLongArr
->begin() + nIdx
+ 1 + nDiff
);
293 sal_uInt16 nStop
= nMaxIdx
+ nDiff
;
294 for( sal_uInt16 i
= nMaxIdx
; i
< nStop
; ++i
)
295 bToggle
^= aBoolArr
[ i
];
296 aBoolArr
.erase( aBoolArr
.begin() + nMaxIdx
, aBoolArr
.begin() + (nMaxIdx
+ nDiff
) );
298 DBG_ASSERT( nMaxIdx
< aBoolArr
.size(), "NoteRange: Too much deleted" );
299 aBoolArr
[ nMaxIdx
] = aBoolArr
[ nMaxIdx
] != bToggle
;
303 void SvxBoundArgs::Calc( const tools::PolyPolygon
& rPoly
)
307 for( sal_uInt16 i
= 0; i
< rPoly
.Count(); ++i
)
309 const Polygon
& rPol
= rPoly
[ i
];
310 nCount
= rPol
.GetSize();
313 const Point
& rNull
= rPol
[ 0 ];
314 SetClosed( IsConcat() || ( rNull
== rPol
[ nCount
- 1 ] ) );
315 nLast
= Area( rNull
);
324 // The first point of the polygon is within the line.
327 if( bMultiple
|| !nAct
)
333 NoteFarPoint( A(rNull
), nLower
- B(rNull
), nLowDiff
);
335 NoteFarPoint( A(rNull
), B(rNull
) - nUpper
, nUpDiff
);
339 if( bMultiple
|| !nAct
)
346 NotePoint( A(rNull
) );
348 nFirst
= 0; // leaving the line in which direction?
349 nAct
= 3; // we are within the line at the moment.
356 const Point
& rLast
= rPol
[ nIdx
- 1 ];
359 const Point
& rNext
= rPol
[ nIdx
];
360 nNext
= Area( rNext
);
361 nCut
= nNext
^ nLast
;
362 sal_uInt16 nOldAct
= nAct
;
364 CheckCut( rLast
, rNext
);
367 NoteUpLow( Cut( nLower
, rLast
, rNext
), 2 );
368 if( nAct
&& nAct
!= nOldAct
)
371 CheckCut( rLast
, rNext
);
376 NoteUpLow( Cut( nUpper
, rLast
, rNext
), 1 );
377 if( nAct
&& nAct
!= nOldAct
)
378 CheckCut( rLast
, rNext
);
382 if( !( nNext
& 12 ) )
386 if( !( nNext
& 12 ) )
389 NotePoint( A(rNext
) );
391 NoteFarPoint( A(rNext
), nLower
-B(rNext
), nLowDiff
);
393 NoteFarPoint( A(rNext
), B(rNext
)-nUpper
, nUpDiff
);
396 if( ++nIdx
== nCount
&& !IsClosed() )
398 if( !( nNext
& 12 ) )
404 if( bMultiple
&& IsConcat() )
413 DBG_ASSERT( pLongArr
->empty(), "I said: Simple!" );
418 long nTmpMin
= nMin
+ 2 * nStart
;
419 long nTmpMax
= nMax
- 2 * nEnd
;
420 if( nTmpMin
<= nTmpMax
)
422 pLongArr
->push_front(nTmpMax
);
423 pLongArr
->push_front(nTmpMin
);
428 pLongArr
->push_front(nMax
);
429 pLongArr
->push_front(nMin
);
433 else if( !IsConcat() )
437 void SvxBoundArgs::Add()
439 size_t nCount
= aBoolArr
.size();
440 if( nCount
&& ( !bInner
|| !pTextRanger
->IsSimple() ) )
442 bool bDelete
= aBoolArr
.front();
445 sal_uInt16 nLongIdx
= 1;
446 for( size_t nBoolIdx
= 1; nBoolIdx
< nCount
; ++nBoolIdx
)
451 while( nBoolIdx
< nCount
&& !aBoolArr
[ nBoolIdx
++ ] &&
452 (!bInner
|| nBoolIdx
< nCount
) )
454 pLongArr
->erase( pLongArr
->begin() + nLongIdx
, pLongArr
->begin() + nLongIdx
+ next
);
456 nBoolIdx
= nBoolIdx
- next
;
457 nCount
= nCount
- next
;
458 aBoolArr
.erase( aBoolArr
.begin() + nBoolIdx
, aBoolArr
.begin() + (nBoolIdx
+ next
) );
460 aBoolArr
[ nBoolIdx
- 1 ] = false;
461 #if OSL_DEBUG_LEVEL > 1
466 bDelete
= nBoolIdx
< nCount
&& aBoolArr
[ nBoolIdx
];
468 DBG_ASSERT( nLongIdx
== 2*nBoolIdx
+1, "BoundArgs: Array-Idx Confusion" );
469 DBG_ASSERT( aBoolArr
.size()*2 == pLongArr
->size(),
470 "BoundArgs: Array-Count: Confusion" );
473 if( !pLongArr
->empty() )
477 pLongArr
->pop_front();
478 pLongArr
->pop_back();
480 // Here the line is held inside a large rectangle for "simple"
481 // contour wrap. Currently (April 1999) the EditEngine evaluates
482 // only the first rectangle. If it one day is able to output a line
483 // in several parts, it may be advisable to delete the following lines.
484 if( pTextRanger
->IsSimple() && pLongArr
->size() > 2 )
485 pLongArr
->erase( pLongArr
->begin() + 1, pLongArr
->end() - 1 );
491 void SvxBoundArgs::Concat( const tools::PolyPolygon
* pPoly
)
494 DBG_ASSERT( pPoly
, "Nothing to do?" );
495 LongDqPtr pOld
= pLongArr
;
496 pLongArr
= new std::deque
<long>();
499 Calc( *pPoly
); // Note that this updates pLongArr, which is why we swapped it out earlier.
500 sal_uInt16 nCount
= pLongArr
->size();
503 bool bSubtract
= pTextRanger
->IsInner();
506 sal_uLong nOldCount
= pOld
->size();
507 if( nIdx
== nOldCount
)
508 { // Reached the end of the old Array...
510 pOld
->insert( pOld
->begin() + nIdx
, pLongArr
->begin() + i
, pLongArr
->end() );
513 long nLeft
= (*pLongArr
)[ i
++ ];
514 long nRight
= (*pLongArr
)[ i
++ ];
515 sal_uInt16 nLeftPos
= nIdx
+ 1;
516 while( nLeftPos
< nOldCount
&& nLeft
> (*pOld
)[ nLeftPos
] )
518 if( nLeftPos
>= nOldCount
)
519 { // The current interval belongs to the end of the old array ...
521 pOld
->insert( pOld
->begin() + nOldCount
, pLongArr
->begin() + i
- 2, pLongArr
->end() );
524 sal_uInt16 nRightPos
= nLeftPos
- 1;
525 while( nRightPos
< nOldCount
&& nRight
>= (*pOld
)[ nRightPos
] )
527 if( nRightPos
< nLeftPos
)
528 { // The current interval belongs between two old intervals
530 pOld
->insert( pOld
->begin() + nRightPos
, pLongArr
->begin() + i
- 2, pLongArr
->begin() + i
);
532 else if( bSubtract
) // Subtract, if necessary separate
535 if( nLeft
> ( nOld
= (*pOld
)[ nLeftPos
- 1 ] ) )
536 { // Now we split the left part...
537 if( nLeft
- 1 > nOld
)
539 pOld
->insert( pOld
->begin() + nLeftPos
- 1, nOld
);
540 pOld
->insert( pOld
->begin() + nLeftPos
, nLeft
- 1 );
545 if( nRightPos
- nLeftPos
> 1 )
546 pOld
->erase( pOld
->begin() + nLeftPos
, pOld
->begin() + nRightPos
- 1 );
547 if( ++nRight
>= ( nOld
= (*pOld
)[ nLeftPos
] ) )
548 pOld
->erase( pOld
->begin() + nLeftPos
- 1, pOld
->begin() + nLeftPos
+ 1 );
550 (*pOld
)[ nLeftPos
- 1 ] = nRight
;
554 if( nLeft
< (*pOld
)[ nLeftPos
- 1 ] )
555 (*pOld
)[ nLeftPos
- 1 ] = nLeft
;
556 if( nRight
> (*pOld
)[ nRightPos
- 1 ] )
557 (*pOld
)[ nRightPos
- 1 ] = nRight
;
558 if( nRightPos
- nLeftPos
> 1 )
559 pOld
->erase( pOld
->begin() + nLeftPos
, pOld
->begin() + nRightPos
- 1 );
567 /*************************************************************************
568 * SvxBoundArgs::Area returns the area in which the point is located.
569 * 0 = within the line
570 * 1 = below, but within the upper edge
571 * 2 = above, but within the lower edge
572 * 5 = below the upper edge
573 *10 = above the lower edge
574 *************************************************************************/
576 sal_uInt16
SvxBoundArgs::Area( const Point
& rPt
)
594 /*************************************************************************
595 * lcl_Cut calculates the X-Coordinate of the distance (Pt1-Pt2) at the
597 * It is assumed that the one of the points are located above and the other
598 * one below the Y-Coordinate.
599 *************************************************************************/
601 long SvxBoundArgs::Cut( long nB
, const Point
& rPt1
, const Point
& rPt2
)
603 if( pTextRanger
->IsVertical() )
605 double nQuot
= nB
- rPt1
.X();
606 nQuot
/= ( rPt2
.X() - rPt1
.X() );
607 nQuot
*= ( rPt2
.Y() - rPt1
.Y() );
608 return long( rPt1
.Y() + nQuot
);
610 double nQuot
= nB
- rPt1
.Y();
611 nQuot
/= ( rPt2
.Y() - rPt1
.Y() );
612 nQuot
*= ( rPt2
.X() - rPt1
.X() );
613 return long( rPt1
.X() + nQuot
);
616 void SvxBoundArgs::NoteUpLow( long nA
, const sal_uInt8 nArea
)
620 NoteMargin( nA
, nA
);
623 NoteRange( nArea
!= nAct
);
637 LongDqPtr
TextRanger::GetTextRanges( const Range
& rRange
)
639 DBG_ASSERT( rRange
.Min() || rRange
.Max(), "Zero-Range not allowed, Bye Bye" );
640 //Can we find the result we need in the cache?
641 for (std::deque
<RangeCache
>::iterator it
= mRangeCache
.begin(); it
!= mRangeCache
.end(); ++it
)
643 if (it
->range
== rRange
)
644 return &(it
->results
);
646 //Calculate a new result
647 RangeCache
rngCache(rRange
);
648 SvxBoundArgs
aArg( this, &(rngCache
.results
), rRange
);
649 aArg
.Calc( *mpPolyPolygon
);
650 if( mpLinePolyPolygon
)
651 aArg
.Concat( mpLinePolyPolygon
);
652 //Add new result to the cache
653 mRangeCache
.push_back(rngCache
);
654 if (mRangeCache
.size() > nCacheSize
)
655 mRangeCache
.pop_front();
656 return &(mRangeCache
.back().results
);
659 const Rectangle
& TextRanger::_GetBoundRect()
661 DBG_ASSERT( 0 == pBound
, "Don't call twice." );
662 pBound
= new Rectangle( mpPolyPolygon
->GetBoundRect() );
667 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */