1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <editeng/txtrange.hxx>
32 #include <tools/poly.hxx>
33 #include <tools/debug.hxx>
34 #include <basegfx/polygon/b2dpolygon.hxx>
35 #include <basegfx/polygon/b2dpolygontools.hxx>
39 TextRanger::TextRanger( const basegfx::B2DPolyPolygon
& rPolyPolygon
,
40 const basegfx::B2DPolyPolygon
* pLinePolyPolygon
,
41 sal_uInt16 nCacheSz
, sal_uInt16 nLft
, sal_uInt16 nRght
,
42 sal_Bool bSimpl
, sal_Bool bInnr
, sal_Bool bVert
) :
44 nCacheSize( nCacheSz
),
55 bFlag3
= bFlag4
= bFlag5
= bFlag6
= bFlag7
= sal_False
;
57 sal_uInt32
nCount(rPolyPolygon
.count());
58 mpPolyPolygon
= new PolyPolygon( (sal_uInt16
)nCount
);
60 for(sal_uInt32
i(0L); i
< nCount
; i
++)
62 const basegfx::B2DPolygon
aCandidate(rPolyPolygon
.getB2DPolygon(i
).getDefaultAdaptiveSubdivision());
63 nPointCount
+= aCandidate
.count();
64 mpPolyPolygon
->Insert( Polygon(aCandidate
), (sal_uInt16
)i
);
67 if( pLinePolyPolygon
)
69 nCount
= pLinePolyPolygon
->count();
70 mpLinePolyPolygon
= new PolyPolygon();
72 for(sal_uInt32
i(0L); i
< nCount
; i
++)
74 const basegfx::B2DPolygon
aCandidate(pLinePolyPolygon
->getB2DPolygon(i
).getDefaultAdaptiveSubdivision());
75 nPointCount
+= aCandidate
.count();
76 mpLinePolyPolygon
->Insert( Polygon(aCandidate
), (sal_uInt16
)i
);
80 mpLinePolyPolygon
= NULL
;
84 TextRanger::~TextRanger()
88 delete mpLinePolyPolygon
;
91 /* TextRanger::SetVertical(..)
92 If there's is a change in the writing direction,
93 the cache has to be cleared.
95 void TextRanger::SetVertical( sal_Bool bNew
)
97 if( IsVertical() != bNew
)
104 //! SvxBoundArgs is used to perform temporary calculations on a range array.
105 //! Temporary instances are created in TextRanger::GetTextRanges()
108 std::vector
<bool> aBoolArr
;
110 TextRanger
*pTextRanger
;
126 sal_Bool bClosed
: 1;
128 sal_Bool bMultiple
: 1;
129 sal_Bool bConcat
: 1;
130 sal_Bool bRotate
: 1;
131 void NoteRange( bool bToggle
);
132 long Cut( long nY
, const Point
& rPt1
, const Point
& rPt2
);
134 void _NoteFarPoint( long nPx
, long nPyDiff
, long nDiff
);
135 void NoteFarPoint( long nPx
, long nPyDiff
, long nDiff
)
136 { if( nDiff
) _NoteFarPoint( nPx
, nPyDiff
, nDiff
); }
137 long CalcMax( const Point
& rPt1
, const Point
& rPt2
, long nRange
, long nFar
);
138 void CheckCut( const Point
& rLst
, const Point
& rNxt
);
139 inline long A( const Point
& rP
) const { return bRotate
? rP
.Y() : rP
.X(); }
140 inline long B( const Point
& rP
) const { return bRotate
? rP
.X() : rP
.Y(); }
142 SvxBoundArgs( TextRanger
* pRanger
, LongDqPtr pLong
, const Range
& rRange
);
143 void NotePoint( const long nA
) { NoteMargin( nA
- nStart
, nA
+ nEnd
); }
144 void NoteMargin( const long nL
, const long nR
)
145 { if( nMin
> nL
) nMin
= nL
; if( nMax
< nR
) nMax
= nR
; }
146 sal_uInt16
Area( const Point
& rPt
);
147 void NoteUpLow( long nA
, const sal_uInt8 nArea
);
148 void Calc( const PolyPolygon
& rPoly
);
149 void Concat( const PolyPolygon
* pPoly
);
151 void NoteLast() { if( bMultiple
) NoteRange( nAct
== nFirst
); }
152 void SetClosed( const sal_Bool bNew
){ bClosed
= bNew
; }
153 sal_Bool
IsClosed() const { return bClosed
; }
154 void SetConcat( const sal_Bool bNew
){ bConcat
= bNew
; }
155 sal_Bool
IsConcat() const { return bConcat
; }
156 sal_uInt8
GetAct() const { return nAct
; }
159 SvxBoundArgs::SvxBoundArgs( TextRanger
* pRanger
, LongDqPtr pLong
,
160 const Range
& rRange
)
161 : pLongArr( pLong
), pTextRanger( pRanger
),
162 nTop( rRange
.Min() ), nBottom( rRange
.Max() ),
163 bInner( pRanger
->IsInner() ), bMultiple( bInner
|| !pRanger
->IsSimple() ),
164 bConcat( sal_False
), bRotate( pRanger
->IsVertical() )
168 nStart
= pRanger
->GetUpper();
169 nEnd
= pRanger
->GetLower();
170 nLowDiff
= pRanger
->GetLeft();
171 nUpDiff
= pRanger
->GetRight();
175 nStart
= pRanger
->GetLeft();
176 nEnd
= pRanger
->GetRight();
177 nLowDiff
= pRanger
->GetUpper();
178 nUpDiff
= pRanger
->GetLower();
180 nUpper
= nTop
- nUpDiff
;
181 nLower
= nBottom
+ nLowDiff
;
185 long SvxBoundArgs::CalcMax( const Point
& rPt1
, const Point
& rPt2
,
186 long nRange
, long nFarRange
)
188 double nDa
= Cut( nRange
, rPt1
, rPt2
) - Cut( nFarRange
, rPt1
, rPt2
);
199 nB
= nRange
+ nDa
* ( nFarRange
- nRange
) / sqrt( nB
);
203 bNote
= nB
> B(rPt1
);
205 bNote
= nB
< B(rPt1
);
207 return( long( nB
) );
211 void SvxBoundArgs::CheckCut( const Point
& rLst
, const Point
& rNxt
)
214 NotePoint( Cut( nBottom
, rLst
, rNxt
) );
216 NotePoint( Cut( nTop
, rLst
, rNxt
) );
217 if( rLst
.X() != rNxt
.X() && rLst
.Y() != rNxt
.Y() )
220 if( nLowDiff
&& ( ( nCut
& 1 ) || nLast
== 1 || nNext
== 1 ) )
222 nYps
= CalcMax( rLst
, rNxt
, nBottom
, nLower
);
224 _NoteFarPoint( Cut( nYps
, rLst
, rNxt
), nLower
-nYps
, nLowDiff
);
226 if( nUpDiff
&& ( ( nCut
& 2 ) || nLast
== 2 || nNext
== 2 ) )
228 nYps
= CalcMax( rLst
, rNxt
, nTop
, nUpper
);
230 _NoteFarPoint( Cut( nYps
, rLst
, rNxt
), nYps
-nUpper
, nUpDiff
);
235 void SvxBoundArgs::_NoteFarPoint( long nPa
, long nPbDiff
, long nDiff
)
238 double nQuot
= 2 * nDiff
- nPbDiff
;
240 nQuot
= sqrt( nQuot
);
242 nTmpA
= nPa
- long( nStart
* nQuot
);
243 nPbDiff
= nPa
+ long( nEnd
* nQuot
);
244 NoteMargin( nTmpA
, nPbDiff
);
247 void SvxBoundArgs::NoteRange( bool bToggle
)
249 DBG_ASSERT( nMax
>= nMin
|| bInner
, "NoteRange: Min > Max?");
255 sal_uInt16 nCount
= pLongArr
->size();
256 DBG_ASSERT( nCount
== 2 * aBoolArr
.size(), "NoteRange: Incompatible Sizes" );
257 while( nIdx
< nCount
&& (*pLongArr
)[ nIdx
] < nMin
)
259 sal_Bool bOdd
= (nIdx
% 2) ? sal_True
: sal_False
;
260 // No overlap with existing intervals?
261 if( nIdx
== nCount
|| ( !bOdd
&& nMax
< (*pLongArr
)[ nIdx
] ) )
262 { // Then a new one is inserted ...
263 pLongArr
->insert( pLongArr
->begin() + nIdx
, nMin
);
264 pLongArr
->insert( pLongArr
->begin() + nIdx
+ 1, nMax
);
265 aBoolArr
.insert( aBoolArr
.begin() + (nIdx
/2), bToggle
);
268 { // expand an existing interval ...
269 sal_uInt16 nMaxIdx
= nIdx
;
270 // If we end up on a left interval boundary, it must be reduced to nMin.
274 (*pLongArr
)[ nIdx
] = nMin
;
275 while( nMaxIdx
< nCount
&& (*pLongArr
)[ nMaxIdx
] < nMax
)
277 DBG_ASSERT( nMaxIdx
> nIdx
|| nMin
== nMax
, "NoteRange: Funny Situation." );
282 // If we end up on a right interval boundary, it must be raised to nMax.
284 (*pLongArr
)[ nMaxIdx
-- ] = nMax
;
285 // Possible merge of intervals.
286 sal_uInt16 nDiff
= nMaxIdx
- nIdx
;
287 nMaxIdx
= nIdx
/ 2; // From here on is nMaxIdx the Index in BoolArray.
290 pLongArr
->erase( pLongArr
->begin() + nIdx
+ 1, pLongArr
->begin() + nIdx
+ 1 + nDiff
);
292 sal_uInt16 nStop
= nMaxIdx
+ nDiff
;
293 for( sal_uInt16 i
= nMaxIdx
; i
< nStop
; ++i
)
294 bToggle
^= aBoolArr
[ i
];
295 aBoolArr
.erase( aBoolArr
.begin() + nMaxIdx
, aBoolArr
.begin() + (nMaxIdx
+ nDiff
) );
297 DBG_ASSERT( nMaxIdx
< aBoolArr
.size(), "NoteRange: Too much deleted" );
298 aBoolArr
[ nMaxIdx
] = aBoolArr
[ nMaxIdx
] ^ bToggle
;
302 void SvxBoundArgs::Calc( const PolyPolygon
& rPoly
)
306 for( sal_uInt16 i
= 0; i
< rPoly
.Count(); ++i
)
308 const Polygon
& rPol
= rPoly
[ i
];
309 nCount
= rPol
.GetSize();
312 const Point
& rNull
= rPol
[ 0 ];
313 SetClosed( IsConcat() || ( rNull
== rPol
[ nCount
- 1 ] ) );
314 nLast
= Area( rNull
);
323 // The first point of the polygon is within the line.
326 if( bMultiple
|| !nAct
)
332 NoteFarPoint( A(rNull
), nLower
- B(rNull
), nLowDiff
);
334 NoteFarPoint( A(rNull
), B(rNull
) - nUpper
, nUpDiff
);
338 if( bMultiple
|| !nAct
)
345 NotePoint( A(rNull
) );
347 nFirst
= 0; // leaving the line in which direction?
348 nAct
= 3; // we are within the line at the moment.
355 const Point
& rLast
= rPol
[ nIdx
- 1 ];
358 const Point
& rNext
= rPol
[ nIdx
];
359 nNext
= Area( rNext
);
360 nCut
= nNext
^ nLast
;
361 sal_uInt16 nOldAct
= nAct
;
363 CheckCut( rLast
, rNext
);
366 NoteUpLow( Cut( nLower
, rLast
, rNext
), 2 );
367 if( nAct
&& nAct
!= nOldAct
)
370 CheckCut( rLast
, rNext
);
375 NoteUpLow( Cut( nUpper
, rLast
, rNext
), 1 );
376 if( nAct
&& nAct
!= nOldAct
)
377 CheckCut( rLast
, rNext
);
381 if( !( nNext
& 12 ) )
385 if( !( nNext
& 12 ) )
388 NotePoint( A(rNext
) );
390 NoteFarPoint( A(rNext
), nLower
-B(rNext
), nLowDiff
);
392 NoteFarPoint( A(rNext
), B(rNext
)-nUpper
, nUpDiff
);
395 if( ++nIdx
== nCount
&& !IsClosed() )
397 if( !( nNext
& 12 ) )
403 if( bMultiple
&& IsConcat() )
412 DBG_ASSERT( pLongArr
->empty(), "I said: Simple!" );
417 long nTmpMin
= nMin
+ 2 * nStart
;
418 long nTmpMax
= nMax
- 2 * nEnd
;
419 if( nTmpMin
<= nTmpMax
)
421 pLongArr
->push_front(nTmpMax
);
422 pLongArr
->push_front(nTmpMin
);
427 pLongArr
->push_front(nMax
);
428 pLongArr
->push_front(nMin
);
432 else if( !IsConcat() )
436 void SvxBoundArgs::Add()
438 size_t nCount
= aBoolArr
.size();
439 if( nCount
&& ( !bInner
|| !pTextRanger
->IsSimple() ) )
441 sal_Bool bDelete
= aBoolArr
.front();
444 sal_uInt16 nLongIdx
= 1;
445 for( size_t nBoolIdx
= 1; nBoolIdx
< nCount
; ++nBoolIdx
)
450 while( nBoolIdx
< nCount
&& !aBoolArr
[ nBoolIdx
++ ] &&
451 (!bInner
|| nBoolIdx
< nCount
) )
453 pLongArr
->erase( pLongArr
->begin() + nLongIdx
, pLongArr
->begin() + nLongIdx
+ next
);
455 nBoolIdx
= nBoolIdx
- next
;
456 nCount
= nCount
- next
;
457 aBoolArr
.erase( aBoolArr
.begin() + nBoolIdx
, aBoolArr
.begin() + (nBoolIdx
+ next
) );
459 aBoolArr
[ nBoolIdx
- 1 ] = sal_False
;
460 #if OSL_DEBUG_LEVEL > 1
465 bDelete
= nBoolIdx
< nCount
&& aBoolArr
[ nBoolIdx
];
467 DBG_ASSERT( nLongIdx
== 2*nBoolIdx
+1, "BoundArgs: Array-Idx Confusion" );
468 DBG_ASSERT( aBoolArr
.size()*2 == pLongArr
->size(),
469 "BoundArgs: Array-Count: Confusion" );
472 if( 0 != ( nCount
= pLongArr
->size() ) )
476 pLongArr
->pop_front();
477 pLongArr
->pop_back();
479 // Here the line is held inside a large rectangle for "simple"
480 // contour wrap. Currently (April 1999) the EditEngine evaluates
481 // only the first rectangle. If it one day is able to output a line
482 // in several parts, it may be advisable to delete the following lines.
483 if( pTextRanger
->IsSimple() && pLongArr
->size() > 2 )
484 pLongArr
->erase( pLongArr
->begin() + 1, pLongArr
->end() - 1 );
490 void SvxBoundArgs::Concat( const PolyPolygon
* pPoly
)
492 SetConcat( sal_True
);
493 DBG_ASSERT( pPoly
, "Nothing to do?" );
494 LongDqPtr pOld
= pLongArr
;
495 pLongArr
= new std::deque
<long>();
498 Calc( *pPoly
); // Note that this updates pLongArr, which is why we swapped it out earlier.
499 sal_uInt16 nCount
= pLongArr
->size();
502 sal_Bool bSubtract
= pTextRanger
->IsInner();
505 sal_uLong nOldCount
= pOld
->size();
506 if( nIdx
== nOldCount
)
507 { // Reached the end of the old Array...
509 pOld
->insert( pOld
->begin() + nIdx
, pLongArr
->begin() + i
, pLongArr
->end() );
512 long nLeft
= (*pLongArr
)[ i
++ ];
513 long nRight
= (*pLongArr
)[ i
++ ];
514 sal_uInt16 nLeftPos
= nIdx
+ 1;
515 while( nLeftPos
< nOldCount
&& nLeft
> (*pOld
)[ nLeftPos
] )
517 if( nLeftPos
>= nOldCount
)
518 { // The current interval belongs to the end of the old array ...
520 pOld
->insert( pOld
->begin() + nOldCount
, pLongArr
->begin() + i
- 2, pLongArr
->end() );
523 sal_uInt16 nRightPos
= nLeftPos
- 1;
524 while( nRightPos
< nOldCount
&& nRight
>= (*pOld
)[ nRightPos
] )
526 if( nRightPos
< nLeftPos
)
527 { // The current interval belongs between two old intervals
529 pOld
->insert( pOld
->begin() + nRightPos
, pLongArr
->begin() + i
- 2, pLongArr
->begin() + i
);
530 nIdx
= nRightPos
+ 2;
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: */