1
0
Fork 0

Codechange: simplify how GetCharPosition() works

pull/10794/head
Patric Stout 2023-05-06 20:19:15 +02:00 committed by Patric Stout
parent 60399e17bd
commit a05ae2497f
1 changed files with 24 additions and 21 deletions

View File

@ -213,41 +213,44 @@ Dimension Layouter::GetBounds()
*/ */
Point Layouter::GetCharPosition(std::string_view::const_iterator ch) const Point Layouter::GetCharPosition(std::string_view::const_iterator ch) const
{ {
const auto &line = this->front();
/* Pointer to the end-of-string marker? Return total line width. */
if (ch == this->string.end()) {
Point p = { line->GetWidth(), 0 };
return p;
}
/* Find the code point index which corresponds to the char /* Find the code point index which corresponds to the char
* pointer into our UTF-8 source string. */ * pointer into our UTF-8 source string. */
size_t index = 0; size_t index = 0;
auto str = this->string.begin(); auto str = this->string.begin();
while (str < ch && str != this->string.end()) { while (str < ch) {
WChar c = Utf8Consume(str); WChar c = Utf8Consume(str);
index += this->front()->GetInternalCharLength(c); index += line->GetInternalCharLength(c);
} }
if (str == ch) { /* We couldn't find the code point index. */
/* Valid character. */ if (str != ch) {
const auto &line = this->front(); return { 0, 0 };
}
/* Pointer to the end-of-string/line marker? Return total line width. */ /* Valid character. */
if (ch == this->string.end() || *ch == '\0' || *ch == '\n') {
Point p = { line->GetWidth(), 0 };
return p;
}
/* Scan all runs until we've found our code point index. */ /* Scan all runs until we've found our code point index. */
for (int run_index = 0; run_index < line->CountRuns(); run_index++) { for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index); const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
for (int i = 0; i < run.GetGlyphCount(); i++) { for (int i = 0; i < run.GetGlyphCount(); i++) {
/* Matching glyph? Return position. */ /* Matching glyph? Return position. */
if ((size_t)run.GetGlyphToCharMap()[i] == index) { if ((size_t)run.GetGlyphToCharMap()[i] == index) {
Point p = { (int)run.GetPositions()[i * 2], (int)run.GetPositions()[i * 2 + 1] }; Point p = { (int)run.GetPositions()[i * 2], (int)run.GetPositions()[i * 2 + 1] };
return p; return p;
}
} }
} }
} }
Point p = { 0, 0 }; NOT_REACHED();
return p;
} }
/** /**