MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / GetLayouter

Function GetLayouter

src/gfx_layout.cpp:65–121  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63 */
64template <typename T>
65static inline void GetLayouter(Layouter::LineCacheItem &line, std::string_view str, FontState &state)
66{
67 typename T::CharType *buff_begin = new typename T::CharType[str.size() + 1];
68 /* Move ownership of buff_begin into the Buffer/unique_ptr. */
69 line.buffer = Layouter::LineCacheItem::Buffer(buff_begin, [](void *p) { delete[] reinterpret_cast<T::CharType *>(p); });
70
71 const typename T::CharType *buffer_last = buff_begin + str.size() + 1;
72 typename T::CharType *buff = buff_begin;
73 FontMap &font_mapping = line.runs;
74 Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
75
76 font_mapping.clear();
77
78 /*
79 * Go through the whole string while adding Font instances to the font map
80 * whenever the font changes, and convert the wide characters into a format
81 * usable by ParagraphLayout.
82 */
83 for (char32_t c : Utf8View(str)) {
84 if (c == '\0' || c == '\n') {
85 /* Caller should already have filtered out these characters. */
86 NOT_REACHED();
87 } else if (c >= SCC_BLUE && c <= SCC_BLACK) {
88 state.SetColour((TextColour)(c - SCC_BLUE));
89 } else if (c == SCC_PUSH_COLOUR) {
90 state.PushColour();
91 } else if (c == SCC_POP_COLOUR) {
92 state.PopColour();
93 } else if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
94 state.SetFontSize((FontSize)(c - SCC_FIRST_FONT));
95 } else {
96 /* Filter out non printable characters */
97 if (!IsPrintable(c)) continue;
98 /* Filter out text direction characters that shouldn't be drawn, and
99 * will not be handled in the fallback case because they are mostly
100 * needed for RTL languages which need more proper shaping support. */
101 if (!T::SUPPORTS_RTL && IsTextDirectionChar(c)) continue;
102 buff += T::AppendToBuffer(buff, buffer_last, c);
103 if (buff >= buffer_last) break;
104 continue;
105 }
106
107 if (font_mapping.empty() || font_mapping.back().first != buff - buff_begin) {
108 font_mapping.emplace_back(buff - buff_begin, f);
109 }
110 f = Layouter::GetFont(state.fontsize, state.cur_colour);
111 }
112
113 /* Better safe than sorry. */
114 *buff = '\0';
115
116 if (font_mapping.empty() || font_mapping.back().first != buff - buff_begin) {
117 font_mapping.emplace_back(buff - buff_begin, f);
118 }
119 line.layout = T::GetParagraphLayout(buff_begin, buff, font_mapping);
120 line.state_after = state;
121}
122

Callers

nothing calls this directly

Calls 11

Utf8ViewClass · 0.85
NOT_REACHEDFunction · 0.85
IsPrintableFunction · 0.85
IsTextDirectionCharFunction · 0.85
PushColourMethod · 0.80
PopColourMethod · 0.80
sizeMethod · 0.45
clearMethod · 0.45
SetColourMethod · 0.45
SetFontSizeMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected