1 // Scintilla source code edit control
2 /** @file PositionCache.h
3 ** Classes for caching layout information.
5 // Copyright 1998-2009 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #ifndef POSITIONCACHE_H
9 #define POSITIONCACHE_H
15 static inline bool IsEOLChar(char ch
) {
16 return (ch
== '\r') || (ch
== '\n');
19 // There are two points for some positions and this enumeration
20 // can choose between the end of the first line or subline
21 // and the start of the next line or subline.
32 friend class LineLayoutCache
;
35 /// Drawing is only performed for @a maxLineLength characters on each line.
39 enum { wrapWidthInfinite
= 0x7ffffff };
43 int numCharsBeforeEOL
;
44 enum validLevel
{ llInvalid
, llCheckTextAndStyle
, llPositions
, llLines
} validity
;
50 unsigned char *styles
;
51 XYPOSITION
*positions
;
52 char bracePreviousStyles
[2];
57 // Wrapped line support
60 XYPOSITION wrapIndent
; // In pixels
62 explicit LineLayout(int maxLineLength_
);
63 virtual ~LineLayout();
64 void Resize(int maxLineLength_
);
66 void Invalidate(validLevel validity_
);
67 int LineStart(int line
) const;
68 int LineLastVisible(int line
) const;
69 Range
SubLineRange(int line
) const;
70 bool InLine(int offset
, int line
) const;
71 void SetLineStart(int line
, int start
);
72 void SetBracesHighlight(Range rangeLine
, const Position braces
[],
73 char bracesMatchStyle
, int xHighlight
, bool ignoreStyle
);
74 void RestoreBracesHighlight(Range rangeLine
, const Position braces
[], bool ignoreStyle
);
75 int FindBefore(XYPOSITION x
, int lower
, int upper
) const;
76 int FindPositionFromX(XYPOSITION x
, Range range
, bool charPosition
) const;
77 Point
PointFromPosition(int posInLine
, int lineHeight
, PointEnd pe
) const;
78 int EndLineStyle() const;
83 class LineLayoutCache
{
85 std::vector
<LineLayout
*>cache
;
89 void Allocate(size_t length_
);
90 void AllocateForLevel(int linesOnScreen
, int linesInDoc
);
93 virtual ~LineLayoutCache();
96 llcNone
=SC_CACHE_NONE
,
97 llcCaret
=SC_CACHE_CARET
,
98 llcPage
=SC_CACHE_PAGE
,
99 llcDocument
=SC_CACHE_DOCUMENT
101 void Invalidate(LineLayout::validLevel validity_
);
102 void SetLevel(int level_
);
103 int GetLevel() const { return level
; }
104 LineLayout
*Retrieve(int lineNumber
, int lineCaret
, int maxChars
, int styleClock_
,
105 int linesOnScreen
, int linesInDoc
);
106 void Dispose(LineLayout
*ll
);
109 class PositionCacheEntry
{
110 unsigned int styleNumber
:8;
112 unsigned int clock
:16;
113 XYPOSITION
*positions
;
115 PositionCacheEntry();
116 ~PositionCacheEntry();
117 void Set(unsigned int styleNumber_
, const char *s_
, unsigned int len_
, XYPOSITION
*positions_
, unsigned int clock_
);
119 bool Retrieve(unsigned int styleNumber_
, const char *s_
, unsigned int len_
, XYPOSITION
*positions_
) const;
120 static unsigned int Hash(unsigned int styleNumber_
, const char *s
, unsigned int len
);
121 bool NewerThan(const PositionCacheEntry
&other
) const;
125 class Representation
{
127 std::string stringRep
;
128 explicit Representation(const char *value
="") : stringRep(value
) {
132 typedef std::map
<int, Representation
> MapRepresentation
;
134 class SpecialRepresentations
{
135 MapRepresentation mapReprs
;
136 short startByteHasReprs
[0x100];
138 SpecialRepresentations();
139 void SetRepresentation(const char *charBytes
, const char *value
);
140 void ClearRepresentation(const char *charBytes
);
141 const Representation
*RepresentationFromCharacter(const char *charBytes
, size_t len
) const;
142 bool Contains(const char *charBytes
, size_t len
) const;
149 const Representation
*representation
;
150 TextSegment(int start_
=0, int length_
=0, const Representation
*representation_
=0) :
151 start(start_
), length(length_
), representation(representation_
) {
154 return start
+ length
;
158 // Class to break a line of text into shorter runs at sensible places.
160 const LineLayout
*ll
;
164 std::vector
<int> selAndEdge
;
165 unsigned int saeCurrentPos
;
168 const Document
*pdoc
;
169 EncodingFamily encodingFamily
;
170 const SpecialRepresentations
*preprs
;
171 void Insert(int val
);
172 // Private so BreakFinder objects can not be copied
173 BreakFinder(const BreakFinder
&);
175 // If a whole run is longer than lengthStartSubdivision then subdivide
176 // into smaller runs at spaces or punctuation.
177 enum { lengthStartSubdivision
= 300 };
178 // Try to make each subdivided run lengthEachSubdivision or shorter.
179 enum { lengthEachSubdivision
= 100 };
180 BreakFinder(const LineLayout
*ll_
, const Selection
*psel
, Range rangeLine_
, int posLineStart_
,
181 int xStart
, bool breakForSelection
, const Document
*pdoc_
, const SpecialRepresentations
*preprs_
, const ViewStyle
*pvsDraw
);
187 class PositionCache
{
188 std::vector
<PositionCacheEntry
> pces
;
191 // Private so PositionCache objects can not be copied
192 PositionCache(const PositionCache
&);
197 void SetSize(size_t size_
);
198 size_t GetSize() const { return pces
.size(); }
199 void MeasureWidths(Surface
*surface
, const ViewStyle
&vstyle
, unsigned int styleNumber
,
200 const char *s
, unsigned int len
, XYPOSITION
*positions
, Document
*pdoc
);
203 inline bool IsSpaceOrTab(int ch
) {
204 return ch
== ' ' || ch
== '\t';