1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-18 01:42:38 +01:00

Codechange: Cache layouted text for the last used width. (#14177)

This commit is contained in:
Peter Nelson
2025-05-03 18:33:47 +01:00
committed by GitHub
parent bd1a3fe0b7
commit 7596f98e2d
2 changed files with 19 additions and 6 deletions

View File

@@ -174,11 +174,21 @@ Layouter::Layouter(std::string_view str, int maxw, FontSize fontsize) : string(s
}
}
/* Move all lines into a local cache so we can reuse them later on more easily. */
for (;;) {
auto l = line.layout->NextLine(maxw);
if (l == nullptr) break;
this->push_back(std::move(l));
if (line.cached_width != maxw) {
/* First run or width has changed, so we need to go through the layouter. Lines are moved into a cache to
* be reused if the width is not changed. */
line.cached_layout.clear();
line.cached_width = maxw;
for (;;) {
auto l = line.layout->NextLine(maxw);
if (l == nullptr) break;
line.cached_layout.push_back(std::move(l));
}
}
/* Retrieve layout from the cache. */
for (const auto &l : line.cached_layout) {
this->push_back(l.get());
}
/* Break out if this was the last line. */