2009-04-24 23:35:41 +00:00
|
|
|
// Scintilla source code edit control
|
|
|
|
/** @file PositionCache.cxx
|
|
|
|
** Classes for caching layout information.
|
|
|
|
**/
|
|
|
|
// Copyright 1998-2007 by Neil Hodgson <neilh@scintilla.org>
|
|
|
|
// The License.txt file describes the conditions under which this software may be distributed.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
#include <stdexcept>
|
2010-07-12 22:19:51 +00:00
|
|
|
#include <string>
|
2009-08-23 02:24:48 +00:00
|
|
|
#include <vector>
|
2013-08-28 00:44:27 +00:00
|
|
|
#include <map>
|
2015-06-07 21:19:26 +00:00
|
|
|
#include <algorithm>
|
2009-08-23 02:24:48 +00:00
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
#include "Platform.h"
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
#include "ILexer.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
#include "Scintilla.h"
|
|
|
|
|
|
|
|
#include "SplitVector.h"
|
|
|
|
#include "Partitioning.h"
|
|
|
|
#include "RunStyles.h"
|
|
|
|
#include "ContractionState.h"
|
|
|
|
#include "CellBuffer.h"
|
|
|
|
#include "KeyMap.h"
|
|
|
|
#include "Indicator.h"
|
|
|
|
#include "XPM.h"
|
|
|
|
#include "LineMarker.h"
|
|
|
|
#include "Style.h"
|
|
|
|
#include "ViewStyle.h"
|
|
|
|
#include "CharClassify.h"
|
|
|
|
#include "Decoration.h"
|
2013-08-28 00:44:27 +00:00
|
|
|
#include "CaseFolder.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
#include "Document.h"
|
2015-06-07 21:19:26 +00:00
|
|
|
#include "UniConversion.h"
|
2009-08-23 02:24:48 +00:00
|
|
|
#include "Selection.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
#include "PositionCache.h"
|
|
|
|
|
|
|
|
#ifdef SCI_NAMESPACE
|
|
|
|
using namespace Scintilla;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LineLayout::LineLayout(int maxLineLength_) :
|
|
|
|
lineStarts(0),
|
|
|
|
lenLineStarts(0),
|
|
|
|
lineNumber(-1),
|
|
|
|
inCache(false),
|
|
|
|
maxLineLength(-1),
|
|
|
|
numCharsInLine(0),
|
2009-08-23 02:24:48 +00:00
|
|
|
numCharsBeforeEOL(0),
|
2009-04-24 23:35:41 +00:00
|
|
|
validity(llInvalid),
|
|
|
|
xHighlightGuide(0),
|
|
|
|
highlightColumn(0),
|
|
|
|
containsCaret(false),
|
|
|
|
edgeColumn(0),
|
|
|
|
chars(0),
|
|
|
|
styles(0),
|
|
|
|
positions(0),
|
2015-06-07 21:19:26 +00:00
|
|
|
hotspot(0,0),
|
2009-04-24 23:35:41 +00:00
|
|
|
widthLine(wrapWidthInfinite),
|
2009-08-23 02:24:48 +00:00
|
|
|
lines(1),
|
|
|
|
wrapIndent(0) {
|
2011-03-22 00:16:49 +00:00
|
|
|
bracePreviousStyles[0] = 0;
|
|
|
|
bracePreviousStyles[1] = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
Resize(maxLineLength_);
|
|
|
|
}
|
|
|
|
|
|
|
|
LineLayout::~LineLayout() {
|
|
|
|
Free();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineLayout::Resize(int maxLineLength_) {
|
|
|
|
if (maxLineLength_ > maxLineLength) {
|
|
|
|
Free();
|
|
|
|
chars = new char[maxLineLength_ + 1];
|
|
|
|
styles = new unsigned char[maxLineLength_ + 1];
|
|
|
|
// Extra position allocated as sometimes the Windows
|
|
|
|
// GetTextExtentExPoint API writes an extra element.
|
2013-08-28 00:44:27 +00:00
|
|
|
positions = new XYPOSITION[maxLineLength_ + 1 + 1];
|
2009-04-24 23:35:41 +00:00
|
|
|
maxLineLength = maxLineLength_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineLayout::Free() {
|
|
|
|
delete []chars;
|
|
|
|
chars = 0;
|
|
|
|
delete []styles;
|
|
|
|
styles = 0;
|
|
|
|
delete []positions;
|
|
|
|
positions = 0;
|
|
|
|
delete []lineStarts;
|
|
|
|
lineStarts = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineLayout::Invalidate(validLevel validity_) {
|
|
|
|
if (validity > validity_)
|
|
|
|
validity = validity_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LineLayout::LineStart(int line) const {
|
|
|
|
if (line <= 0) {
|
|
|
|
return 0;
|
|
|
|
} else if ((line >= lines) || !lineStarts) {
|
|
|
|
return numCharsInLine;
|
|
|
|
} else {
|
|
|
|
return lineStarts[line];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int LineLayout::LineLastVisible(int line) const {
|
|
|
|
if (line < 0) {
|
|
|
|
return 0;
|
|
|
|
} else if ((line >= lines-1) || !lineStarts) {
|
2009-08-23 02:24:48 +00:00
|
|
|
return numCharsBeforeEOL;
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
|
|
|
return lineStarts[line+1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
Range LineLayout::SubLineRange(int subLine) const {
|
|
|
|
return Range(LineStart(subLine), LineLastVisible(subLine));
|
|
|
|
}
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
bool LineLayout::InLine(int offset, int line) const {
|
2009-06-24 19:09:31 +00:00
|
|
|
return ((offset >= LineStart(line)) && (offset < LineStart(line + 1))) ||
|
|
|
|
((offset == numCharsInLine) && (line == (lines-1)));
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LineLayout::SetLineStart(int line, int start) {
|
|
|
|
if ((line >= lenLineStarts) && (line != 0)) {
|
|
|
|
int newMaxLines = line + 20;
|
|
|
|
int *newLineStarts = new int[newMaxLines];
|
|
|
|
for (int i = 0; i < newMaxLines; i++) {
|
|
|
|
if (i < lenLineStarts)
|
|
|
|
newLineStarts[i] = lineStarts[i];
|
|
|
|
else
|
|
|
|
newLineStarts[i] = 0;
|
|
|
|
}
|
|
|
|
delete []lineStarts;
|
|
|
|
lineStarts = newLineStarts;
|
|
|
|
lenLineStarts = newMaxLines;
|
|
|
|
}
|
|
|
|
lineStarts[line] = start;
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
void LineLayout::SetBracesHighlight(Range rangeLine, const Position braces[],
|
2011-07-17 22:30:49 +00:00
|
|
|
char bracesMatchStyle, int xHighlight, bool ignoreStyle) {
|
|
|
|
if (!ignoreStyle && rangeLine.ContainsCharacter(braces[0])) {
|
2009-04-24 23:35:41 +00:00
|
|
|
int braceOffset = braces[0] - rangeLine.start;
|
|
|
|
if (braceOffset < numCharsInLine) {
|
|
|
|
bracePreviousStyles[0] = styles[braceOffset];
|
|
|
|
styles[braceOffset] = bracesMatchStyle;
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
if (!ignoreStyle && rangeLine.ContainsCharacter(braces[1])) {
|
2009-04-24 23:35:41 +00:00
|
|
|
int braceOffset = braces[1] - rangeLine.start;
|
|
|
|
if (braceOffset < numCharsInLine) {
|
|
|
|
bracePreviousStyles[1] = styles[braceOffset];
|
|
|
|
styles[braceOffset] = bracesMatchStyle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((braces[0] >= rangeLine.start && braces[1] <= rangeLine.end) ||
|
|
|
|
(braces[1] >= rangeLine.start && braces[0] <= rangeLine.end)) {
|
|
|
|
xHighlightGuide = xHighlight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
void LineLayout::RestoreBracesHighlight(Range rangeLine, const Position braces[], bool ignoreStyle) {
|
2011-07-17 22:30:49 +00:00
|
|
|
if (!ignoreStyle && rangeLine.ContainsCharacter(braces[0])) {
|
2009-04-24 23:35:41 +00:00
|
|
|
int braceOffset = braces[0] - rangeLine.start;
|
|
|
|
if (braceOffset < numCharsInLine) {
|
|
|
|
styles[braceOffset] = bracePreviousStyles[0];
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
if (!ignoreStyle && rangeLine.ContainsCharacter(braces[1])) {
|
2009-04-24 23:35:41 +00:00
|
|
|
int braceOffset = braces[1] - rangeLine.start;
|
|
|
|
if (braceOffset < numCharsInLine) {
|
|
|
|
styles[braceOffset] = bracePreviousStyles[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xHighlightGuide = 0;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
int LineLayout::FindBefore(XYPOSITION x, int lower, int upper) const {
|
2009-04-24 23:35:41 +00:00
|
|
|
do {
|
|
|
|
int middle = (upper + lower + 1) / 2; // Round high
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION posMiddle = positions[middle];
|
2009-04-24 23:35:41 +00:00
|
|
|
if (x < posMiddle) {
|
|
|
|
upper = middle - 1;
|
|
|
|
} else {
|
|
|
|
lower = middle;
|
|
|
|
}
|
|
|
|
} while (lower < upper);
|
|
|
|
return lower;
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
|
|
|
|
int LineLayout::FindPositionFromX(XYPOSITION x, Range range, bool charPosition) const {
|
|
|
|
int pos = FindBefore(x, range.start, range.end);
|
|
|
|
while (pos < range.end) {
|
|
|
|
if (charPosition) {
|
|
|
|
if (x < (positions[pos + 1])) {
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (x < ((positions[pos] + positions[pos + 1]) / 2)) {
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
return range.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
Point LineLayout::PointFromPosition(int posInLine, int lineHeight) const {
|
|
|
|
Point pt;
|
|
|
|
// In case of very long line put x at arbitrary large position
|
|
|
|
if (posInLine > maxLineLength) {
|
|
|
|
pt.x = positions[maxLineLength] - positions[LineStart(lines)];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int subLine = 0; subLine < lines; subLine++) {
|
|
|
|
const Range rangeSubLine = SubLineRange(subLine);
|
|
|
|
if (posInLine >= rangeSubLine.start) {
|
|
|
|
pt.y = static_cast<XYPOSITION>(subLine*lineHeight);
|
|
|
|
if (posInLine <= rangeSubLine.end) {
|
|
|
|
pt.x = positions[posInLine] - positions[rangeSubLine.start];
|
|
|
|
if (rangeSubLine.start != 0) // Wrapped lines may be indented
|
|
|
|
pt.x += wrapIndent;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
int LineLayout::EndLineStyle() const {
|
|
|
|
return styles[numCharsBeforeEOL > 0 ? numCharsBeforeEOL-1 : 0];
|
|
|
|
}
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
LineLayoutCache::LineLayoutCache() :
|
2013-08-28 00:44:27 +00:00
|
|
|
level(0),
|
2009-04-24 23:35:41 +00:00
|
|
|
allInvalidated(false), styleClock(-1), useCount(0) {
|
|
|
|
Allocate(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
LineLayoutCache::~LineLayoutCache() {
|
|
|
|
Deallocate();
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void LineLayoutCache::Allocate(size_t length_) {
|
|
|
|
PLATFORM_ASSERT(cache.empty());
|
2009-04-24 23:35:41 +00:00
|
|
|
allInvalidated = false;
|
2013-08-28 00:44:27 +00:00
|
|
|
cache.resize(length_);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LineLayoutCache::AllocateForLevel(int linesOnScreen, int linesInDoc) {
|
|
|
|
PLATFORM_ASSERT(useCount == 0);
|
2013-08-28 00:44:27 +00:00
|
|
|
size_t lengthForLevel = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
if (level == llcCaret) {
|
|
|
|
lengthForLevel = 1;
|
|
|
|
} else if (level == llcPage) {
|
|
|
|
lengthForLevel = linesOnScreen + 1;
|
|
|
|
} else if (level == llcDocument) {
|
|
|
|
lengthForLevel = linesInDoc;
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
if (lengthForLevel > cache.size()) {
|
2009-04-24 23:35:41 +00:00
|
|
|
Deallocate();
|
|
|
|
Allocate(lengthForLevel);
|
|
|
|
} else {
|
2013-08-28 00:44:27 +00:00
|
|
|
if (lengthForLevel < cache.size()) {
|
|
|
|
for (size_t i = lengthForLevel; i < cache.size(); i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
delete cache[i];
|
|
|
|
cache[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
cache.resize(lengthForLevel);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
PLATFORM_ASSERT(cache.size() == lengthForLevel);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LineLayoutCache::Deallocate() {
|
|
|
|
PLATFORM_ASSERT(useCount == 0);
|
2013-08-28 00:44:27 +00:00
|
|
|
for (size_t i = 0; i < cache.size(); i++)
|
2009-04-24 23:35:41 +00:00
|
|
|
delete cache[i];
|
2013-08-28 00:44:27 +00:00
|
|
|
cache.clear();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LineLayoutCache::Invalidate(LineLayout::validLevel validity_) {
|
2013-08-28 00:44:27 +00:00
|
|
|
if (!cache.empty() && !allInvalidated) {
|
|
|
|
for (size_t i = 0; i < cache.size(); i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (cache[i]) {
|
|
|
|
cache[i]->Invalidate(validity_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (validity_ == LineLayout::llInvalid) {
|
|
|
|
allInvalidated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineLayoutCache::SetLevel(int level_) {
|
|
|
|
allInvalidated = false;
|
|
|
|
if ((level_ != -1) && (level != level_)) {
|
|
|
|
level = level_;
|
|
|
|
Deallocate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LineLayout *LineLayoutCache::Retrieve(int lineNumber, int lineCaret, int maxChars, int styleClock_,
|
|
|
|
int linesOnScreen, int linesInDoc) {
|
|
|
|
AllocateForLevel(linesOnScreen, linesInDoc);
|
|
|
|
if (styleClock != styleClock_) {
|
|
|
|
Invalidate(LineLayout::llCheckTextAndStyle);
|
|
|
|
styleClock = styleClock_;
|
|
|
|
}
|
|
|
|
allInvalidated = false;
|
|
|
|
int pos = -1;
|
|
|
|
LineLayout *ret = 0;
|
|
|
|
if (level == llcCaret) {
|
|
|
|
pos = 0;
|
|
|
|
} else if (level == llcPage) {
|
|
|
|
if (lineNumber == lineCaret) {
|
|
|
|
pos = 0;
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (cache.size() > 1) {
|
|
|
|
pos = 1 + (lineNumber % (cache.size() - 1));
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
} else if (level == llcDocument) {
|
|
|
|
pos = lineNumber;
|
|
|
|
}
|
|
|
|
if (pos >= 0) {
|
|
|
|
PLATFORM_ASSERT(useCount == 0);
|
2013-08-28 00:44:27 +00:00
|
|
|
if (!cache.empty() && (pos < static_cast<int>(cache.size()))) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (cache[pos]) {
|
|
|
|
if ((cache[pos]->lineNumber != lineNumber) ||
|
|
|
|
(cache[pos]->maxLineLength < maxChars)) {
|
|
|
|
delete cache[pos];
|
|
|
|
cache[pos] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!cache[pos]) {
|
|
|
|
cache[pos] = new LineLayout(maxChars);
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
cache[pos]->lineNumber = lineNumber;
|
|
|
|
cache[pos]->inCache = true;
|
|
|
|
ret = cache[pos];
|
|
|
|
useCount++;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
ret = new LineLayout(maxChars);
|
|
|
|
ret->lineNumber = lineNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineLayoutCache::Dispose(LineLayout *ll) {
|
|
|
|
allInvalidated = false;
|
|
|
|
if (ll) {
|
|
|
|
if (!ll->inCache) {
|
|
|
|
delete ll;
|
|
|
|
} else {
|
|
|
|
useCount--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
// Simply pack the (maximum 4) character bytes into an int
|
|
|
|
static inline int KeyFromString(const char *charBytes, size_t len) {
|
|
|
|
PLATFORM_ASSERT(len <= 4);
|
|
|
|
int k=0;
|
|
|
|
for (size_t i=0; i<len && charBytes[i]; i++) {
|
|
|
|
k = k * 0x100;
|
|
|
|
k += static_cast<unsigned char>(charBytes[i]);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
return k;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
SpecialRepresentations::SpecialRepresentations() {
|
|
|
|
std::fill(startByteHasReprs, startByteHasReprs+0x100, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpecialRepresentations::SetRepresentation(const char *charBytes, const char *value) {
|
|
|
|
MapRepresentation::iterator it = mapReprs.find(KeyFromString(charBytes, UTF8MaxBytes));
|
|
|
|
if (it == mapReprs.end()) {
|
|
|
|
// New entry so increment for first byte
|
|
|
|
startByteHasReprs[static_cast<unsigned char>(charBytes[0])]++;
|
|
|
|
}
|
|
|
|
mapReprs[KeyFromString(charBytes, UTF8MaxBytes)] = Representation(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpecialRepresentations::ClearRepresentation(const char *charBytes) {
|
|
|
|
MapRepresentation::iterator it = mapReprs.find(KeyFromString(charBytes, UTF8MaxBytes));
|
|
|
|
if (it != mapReprs.end()) {
|
|
|
|
mapReprs.erase(it);
|
|
|
|
startByteHasReprs[static_cast<unsigned char>(charBytes[0])]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Representation *SpecialRepresentations::RepresentationFromCharacter(const char *charBytes, size_t len) const {
|
|
|
|
PLATFORM_ASSERT(len <= 4);
|
|
|
|
if (!startByteHasReprs[static_cast<unsigned char>(charBytes[0])])
|
|
|
|
return 0;
|
|
|
|
MapRepresentation::const_iterator it = mapReprs.find(KeyFromString(charBytes, len));
|
|
|
|
if (it != mapReprs.end()) {
|
|
|
|
return &(it->second);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpecialRepresentations::Contains(const char *charBytes, size_t len) const {
|
|
|
|
PLATFORM_ASSERT(len <= 4);
|
|
|
|
if (!startByteHasReprs[static_cast<unsigned char>(charBytes[0])])
|
|
|
|
return false;
|
|
|
|
MapRepresentation::const_iterator it = mapReprs.find(KeyFromString(charBytes, len));
|
|
|
|
return it != mapReprs.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpecialRepresentations::Clear() {
|
|
|
|
mapReprs.clear();
|
|
|
|
std::fill(startByteHasReprs, startByteHasReprs+0x100, 0);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
void BreakFinder::Insert(int val) {
|
|
|
|
if (val > nextBreak) {
|
|
|
|
const std::vector<int>::iterator it = std::lower_bound(selAndEdge.begin(), selAndEdge.end(), val);
|
|
|
|
if (it == selAndEdge.end()) {
|
|
|
|
selAndEdge.push_back(val);
|
|
|
|
} else if (*it != val) {
|
|
|
|
selAndEdge.insert(it, 1, val);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
BreakFinder::BreakFinder(const LineLayout *ll_, const Selection *psel, Range lineRange_, int posLineStart_,
|
|
|
|
int xStart, bool breakForSelection, const Document *pdoc_, const SpecialRepresentations *preprs_, const ViewStyle *pvsDraw) :
|
2009-04-24 23:35:41 +00:00
|
|
|
ll(ll_),
|
2015-06-07 21:19:26 +00:00
|
|
|
lineRange(lineRange_),
|
2009-04-24 23:35:41 +00:00
|
|
|
posLineStart(posLineStart_),
|
2015-06-07 21:19:26 +00:00
|
|
|
nextBreak(lineRange_.start),
|
2009-04-24 23:35:41 +00:00
|
|
|
saeCurrentPos(0),
|
|
|
|
saeNext(0),
|
2011-07-17 22:30:49 +00:00
|
|
|
subBreak(-1),
|
2015-06-07 21:19:26 +00:00
|
|
|
pdoc(pdoc_),
|
|
|
|
encodingFamily(pdoc_->CodePageFamily()),
|
|
|
|
preprs(preprs_) {
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
// Search for first visible break
|
|
|
|
// First find the first visible character
|
2015-06-07 21:19:26 +00:00
|
|
|
if (xStart > 0.0f)
|
|
|
|
nextBreak = ll->FindBefore(static_cast<XYPOSITION>(xStart), lineRange.start, lineRange.end);
|
2009-04-24 23:35:41 +00:00
|
|
|
// Now back to a style break
|
2015-06-07 21:19:26 +00:00
|
|
|
while ((nextBreak > lineRange.start) && (ll->styles[nextBreak] == ll->styles[nextBreak - 1])) {
|
2009-04-24 23:35:41 +00:00
|
|
|
nextBreak--;
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:19:51 +00:00
|
|
|
if (breakForSelection) {
|
|
|
|
SelectionPosition posStart(posLineStart);
|
2015-06-07 21:19:26 +00:00
|
|
|
SelectionPosition posEnd(posLineStart + lineRange.end);
|
2010-07-12 22:19:51 +00:00
|
|
|
SelectionSegment segmentLine(posStart, posEnd);
|
2015-06-07 21:19:26 +00:00
|
|
|
for (size_t r=0; r<psel->Count(); r++) {
|
|
|
|
SelectionSegment portion = psel->Range(r).Intersect(segmentLine);
|
2010-07-12 22:19:51 +00:00
|
|
|
if (!(portion.start == portion.end)) {
|
|
|
|
if (portion.start.IsValid())
|
2015-06-07 21:19:26 +00:00
|
|
|
Insert(portion.start.Position() - posLineStart);
|
2010-07-12 22:19:51 +00:00
|
|
|
if (portion.end.IsValid())
|
2015-06-07 21:19:26 +00:00
|
|
|
Insert(portion.end.Position() - posLineStart);
|
2010-07-12 22:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
if (pvsDraw && pvsDraw->indicatorsSetFore > 0) {
|
|
|
|
for (Decoration *deco = pdoc->decorations.root; deco; deco = deco->next) {
|
|
|
|
if (pvsDraw->indicators[deco->indicator].OverridesTextFore()) {
|
|
|
|
int startPos = deco->rs.EndRun(posLineStart);
|
|
|
|
while (startPos < (posLineStart + lineRange.end)) {
|
|
|
|
Insert(startPos - posLineStart);
|
|
|
|
startPos = deco->rs.EndRun(startPos);
|
|
|
|
}
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
Insert(ll->edgeColumn);
|
|
|
|
Insert(lineRange.end);
|
2013-08-28 00:44:27 +00:00
|
|
|
saeNext = (!selAndEdge.empty()) ? selAndEdge[0] : -1;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BreakFinder::~BreakFinder() {
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
TextSegment BreakFinder::Next() {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (subBreak == -1) {
|
|
|
|
int prev = nextBreak;
|
2015-06-07 21:19:26 +00:00
|
|
|
while (nextBreak < lineRange.end) {
|
|
|
|
int charWidth = 1;
|
|
|
|
if (encodingFamily == efUnicode)
|
|
|
|
charWidth = UTF8DrawBytes(reinterpret_cast<unsigned char *>(ll->chars) + nextBreak, lineRange.end - nextBreak);
|
|
|
|
else if (encodingFamily == efDBCS)
|
|
|
|
charWidth = pdoc->IsDBCSLeadByte(ll->chars[nextBreak]) ? 2 : 1;
|
|
|
|
const Representation *repr = preprs->RepresentationFromCharacter(ll->chars + nextBreak, charWidth);
|
|
|
|
if (((nextBreak > 0) && (ll->styles[nextBreak] != ll->styles[nextBreak - 1])) ||
|
|
|
|
repr ||
|
|
|
|
(nextBreak == saeNext)) {
|
|
|
|
while ((nextBreak >= saeNext) && (saeNext < lineRange.end)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
saeCurrentPos++;
|
2015-06-07 21:19:26 +00:00
|
|
|
saeNext = (saeCurrentPos < selAndEdge.size()) ? selAndEdge[saeCurrentPos] : lineRange.end;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
if ((nextBreak > prev) || repr) {
|
|
|
|
// Have a segment to report
|
|
|
|
if (nextBreak == prev) {
|
|
|
|
nextBreak += charWidth;
|
|
|
|
} else {
|
|
|
|
repr = 0; // Optimize -> should remember repr
|
|
|
|
}
|
|
|
|
if ((nextBreak - prev) < lengthStartSubdivision) {
|
|
|
|
return TextSegment(prev, nextBreak - prev, repr);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
nextBreak += charWidth;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
if ((nextBreak - prev) < lengthStartSubdivision) {
|
2015-06-07 21:19:26 +00:00
|
|
|
return TextSegment(prev, nextBreak - prev);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
subBreak = prev;
|
|
|
|
}
|
|
|
|
// Splitting up a long run from prev to nextBreak in lots of approximately lengthEachSubdivision.
|
|
|
|
// For very long runs add extra breaks after spaces or if no spaces before low punctuation.
|
2015-06-07 21:19:26 +00:00
|
|
|
int startSegment = subBreak;
|
2009-04-24 23:35:41 +00:00
|
|
|
if ((nextBreak - subBreak) <= lengthEachSubdivision) {
|
|
|
|
subBreak = -1;
|
2015-06-07 21:19:26 +00:00
|
|
|
return TextSegment(startSegment, nextBreak - startSegment);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
2011-07-17 22:30:49 +00:00
|
|
|
subBreak += pdoc->SafeSegment(ll->chars + subBreak, nextBreak-subBreak, lengthEachSubdivision);
|
2009-04-24 23:35:41 +00:00
|
|
|
if (subBreak >= nextBreak) {
|
|
|
|
subBreak = -1;
|
2015-06-07 21:19:26 +00:00
|
|
|
return TextSegment(startSegment, nextBreak - startSegment);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
2015-06-07 21:19:26 +00:00
|
|
|
return TextSegment(startSegment, subBreak - startSegment);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
bool BreakFinder::More() const {
|
|
|
|
return (subBreak >= 0) || (nextBreak < lineRange.end);
|
|
|
|
}
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
PositionCacheEntry::PositionCacheEntry() :
|
|
|
|
styleNumber(0), len(0), clock(0), positions(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void PositionCacheEntry::Set(unsigned int styleNumber_, const char *s_,
|
2013-08-28 00:44:27 +00:00
|
|
|
unsigned int len_, XYPOSITION *positions_, unsigned int clock_) {
|
2009-04-24 23:35:41 +00:00
|
|
|
Clear();
|
|
|
|
styleNumber = styleNumber_;
|
|
|
|
len = len_;
|
|
|
|
clock = clock_;
|
|
|
|
if (s_ && positions_) {
|
2013-08-28 00:44:27 +00:00
|
|
|
positions = new XYPOSITION[len + (len / 4) + 1];
|
2010-07-12 22:19:51 +00:00
|
|
|
for (unsigned int i=0; i<len; i++) {
|
2015-06-07 21:19:26 +00:00
|
|
|
positions[i] = positions_[i];
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
memcpy(reinterpret_cast<char *>(reinterpret_cast<void *>(positions + len)), s_, len);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PositionCacheEntry::~PositionCacheEntry() {
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PositionCacheEntry::Clear() {
|
|
|
|
delete []positions;
|
|
|
|
positions = 0;
|
|
|
|
styleNumber = 0;
|
|
|
|
len = 0;
|
|
|
|
clock = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PositionCacheEntry::Retrieve(unsigned int styleNumber_, const char *s_,
|
2013-08-28 00:44:27 +00:00
|
|
|
unsigned int len_, XYPOSITION *positions_) const {
|
2009-04-24 23:35:41 +00:00
|
|
|
if ((styleNumber == styleNumber_) && (len == len_) &&
|
2015-06-07 21:19:26 +00:00
|
|
|
(memcmp(reinterpret_cast<char *>(reinterpret_cast<void *>(positions + len)), s_, len)== 0)) {
|
2010-07-12 22:19:51 +00:00
|
|
|
for (unsigned int i=0; i<len; i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
positions_[i] = positions[i];
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
unsigned int PositionCacheEntry::Hash(unsigned int styleNumber_, const char *s, unsigned int len_) {
|
2009-04-24 23:35:41 +00:00
|
|
|
unsigned int ret = s[0] << 7;
|
2013-08-28 00:44:27 +00:00
|
|
|
for (unsigned int i=0; i<len_; i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
ret *= 1000003;
|
|
|
|
ret ^= s[i];
|
|
|
|
}
|
|
|
|
ret *= 1000003;
|
2013-08-28 00:44:27 +00:00
|
|
|
ret ^= len_;
|
2009-04-24 23:35:41 +00:00
|
|
|
ret *= 1000003;
|
2013-08-28 00:44:27 +00:00
|
|
|
ret ^= styleNumber_;
|
2009-04-24 23:35:41 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:19:51 +00:00
|
|
|
bool PositionCacheEntry::NewerThan(const PositionCacheEntry &other) const {
|
2009-04-24 23:35:41 +00:00
|
|
|
return clock > other.clock;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PositionCacheEntry::ResetClock() {
|
|
|
|
if (clock > 0) {
|
|
|
|
clock = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PositionCache::PositionCache() {
|
|
|
|
clock = 1;
|
2013-08-28 00:44:27 +00:00
|
|
|
pces.resize(0x400);
|
2009-04-24 23:35:41 +00:00
|
|
|
allClear = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PositionCache::~PositionCache() {
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PositionCache::Clear() {
|
|
|
|
if (!allClear) {
|
2013-08-28 00:44:27 +00:00
|
|
|
for (size_t i=0; i<pces.size(); i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
pces[i].Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clock = 1;
|
|
|
|
allClear = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PositionCache::SetSize(size_t size_) {
|
|
|
|
Clear();
|
2013-08-28 00:44:27 +00:00
|
|
|
pces.resize(size_);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
void PositionCache::MeasureWidths(Surface *surface, const ViewStyle &vstyle, unsigned int styleNumber,
|
2013-08-28 00:44:27 +00:00
|
|
|
const char *s, unsigned int len, XYPOSITION *positions, Document *pdoc) {
|
2011-07-17 22:30:49 +00:00
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
allClear = false;
|
2015-06-07 21:19:26 +00:00
|
|
|
size_t probe = pces.size(); // Out of bounds
|
2013-08-28 00:44:27 +00:00
|
|
|
if ((!pces.empty()) && (len < 30)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// Only store short strings in the cache so it doesn't churn with
|
|
|
|
// long comments with only a single comment.
|
|
|
|
|
|
|
|
// Two way associative: try two probe positions.
|
2015-06-07 21:19:26 +00:00
|
|
|
unsigned int hashValue = PositionCacheEntry::Hash(styleNumber, s, len);
|
|
|
|
probe = hashValue % pces.size();
|
2009-04-24 23:35:41 +00:00
|
|
|
if (pces[probe].Retrieve(styleNumber, s, len, positions)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
unsigned int probe2 = (hashValue * 37) % pces.size();
|
2009-04-24 23:35:41 +00:00
|
|
|
if (pces[probe2].Retrieve(styleNumber, s, len, positions)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Not found. Choose the oldest of the two slots to replace
|
|
|
|
if (pces[probe].NewerThan(pces[probe2])) {
|
|
|
|
probe = probe2;
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
if (len > BreakFinder::lengthStartSubdivision) {
|
|
|
|
// Break up into segments
|
|
|
|
unsigned int startSegment = 0;
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION xStartSegment = 0;
|
2011-07-17 22:30:49 +00:00
|
|
|
while (startSegment < len) {
|
|
|
|
unsigned int lenSegment = pdoc->SafeSegment(s + startSegment, len - startSegment, BreakFinder::lengthEachSubdivision);
|
2015-06-07 21:19:26 +00:00
|
|
|
FontAlias fontStyle = vstyle.styles[styleNumber].font;
|
|
|
|
surface->MeasureWidths(fontStyle, s + startSegment, lenSegment, positions + startSegment);
|
2011-07-17 22:30:49 +00:00
|
|
|
for (unsigned int inSeg = 0; inSeg < lenSegment; inSeg++) {
|
|
|
|
positions[startSegment + inSeg] += xStartSegment;
|
|
|
|
}
|
|
|
|
xStartSegment = positions[startSegment + lenSegment - 1];
|
|
|
|
startSegment += lenSegment;
|
|
|
|
}
|
|
|
|
} else {
|
2015-06-07 21:19:26 +00:00
|
|
|
FontAlias fontStyle = vstyle.styles[styleNumber].font;
|
|
|
|
surface->MeasureWidths(fontStyle, s, len, positions);
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
if (probe < pces.size()) {
|
|
|
|
// Store into cache
|
2009-04-24 23:35:41 +00:00
|
|
|
clock++;
|
|
|
|
if (clock > 60000) {
|
|
|
|
// Since there are only 16 bits for the clock, wrap it round and
|
|
|
|
// reset all cache entries so none get stuck with a high clock.
|
2013-08-28 00:44:27 +00:00
|
|
|
for (size_t i=0; i<pces.size(); i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
pces[i].ResetClock();
|
|
|
|
}
|
|
|
|
clock = 2;
|
|
|
|
}
|
|
|
|
pces[probe].Set(styleNumber, s, len, positions, clock);
|
|
|
|
}
|
|
|
|
}
|