nss: upgrade to release 3.73
[LibreOffice.git] / editeng / source / misc / txtrange.cxx
blobb8c46e0cdcbaa9b47c3bb3fb58be57a0727b63aa
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 <basegfx/polygon/b2dpolygon.hxx>
26 #include <basegfx/polygon/b2dpolypolygon.hxx>
28 #include <vector>
30 TextRanger::TextRanger( const basegfx::B2DPolyPolygon& rPolyPolygon,
31 const basegfx::B2DPolyPolygon* pLinePolyPolygon,
32 sal_uInt16 nCacheSz, sal_uInt16 nLft, sal_uInt16 nRght,
33 bool bSimpl, bool bInnr, bool bVert ) :
34 maPolyPolygon( rPolyPolygon.count() ),
35 nCacheSize( nCacheSz ),
36 nRight( nRght ),
37 nLeft( nLft ),
38 nUpper( 0 ),
39 nLower( 0 ),
40 nPointCount( 0 ),
41 bSimple( bSimpl ),
42 bInner( bInnr ),
43 bVertical( bVert )
45 sal_uInt32 nCount(rPolyPolygon.count());
47 for(sal_uInt32 i(0); i < nCount; i++)
49 const basegfx::B2DPolygon aCandidate(rPolyPolygon.getB2DPolygon(i).getDefaultAdaptiveSubdivision());
50 nPointCount += aCandidate.count();
51 maPolyPolygon.Insert( tools::Polygon(aCandidate), static_cast<sal_uInt16>(i) );
54 if( pLinePolyPolygon )
56 nCount = pLinePolyPolygon->count();
57 mpLinePolyPolygon.reset( new tools::PolyPolygon() );
59 for(sal_uInt32 i(0); i < nCount; i++)
61 const basegfx::B2DPolygon aCandidate(pLinePolyPolygon->getB2DPolygon(i).getDefaultAdaptiveSubdivision());
62 nPointCount += aCandidate.count();
63 mpLinePolyPolygon->Insert( tools::Polygon(aCandidate), static_cast<sal_uInt16>(i) );
66 else
67 mpLinePolyPolygon = nullptr;
71 TextRanger::~TextRanger()
73 mRangeCache.clear();
76 /* TextRanger::SetVertical(..)
77 If there's is a change in the writing direction,
78 the cache has to be cleared.
80 void TextRanger::SetVertical( bool bNew )
82 if( IsVertical() != bNew )
84 bVertical = bNew;
85 mRangeCache.clear();
89 namespace {
91 //! SvxBoundArgs is used to perform temporary calculations on a range array.
92 //! Temporary instances are created in TextRanger::GetTextRanges()
93 class SvxBoundArgs
95 std::vector<bool> aBoolArr;
96 std::deque<tools::Long>* pLongArr;
97 TextRanger *pTextRanger;
98 tools::Long nMin;
99 tools::Long nMax;
100 tools::Long nTop;
101 tools::Long nBottom;
102 tools::Long nUpDiff;
103 tools::Long nLowDiff;
104 tools::Long nUpper;
105 tools::Long nLower;
106 tools::Long nStart;
107 tools::Long nEnd;
108 sal_uInt16 nCut;
109 sal_uInt16 nLast;
110 sal_uInt16 nNext;
111 sal_uInt8 nAct;
112 sal_uInt8 nFirst;
113 bool bClosed : 1;
114 bool bInner : 1;
115 bool bMultiple : 1;
116 bool bConcat : 1;
117 bool bRotate : 1;
118 void NoteRange( bool bToggle );
119 tools::Long Cut( tools::Long nY, const Point& rPt1, const Point& rPt2 );
120 void Add();
121 void NoteFarPoint_( tools::Long nPx, tools::Long nPyDiff, tools::Long nDiff );
122 void NoteFarPoint( tools::Long nPx, tools::Long nPyDiff, tools::Long nDiff )
123 { if( nDiff ) NoteFarPoint_( nPx, nPyDiff, nDiff ); }
124 tools::Long CalcMax( const Point& rPt1, const Point& rPt2, tools::Long nRange, tools::Long nFar );
125 void CheckCut( const Point& rLst, const Point& rNxt );
126 tools::Long A( const Point& rP ) const { return bRotate ? rP.Y() : rP.X(); }
127 tools::Long B( const Point& rP ) const { return bRotate ? rP.X() : rP.Y(); }
128 public:
129 SvxBoundArgs( TextRanger* pRanger, std::deque<tools::Long>* pLong, const Range& rRange );
130 void NotePoint( const tools::Long nA ) { NoteMargin( nA - nStart, nA + nEnd ); }
131 void NoteMargin( const tools::Long nL, const tools::Long nR )
132 { if( nMin > nL ) nMin = nL; if( nMax < nR ) nMax = nR; }
133 sal_uInt16 Area( const Point& rPt );
134 void NoteUpLow( tools::Long nA, const sal_uInt8 nArea );
135 void Calc( const tools::PolyPolygon& rPoly );
136 void Concat( const tools::PolyPolygon* pPoly );
137 // inlines
138 void NoteLast() { if( bMultiple ) NoteRange( nAct == nFirst ); }
139 void SetConcat( const bool bNew ){ bConcat = bNew; }
140 bool IsConcat() const { return bConcat; }
145 SvxBoundArgs::SvxBoundArgs( TextRanger* pRanger, std::deque<tools::Long>* pLong,
146 const Range& rRange )
147 : pLongArr(pLong)
148 , pTextRanger(pRanger)
149 , nMin(0)
150 , nMax(0)
151 , nTop(rRange.Min())
152 , nBottom(rRange.Max())
153 , nCut(0)
154 , nLast(0)
155 , nNext(0)
156 , nAct(0)
157 , nFirst(0)
158 , bClosed(false)
159 , bInner(pRanger->IsInner())
160 , bMultiple(bInner || !pRanger->IsSimple())
161 , bConcat(false)
162 , bRotate(pRanger->IsVertical())
164 if( bRotate )
166 nStart = pRanger->GetUpper();
167 nEnd = pRanger->GetLower();
168 nLowDiff = pRanger->GetLeft();
169 nUpDiff = pRanger->GetRight();
171 else
173 nStart = pRanger->GetLeft();
174 nEnd = pRanger->GetRight();
175 nLowDiff = pRanger->GetUpper();
176 nUpDiff = pRanger->GetLower();
178 nUpper = nTop - nUpDiff;
179 nLower = nBottom + nLowDiff;
180 pLongArr->clear();
183 tools::Long SvxBoundArgs::CalcMax( const Point& rPt1, const Point& rPt2,
184 tools::Long nRange, tools::Long nFarRange )
186 double nDa = Cut( nRange, rPt1, rPt2 ) - Cut( nFarRange, rPt1, rPt2 );
187 double nB;
188 if( nDa < 0 )
190 nDa = -nDa;
191 nB = nEnd;
193 else
194 nB = nStart;
195 nB *= nB;
196 nB += nDa * nDa;
197 nB = nRange + nDa * ( nFarRange - nRange ) / sqrt( nB );
199 bool bNote;
200 if( nB < B(rPt2) )
201 bNote = nB > B(rPt1);
202 else
203 bNote = nB < B(rPt1);
204 if( bNote )
205 return( tools::Long( nB ) );
206 return 0;
209 void SvxBoundArgs::CheckCut( const Point& rLst, const Point& rNxt )
211 if( nCut & 1 )
212 NotePoint( Cut( nBottom, rLst, rNxt ) );
213 if( nCut & 2 )
214 NotePoint( Cut( nTop, rLst, rNxt ) );
215 if( rLst.X() == rNxt.X() || rLst.Y() == rNxt.Y() )
216 return;
218 tools::Long nYps;
219 if( nLowDiff && ( ( nCut & 1 ) || nLast == 1 || nNext == 1 ) )
221 nYps = CalcMax( rLst, rNxt, nBottom, nLower );
222 if( nYps )
223 NoteFarPoint_( Cut( nYps, rLst, rNxt ), nLower-nYps, nLowDiff );
225 if( nUpDiff && ( ( nCut & 2 ) || nLast == 2 || nNext == 2 ) )
227 nYps = CalcMax( rLst, rNxt, nTop, nUpper );
228 if( nYps )
229 NoteFarPoint_( Cut( nYps, rLst, rNxt ), nYps-nUpper, nUpDiff );
233 void SvxBoundArgs::NoteFarPoint_( tools::Long nPa, tools::Long nPbDiff, tools::Long nDiff )
235 tools::Long nTmpA;
236 double nQuot = 2 * nDiff - nPbDiff;
237 nQuot *= nPbDiff;
238 nQuot = sqrt( nQuot );
239 nQuot /= nDiff;
240 nTmpA = nPa - tools::Long( nStart * nQuot );
241 nPbDiff = nPa + tools::Long( nEnd * nQuot );
242 NoteMargin( nTmpA, nPbDiff );
245 void SvxBoundArgs::NoteRange( bool bToggle )
247 DBG_ASSERT( nMax >= nMin || bInner, "NoteRange: Min > Max?");
248 if( nMax < nMin )
249 return;
250 if( !bClosed )
251 bToggle = false;
252 sal_uInt16 nIdx = 0;
253 sal_uInt16 nCount = pLongArr->size();
254 DBG_ASSERT( nCount == 2 * aBoolArr.size(), "NoteRange: Incompatible Sizes" );
255 while( nIdx < nCount && (*pLongArr)[ nIdx ] < nMin )
256 ++nIdx;
257 bool bOdd = (nIdx % 2) != 0;
258 // No overlap with existing intervals?
259 if( nIdx == nCount || ( !bOdd && nMax < (*pLongArr)[ nIdx ] ) )
260 { // Then a new one is inserted ...
261 pLongArr->insert( pLongArr->begin() + nIdx, nMin );
262 pLongArr->insert( pLongArr->begin() + nIdx + 1, nMax );
263 aBoolArr.insert( aBoolArr.begin() + (nIdx/2), bToggle );
265 else
266 { // expand an existing interval ...
267 sal_uInt16 nMaxIdx = nIdx;
268 // If we end up on a left interval boundary, it must be reduced to nMin.
269 if( bOdd )
270 --nIdx;
271 else
272 (*pLongArr)[ nIdx ] = nMin;
273 while( nMaxIdx < nCount && (*pLongArr)[ nMaxIdx ] < nMax )
274 ++nMaxIdx;
275 DBG_ASSERT( nMaxIdx > nIdx || nMin == nMax, "NoteRange: Funny Situation." );
276 if( nMaxIdx )
277 --nMaxIdx;
278 if( nMaxIdx < nIdx )
279 nMaxIdx = nIdx;
280 // If we end up on a right interval boundary, it must be raised to nMax.
281 if( nMaxIdx % 2 )
282 (*pLongArr)[ nMaxIdx-- ] = nMax;
283 // Possible merge of intervals.
284 sal_uInt16 nDiff = nMaxIdx - nIdx;
285 nMaxIdx = nIdx / 2; // From here on is nMaxIdx the Index in BoolArray.
286 if( nDiff )
288 pLongArr->erase( pLongArr->begin() + nIdx + 1, pLongArr->begin() + nIdx + 1 + nDiff );
289 nDiff /= 2;
290 sal_uInt16 nStop = nMaxIdx + nDiff;
291 for( sal_uInt16 i = nMaxIdx; i < nStop; ++i )
292 bToggle ^= aBoolArr[ i ];
293 aBoolArr.erase( aBoolArr.begin() + nMaxIdx, aBoolArr.begin() + (nMaxIdx + nDiff) );
295 DBG_ASSERT( nMaxIdx < aBoolArr.size(), "NoteRange: Too much deleted" );
296 aBoolArr[ nMaxIdx ] = aBoolArr[ nMaxIdx ] != bToggle;
300 void SvxBoundArgs::Calc( const tools::PolyPolygon& rPoly )
302 sal_uInt16 nCount;
303 nAct = 0;
304 for( sal_uInt16 i = 0; i < rPoly.Count(); ++i )
306 const tools::Polygon& rPol = rPoly[ i ];
307 nCount = rPol.GetSize();
308 if( nCount )
310 const Point& rNull = rPol[ 0 ];
311 bClosed = IsConcat() || ( rNull == rPol[ nCount - 1 ] );
312 nLast = Area( rNull );
313 if( nLast & 12 )
315 nFirst = 3;
316 if( bMultiple )
317 nAct = 0;
319 else
321 // The first point of the polygon is within the line.
322 if( nLast )
324 if( bMultiple || !nAct )
326 nMin = USHRT_MAX;
327 nMax = 0;
329 if( nLast & 1 )
330 NoteFarPoint( A(rNull), nLower - B(rNull), nLowDiff );
331 else
332 NoteFarPoint( A(rNull), B(rNull) - nUpper, nUpDiff );
334 else
336 if( bMultiple || !nAct )
338 nMin = A(rNull);
339 nMax = nMin + nEnd;
340 nMin -= nStart;
342 else
343 NotePoint( A(rNull) );
345 nFirst = 0; // leaving the line in which direction?
346 nAct = 3; // we are within the line at the moment.
348 if( nCount > 1 )
350 sal_uInt16 nIdx = 1;
351 while( true )
353 const Point& rLast = rPol[ nIdx - 1 ];
354 if( nIdx == nCount )
355 nIdx = 0;
356 const Point& rNext = rPol[ nIdx ];
357 nNext = Area( rNext );
358 nCut = nNext ^ nLast;
359 sal_uInt16 nOldAct = nAct;
360 if( nAct )
361 CheckCut( rLast, rNext );
362 if( nCut & 4 )
364 NoteUpLow( Cut( nLower, rLast, rNext ), 2 );
365 if( nAct && nAct != nOldAct )
367 nOldAct = nAct;
368 CheckCut( rLast, rNext );
371 if( nCut & 8 )
373 NoteUpLow( Cut( nUpper, rLast, rNext ), 1 );
374 if( nAct && nAct != nOldAct )
375 CheckCut( rLast, rNext );
377 if( !nIdx )
379 if( !( nNext & 12 ) )
380 NoteLast();
381 break;
383 if( !( nNext & 12 ) )
385 if( !nNext )
386 NotePoint( A(rNext) );
387 else if( nNext & 1 )
388 NoteFarPoint( A(rNext), nLower-B(rNext), nLowDiff );
389 else
390 NoteFarPoint( A(rNext), B(rNext)-nUpper, nUpDiff );
392 nLast = nNext;
393 if( ++nIdx == nCount && !bClosed )
395 if( !( nNext & 12 ) )
396 NoteLast();
397 break;
401 if( bMultiple && IsConcat() )
403 Add();
404 nAct = 0;
408 if( !bMultiple )
410 DBG_ASSERT( pLongArr->empty(), "I said: Simple!" );
411 if( nAct )
413 if( bInner )
415 tools::Long nTmpMin = nMin + 2 * nStart;
416 tools::Long nTmpMax = nMax - 2 * nEnd;
417 if( nTmpMin <= nTmpMax )
419 pLongArr->push_front(nTmpMax);
420 pLongArr->push_front(nTmpMin);
423 else
425 pLongArr->push_front(nMax);
426 pLongArr->push_front(nMin);
430 else if( !IsConcat() )
431 Add();
434 void SvxBoundArgs::Add()
436 size_t nCount = aBoolArr.size();
437 if( nCount && ( !bInner || !pTextRanger->IsSimple() ) )
439 bool bDelete = aBoolArr.front();
440 if( bInner )
441 bDelete = !bDelete;
442 sal_uInt16 nLongIdx = 1;
443 for( size_t nBoolIdx = 1; nBoolIdx < nCount; ++nBoolIdx )
445 if( bDelete )
447 sal_uInt16 next = 2;
448 while( nBoolIdx < nCount && !aBoolArr[ nBoolIdx++ ] &&
449 (!bInner || nBoolIdx < nCount ) )
450 next += 2;
451 pLongArr->erase( pLongArr->begin() + nLongIdx, pLongArr->begin() + nLongIdx + next );
452 next /= 2;
453 nBoolIdx = nBoolIdx - next;
454 nCount = nCount - next;
455 aBoolArr.erase( aBoolArr.begin() + nBoolIdx, aBoolArr.begin() + (nBoolIdx + next) );
456 if( nBoolIdx )
457 aBoolArr[ nBoolIdx - 1 ] = false;
458 #if OSL_DEBUG_LEVEL > 1
459 else
460 ++next;
461 #endif
463 bDelete = nBoolIdx < nCount && aBoolArr[ nBoolIdx ];
464 nLongIdx += 2;
465 DBG_ASSERT( nLongIdx == 2*nBoolIdx+1, "BoundArgs: Array-Idx Confusion" );
466 DBG_ASSERT( aBoolArr.size()*2 == pLongArr->size(),
467 "BoundArgs: Array-Count: Confusion" );
470 if( pLongArr->empty() )
471 return;
473 if( !bInner )
474 return;
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 );
487 void SvxBoundArgs::Concat( const tools::PolyPolygon* pPoly )
489 SetConcat( true );
490 DBG_ASSERT( pPoly, "Nothing to do?" );
491 std::deque<tools::Long>* pOld = pLongArr;
492 pLongArr = new std::deque<tools::Long>;
493 aBoolArr.clear();
494 bInner = false;
495 Calc( *pPoly ); // Note that this updates pLongArr, which is why we swapped it out earlier.
496 std::deque<tools::Long>::size_type nCount = pLongArr->size();
497 std::deque<tools::Long>::size_type nIdx = 0;
498 std::deque<tools::Long>::size_type i = 0;
499 bool bSubtract = pTextRanger->IsInner();
500 while( i < nCount )
502 std::deque<tools::Long>::size_type nOldCount = pOld->size();
503 if( nIdx == nOldCount )
504 { // Reached the end of the old Array...
505 if( !bSubtract )
506 pOld->insert( pOld->begin() + nIdx, pLongArr->begin() + i, pLongArr->end() );
507 break;
509 tools::Long nLeft = (*pLongArr)[ i++ ];
510 tools::Long nRight = (*pLongArr)[ i++ ];
511 std::deque<tools::Long>::size_type nLeftPos = nIdx + 1;
512 while( nLeftPos < nOldCount && nLeft > (*pOld)[ nLeftPos ] )
513 nLeftPos += 2;
514 if( nLeftPos >= nOldCount )
515 { // The current interval belongs to the end of the old array ...
516 if( !bSubtract )
517 pOld->insert( pOld->begin() + nOldCount, pLongArr->begin() + i - 2, pLongArr->end() );
518 break;
520 std::deque<tools::Long>::size_type nRightPos = nLeftPos - 1;
521 while( nRightPos < nOldCount && nRight >= (*pOld)[ nRightPos ] )
522 nRightPos += 2;
523 if( nRightPos < nLeftPos )
524 { // The current interval belongs between two old intervals
525 if( !bSubtract )
526 pOld->insert( pOld->begin() + nRightPos, pLongArr->begin() + i - 2, pLongArr->begin() + i );
528 else if( bSubtract ) // Subtract, if necessary separate
530 const tools::Long nOld = (*pOld)[nLeftPos - 1];
531 if (nLeft > nOld)
532 { // Now we split the left part...
533 if( nLeft - 1 > nOld )
535 pOld->insert( pOld->begin() + nLeftPos - 1, nOld );
536 pOld->insert( pOld->begin() + nLeftPos, nLeft - 1 );
537 nLeftPos += 2;
538 nRightPos += 2;
541 if( nRightPos - nLeftPos > 1 )
542 pOld->erase( pOld->begin() + nLeftPos, pOld->begin() + nRightPos - 1 );
543 if (++nRight >= (*pOld)[nLeftPos])
544 pOld->erase( pOld->begin() + nLeftPos - 1, pOld->begin() + nLeftPos + 1 );
545 else
546 (*pOld)[ nLeftPos - 1 ] = nRight;
548 else // Merge
550 if( nLeft < (*pOld)[ nLeftPos - 1 ] )
551 (*pOld)[ nLeftPos - 1 ] = nLeft;
552 if( nRight > (*pOld)[ nRightPos - 1 ] )
553 (*pOld)[ nRightPos - 1 ] = nRight;
554 if( nRightPos - nLeftPos > 1 )
555 pOld->erase( pOld->begin() + nLeftPos, pOld->begin() + nRightPos - 1 );
558 nIdx = nLeftPos - 1;
560 delete pLongArr;
563 /*************************************************************************
564 * SvxBoundArgs::Area returns the area in which the point is located.
565 * 0 = within the line
566 * 1 = below, but within the upper edge
567 * 2 = above, but within the lower edge
568 * 5 = below the upper edge
569 *10 = above the lower edge
570 *************************************************************************/
572 sal_uInt16 SvxBoundArgs::Area( const Point& rPt )
574 tools::Long nB = B( rPt );
575 if( nB >= nBottom )
577 if( nB >= nLower )
578 return 5;
579 return 1;
581 if( nB <= nTop )
583 if( nB <= nUpper )
584 return 10;
585 return 2;
587 return 0;
590 /*************************************************************************
591 * lcl_Cut calculates the X-Coordinate of the distance (Pt1-Pt2) at the
592 * Y-Coordinate nY.
593 * It is assumed that the one of the points are located above and the other
594 * one below the Y-Coordinate.
595 *************************************************************************/
597 tools::Long SvxBoundArgs::Cut( tools::Long nB, const Point& rPt1, const Point& rPt2 )
599 if( pTextRanger->IsVertical() )
601 double nQuot = nB - rPt1.X();
602 nQuot /= ( rPt2.X() - rPt1.X() );
603 nQuot *= ( rPt2.Y() - rPt1.Y() );
604 return tools::Long( rPt1.Y() + nQuot );
606 double nQuot = nB - rPt1.Y();
607 nQuot /= ( rPt2.Y() - rPt1.Y() );
608 nQuot *= ( rPt2.X() - rPt1.X() );
609 return tools::Long( rPt1.X() + nQuot );
612 void SvxBoundArgs::NoteUpLow( tools::Long nA, const sal_uInt8 nArea )
614 if( nAct )
616 NoteMargin( nA, nA );
617 if( bMultiple )
619 NoteRange( nArea != nAct );
620 nAct = 0;
622 if( !nFirst )
623 nFirst = nArea;
625 else
627 nAct = nArea;
628 nMin = nA;
629 nMax = nA;
633 std::deque<tools::Long>* TextRanger::GetTextRanges( const Range& rRange )
635 DBG_ASSERT( rRange.Min() || rRange.Max(), "Zero-Range not allowed, Bye Bye" );
636 //Can we find the result we need in the cache?
637 for (auto & elem : mRangeCache)
639 if (elem.range == rRange)
640 return &(elem.results);
642 //Calculate a new result
643 RangeCacheItem rngCache(rRange);
644 SvxBoundArgs aArg( this, &(rngCache.results), rRange );
645 aArg.Calc( maPolyPolygon );
646 if( mpLinePolyPolygon )
647 aArg.Concat( mpLinePolyPolygon.get() );
648 //Add new result to the cache
649 mRangeCache.push_back(std::move(rngCache));
650 if (mRangeCache.size() > nCacheSize)
651 mRangeCache.pop_front();
652 return &(mRangeCache.back().results);
655 const tools::Rectangle& TextRanger::GetBoundRect_() const
657 DBG_ASSERT( !mxBound, "Don't call twice." );
658 mxBound = maPolyPolygon.GetBoundRect();
659 return *mxBound;
663 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */