mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-07-19 18:35:48 +01:00
Codechange: simplify how GetCharPosition() works
This commit is contained in:
parent
60399e17bd
commit
a05ae2497f
@ -213,24 +213,29 @@ Dimension Layouter::GetBounds()
|
||||
*/
|
||||
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
|
||||
* pointer into our UTF-8 source string. */
|
||||
size_t index = 0;
|
||||
auto str = this->string.begin();
|
||||
while (str < ch && str != this->string.end()) {
|
||||
while (str < ch) {
|
||||
WChar c = Utf8Consume(str);
|
||||
index += this->front()->GetInternalCharLength(c);
|
||||
index += line->GetInternalCharLength(c);
|
||||
}
|
||||
|
||||
/* We couldn't find the code point index. */
|
||||
if (str != ch) {
|
||||
return { 0, 0 };
|
||||
}
|
||||
|
||||
if (str == ch) {
|
||||
/* Valid character. */
|
||||
const auto &line = this->front();
|
||||
|
||||
/* Pointer to the end-of-string/line marker? Return total line width. */
|
||||
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. */
|
||||
for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
|
||||
@ -244,10 +249,8 @@ Point Layouter::GetCharPosition(std::string_view::const_iterator ch) const
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Point p = { 0, 0 };
|
||||
return p;
|
||||
NOT_REACHED();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user