OpenTTD Source: src/strings_func.h Source File

OpenTTD Source 1.11.0-RC1

strings_func.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 <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef STRINGS_FUNC_H
11 #define STRINGS_FUNC_H
12
13 #include "strings_type.h"
14 #include "string_type.h"
15 #include "gfx_type.h"
16 #include "core/bitmath_func.hpp"
17
23 static inline StringTab GetStringTab(StringID str)
24 {
25 StringTab result = (StringTab)(str >> TAB_SIZE_BITS);
26 if (result >= TEXT_TAB_NEWGRF_START) return TEXT_TAB_NEWGRF_START;
27 if (result >= TEXT_TAB_GAMESCRIPT_START) return TEXT_TAB_GAMESCRIPT_START;
28 return result;
29 }
30
36 static inline uint GetStringIndex(StringID str)
37 {
38 return str - (GetStringTab(str) << TAB_SIZE_BITS);
39 }
40
47 static inline StringID MakeStringID(StringTab tab, uint index)
48 {
49 if (tab == TEXT_TAB_NEWGRF_START) {
50 assert(index < TAB_SIZE_NEWGRF);
51 } else if (tab == TEXT_TAB_GAMESCRIPT_START) {
52 assert(index < TAB_SIZE_GAMESCRIPT);
53 } else {
54 assert(tab < TEXT_TAB_END);
55 assert(index < TAB_SIZE);
56 }
57 return (tab << TAB_SIZE_BITS) + index;
58 }
59
60 class StringParameters {
61 StringParameters *parent;
62 uint64 *data;
63 WChar *type;
64
65 public:
66 uint offset;
67 uint num_param;
68
70 StringParameters(uint64 *data, uint num_param, WChar *type) :
71 parent(nullptr),
72 data(data),
73 type(type),
74 offset(0),
75 num_param(num_param)
76 { }
77
79 template <size_t Tnum_param>
80 StringParameters(int64 (&data)[Tnum_param]) :
81 parent(nullptr),
82 data((uint64 *)data),
83 type(nullptr),
84 offset(0),
85 num_param(Tnum_param)
86 {
87 static_assert(sizeof(data[0]) == sizeof(uint64));
88 }
89
94 StringParameters(StringParameters &parent, uint size) :
95 parent(&parent),
96 data(parent.data + parent.offset),
97 offset(0),
98 num_param(size)
99 {
100 assert(size <= parent.GetDataLeft());
101 if (parent.type == nullptr) {
102 this->type = nullptr;
103 } else {
104 this->type = parent.type + parent.offset;
105 }
106 }
107
108 ~StringParameters()
109 {
110 if (this->parent != nullptr) {
111 this->parent->offset += this->num_param;
112 }
113 }
114
115 void ClearTypeInformation();
116
117 int64 GetInt64(WChar type = 0);
118
120 int32 GetInt32(WChar type = 0)
121 {
122 return (int32)this->GetInt64(type);
123 }
124
125 void ShiftParameters(uint amount);
126
128 uint64 *GetDataPointer() const
129 {
130 return &this->data[this->offset];
131 }
132
134 uint GetDataLeft() const
135 {
136 return this->num_param - this->offset;
137 }
138
140 uint64 *GetPointerToOffset(uint offset) const
141 {
142 assert(offset < this->num_param);
143 return &this->data[offset];
144 }
145
147 bool HasTypeInformation() const
148 {
149 return this->type != nullptr;
150 }
151
153 WChar GetTypeAtOffset(uint offset) const
154 {
155 assert(offset < this->num_param);
156 assert(this->HasTypeInformation());
157 return this->type[offset];
158 }
159
160 void SetParam(uint n, uint64 v)
161 {
162 assert(n < this->num_param);
163 this->data[n] = v;
164 }
165
166 uint64 GetParam(uint n) const
167 {
168 assert(n < this->num_param);
169 return this->data[n];
170 }
171 };
172 extern StringParameters _global_string_params;
173
174 char *GetString(char *buffr, StringID string, const char *last);
175 char *GetStringWithArgs(char *buffr, StringID string, StringParameters *args, const char *last, uint case_index = 0, bool game_script = false);
176 const char *GetStringPtr(StringID string);
177
178 uint ConvertKmhishSpeedToDisplaySpeed(uint speed);
179 uint ConvertDisplaySpeedToKmhishSpeed(uint speed);
180
181 void InjectDParam(uint amount);
182
189 static inline void SetDParamX(uint64 *s, uint n, uint64 v)
190 {
191 s[n] = v;
192 }
193
199 static inline void SetDParam(uint n, uint64 v)
200 {
201 _global_string_params.SetParam(n, v);
202 }
203
204 void SetDParamMaxValue(uint n, uint64 max_value, uint min_count = 0, FontSize size = FS_NORMAL);
205 void SetDParamMaxDigits(uint n, uint count, FontSize size = FS_NORMAL);
206
207 void SetDParamStr(uint n, const char *str);
208
209 void CopyInDParam(int offs, const uint64 *src, int num);
210 void CopyOutDParam(uint64 *dst, int offs, int num);
211 void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num);
212
219 static inline uint64 GetDParamX(const uint64 *s, uint n)
220 {
221 return s[n];
222 }
223
229 static inline uint64 GetDParam(uint n)
230 {
231 return _global_string_params.GetParam(n);
232 }
233
234 extern TextDirection _current_text_dir;
235
236 void InitializeLanguagePacks();
237 const char *GetCurrentLanguageIsoCode();
238
239 bool StringIDSorter(const StringID &a, const StringID &b);
240
244 class MissingGlyphSearcher {
245 public:
247 virtual ~MissingGlyphSearcher() {}
248
253 virtual const char *NextString() = 0;
254
259 virtual FontSize DefaultSize() = 0;
260
264 virtual void Reset() = 0;
265
270 virtual bool Monospace() = 0;
271
278 virtual void SetFontNames(struct FreeTypeSettings *settings, const char *font_name, const void *os_data = nullptr) = 0;
279
280 bool FindMissingGlyphs();
281 };
282
283 void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = nullptr);
284
285 #endif /* STRINGS_FUNC_H */
ConvertKmhishSpeedToDisplaySpeed
uint ConvertKmhishSpeedToDisplaySpeed(uint speed)
Convert the given km/h-ish speed to the display speed.
Definition: strings.cpp:756
StringParameters::StringParameters
StringParameters(StringParameters &parent, uint size)
Create a new StringParameters instance that can reference part of the data of the given partent insta...
Definition: strings_func.h:94
MissingGlyphSearcher::FindMissingGlyphs
bool FindMissingGlyphs()
Check whether there are glyphs missing in the current language.
Definition: strings.cpp:1987
GetStringTab
static StringTab GetStringTab(StringID str)
Extract the StringTab from a StringID.
Definition: strings_func.h:23
MissingGlyphSearcher::DefaultSize
virtual FontSize DefaultSize()=0
Get the default (font) size of the string.
MissingGlyphSearcher::~MissingGlyphSearcher
virtual ~MissingGlyphSearcher()
Make sure everything gets destructed right.
Definition: strings_func.h:247
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
MissingGlyphSearcher
A searcher for missing glyphs.
Definition: strings_func.h:244
TEXT_TAB_END
@ TEXT_TAB_END
End of language files.
Definition: strings_type.h:38
MissingGlyphSearcher::NextString
virtual const char * NextString()=0
Get the next string to search through.
TEXT_TAB_NEWGRF_START
@ TEXT_TAB_NEWGRF_START
Start of NewGRF supplied strings.
Definition: strings_type.h:40
MakeStringID
static StringID MakeStringID(StringTab tab, uint index)
Create a StringID.
Definition: strings_func.h:47
SetDParamStr
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:286
StringParameters::parent
StringParameters * parent
If not nullptr, this instance references data from this parent instance.
Definition: strings_func.h:61
GetDParamX
static uint64 GetDParamX(const uint64 *s, uint n)
Get the current string parameter at index n from parameter array s.
Definition: strings_func.h:219
StringTab
StringTab
StringTabs to group StringIDs.
Definition: strings_type.h:28
MissingGlyphSearcher::Reset
virtual void Reset()=0
Reset the search, i.e.
SetDParamX
static void SetDParamX(uint64 *s, uint n, uint64 v)
Set a string parameter v at index n in a given array s.
Definition: strings_func.h:189
strings_type.h
TAB_SIZE_NEWGRF
static const uint TAB_SIZE_NEWGRF
Number of strings for NewGRFs.
Definition: strings_type.h:52
StringParameters::GetTypeAtOffset
WChar GetTypeAtOffset(uint offset) const
Get the type of a specific element.
Definition: strings_func.h:153
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:199
ConvertDisplaySpeedToKmhishSpeed
uint ConvertDisplaySpeedToKmhishSpeed(uint speed)
Convert the given display speed to the km/h-ish speed.
Definition: strings.cpp:766
GetStringWithArgs
char * GetStringWithArgs(char *buffr, StringID string, StringParameters *args, const char *last, uint case_index=0, bool game_script=false)
Get a parsed string with most special stringcodes replaced by the string parameters.
Definition: strings.cpp:223
TAB_SIZE
static const uint TAB_SIZE
Number of strings per StringTab.
Definition: strings_type.h:46
StringParameters::GetInt64
int64 GetInt64(WChar type=0)
Read an int64 from the argument array.
Definition: strings.cpp:70
bitmath_func.hpp
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:48
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:207
InjectDParam
void InjectDParam(uint amount)
Shift the string parameters in the global string parameter array by amount positions,...
Definition: strings.cpp:295
StringParameters::GetDataLeft
uint GetDataLeft() const
Return the amount of elements which can still be read.
Definition: strings_func.h:134
StringParameters::ShiftParameters
void ShiftParameters(uint amount)
Shift all data in the data array by the given amount to make room for some extra parameters.
Definition: strings.cpp:90
CheckForMissingGlyphs
void CheckForMissingGlyphs(bool base_font=true, MissingGlyphSearcher *search=nullptr)
Check whether the currently loaded language pack uses characters that the currently loaded font does ...
Definition: strings.cpp:2085
StringParameters::offset
uint offset
Current offset in the data/type arrays.
Definition: strings_func.h:66
TAB_SIZE_GAMESCRIPT
static const uint TAB_SIZE_GAMESCRIPT
Number of strings for GameScripts.
Definition: strings_type.h:49
StringParameters::StringParameters
StringParameters(int64(&data)[Tnum_param])
Create a new StringParameters instance.
Definition: strings_func.h:80
GetDParam
static uint64 GetDParam(uint n)
Get the current string parameter at index n from the global string parameter array.
Definition: strings_func.h:229
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
StringParameters::type
WChar * type
Array with type information about the data. Can be nullptr when no type information is needed....
Definition: strings_func.h:63
MissingGlyphSearcher::Monospace
virtual bool Monospace()=0
Whether to search for a monospace font or not.
StringParameters::GetPointerToOffset
uint64 * GetPointerToOffset(uint offset) const
Get a pointer to a specific element in the data array.
Definition: strings_func.h:140
StringParameters::GetDataPointer
uint64 * GetDataPointer() const
Get a pointer to the current element in the data array.
Definition: strings_func.h:128
string_type.h
StringParameters::num_param
uint num_param
Length of the data array.
Definition: strings_func.h:67
SetDParamMaxDigits
void SetDParamMaxDigits(uint n, uint count, FontSize size=FS_NORMAL)
Set DParam n to some number that is suitable for string size computations.
Definition: strings.cpp:120
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
GetCurrentLanguageIsoCode
const char * GetCurrentLanguageIsoCode()
Get the ISO language code of the currently loaded language.
Definition: strings.cpp:1978
StringParameters::data
uint64 * data
Array with the actual data.
Definition: strings_func.h:62
CopyOutDParam
void CopyOutDParam(uint64 *dst, int offs, int num)
Copy num string parameters from the global string parameter array to the dst array.
Definition: strings.cpp:149
StringParameters::GetInt32
int32 GetInt32(WChar type=0)
Read an int32 from the argument array.
Definition: strings_func.h:120
TextDirection
TextDirection
Directions a text can go to.
Definition: strings_type.h:22
StringParameters
Definition: strings_func.h:60
TEXT_TAB_GAMESCRIPT_START
@ TEXT_TAB_GAMESCRIPT_START
Start of GameScript supplied strings.
Definition: strings_type.h:39
StringParameters::ClearTypeInformation
void ClearTypeInformation()
Reset the type array.
Definition: strings.cpp:59
InitializeLanguagePacks
void InitializeLanguagePacks()
Make a list of the available language packs.
Definition: strings.cpp:1931
CopyInDParam
void CopyInDParam(int offs, const uint64 *src, int num)
Copy num string parameters from array src into the global string parameter array.
Definition: strings.cpp:138
SetDParamMaxValue
void SetDParamMaxValue(uint n, uint64 max_value, uint min_count=0, FontSize size=FS_NORMAL)
Set DParam n to some number that is suitable for string size computations.
Definition: strings.cpp:104
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:206
GetStringIndex
static uint GetStringIndex(StringID str)
Extract the StringIndex from a StringID.
Definition: strings_func.h:36
MissingGlyphSearcher::SetFontNames
virtual void SetFontNames(struct FreeTypeSettings *settings, const char *font_name, const void *os_data=nullptr)=0
Set the right font names.
StringParameters::HasTypeInformation
bool HasTypeInformation() const
Does this instance store information about the type of the parameters.
Definition: strings_func.h:147
gfx_type.h
TAB_SIZE_BITS
static const uint TAB_SIZE_BITS
Number of bits for the StringIndex within a StringTab.
Definition: strings_type.h:44
FreeTypeSettings
Settings for the freetype fonts.
Definition: fontcache.h:225
StringParameters::StringParameters
StringParameters(uint64 *data, uint num_param, WChar *type)
Create a new StringParameters instance.
Definition: strings_func.h:70