OpenTTD Source 20251213-master-g1091fa6071
gfx_layout_fallback.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11
12#include "gfx_layout_fallback.h"
13#include "gfx_func.h"
14#include "string_func.h"
15#include "zoom_func.h"
16
17#include "table/control_codes.h"
18
19#include "safeguards.h"
20
21/*** Paragraph layout ***/
40public:
43 std::vector<GlyphID> glyphs;
44 std::vector<Position> positions;
45 std::vector<int> glyph_to_char;
46
48
49 public:
50 FallbackVisualRun(const Font &font, const char32_t *chars, int glyph_count, int char_offset, int x);
51 const Font &GetFont() const override { return this->font; }
52 int GetGlyphCount() const override { return static_cast<int>(this->glyphs.size()); }
53 std::span<const GlyphID> GetGlyphs() const override { return this->glyphs; }
54 std::span<const Position> GetPositions() const override { return this->positions; }
55 int GetLeading() const override { return GetCharacterHeight(this->GetFont().GetFontCache().GetSize()); }
56 std::span<const int> GetGlyphToCharMap() const override { return this->glyph_to_char; }
57 };
58
60 class FallbackLine : public std::vector<FallbackVisualRun>, public ParagraphLayouter::Line {
61 public:
62 int GetLeading() const override;
63 int GetWidth() const override;
64 int CountRuns() const override;
65 const ParagraphLayouter::VisualRun &GetVisualRun(int run) const override;
66
67 int GetInternalCharLength(char32_t) const override { return 1; }
68 };
69
70 const char32_t *buffer_begin;
71 const char32_t *buffer;
73
74 FallbackParagraphLayout(char32_t *buffer, int length, FontMap &runs);
75 void Reflow() override;
76 std::unique_ptr<const Line> NextLine(int max_width) override;
77};
78
86/* static */ std::unique_ptr<ParagraphLayouter> FallbackParagraphLayoutFactory::GetParagraphLayout(char32_t *buff, char32_t *buff_end, FontMap &font_mapping)
87{
88 return std::make_unique<FallbackParagraphLayout>(buff, buff_end - buff, font_mapping);
89}
90
98/* static */ size_t FallbackParagraphLayoutFactory::AppendToBuffer(char32_t *buff, [[maybe_unused]] const char32_t *buffer_last, char32_t c)
99{
100 assert(buff < buffer_last);
101 *buff = c;
102 return 1;
103}
104
113FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(const Font &font, const char32_t *chars, int char_count, int char_offset, int x) :
114 font(font)
115{
116 this->glyphs.reserve(char_count);
117 this->glyph_to_char.reserve(char_count);
118 this->positions.reserve(char_count);
119
120 FontCache &fc = this->font.GetFontCache();
121 int y_offset = fc.GetGlyphYOffset();;
122 int advance = x;
123 for (int i = 0; i < char_count; i++) {
124 const GlyphID &glyph_id = this->glyphs.emplace_back(fc.MapCharToGlyph(chars[i]));
125 int x_advance = fc.GetGlyphWidth(glyph_id);
126 this->positions.emplace_back(advance, advance + x_advance - 1, y_offset); // No ascender adjustment.
127 advance += x_advance;
128 this->glyph_to_char.push_back(char_offset + i);
129 }
130}
131
137{
138 int leading = 0;
139 for (const auto &run : *this) {
140 leading = std::max(leading, run.GetLeading());
141 }
142
143 return leading;
144}
145
151{
152 if (this->empty()) return 0;
153
154 /*
155 * The last X position of a run contains is the end of that run.
156 * Since there is no left-to-right support, taking this value of
157 * the last run gives us the end of the line and thus the width.
158 */
159 const auto &run = this->GetVisualRun(this->CountRuns() - 1);
160 const auto &positions = run.GetPositions();
161 if (positions.empty()) return 0;
162 return positions.back().right + 1;
163}
164
170{
171 return (uint)this->size();
172}
173
179{
180 return this->at(run);
181}
182
190{
191 assert(runs.rbegin()->first == length);
192}
193
198{
199 this->buffer = this->buffer_begin;
200}
201
207std::unique_ptr<const ParagraphLayouter::Line> FallbackParagraphLayout::NextLine(int max_width)
208{
209 /* Simple idea:
210 * - split a line at a newline character, or at a space where we can break a line.
211 * - split for a visual run whenever a new line happens, or the font changes.
212 */
213 if (this->buffer == nullptr) return nullptr;
214
215 std::unique_ptr<FallbackLine> l = std::make_unique<FallbackLine>();
216
217 if (*this->buffer == '\0') {
218 /* Only a newline. */
219 this->buffer = nullptr;
220 l->emplace_back(this->runs.begin()->second, this->buffer, 0, 0, 0);
221 return l;
222 }
223
224 int offset = this->buffer - this->buffer_begin;
225 FontMap::iterator iter = this->runs.begin();
226 while (iter->first <= offset) {
227 ++iter;
228 assert(iter != this->runs.end());
229 }
230
231 const FontCache *fc = &iter->second.GetFontCache();
232 assert(fc != nullptr);
233 const char32_t *next_run = this->buffer_begin + iter->first;
234
235 const char32_t *begin = this->buffer;
236 const char32_t *last_space = nullptr;
237 const char32_t *last_char;
238 int width = 0;
239 for (;;) {
240 char32_t c = *this->buffer;
241 last_char = this->buffer;
242
243 if (c == '\0') {
244 this->buffer = nullptr;
245 break;
246 }
247
248 if (this->buffer == next_run) {
249 int w = l->GetWidth();
250 assert(iter->second.font_index != INVALID_FONT_INDEX);
251 l->emplace_back(iter->second, begin, this->buffer - begin, begin - this->buffer_begin, w);
252 ++iter;
253 assert(iter != this->runs.end());
254
255 next_run = this->buffer_begin + iter->first;
256 begin = this->buffer;
257 /* Since a next run is started, there is already some text that
258 * will be shown for this line. However, we do not want to break
259 * this line at the previous space, so pretend we passed a space
260 * just before this next run. */
261 last_space = begin - 1;
262 }
263
264 if (IsWhitespace(c)) last_space = this->buffer;
265
266 if (IsPrintable(c) && !IsTextDirectionChar(c)) {
267 int char_width = GetCharacterWidth(fc->GetSize(), c);
268 width += char_width;
269 if (width > max_width) {
270 /* The string is longer than maximum width so we need to decide
271 * what to do with it. */
272 if (width == char_width) {
273 /* The character is wider than allowed width; don't know
274 * what to do with this case... bail out! */
275 this->buffer = nullptr;
276 return l;
277 }
278
279 if (last_space == nullptr) {
280 /* No space has been found. Just terminate at our current
281 * location. This usually happens for languages that do not
282 * require spaces in strings, like Chinese, Japanese and
283 * Korean. For other languages terminating mid-word might
284 * not be the best, but terminating the whole string instead
285 * of continuing the word at the next line is worse. */
286 last_char = this->buffer;
287 } else {
288 /* A space is found; perfect place to terminate */
289 this->buffer = last_space + 1;
290 last_char = last_space;
291 }
292 break;
293 }
294 }
295
296 this->buffer++;
297 }
298
299 if (l->empty() || last_char - begin > 0) {
300 int w = l->GetWidth();
301 l->emplace_back(iter->second, begin, last_char - begin, begin - this->buffer_begin, w);
302 }
303 return l;
304}
static std::unique_ptr< ParagraphLayouter > GetParagraphLayout(char32_t *buff, char32_t *buff_end, FontMap &font_mapping)
Get the actual ParagraphLayout for the given buffer.
static size_t AppendToBuffer(char32_t *buff, const char32_t *buffer_last, char32_t c)
Append a wide character to the internal buffer.
A single line worth of VisualRuns.
int GetWidth() const override
Get the width of this line.
int GetLeading() const override
Get the height of the line.
const ParagraphLayouter::VisualRun & GetVisualRun(int run) const override
Get a specific visual run.
int CountRuns() const override
Get the number of runs in this line.
Visual run contains data about the bit of text with the same font.
FallbackVisualRun(const Font &font, const char32_t *chars, int glyph_count, int char_offset, int x)
Create the visual run.
std::vector< int > glyph_to_char
The char index of the glyphs.
std::vector< GlyphID > glyphs
The glyphs we're drawing.
Font font
The font used to layout these.
std::vector< Position > positions
The positions of the glyphs.
Class handling the splitting of a paragraph of text into lines and visual runs.
const char32_t * buffer_begin
Begin of the buffer.
std::unique_ptr< const Line > NextLine(int max_width) override
Construct a new line with a maximum width.
const char32_t * buffer
The current location in the buffer.
FontMap & runs
The fonts we have to use for this paragraph.
FallbackParagraphLayout(char32_t *buffer, int length, FontMap &runs)
Create a new paragraph layouter.
void Reflow() override
Reset the position to the start of the paragraph.
Font cache for basic fonts.
Definition fontcache.h:32
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
virtual GlyphID MapCharToGlyph(char32_t key)=0
Map a character into a glyph.
FontSize GetSize() const
Get the FontSize of the font.
Definition fontcache.h:96
Container with information about a font.
Definition gfx_layout.h:98
A single line worth of VisualRuns.
Definition gfx_layout.h:142
Visual run contains data about the bit of text with the same font.
Definition gfx_layout.h:130
Interface to glue fallback and normal layouter into one.
Definition gfx_layout.h:112
Control codes that are embedded in the translation strings.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
uint32_t GlyphID
Glyphs are characters from a font.
Definition fontcache.h:18
uint8_t GetCharacterWidth(FontSize size, char32_t key)
Return width of character glyph.
Definition gfx.cpp:1267
Functions related to the gfx engine.
std::vector< std::pair< int, Font > > FontMap
Mapping from index to font.
Definition gfx_layout.h:107
Functions related to laying out the texts as fallback.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.
bool IsWhitespace(char32_t c)
Check whether UNICODE character is whitespace or not, i.e.
bool IsTextDirectionChar(char32_t c)
Is the given character a text direction character.
Functions related to zooming.