From 9bb9cb3f962bea7365e21bb1a836bf140deb6570 Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Sun, 4 Aug 2024 00:32:05 +0200 Subject: Optimize Fl_Text_Display scrolling speed (#596). --- src/Fl_Text_Buffer.cxx | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src/Fl_Text_Buffer.cxx') diff --git a/src/Fl_Text_Buffer.cxx b/src/Fl_Text_Buffer.cxx index 3bb8a10f0..f26afb39d 100644 --- a/src/Fl_Text_Buffer.cxx +++ b/src/Fl_Text_Buffer.cxx @@ -1166,6 +1166,48 @@ int Fl_Text_Buffer::count_lines(int startPos, int endPos) const { return lineCount; } +/** + Estimate the number of newlines between \p startPos and \p endPos in buffer. + This call takes line wrapping into account. It assumes a line break at every + `lineLen` characters after the beginning of a line. + */ +int Fl_Text_Buffer::estimate_lines(int startPos, int endPos, int lineLen) const +{ + IS_UTF8_ALIGNED2(this, (startPos)) + IS_UTF8_ALIGNED2(this, (endPos)) + + int gapLen = mGapEnd - mGapStart; + int lineCount = 0; + int softLineBreaks = 0, softLineBreakCount = lineLen; + + int pos = startPos; + while (pos < mGapStart) + { + if (pos == endPos) + return lineCount + softLineBreaks; + if (mBuf[pos++] == '\n') { + softLineBreakCount = lineLen; + lineCount++; + } + if (--softLineBreakCount == 0) { + softLineBreakCount = lineLen; + softLineBreaks++; + } + } + while (pos < mLength) { + if (pos == endPos) + return lineCount + softLineBreaks; + if (mBuf[pos++ + gapLen] == '\n') { + softLineBreakCount = lineLen; + lineCount++; + } + if (--softLineBreakCount == 0) { + softLineBreakCount = lineLen; + softLineBreaks++; + } + } + return lineCount + softLineBreaks; +} /* Skip to the first character, n lines ahead. -- cgit v1.2.3