OpenTTD Source 20260621-master-g720d10536d
strgen_tables.h
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
9
10#include "../core/enum_type.hpp"
11
13enum class CmdFlag : uint8_t {
17};
18
21
22class StringBuilder;
23typedef void (*ParseCmdProc)(StringBuilder &builder, std::string_view param, char32_t value);
24
25struct CmdStruct {
26 std::string_view cmd;
27 ParseCmdProc proc;
28 char32_t value;
29 uint8_t consumes;
30 std::optional<size_t> default_plural_offset;
31 CmdFlags flags;
32};
33
34extern void EmitSingleChar(StringBuilder &builder, std::string_view param, char32_t value);
35extern void EmitPlural(StringBuilder &builder, std::string_view param, char32_t value);
36extern void EmitGender(StringBuilder &builder, std::string_view param, char32_t value);
37
38static const CmdStruct _cmd_structs[] = {
39 /* Font size */
40 {"NORMAL_FONT", EmitSingleChar, SCC_NORMALFONT, 0, std::nullopt, {}},
41 {"TINY_FONT", EmitSingleChar, SCC_TINYFONT, 0, std::nullopt, {}},
42 {"BIG_FONT", EmitSingleChar, SCC_BIGFONT, 0, std::nullopt, {}},
43 {"MONO_FONT", EmitSingleChar, SCC_MONOFONT, 0, std::nullopt, {}},
44
45 /* Colours */
46 {"BLUE", EmitSingleChar, SCC_BLUE, 0, std::nullopt, {CmdFlag::DontCount}},
47 {"SILVER", EmitSingleChar, SCC_SILVER, 0, std::nullopt, {CmdFlag::DontCount}},
48 {"GOLD", EmitSingleChar, SCC_GOLD, 0, std::nullopt, {CmdFlag::DontCount}},
49 {"RED", EmitSingleChar, SCC_RED, 0, std::nullopt, {CmdFlag::DontCount}},
50 {"PURPLE", EmitSingleChar, SCC_PURPLE, 0, std::nullopt, {CmdFlag::DontCount}},
51 {"LTBROWN", EmitSingleChar, SCC_LTBROWN, 0, std::nullopt, {CmdFlag::DontCount}},
52 {"ORANGE", EmitSingleChar, SCC_ORANGE, 0, std::nullopt, {CmdFlag::DontCount}},
53 {"GREEN", EmitSingleChar, SCC_GREEN, 0, std::nullopt, {CmdFlag::DontCount}},
54 {"YELLOW", EmitSingleChar, SCC_YELLOW, 0, std::nullopt, {CmdFlag::DontCount}},
55 {"DKGREEN", EmitSingleChar, SCC_DKGREEN, 0, std::nullopt, {CmdFlag::DontCount}},
56 {"CREAM", EmitSingleChar, SCC_CREAM, 0, std::nullopt, {CmdFlag::DontCount}},
57 {"BROWN", EmitSingleChar, SCC_BROWN, 0, std::nullopt, {CmdFlag::DontCount}},
58 {"WHITE", EmitSingleChar, SCC_WHITE, 0, std::nullopt, {CmdFlag::DontCount}},
59 {"LTBLUE", EmitSingleChar, SCC_LTBLUE, 0, std::nullopt, {CmdFlag::DontCount}},
60 {"GRAY", EmitSingleChar, SCC_GRAY, 0, std::nullopt, {CmdFlag::DontCount}},
61 {"DKBLUE", EmitSingleChar, SCC_DKBLUE, 0, std::nullopt, {CmdFlag::DontCount}},
62 {"BLACK", EmitSingleChar, SCC_BLACK, 0, std::nullopt, {CmdFlag::DontCount}},
63 {"COLOUR", EmitSingleChar, SCC_COLOUR, 1, std::nullopt, {}},
64 {"PUSH_COLOUR", EmitSingleChar, SCC_PUSH_COLOUR, 0, std::nullopt, {CmdFlag::DontCount}},
65 {"POP_COLOUR", EmitSingleChar, SCC_POP_COLOUR, 0, std::nullopt, {CmdFlag::DontCount}},
66
67 {"REV", EmitSingleChar, SCC_REVISION, 0, std::nullopt, {}}, // openttd revision string
68
69 {"STRING1", EmitSingleChar, SCC_STRING1, 2, std::nullopt, {CmdFlag::Case, CmdFlag::Gender}}, // included string that consumes the string id and ONE argument
70 {"STRING2", EmitSingleChar, SCC_STRING2, 3, std::nullopt, {CmdFlag::Case, CmdFlag::Gender}}, // included string that consumes the string id and TWO arguments
71 {"STRING3", EmitSingleChar, SCC_STRING3, 4, std::nullopt, {CmdFlag::Case, CmdFlag::Gender}}, // included string that consumes the string id and THREE arguments
72 {"STRING4", EmitSingleChar, SCC_STRING4, 5, std::nullopt, {CmdFlag::Case, CmdFlag::Gender}}, // included string that consumes the string id and FOUR arguments
73 {"STRING5", EmitSingleChar, SCC_STRING5, 6, std::nullopt, {CmdFlag::Case, CmdFlag::Gender}}, // included string that consumes the string id and FIVE arguments
74 {"STRING6", EmitSingleChar, SCC_STRING6, 7, std::nullopt, {CmdFlag::Case, CmdFlag::Gender}}, // included string that consumes the string id and SIX arguments
75 {"STRING7", EmitSingleChar, SCC_STRING7, 8, std::nullopt, {CmdFlag::Case, CmdFlag::Gender}}, // included string that consumes the string id and SEVEN arguments
76
77 {"STATION_FEATURES", EmitSingleChar, SCC_STATION_FEATURES, 1, std::nullopt, {}}, // station features string, icons of the features
78 {"INDUSTRY", EmitSingleChar, SCC_INDUSTRY_NAME, 1, std::nullopt, {CmdFlag::Case, CmdFlag::Gender}}, // industry, takes an industry #, can have cases
79 {"CARGO_LONG", EmitSingleChar, SCC_CARGO_LONG, 2, 1, {CmdFlag::Gender}},
80 {"CARGO_SHORT", EmitSingleChar, SCC_CARGO_SHORT, 2, 1, {CmdFlag::Gender}}, // short cargo description, only ### tons, or ### litres
81 {"CARGO_TINY", EmitSingleChar, SCC_CARGO_TINY, 2, 1, {}}, // tiny cargo description with only the amount, not a specifier for the amount or the actual cargo name
82 {"CARGO_LIST", EmitSingleChar, SCC_CARGO_LIST, 1, std::nullopt, {CmdFlag::Case}},
83 {"POWER", EmitSingleChar, SCC_POWER, 1, 0, {}},
84 {"POWER_TO_WEIGHT", EmitSingleChar, SCC_POWER_TO_WEIGHT, 1, 0, {}},
85 {"VOLUME_LONG", EmitSingleChar, SCC_VOLUME_LONG, 1, 0, {}},
86 {"VOLUME_SHORT", EmitSingleChar, SCC_VOLUME_SHORT, 1, 0, {}},
87 {"WEIGHT_LONG", EmitSingleChar, SCC_WEIGHT_LONG, 1, 0, {}},
88 {"WEIGHT_SHORT", EmitSingleChar, SCC_WEIGHT_SHORT, 1, 0, {}},
89 {"FORCE", EmitSingleChar, SCC_FORCE, 1, 0, {}},
90 {"VELOCITY", EmitSingleChar, SCC_VELOCITY, 1, 0, {}},
91 {"HEIGHT", EmitSingleChar, SCC_HEIGHT, 1, 0, {}},
92
93 {"UNITS_DAYS_OR_SECONDS", EmitSingleChar, SCC_UNITS_DAYS_OR_SECONDS, 1, 0, {CmdFlag::Gender}},
94 {"UNITS_MONTHS_OR_MINUTES", EmitSingleChar, SCC_UNITS_MONTHS_OR_MINUTES, 1, 0, {CmdFlag::Gender}},
95 {"UNITS_YEARS_OR_PERIODS", EmitSingleChar, SCC_UNITS_YEARS_OR_PERIODS, 1, 0, {CmdFlag::Gender}},
96 {"UNITS_YEARS_OR_MINUTES", EmitSingleChar, SCC_UNITS_YEARS_OR_MINUTES, 1, 0, {CmdFlag::Gender}},
97
98 {"P", EmitPlural, 0, 0, std::nullopt, {CmdFlag::DontCount}}, // plural specifier
99 {"G", EmitGender, 0, 0, std::nullopt, {CmdFlag::DontCount}}, // gender specifier
100
101 {"DATE_TINY", EmitSingleChar, SCC_DATE_TINY, 1, std::nullopt, {}},
102 {"DATE_SHORT", EmitSingleChar, SCC_DATE_SHORT, 1, std::nullopt, {CmdFlag::Case}},
103 {"DATE_LONG", EmitSingleChar, SCC_DATE_LONG, 1, std::nullopt, {CmdFlag::Case}},
104 {"DATE_ISO", EmitSingleChar, SCC_DATE_ISO, 1, std::nullopt, {}},
105
106 {"STRING", EmitSingleChar, SCC_STRING, 1, std::nullopt, {CmdFlag::Case, CmdFlag::Gender}},
107 {"RAW_STRING", EmitSingleChar, SCC_RAW_STRING_POINTER, 1, std::nullopt, {CmdFlag::Gender}},
108
109 /* Numbers */
110 {"COMMA", EmitSingleChar, SCC_COMMA, 1, 0, {}}, // Number with comma
111 {"DECIMAL", EmitSingleChar, SCC_DECIMAL, 2, 0, {}}, // Number with comma and fractional part. Second parameter is number of fractional digits, first parameter is number times 10**(second parameter).
112 {"NUM", EmitSingleChar, SCC_NUM, 1, 0, {}}, // Signed number
113 {"ZEROFILL_NUM", EmitSingleChar, SCC_ZEROFILL_NUM, 2, 0, {}}, // Unsigned number with zero fill, e.g. "02". First parameter is number, second minimum length
114 {"BYTES", EmitSingleChar, SCC_BYTES, 1, 0, {}}, // Unsigned number with "bytes", i.e. "1.02 MiB or 123 KiB"
115 {"HEX", EmitSingleChar, SCC_HEX, 1, 0, {}}, // Hexadecimally printed number
116
117 {"CURRENCY_LONG", EmitSingleChar, SCC_CURRENCY_LONG, 1, 0, {}},
118 {"CURRENCY_SHORT", EmitSingleChar, SCC_CURRENCY_SHORT, 1, 0, {}}, // compact currency
119
120 {"WAYPOINT", EmitSingleChar, SCC_WAYPOINT_NAME, 1, std::nullopt, {CmdFlag::Gender}}, // waypoint name
121 {"STATION", EmitSingleChar, SCC_STATION_NAME, 1, std::nullopt, {CmdFlag::Gender}},
122 {"DEPOT", EmitSingleChar, SCC_DEPOT_NAME, 2, std::nullopt, {CmdFlag::Gender}},
123 {"TOWN", EmitSingleChar, SCC_TOWN_NAME, 1, std::nullopt, {CmdFlag::Gender}},
124 {"GROUP", EmitSingleChar, SCC_GROUP_NAME, 1, std::nullopt, {CmdFlag::Gender}},
125 {"SIGN", EmitSingleChar, SCC_SIGN_NAME, 1, std::nullopt, {CmdFlag::Gender}},
126 {"ENGINE", EmitSingleChar, SCC_ENGINE_NAME, 1, std::nullopt, {CmdFlag::Gender}},
127 {"VEHICLE", EmitSingleChar, SCC_VEHICLE_NAME, 1, std::nullopt, {CmdFlag::Gender}},
128 {"COMPANY", EmitSingleChar, SCC_COMPANY_NAME, 1, std::nullopt, {CmdFlag::Gender}},
129 {"COMPANY_NUM", EmitSingleChar, SCC_COMPANY_NUM, 1, std::nullopt, {}},
130 {"PRESIDENT_NAME", EmitSingleChar, SCC_PRESIDENT_NAME, 1, std::nullopt, {CmdFlag::Gender}},
131
132 {"SPACE", EmitSingleChar, ' ', 0, std::nullopt, {CmdFlag::DontCount}},
133 {"", EmitSingleChar, '\n', 0, std::nullopt, {CmdFlag::DontCount}},
134 {"{", EmitSingleChar, '{', 0, std::nullopt, {CmdFlag::DontCount}},
135 {"UP_ARROW", EmitSingleChar, SCC_UP_ARROW, 0, std::nullopt, {CmdFlag::DontCount}},
136 {"SMALL_UP_ARROW", EmitSingleChar, SCC_SMALL_UP_ARROW, 0, std::nullopt, {CmdFlag::DontCount}},
137 {"SMALL_DOWN_ARROW", EmitSingleChar, SCC_SMALL_DOWN_ARROW, 0, std::nullopt, {CmdFlag::DontCount}},
138 {"TRAIN", EmitSingleChar, SCC_TRAIN, 0, std::nullopt, {CmdFlag::DontCount}},
139 {"LORRY", EmitSingleChar, SCC_LORRY, 0, std::nullopt, {CmdFlag::DontCount}},
140 {"BUS", EmitSingleChar, SCC_BUS, 0, std::nullopt, {CmdFlag::DontCount}},
141 {"PLANE", EmitSingleChar, SCC_PLANE, 0, std::nullopt, {CmdFlag::DontCount}},
142 {"SHIP", EmitSingleChar, SCC_SHIP, 0, std::nullopt, {CmdFlag::DontCount}},
143 {"NBSP", EmitSingleChar, 0xA0, 0, std::nullopt, {CmdFlag::DontCount}},
144 {"COPYRIGHT", EmitSingleChar, 0xA9, 0, std::nullopt, {CmdFlag::DontCount}},
145 {"DOWN_ARROW", EmitSingleChar, SCC_DOWN_ARROW, 0, std::nullopt, {CmdFlag::DontCount}},
146 {"CHECKMARK", EmitSingleChar, SCC_CHECKMARK, 0, std::nullopt, {CmdFlag::DontCount}},
147 {"CROSS", EmitSingleChar, SCC_CROSS, 0, std::nullopt, {CmdFlag::DontCount}},
148 {"LEFT_ARROW", EmitSingleChar, SCC_LEFT_ARROW, 0, std::nullopt, {CmdFlag::DontCount}},
149 {"RIGHT_ARROW", EmitSingleChar, SCC_RIGHT_ARROW, 0, std::nullopt, {CmdFlag::DontCount}},
150 {"SMALL_LEFT_ARROW", EmitSingleChar, SCC_LESS_THAN, 0, std::nullopt, {CmdFlag::DontCount}},
151 {"SMALL_RIGHT_ARROW", EmitSingleChar, SCC_GREATER_THAN, 0, std::nullopt, {CmdFlag::DontCount}},
152 {"TOWN_ICON", EmitSingleChar, SCC_TOWN, 0, std::nullopt, {CmdFlag::DontCount}},
153 {"CITY_ICON", EmitSingleChar, SCC_CITY, 0, std::nullopt, {CmdFlag::DontCount}},
154
155 /* The following are directional formatting codes used to get the RTL strings right:
156 * http://www.unicode.org/unicode/reports/tr9/#Directional_Formatting_Codes */
157 {"LRM", EmitSingleChar, CHAR_TD_LRM, 0, std::nullopt, {CmdFlag::DontCount}},
158 {"RLM", EmitSingleChar, CHAR_TD_RLM, 0, std::nullopt, {CmdFlag::DontCount}},
159 {"LRE", EmitSingleChar, CHAR_TD_LRE, 0, std::nullopt, {CmdFlag::DontCount}},
160 {"RLE", EmitSingleChar, CHAR_TD_RLE, 0, std::nullopt, {CmdFlag::DontCount}},
161 {"LRO", EmitSingleChar, CHAR_TD_LRO, 0, std::nullopt, {CmdFlag::DontCount}},
162 {"RLO", EmitSingleChar, CHAR_TD_RLO, 0, std::nullopt, {CmdFlag::DontCount}},
163 {"PDF", EmitSingleChar, CHAR_TD_PDF, 0, std::nullopt, {CmdFlag::DontCount}},
164};
165
169 std::string_view description;
170 std::string_view names;
171};
172
174static const size_t MAX_PLURALS = 5;
175
177static const PluralForm _plural_forms[] = {
178 { 2, "Two forms: special case for 1.", "\"1\" \"other\"" },
179 { 1, "Only one form.", "\"other\"" },
180 { 2, "Two forms: special case for 0 to 1.", "\"0..1\" \"other\"" },
181 { 3, "Three forms: special cases for 0, and numbers ending in 1 except when ending in 11.", "\"1,21,31,...\" \"other\" \"0\"" },
182 { 5, "Five forms: special cases for 1, 2, 3 to 6, and 7 to 10.", "\"1\" \"2\" \"3..6\" \"7..10\" \"other\"" },
183 { 3, "Three forms: special cases for numbers ending in 1 except when ending in 11, and 2 to 9 except when ending in 12 to 19.", "\"1,21,31,...\" \"2..9,22..29,32..39,...\" \"other\"" },
184 { 3, "Three forms: special cases for numbers ending in 1 except when ending in 11, and 2 to 4 except when ending in 12 to 14.", "\"1,21,31,...\" \"2..4,22..24,32..34,...\" \"other\"" },
185 { 3, "Three forms: special cases for 1, and numbers ending in 2 to 4 except when ending in 12 to 14.", "\"1\" \"2..4,22..24,32..34,...\" \"other\"" },
186 { 4, "Four forms: special cases for numbers ending in 01, 02, and 03 to 04.", "\"1,101,201,...\" \"2,102,202,...\" \"3..4,103..104,203..204,...\" \"other\"" },
187 { 2, "Two forms: special case for numbers ending in 1 except when ending in 11.", "\"1,21,31,...\" \"other\"" },
188 { 3, "Three forms: special cases for 1, and 2 to 4.", "\"1\" \"2..4\" \"other\"" },
189 { 2, "Two forms: cases for numbers ending with a consonant, and with a vowel.", "\"yeong,il,sam,yuk,chil,pal\" \"i,sa,o,gu\"" },
190 { 4, "Four forms: special cases for 1, 0 and numbers ending in 02 to 10, and numbers ending in 11 to 19.", "\"1\" \"0,2..10,102..110,202..210,...\" \"11..19,111..119,211..219,...\" \"other\"" },
191 { 4, "Four forms: special cases for 1 and 11, 2 and 12, 3..10 and 13..19.", "\"1,11\" \"2,12\" \"3..10,13..19\" \"other\"" },
192 { 3, "Three forms: special cases for 1, 0 and numbers ending in 01 to 19.", "\"1\" \"0,2..19,101..119,201..219,...\" \"other\"" },
193};
194
195/* Flags:
196 * 0 = nothing
197 * t = translator editable
198 * l = ltr/rtl choice
199 * p = plural choice
200 * d = separator char (replace spaces with {NBSP})
201 * x1 = hexadecimal number of 1 byte
202 * x2 = hexadecimal number of 2 bytes
203 * g = gender
204 * c = cases
205 * a = array, i.e. list of strings
206 */
208static const std::string_view _pragmas[][4] = {
209 /* name flags default description */
210 { "name", "0", "", "English name for the language" },
211 { "ownname", "t", "", "Localised name for the language" },
212 { "isocode", "0", "", "ISO code for the language" },
213 { "plural", "tp", "0", "Plural form to use" },
214 { "textdir", "tl", "ltr", "Text direction. Either ltr (left-to-right) or rtl (right-to-left)" },
215 { "digitsep", "td", ",", "Digit grouping separator for non-currency numbers" },
216 { "digitsepcur", "td", ",", "Digit grouping separator for currency numbers" },
217 { "decimalsep", "td", ".", "Decimal separator" },
218 { "winlangid", "x2", "0x0000", "Language ID for Windows" },
219 { "grflangid", "x1", "0x00", "Language ID for NewGRFs" },
220 { "gender", "tag", "", "List of genders" },
221 { "case", "tac", "", "List of cases" },
222};
Enum-as-bit-set wrapper.
Compose data into a growing std::string.
@ SCC_TINYFONT
Switch to small font.
@ SCC_NORMALFONT
Switch to normal size font.
@ SCC_BIGFONT
Switch to large font.
@ SCC_MONOFONT
Switch to monospaced font.
Type (helpers) for enums.
static const std::string_view _pragmas[][4]
All pragmas used.
static const PluralForm _plural_forms[]
All plural forms used.
static const size_t MAX_PLURALS
The maximum number of plurals.
EnumBitSet< CmdFlag, uint8_t > CmdFlags
Bitset of CmdFlag elements.
CmdFlag
Flags describing how to process a string command.
@ Gender
These commands support genders.
@ Case
These commands support cases.
@ DontCount
These commands aren't counted for comparison.
static const char32_t CHAR_TD_RLE
The following text is embedded right-to-left.
Definition string_type.h:38
static const char32_t CHAR_TD_LRO
Force the following characters to be treated as left-to-right characters.
Definition string_type.h:39
static const char32_t CHAR_TD_LRM
The next character acts like a left-to-right character.
Definition string_type.h:35
static const char32_t CHAR_TD_RLO
Force the following characters to be treated as right-to-left characters.
Definition string_type.h:40
static const char32_t CHAR_TD_LRE
The following text is embedded left-to-right.
Definition string_type.h:37
static const char32_t CHAR_TD_RLM
The next character acts like a right-to-left character.
Definition string_type.h:36
static const char32_t CHAR_TD_PDF
Restore the text-direction state to before the last LRE, RLE, LRO or RLO.
Definition string_type.h:41
Container for the different cases of a string.
Definition strgen.h:22
Description of a plural form.
size_t plural_count
The number of plural forms.
std::string_view names
Plural names.
std::string_view description
Human readable description of the form.