bump product version to 5.0.4.1
[LibreOffice.git] / editeng / source / misc / txtrange.cxx
blobb2964f15a5ee518d388ca1f18fa62a60da99fe06
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include <math.h>
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>
29 #include <vector>
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 ) :
35 pBound( NULL ),
36 nCacheSize( nCacheSz ),
37 nRight( nRght ),
38 nLeft( nLft ),
39 nUpper( 0 ),
40 nLower( 0 ),
41 nPointCount( 0 ),
42 bSimple( bSimpl ),
43 bInner( bInnr ),
44 bVertical( bVert )
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 );
68 else
69 mpLinePolyPolygon = NULL;
73 TextRanger::~TextRanger()
75 mRangeCache.clear();
76 delete mpPolyPolygon;
77 delete mpLinePolyPolygon;
78 delete pBound;
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 )
89 bVertical = bNew;
90 mRangeCache.clear();
94 //! SvxBoundArgs is used to perform temporary calculations on a range array.
95 //! Temporary instances are created in TextRanger::GetTextRanges()
96 class SvxBoundArgs
98 std::vector<bool> aBoolArr;
99 LongDqPtr pLongArr;
100 TextRanger *pTextRanger;
101 long nMin;
102 long nMax;
103 long nTop;
104 long nBottom;
105 long nUpDiff;
106 long nLowDiff;
107 long nUpper;
108 long nLower;
109 long nStart;
110 long nEnd;
111 sal_uInt16 nCut;
112 sal_uInt16 nLast;
113 sal_uInt16 nNext;
114 sal_uInt8 nAct;
115 sal_uInt8 nFirst;
116 bool bClosed : 1;
117 bool bInner : 1;
118 bool bMultiple : 1;
119 bool bConcat : 1;
120 bool bRotate : 1;
121 void NoteRange( bool bToggle );
122 long Cut( long nY, const Point& rPt1, const Point& rPt2 );
123 void Add();
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(); }
131 public:
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 );
140 // inlines
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 )
150 : pLongArr(pLong)
151 , pTextRanger(pRanger)
152 , nMin(0)
153 , nMax(0)
154 , nTop(rRange.Min())
155 , nBottom(rRange.Max())
156 , nCut(0)
157 , nLast(0)
158 , nNext(0)
159 , nAct(0)
160 , nFirst(0)
161 , bClosed(false)
162 , bInner(pRanger->IsInner())
163 , bMultiple(bInner || !pRanger->IsSimple())
164 , bConcat(false)
165 , bRotate(pRanger->IsVertical())
167 if( bRotate )
169 nStart = pRanger->GetUpper();
170 nEnd = pRanger->GetLower();
171 nLowDiff = pRanger->GetLeft();
172 nUpDiff = pRanger->GetRight();
174 else
176 nStart = pRanger->GetLeft();
177 nEnd = pRanger->GetRight();
178 nLowDiff = pRanger->GetUpper();
179 nUpDiff = pRanger->GetLower();
181 nUpper = nTop - nUpDiff;
182 nLower = nBottom + nLowDiff;
183 pLongArr->clear();
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 );
190 double nB;
191 if( nDa < 0 )
193 nDa = -nDa;
194 nB = nEnd;
196 else
197 nB = nStart;
198 nB *= nB;
199 nB += nDa * nDa;
200 nB = nRange + nDa * ( nFarRange - nRange ) / sqrt( nB );
202 bool bNote;
203 if( nB < B(rPt2) )
204 bNote = nB > B(rPt1);
205 else
206 bNote = nB < B(rPt1);
207 if( bNote )
208 return( long( nB ) );
209 return 0;
212 void SvxBoundArgs::CheckCut( const Point& rLst, const Point& rNxt )
214 if( nCut & 1 )
215 NotePoint( Cut( nBottom, rLst, rNxt ) );
216 if( nCut & 2 )
217 NotePoint( Cut( nTop, rLst, rNxt ) );
218 if( rLst.X() != rNxt.X() && rLst.Y() != rNxt.Y() )
220 long nYps;
221 if( nLowDiff && ( ( nCut & 1 ) || nLast == 1 || nNext == 1 ) )
223 nYps = CalcMax( rLst, rNxt, nBottom, nLower );
224 if( nYps )
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 );
230 if( nYps )
231 _NoteFarPoint( Cut( nYps, rLst, rNxt ), nYps-nUpper, nUpDiff );
236 void SvxBoundArgs::_NoteFarPoint( long nPa, long nPbDiff, long nDiff )
238 long nTmpA;
239 double nQuot = 2 * nDiff - nPbDiff;
240 nQuot *= nPbDiff;
241 nQuot = sqrt( nQuot );
242 nQuot /= nDiff;
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?");
251 if( nMax < nMin )
252 return;
253 if( !bClosed )
254 bToggle = false;
255 sal_uInt16 nIdx = 0;
256 sal_uInt16 nCount = pLongArr->size();
257 DBG_ASSERT( nCount == 2 * aBoolArr.size(), "NoteRange: Incompatible Sizes" );
258 while( nIdx < nCount && (*pLongArr)[ nIdx ] < nMin )
259 ++nIdx;
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 );
268 else
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.
272 if( bOdd )
273 --nIdx;
274 else
275 (*pLongArr)[ nIdx ] = nMin;
276 while( nMaxIdx < nCount && (*pLongArr)[ nMaxIdx ] < nMax )
277 ++nMaxIdx;
278 DBG_ASSERT( nMaxIdx > nIdx || nMin == nMax, "NoteRange: Funny Situation." );
279 if( nMaxIdx )
280 --nMaxIdx;
281 if( nMaxIdx < nIdx )
282 nMaxIdx = nIdx;
283 // If we end up on a right interval boundary, it must be raised to nMax.
284 if( nMaxIdx % 2 )
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.
289 if( nDiff )
291 pLongArr->erase( pLongArr->begin() + nIdx + 1, pLongArr->begin() + nIdx + 1 + nDiff );
292 nDiff /= 2;
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 )
305 sal_uInt16 nCount;
306 nAct = 0;
307 for( sal_uInt16 i = 0; i < rPoly.Count(); ++i )
309 const Polygon& rPol = rPoly[ i ];
310 nCount = rPol.GetSize();
311 if( nCount )
313 const Point& rNull = rPol[ 0 ];
314 SetClosed( IsConcat() || ( rNull == rPol[ nCount - 1 ] ) );
315 nLast = Area( rNull );
316 if( nLast & 12 )
318 nFirst = 3;
319 if( bMultiple )
320 nAct = 0;
322 else
324 // The first point of the polygon is within the line.
325 if( nLast )
327 if( bMultiple || !nAct )
329 nMin = USHRT_MAX;
330 nMax = 0;
332 if( nLast & 1 )
333 NoteFarPoint( A(rNull), nLower - B(rNull), nLowDiff );
334 else
335 NoteFarPoint( A(rNull), B(rNull) - nUpper, nUpDiff );
337 else
339 if( bMultiple || !nAct )
341 nMin = A(rNull);
342 nMax = nMin + nEnd;
343 nMin -= nStart;
345 else
346 NotePoint( A(rNull) );
348 nFirst = 0; // leaving the line in which direction?
349 nAct = 3; // we are within the line at the moment.
351 if( nCount > 1 )
353 sal_uInt16 nIdx = 1;
354 while( true )
356 const Point& rLast = rPol[ nIdx - 1 ];
357 if( nIdx == nCount )
358 nIdx = 0;
359 const Point& rNext = rPol[ nIdx ];
360 nNext = Area( rNext );
361 nCut = nNext ^ nLast;
362 sal_uInt16 nOldAct = nAct;
363 if( nAct )
364 CheckCut( rLast, rNext );
365 if( nCut & 4 )
367 NoteUpLow( Cut( nLower, rLast, rNext ), 2 );
368 if( nAct && nAct != nOldAct )
370 nOldAct = nAct;
371 CheckCut( rLast, rNext );
374 if( nCut & 8 )
376 NoteUpLow( Cut( nUpper, rLast, rNext ), 1 );
377 if( nAct && nAct != nOldAct )
378 CheckCut( rLast, rNext );
380 if( !nIdx )
382 if( !( nNext & 12 ) )
383 NoteLast();
384 break;
386 if( !( nNext & 12 ) )
388 if( !nNext )
389 NotePoint( A(rNext) );
390 else if( nNext & 1 )
391 NoteFarPoint( A(rNext), nLower-B(rNext), nLowDiff );
392 else
393 NoteFarPoint( A(rNext), B(rNext)-nUpper, nUpDiff );
395 nLast = nNext;
396 if( ++nIdx == nCount && !IsClosed() )
398 if( !( nNext & 12 ) )
399 NoteLast();
400 break;
404 if( bMultiple && IsConcat() )
406 Add();
407 nAct = 0;
411 if( !bMultiple )
413 DBG_ASSERT( pLongArr->empty(), "I said: Simple!" );
414 if( nAct )
416 if( bInner )
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);
426 else
428 pLongArr->push_front(nMax);
429 pLongArr->push_front(nMin);
433 else if( !IsConcat() )
434 Add();
437 void SvxBoundArgs::Add()
439 size_t nCount = aBoolArr.size();
440 if( nCount && ( !bInner || !pTextRanger->IsSimple() ) )
442 bool bDelete = aBoolArr.front();
443 if( bInner )
444 bDelete = !bDelete;
445 sal_uInt16 nLongIdx = 1;
446 for( size_t nBoolIdx = 1; nBoolIdx < nCount; ++nBoolIdx )
448 if( bDelete )
450 sal_uInt16 next = 2;
451 while( nBoolIdx < nCount && !aBoolArr[ nBoolIdx++ ] &&
452 (!bInner || nBoolIdx < nCount ) )
453 next += 2;
454 pLongArr->erase( pLongArr->begin() + nLongIdx, pLongArr->begin() + nLongIdx + next );
455 next /= 2;
456 nBoolIdx = nBoolIdx - next;
457 nCount = nCount - next;
458 aBoolArr.erase( aBoolArr.begin() + nBoolIdx, aBoolArr.begin() + (nBoolIdx + next) );
459 if( nBoolIdx )
460 aBoolArr[ nBoolIdx - 1 ] = false;
461 #if OSL_DEBUG_LEVEL > 1
462 else
463 ++next;
464 #endif
466 bDelete = nBoolIdx < nCount && aBoolArr[ nBoolIdx ];
467 nLongIdx += 2;
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() )
475 if( bInner )
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 )
493 SetConcat( true );
494 DBG_ASSERT( pPoly, "Nothing to do?" );
495 LongDqPtr pOld = pLongArr;
496 pLongArr = new std::deque<long>();
497 aBoolArr.clear();
498 bInner = false;
499 Calc( *pPoly ); // Note that this updates pLongArr, which is why we swapped it out earlier.
500 sal_uInt16 nCount = pLongArr->size();
501 sal_uInt16 nIdx = 0;
502 sal_uInt16 i = 0;
503 bool bSubtract = pTextRanger->IsInner();
504 while( i < nCount )
506 sal_uLong nOldCount = pOld->size();
507 if( nIdx == nOldCount )
508 { // Reached the end of the old Array...
509 if( !bSubtract )
510 pOld->insert( pOld->begin() + nIdx, pLongArr->begin() + i, pLongArr->end() );
511 break;
513 long nLeft = (*pLongArr)[ i++ ];
514 long nRight = (*pLongArr)[ i++ ];
515 sal_uInt16 nLeftPos = nIdx + 1;
516 while( nLeftPos < nOldCount && nLeft > (*pOld)[ nLeftPos ] )
517 nLeftPos += 2;
518 if( nLeftPos >= nOldCount )
519 { // The current interval belongs to the end of the old array ...
520 if( !bSubtract )
521 pOld->insert( pOld->begin() + nOldCount, pLongArr->begin() + i - 2, pLongArr->end() );
522 break;
524 sal_uInt16 nRightPos = nLeftPos - 1;
525 while( nRightPos < nOldCount && nRight >= (*pOld)[ nRightPos ] )
526 nRightPos += 2;
527 if( nRightPos < nLeftPos )
528 { // The current interval belongs between two old intervals
529 if( !bSubtract )
530 pOld->insert( pOld->begin() + nRightPos, pLongArr->begin() + i - 2, pLongArr->begin() + i );
532 else if( bSubtract ) // Subtract, if necessary separate
534 long nOld;
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 );
541 nLeftPos += 2;
542 nRightPos += 2;
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 );
549 else
550 (*pOld)[ nLeftPos - 1 ] = nRight;
552 else // Merge
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 );
562 nIdx = nLeftPos - 1;
564 delete pLongArr;
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 )
578 long nB = B( rPt );
579 if( nB >= nBottom )
581 if( nB >= nLower )
582 return 5;
583 return 1;
585 if( nB <= nTop )
587 if( nB <= nUpper )
588 return 10;
589 return 2;
591 return 0;
594 /*************************************************************************
595 * lcl_Cut calculates the X-Coordinate of the distance (Pt1-Pt2) at the
596 * Y-Coordinate nY.
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 )
618 if( nAct )
620 NoteMargin( nA, nA );
621 if( bMultiple )
623 NoteRange( nArea != nAct );
624 nAct = 0;
626 if( !nFirst )
627 nFirst = nArea;
629 else
631 nAct = nArea;
632 nMin = nA;
633 nMax = nA;
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() );
663 return *pBound;
667 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */