Merge pull request #3948 from techee/warning_fix2
[geany-mirror.git] / scintilla / src / PositionCache.h
blobe610ac87968cf57af72e7887091ab03ec79e104c
1 // Scintilla source code edit control
2 /** @file PositionCache.h
3 ** Classes for caching layout information.
4 **/
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
11 namespace Scintilla::Internal {
13 /**
14 * A point in document space.
15 * Uses double for sufficient resolution in large (>20,000,000 line) documents.
17 class PointDocument {
18 public:
19 double x;
20 double y;
22 explicit PointDocument(double x_ = 0, double y_ = 0) noexcept : x(x_), y(y_) {
25 // Conversion from Point.
26 explicit PointDocument(Point pt) noexcept : x(pt.x), y(pt.y) {
30 // There are two points for some positions and this enumeration
31 // can choose between the end of the first line or subline
32 // and the start of the next line or subline.
33 enum class PointEnd {
34 start = 0x0,
35 lineEnd = 0x1,
36 subLineEnd = 0x2,
37 endEither = lineEnd | subLineEnd,
40 class BidiData {
41 public:
42 std::vector<std::shared_ptr<Font>> stylesFonts;
43 std::vector<XYPOSITION> widthReprs;
44 void Resize(size_t maxLineLength_);
47 /**
49 class LineLayout {
50 private:
51 std::unique_ptr<int []>lineStarts;
52 int lenLineStarts;
53 /// Drawing is only performed for @a maxLineLength characters on each line.
54 Sci::Line lineNumber;
55 public:
56 enum { wrapWidthInfinite = 0x7ffffff };
58 int maxLineLength;
59 int numCharsInLine;
60 int numCharsBeforeEOL;
61 enum class ValidLevel { invalid, checkTextAndStyle, positions, lines } validity;
62 int xHighlightGuide;
63 bool highlightColumn;
64 bool containsCaret;
65 int edgeColumn;
66 std::unique_ptr<char[]> chars;
67 std::unique_ptr<unsigned char[]> styles;
68 std::unique_ptr<XYPOSITION[]> positions;
69 char bracePreviousStyles[2];
71 std::unique_ptr<BidiData> bidiData;
73 // Wrapped line support
74 int widthLine;
75 int lines;
76 XYPOSITION wrapIndent; // In pixels
78 LineLayout(Sci::Line lineNumber_, int maxLineLength_);
79 // Deleted so LineLayout objects can not be copied.
80 LineLayout(const LineLayout &) = delete;
81 LineLayout(LineLayout &&) = delete;
82 void operator=(const LineLayout &) = delete;
83 void operator=(LineLayout &&) = delete;
84 virtual ~LineLayout();
85 void Resize(int maxLineLength_);
86 void ReSet(Sci::Line lineNumber_, Sci::Position maxLineLength_);
87 void EnsureBidiData();
88 void Free() noexcept;
89 void ClearPositions();
90 void Invalidate(ValidLevel validity_) noexcept;
91 Sci::Line LineNumber() const noexcept;
92 bool CanHold(Sci::Line lineDoc, int lineLength_) const noexcept;
93 int LineStart(int line) const noexcept;
94 int LineLength(int line) const noexcept;
95 enum class Scope { visibleOnly, includeEnd };
96 int LineLastVisible(int line, Scope scope) const noexcept;
97 Range SubLineRange(int subLine, Scope scope) const noexcept;
98 bool InLine(int offset, int line) const noexcept;
99 int SubLineFromPosition(int posInLine, PointEnd pe) const noexcept;
100 void AddLineStart(Sci::Position start);
101 void SetBracesHighlight(Range rangeLine, const Sci::Position braces[],
102 char bracesMatchStyle, int xHighlight, bool ignoreStyle);
103 void RestoreBracesHighlight(Range rangeLine, const Sci::Position braces[], bool ignoreStyle);
104 int FindBefore(XYPOSITION x, Range range) const noexcept;
105 int FindPositionFromX(XYPOSITION x, Range range, bool charPosition) const noexcept;
106 Point PointFromPosition(int posInLine, int lineHeight, PointEnd pe) const noexcept;
107 XYPOSITION XInLine(Sci::Position index) const noexcept;
108 Interval Span(int start, int end) const noexcept;
109 Interval SpanByte(int index) const noexcept;
110 int EndLineStyle() const noexcept;
111 void WrapLine(const Document *pdoc, Sci::Position posLineStart, Wrap wrapState, XYPOSITION wrapWidth);
114 struct ScreenLine : public IScreenLine {
115 const LineLayout *ll;
116 size_t start;
117 size_t len;
118 XYPOSITION width;
119 XYPOSITION height;
120 int ctrlCharPadding;
121 XYPOSITION tabWidth;
122 int tabWidthMinimumPixels;
124 ScreenLine(const LineLayout *ll_, int subLine, const ViewStyle &vs, XYPOSITION width_, int tabWidthMinimumPixels_);
125 // Deleted so ScreenLine objects can not be copied.
126 ScreenLine(const ScreenLine &) = delete;
127 ScreenLine(ScreenLine &&) = delete;
128 void operator=(const ScreenLine &) = delete;
129 void operator=(ScreenLine &&) = delete;
130 virtual ~ScreenLine();
132 std::string_view Text() const override;
133 size_t Length() const override;
134 size_t RepresentationCount() const override;
135 XYPOSITION Width() const override;
136 XYPOSITION Height() const override;
137 XYPOSITION TabWidth() const override;
138 XYPOSITION TabWidthMinimumPixels() const override;
139 const Font *FontOfPosition(size_t position) const override;
140 XYPOSITION RepresentationWidth(size_t position) const override;
141 XYPOSITION TabPositionAfter(XYPOSITION xPosition) const override;
144 struct SignificantLines {
145 Sci::Line lineCaret;
146 Sci::Line lineTop;
147 Sci::Line linesOnScreen;
148 Scintilla::LineCache level;
149 bool LineMayCache(Sci::Line line) const noexcept;
154 class LineLayoutCache {
155 public:
156 private:
157 Scintilla::LineCache level;
158 std::vector<std::shared_ptr<LineLayout>>cache;
159 bool allInvalidated;
160 int styleClock;
161 size_t EntryForLine(Sci::Line line) const noexcept;
162 void AllocateForLevel(Sci::Line linesOnScreen, Sci::Line linesInDoc);
163 public:
164 LineLayoutCache();
165 // Deleted so LineLayoutCache objects can not be copied.
166 LineLayoutCache(const LineLayoutCache &) = delete;
167 LineLayoutCache(LineLayoutCache &&) = delete;
168 void operator=(const LineLayoutCache &) = delete;
169 void operator=(LineLayoutCache &&) = delete;
170 virtual ~LineLayoutCache();
171 void Deallocate() noexcept;
172 void Invalidate(LineLayout::ValidLevel validity_) noexcept;
173 void SetLevel(Scintilla::LineCache level_) noexcept;
174 Scintilla::LineCache GetLevel() const noexcept { return level; }
175 std::shared_ptr<LineLayout> Retrieve(Sci::Line lineNumber, Sci::Line lineCaret, int maxChars, int styleClock_,
176 Sci::Line linesOnScreen, Sci::Line linesInDoc);
179 class Representation {
180 public:
181 static constexpr size_t maxLength = 200;
182 std::string stringRep;
183 RepresentationAppearance appearance;
184 ColourRGBA colour;
185 explicit Representation(std::string_view value="", RepresentationAppearance appearance_= RepresentationAppearance::Blob) :
186 stringRep(value), appearance(appearance_) {
190 typedef std::map<unsigned int, Representation> MapRepresentation;
192 const char *ControlCharacterString(unsigned char ch) noexcept;
193 void Hexits(char *hexits, int ch) noexcept;
195 class SpecialRepresentations {
196 MapRepresentation mapReprs;
197 unsigned short startByteHasReprs[0x100] {};
198 unsigned int maxKey = 0;
199 bool crlf = false;
200 public:
201 void SetRepresentation(std::string_view charBytes, std::string_view value);
202 void SetRepresentationAppearance(std::string_view charBytes, RepresentationAppearance appearance);
203 void SetRepresentationColour(std::string_view charBytes, ColourRGBA colour);
204 void ClearRepresentation(std::string_view charBytes);
205 const Representation *GetRepresentation(std::string_view charBytes) const;
206 const Representation *RepresentationFromCharacter(std::string_view charBytes) const;
207 bool ContainsCrLf() const noexcept {
208 return crlf;
210 bool MayContain(unsigned char ch) const noexcept {
211 return startByteHasReprs[ch] != 0;
213 void Clear();
214 void SetDefaultRepresentations(int dbcsCodePage);
217 struct TextSegment {
218 int start;
219 int length;
220 const Representation *representation;
221 TextSegment(int start_=0, int length_=0, const Representation *representation_=nullptr) noexcept :
222 start(start_), length(length_), representation(representation_) {
224 int end() const noexcept {
225 return start + length;
229 // Class to break a line of text into shorter runs at sensible places.
230 class BreakFinder {
231 const LineLayout *ll;
232 const Range lineRange;
233 int nextBreak;
234 std::vector<int> selAndEdge;
235 unsigned int saeCurrentPos;
236 int saeNext;
237 int subBreak;
238 const Document *pdoc;
239 const EncodingFamily encodingFamily;
240 const SpecialRepresentations *preprs;
241 void Insert(Sci::Position val);
242 public:
243 // If a whole run is longer than lengthStartSubdivision then subdivide
244 // into smaller runs at spaces or punctuation.
245 enum { lengthStartSubdivision = 300 };
246 // Try to make each subdivided run lengthEachSubdivision or shorter.
247 enum { lengthEachSubdivision = 100 };
248 enum class BreakFor {
249 Text = 0,
250 Selection = 1,
251 Foreground = 2,
252 ForegroundAndSelection = 3,
254 BreakFinder(const LineLayout *ll_, const Selection *psel, Range lineRange_, Sci::Position posLineStart,
255 XYPOSITION xStart, BreakFor breakFor, const Document *pdoc_, const SpecialRepresentations *preprs_, const ViewStyle *pvsDraw);
256 // Deleted so BreakFinder objects can not be copied.
257 BreakFinder(const BreakFinder &) = delete;
258 BreakFinder(BreakFinder &&) = delete;
259 void operator=(const BreakFinder &) = delete;
260 void operator=(BreakFinder &&) = delete;
261 ~BreakFinder() noexcept;
262 TextSegment Next();
263 bool More() const noexcept;
266 class IPositionCache {
267 public:
268 virtual ~IPositionCache() = default;
269 virtual void Clear() noexcept = 0;
270 virtual void SetSize(size_t size_) = 0;
271 virtual size_t GetSize() const noexcept = 0;
272 virtual void MeasureWidths(Surface *surface, const ViewStyle &vstyle, unsigned int styleNumber,
273 bool unicode, std::string_view sv, XYPOSITION *positions, bool needsLocking) = 0;
276 std::unique_ptr<IPositionCache> CreatePositionCache();
280 #endif