OpenTTD
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;
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 
62  uint64 *data;
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  assert_compile(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 
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 
235 
237 const char *GetCurrentLanguageIsoCode();
238 
239 bool StringIDSorter(const StringID &a, const StringID &b);
240 
245 public:
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(const char **str);
281 };
282 
283 void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = nullptr);
284 
285 #endif /* STRINGS_FUNC_H */
StringParameters(int64(&data)[Tnum_param])
Create a new StringParameters instance.
Definition: strings_func.h:80
static uint GetStringIndex(StringID str)
Extract the StringIndex from a StringID.
Definition: strings_func.h:36
WChar * type
Array with type information about the data. Can be nullptr when no type information is needed...
Definition: strings_func.h:63
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
void ClearTypeInformation()
Reset the type array.
Definition: strings.cpp:59
uint num_param
Length of the data array.
Definition: strings_func.h:67
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
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:20
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
uint offset
Current offset in the data/type arrays.
Definition: strings_func.h:66
static StringID MakeStringID(StringTab tab, uint index)
Create a StringID.
Definition: strings_func.h:47
bool HasTypeInformation() const
Does this instance store information about the type of the parameters.
Definition: strings_func.h:147
Functions related to bit mathematics.
StringTab
StringTabs to group StringIDs.
Definition: strings_type.h:28
static StringTab GetStringTab(StringID str)
Extract the StringTab from a StringID.
Definition: strings_func.h:23
const char * GetCurrentLanguageIsoCode()
Get the ISO language code of the currently loaded language.
Definition: strings.cpp:1997
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:216
virtual ~MissingGlyphSearcher()
Make sure everything gets destructed right.
Definition: strings_func.h:247
Settings for the freetype fonts.
Definition: fontcache.h:224
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:279
void InitializeLanguagePacks()
Make a list of the available language packs.
Definition: strings.cpp:1949
uint64 * data
Array with the actual data.
Definition: strings_func.h:62
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
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:2097
static const uint TAB_SIZE_NEWGRF
Number of strings for NewGRFs.
Definition: strings_type.h:52
A searcher for missing glyphs.
Definition: strings_func.h:244
static const uint TAB_SIZE
Number of strings per StringTab.
Definition: strings_type.h:46
int64 GetInt64(WChar type=0)
Read an int64 from the argument array.
Definition: strings.cpp:70
static const uint TAB_SIZE_GAMESCRIPT
Number of strings for GameScripts.
Definition: strings_type.h:49
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
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
uint GetDataLeft() const
Return the amount of elements which can still be read.
Definition: strings_func.h:134
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Start of GameScript supplied strings.
Definition: strings_type.h:39
End of language files.
Definition: strings_type.h:38
TextDirection
Directions a text can go to.
Definition: strings_type.h:22
uint ConvertDisplaySpeedToKmhishSpeed(uint speed)
Convert the given display speed to the km/h-ish speed.
Definition: strings.cpp:757
WChar GetTypeAtOffset(uint offset) const
Get the type of a specific element.
Definition: strings_func.h:153
static uint64 GetDParam(uint n)
Get the current string parameter at index n from the global string parameter array.
Definition: strings_func.h:229
StringParameters(uint64 *data, uint num_param, WChar *type)
Create a new StringParameters instance.
Definition: strings_func.h:70
StringParameters * parent
If not nullptr, this instance references data from this parent instance.
Definition: strings_func.h:61
FontSize
Available font sizes.
Definition: gfx_type.h:201
static const uint TAB_SIZE_BITS
Number of bits for the StringIndex within a StringTab.
Definition: strings_type.h:44
Index of the normal font in the font tables.
Definition: gfx_type.h:202
Start of NewGRF supplied strings.
Definition: strings_type.h:40
Types related to strings.
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:48
Types for strings.
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
uint64 * GetPointerToOffset(uint offset) const
Get a pointer to a specific element in the data array.
Definition: strings_func.h:140
int32 GetInt32(WChar type=0)
Read an int32 from the argument array.
Definition: strings_func.h:120
void InjectDParam(uint amount)
Shift the string parameters in the global string parameter array by amount positions, making room at the beginning.
Definition: strings.cpp:288
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
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
Types related to the graphics and/or input devices.
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
uint ConvertKmhishSpeedToDisplaySpeed(uint speed)
Convert the given km/h-ish speed to the display speed.
Definition: strings.cpp:747
uint64 * GetDataPointer() const
Get a pointer to the current element in the data array.
Definition: strings_func.h:128