]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/PositionCache.cxx
1d0bef93c5532d1da2e510f711c14d4da8adf527
1 // Scintilla source code edit control
2 /** @file PositionCache.cxx
3 ** Classes for caching layout information.
5 // Copyright 1998-2007 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
15 #include "Scintilla.h"
17 #include "SplitVector.h"
18 #include "Partitioning.h"
19 #include "RunStyles.h"
20 #include "ContractionState.h"
21 #include "CellBuffer.h"
23 #include "Indicator.h"
25 #include "LineMarker.h"
27 #include "ViewStyle.h"
28 #include "CharClassify.h"
29 #include "Decoration.h"
31 #include "Selection.h"
32 #include "PositionCache.h"
35 using namespace Scintilla
;
38 static inline bool IsControlCharacter(int ch
) {
39 // iscntrl returns true for lots of chars > 127 which are displayable
40 return ch
>= 0 && ch
< ' ';
43 LineLayout::LineLayout(int maxLineLength_
) :
64 widthLine(wrapWidthInfinite
),
67 Resize(maxLineLength_
);
70 LineLayout::~LineLayout() {
74 void LineLayout::Resize(int maxLineLength_
) {
75 if (maxLineLength_
> maxLineLength
) {
77 chars
= new char[maxLineLength_
+ 1];
78 styles
= new unsigned char[maxLineLength_
+ 1];
79 indicators
= new char[maxLineLength_
+ 1];
80 // Extra position allocated as sometimes the Windows
81 // GetTextExtentExPoint API writes an extra element.
82 positions
= new int[maxLineLength_
+ 1 + 1];
83 maxLineLength
= maxLineLength_
;
87 void LineLayout::Free() {
100 void LineLayout::Invalidate(validLevel validity_
) {
101 if (validity
> validity_
)
102 validity
= validity_
;
105 int LineLayout::LineStart(int line
) const {
108 } else if ((line
>= lines
) || !lineStarts
) {
109 return numCharsInLine
;
111 return lineStarts
[line
];
115 int LineLayout::LineLastVisible(int line
) const {
118 } else if ((line
>= lines
-1) || !lineStarts
) {
119 return numCharsBeforeEOL
;
121 return lineStarts
[line
+1];
125 bool LineLayout::InLine(int offset
, int line
) const {
126 return ((offset
>= LineStart(line
)) && (offset
< LineStart(line
+ 1))) ||
127 ((offset
== numCharsInLine
) && (line
== (lines
-1)));
130 void LineLayout::SetLineStart(int line
, int start
) {
131 if ((line
>= lenLineStarts
) && (line
!= 0)) {
132 int newMaxLines
= line
+ 20;
133 int *newLineStarts
= new int[newMaxLines
];
134 for (int i
= 0; i
< newMaxLines
; i
++) {
135 if (i
< lenLineStarts
)
136 newLineStarts
[i
] = lineStarts
[i
];
138 newLineStarts
[i
] = 0;
141 lineStarts
= newLineStarts
;
142 lenLineStarts
= newMaxLines
;
144 lineStarts
[line
] = start
;
147 void LineLayout::SetBracesHighlight(Range rangeLine
, Position braces
[],
148 char bracesMatchStyle
, int xHighlight
) {
149 if (rangeLine
.ContainsCharacter(braces
[0])) {
150 int braceOffset
= braces
[0] - rangeLine
.start
;
151 if (braceOffset
< numCharsInLine
) {
152 bracePreviousStyles
[0] = styles
[braceOffset
];
153 styles
[braceOffset
] = bracesMatchStyle
;
156 if (rangeLine
.ContainsCharacter(braces
[1])) {
157 int braceOffset
= braces
[1] - rangeLine
.start
;
158 if (braceOffset
< numCharsInLine
) {
159 bracePreviousStyles
[1] = styles
[braceOffset
];
160 styles
[braceOffset
] = bracesMatchStyle
;
163 if ((braces
[0] >= rangeLine
.start
&& braces
[1] <= rangeLine
.end
) ||
164 (braces
[1] >= rangeLine
.start
&& braces
[0] <= rangeLine
.end
)) {
165 xHighlightGuide
= xHighlight
;
169 void LineLayout::RestoreBracesHighlight(Range rangeLine
, Position braces
[]) {
170 if (rangeLine
.ContainsCharacter(braces
[0])) {
171 int braceOffset
= braces
[0] - rangeLine
.start
;
172 if (braceOffset
< numCharsInLine
) {
173 styles
[braceOffset
] = bracePreviousStyles
[0];
176 if (rangeLine
.ContainsCharacter(braces
[1])) {
177 int braceOffset
= braces
[1] - rangeLine
.start
;
178 if (braceOffset
< numCharsInLine
) {
179 styles
[braceOffset
] = bracePreviousStyles
[1];
185 int LineLayout::FindBefore(int x
, int lower
, int upper
) const {
187 int middle
= (upper
+ lower
+ 1) / 2; // Round high
188 int posMiddle
= positions
[middle
];
194 } while (lower
< upper
);
198 int LineLayout::EndLineStyle() const {
199 return styles
[numCharsBeforeEOL
> 0 ? numCharsBeforeEOL
-1 : 0];
202 LineLayoutCache::LineLayoutCache() :
203 level(0), length(0), size(0), cache(0),
204 allInvalidated(false), styleClock(-1), useCount(0) {
208 LineLayoutCache::~LineLayoutCache() {
212 void LineLayoutCache::Allocate(int length_
) {
213 PLATFORM_ASSERT(cache
== NULL
);
214 allInvalidated
= false;
218 size
= (size
/ 16 + 1) * 16;
221 cache
= new LineLayout
* [size
];
223 for (int i
= 0; i
< size
; i
++)
227 void LineLayoutCache::AllocateForLevel(int linesOnScreen
, int linesInDoc
) {
228 PLATFORM_ASSERT(useCount
== 0);
229 int lengthForLevel
= 0;
230 if (level
== llcCaret
) {
232 } else if (level
== llcPage
) {
233 lengthForLevel
= linesOnScreen
+ 1;
234 } else if (level
== llcDocument
) {
235 lengthForLevel
= linesInDoc
;
237 if (lengthForLevel
> size
) {
239 Allocate(lengthForLevel
);
241 if (lengthForLevel
< length
) {
242 for (int i
= lengthForLevel
; i
< length
; i
++) {
247 length
= lengthForLevel
;
249 PLATFORM_ASSERT(length
== lengthForLevel
);
250 PLATFORM_ASSERT(cache
!= NULL
|| length
== 0);
253 void LineLayoutCache::Deallocate() {
254 PLATFORM_ASSERT(useCount
== 0);
255 for (int i
= 0; i
< length
; i
++)
263 void LineLayoutCache::Invalidate(LineLayout::validLevel validity_
) {
264 if (cache
&& !allInvalidated
) {
265 for (int i
= 0; i
< length
; i
++) {
267 cache
[i
]->Invalidate(validity_
);
270 if (validity_
== LineLayout::llInvalid
) {
271 allInvalidated
= true;
276 void LineLayoutCache::SetLevel(int level_
) {
277 allInvalidated
= false;
278 if ((level_
!= -1) && (level
!= level_
)) {
284 LineLayout
*LineLayoutCache::Retrieve(int lineNumber
, int lineCaret
, int maxChars
, int styleClock_
,
285 int linesOnScreen
, int linesInDoc
) {
286 AllocateForLevel(linesOnScreen
, linesInDoc
);
287 if (styleClock
!= styleClock_
) {
288 Invalidate(LineLayout::llCheckTextAndStyle
);
289 styleClock
= styleClock_
;
291 allInvalidated
= false;
294 if (level
== llcCaret
) {
296 } else if (level
== llcPage
) {
297 if (lineNumber
== lineCaret
) {
299 } else if (length
> 1) {
300 pos
= 1 + (lineNumber
% (length
- 1));
302 } else if (level
== llcDocument
) {
306 PLATFORM_ASSERT(useCount
== 0);
307 if (cache
&& (pos
< length
)) {
309 if ((cache
[pos
]->lineNumber
!= lineNumber
) ||
310 (cache
[pos
]->maxLineLength
< maxChars
)) {
316 cache
[pos
] = new LineLayout(maxChars
);
319 cache
[pos
]->lineNumber
= lineNumber
;
320 cache
[pos
]->inCache
= true;
328 ret
= new LineLayout(maxChars
);
329 ret
->lineNumber
= lineNumber
;
335 void LineLayoutCache::Dispose(LineLayout
*ll
) {
336 allInvalidated
= false;
346 void BreakFinder::Insert(int val
) {
348 if (saeLen
>= saeSize
) {
350 int *selAndEdgeNew
= new int[saeSize
];
351 for (unsigned int j
= 0; j
<saeLen
; j
++) {
352 selAndEdgeNew
[j
] = selAndEdge
[j
];
355 selAndEdge
= selAndEdgeNew
;
358 if (val
>= nextBreak
) {
359 for (unsigned int j
= 0; j
<saeLen
; j
++) {
360 if (val
== selAndEdge
[j
]) {
362 } if (val
< selAndEdge
[j
]) {
363 for (unsigned int k
= saeLen
; k
>j
; k
--) {
364 selAndEdge
[k
] = selAndEdge
[k
-1];
371 // Not less than any so append
372 selAndEdge
[saeLen
++] = val
;
376 extern bool BadUTF(const char *s
, int len
, int &trailBytes
);
378 static int NextBadU(const char *s
, int p
, int len
, int &trailBytes
) {
381 if (BadUTF(s
+ p
, len
- p
, trailBytes
))
387 BreakFinder::BreakFinder(LineLayout
*ll_
, int lineStart_
, int lineEnd_
, int posLineStart_
, bool utf8_
, int xStart
, bool breakForSelection
) :
389 lineStart(lineStart_
),
391 posLineStart(posLineStart_
),
393 nextBreak(lineStart_
),
400 selAndEdge
= new int[saeSize
];
401 for (unsigned int j
=0; j
< saeSize
; j
++) {
405 // Search for first visible break
406 // First find the first visible character
407 nextBreak
= ll
->FindBefore(xStart
, lineStart
, lineEnd
);
408 // Now back to a style break
409 while ((nextBreak
> lineStart
) && (ll
->styles
[nextBreak
] == ll
->styles
[nextBreak
- 1])) {
413 if (breakForSelection
) {
414 SelectionPosition
posStart(posLineStart
);
415 SelectionPosition
posEnd(posLineStart
+ lineEnd
);
416 SelectionSegment
segmentLine(posStart
, posEnd
);
417 for (size_t r
=0; r
<ll
->psel
->Count(); r
++) {
418 SelectionSegment portion
= ll
->psel
->Range(r
).Intersect(segmentLine
);
419 if (!(portion
.start
== portion
.end
)) {
420 if (portion
.start
.IsValid())
421 Insert(portion
.start
.Position() - posLineStart
- 1);
422 if (portion
.end
.IsValid())
423 Insert(portion
.end
.Position() - posLineStart
- 1);
428 Insert(ll
->edgeColumn
- 1);
433 for (int pos
= -1;;) {
434 pos
= NextBadU(ll
->chars
, pos
, lineEnd
, trailBytes
);
441 saeNext
= (saeLen
> 0) ? selAndEdge
[0] : -1;
444 BreakFinder::~BreakFinder() {
448 int BreakFinder::First() {
452 static bool IsTrailByte(int ch
) {
453 return (ch
>= 0x80) && (ch
< (0x80 + 0x40));
456 int BreakFinder::Next() {
457 if (subBreak
== -1) {
458 int prev
= nextBreak
;
459 while (nextBreak
< lineEnd
) {
460 if ((ll
->styles
[nextBreak
] != ll
->styles
[nextBreak
+ 1]) ||
461 (nextBreak
== saeNext
) ||
462 IsControlCharacter(ll
->chars
[nextBreak
]) || IsControlCharacter(ll
->chars
[nextBreak
+ 1])) {
463 if (nextBreak
== saeNext
) {
465 saeNext
= (saeLen
> saeCurrentPos
) ? selAndEdge
[saeCurrentPos
] : -1;
468 if ((nextBreak
- prev
) < lengthStartSubdivision
) {
475 if ((nextBreak
- prev
) < lengthStartSubdivision
) {
480 // Splitting up a long run from prev to nextBreak in lots of approximately lengthEachSubdivision.
481 // For very long runs add extra breaks after spaces or if no spaces before low punctuation.
482 if ((nextBreak
- subBreak
) <= lengthEachSubdivision
) {
486 int lastGoodBreak
= -1;
487 int lastOKBreak
= -1;
488 int lastUTF8Break
= -1;
490 for (j
= subBreak
+ 1; j
<= nextBreak
; j
++) {
491 if (IsSpaceOrTab(ll
->chars
[j
- 1]) && !IsSpaceOrTab(ll
->chars
[j
])) {
494 if (static_cast<unsigned char>(ll
->chars
[j
]) < 'A') {
497 if (utf8
&& !IsTrailByte(static_cast<unsigned char>(ll
->chars
[j
]))) {
500 if (((j
- subBreak
) >= lengthEachSubdivision
) &&
501 ((lastGoodBreak
>= 0) || (lastOKBreak
>= 0) || (lastUTF8Break
>= 0))) {
505 if (lastGoodBreak
>= 0) {
506 subBreak
= lastGoodBreak
;
507 } else if (lastOKBreak
>= 0) {
508 subBreak
= lastOKBreak
;
509 } else if (lastUTF8Break
>= 0) {
510 subBreak
= lastUTF8Break
;
512 subBreak
= nextBreak
;
514 if (subBreak
>= nextBreak
) {
523 PositionCacheEntry::PositionCacheEntry() :
524 styleNumber(0), len(0), clock(0), positions(0) {
527 void PositionCacheEntry::Set(unsigned int styleNumber_
, const char *s_
,
528 unsigned int len_
, int *positions_
, unsigned int clock_
) {
530 styleNumber
= styleNumber_
;
533 if (s_
&& positions_
) {
534 positions
= new short[len
+ (len
+ 1) / 2];
535 for (unsigned int i
=0;i
<len
;i
++) {
536 positions
[i
] = static_cast<short>(positions_
[i
]);
538 memcpy(reinterpret_cast<char *>(positions
+ len
), s_
, len
);
542 PositionCacheEntry::~PositionCacheEntry() {
546 void PositionCacheEntry::Clear() {
554 bool PositionCacheEntry::Retrieve(unsigned int styleNumber_
, const char *s_
,
555 unsigned int len_
, int *positions_
) const {
556 if ((styleNumber
== styleNumber_
) && (len
== len_
) &&
557 (memcmp(reinterpret_cast<char *>(positions
+ len
), s_
, len
)== 0)) {
558 for (unsigned int i
=0;i
<len
;i
++) {
559 positions_
[i
] = positions
[i
];
567 int PositionCacheEntry::Hash(unsigned int styleNumber
, const char *s
, unsigned int len
) {
568 unsigned int ret
= s
[0] << 7;
569 for (unsigned int i
=0; i
<len
; i
++) {
580 bool PositionCacheEntry::NewerThan(const PositionCacheEntry
&other
) {
581 return clock
> other
.clock
;
584 void PositionCacheEntry::ResetClock() {
590 PositionCache::PositionCache() {
593 pces
= new PositionCacheEntry
[size
];
597 PositionCache::~PositionCache() {
602 void PositionCache::Clear() {
604 for (size_t i
=0;i
<size
;i
++) {
612 void PositionCache::SetSize(size_t size_
) {
616 pces
= new PositionCacheEntry
[size
];
619 void PositionCache::MeasureWidths(Surface
*surface
, ViewStyle
&vstyle
, unsigned int styleNumber
,
620 const char *s
, unsigned int len
, int *positions
) {
623 if ((size
> 0) && (len
< 30)) {
624 // Only store short strings in the cache so it doesn't churn with
625 // long comments with only a single comment.
627 // Two way associative: try two probe positions.
628 int hashValue
= PositionCacheEntry::Hash(styleNumber
, s
, len
);
629 probe
= hashValue
% size
;
630 if (pces
[probe
].Retrieve(styleNumber
, s
, len
, positions
)) {
633 int probe2
= (hashValue
* 37) % size
;
634 if (pces
[probe2
].Retrieve(styleNumber
, s
, len
, positions
)) {
637 // Not found. Choose the oldest of the two slots to replace
638 if (pces
[probe
].NewerThan(pces
[probe2
])) {
642 surface
->MeasureWidths(vstyle
.styles
[styleNumber
].font
, s
, len
, positions
);
646 // Since there are only 16 bits for the clock, wrap it round and
647 // reset all cache entries so none get stuck with a high clock.
648 for (size_t i
=0;i
<size
;i
++) {
649 pces
[i
].ResetClock();
653 pces
[probe
].Set(styleNumber
, s
, len
, positions
, clock
);