OpenTTD
strings_func.h
Go to the documentation of this file.
1 /* $Id: strings_func.h 27758 2017-02-26 19:41:30Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef STRINGS_FUNC_H
13 #define STRINGS_FUNC_H
14 
15 #include "strings_type.h"
16 #include "string_type.h"
17 #include "gfx_type.h"
18 #include "core/bitmath_func.hpp"
19 
25 static inline StringTab GetStringTab(StringID str)
26 {
27  StringTab result = (StringTab)(str >> TAB_SIZE_BITS);
28  if (result >= TEXT_TAB_NEWGRF_START) return TEXT_TAB_NEWGRF_START;
30  return result;
31 }
32 
38 static inline uint GetStringIndex(StringID str)
39 {
40  return str - (GetStringTab(str) << TAB_SIZE_BITS);
41 }
42 
49 static inline StringID MakeStringID(StringTab tab, uint index)
50 {
51  if (tab == TEXT_TAB_NEWGRF_START) {
52  assert(index < TAB_SIZE_NEWGRF);
53  } else if (tab == TEXT_TAB_GAMESCRIPT_START) {
54  assert(index < TAB_SIZE_GAMESCRIPT);
55  } else {
56  assert(tab < TEXT_TAB_END);
57  assert(index < TAB_SIZE);
58  }
59  return (tab << TAB_SIZE_BITS) + index;
60 }
61 
64  uint64 *data;
66 
67 public:
68  uint offset;
69  uint num_param;
70 
72  StringParameters(uint64 *data, uint num_param, WChar *type) :
73  parent(NULL),
74  data(data),
75  type(type),
76  offset(0),
77  num_param(num_param)
78  { }
79 
81  template <size_t Tnum_param>
82  StringParameters(int64 (&data)[Tnum_param]) :
83  parent(NULL),
84  data((uint64 *)data),
85  type(NULL),
86  offset(0),
87  num_param(Tnum_param)
88  {
89  assert_compile(sizeof(data[0]) == sizeof(uint64));
90  }
91 
96  StringParameters(StringParameters &parent, uint size) :
97  parent(&parent),
98  data(parent.data + parent.offset),
99  offset(0),
100  num_param(size)
101  {
102  assert(size <= parent.GetDataLeft());
103  if (parent.type == NULL) {
104  this->type = NULL;
105  } else {
106  this->type = parent.type + parent.offset;
107  }
108  }
109 
111  {
112  if (this->parent != NULL) {
113  this->parent->offset += this->num_param;
114  }
115  }
116 
117  void ClearTypeInformation();
118 
119  int64 GetInt64(WChar type = 0);
120 
122  int32 GetInt32(WChar type = 0)
123  {
124  return (int32)this->GetInt64(type);
125  }
126 
127  void ShiftParameters(uint amount);
128 
130  uint64 *GetDataPointer() const
131  {
132  return &this->data[this->offset];
133  }
134 
136  uint GetDataLeft() const
137  {
138  return this->num_param - this->offset;
139  }
140 
142  uint64 *GetPointerToOffset(uint offset) const
143  {
144  assert(offset < this->num_param);
145  return &this->data[offset];
146  }
147 
149  bool HasTypeInformation() const
150  {
151  return this->type != NULL;
152  }
153 
155  WChar GetTypeAtOffset(uint offset) const
156  {
157  assert(offset < this->num_param);
158  assert(this->HasTypeInformation());
159  return this->type[offset];
160  }
161 
162  void SetParam(uint n, uint64 v)
163  {
164  assert(n < this->num_param);
165  this->data[n] = v;
166  }
167 
168  uint64 GetParam(uint n) const
169  {
170  assert(n < this->num_param);
171  return this->data[n];
172  }
173 };
174 extern StringParameters _global_string_params;
175 
176 char *GetString(char *buffr, StringID string, const char *last);
177 char *GetStringWithArgs(char *buffr, StringID string, StringParameters *args, const char *last, uint case_index = 0, bool game_script = false);
178 const char *GetStringPtr(StringID string);
179 
180 uint ConvertKmhishSpeedToDisplaySpeed(uint speed);
181 uint ConvertDisplaySpeedToKmhishSpeed(uint speed);
182 
183 void InjectDParam(uint amount);
184 
191 static inline void SetDParamX(uint64 *s, uint n, uint64 v)
192 {
193  s[n] = v;
194 }
195 
201 static inline void SetDParam(uint n, uint64 v)
202 {
203  _global_string_params.SetParam(n, v);
204 }
205 
206 void SetDParamMaxValue(uint n, uint64 max_value, uint min_count = 0, FontSize size = FS_NORMAL);
207 void SetDParamMaxDigits(uint n, uint count, FontSize size = FS_NORMAL);
208 
209 void SetDParamStr(uint n, const char *str);
210 
211 void CopyInDParam(int offs, const uint64 *src, int num);
212 void CopyOutDParam(uint64 *dst, int offs, int num);
213 void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num);
214 
221 static inline uint64 GetDParamX(const uint64 *s, uint n)
222 {
223  return s[n];
224 }
225 
231 static inline uint64 GetDParam(uint n)
232 {
233  return _global_string_params.GetParam(n);
234 }
235 
237 
239 const char *GetCurrentLanguageIsoCode();
240 
241 int CDECL StringIDSorter(const StringID *a, const StringID *b);
242 
247 public:
250 
255  virtual const char *NextString() = 0;
256 
261  virtual FontSize DefaultSize() = 0;
262 
266  virtual void Reset() = 0;
267 
272  virtual bool Monospace() = 0;
273 
279  virtual void SetFontNames(struct FreeTypeSettings *settings, const char *font_name) = 0;
280 
281  bool FindMissingGlyphs(const char **str);
282 };
283 
284 void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = NULL);
285 
286 #endif /* STRINGS_FUNC_H */
StringParameters(int64(&data)[Tnum_param])
Create a new StringParameters instance.
Definition: strings_func.h:82
static uint GetStringIndex(StringID str)
Extract the StringIndex from a StringID.
Definition: strings_func.h:38
WChar * type
Array with type information about the data. Can be NULL when no type information is needed...
Definition: strings_func.h:65
static uint64 GetDParamX(const uint64 *s, uint n)
Get the current string parameter at index n from parameter array s.
Definition: strings_func.h:221
void ClearTypeInformation()
Reset the type array.
Definition: strings.cpp:63
void CheckForMissingGlyphs(bool base_font=true, MissingGlyphSearcher *search=NULL)
Check whether the currently loaded language pack uses characters that the currently loaded font does ...
Definition: strings.cpp:2089
uint num_param
Length of the data array.
Definition: strings_func.h:69
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
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:91
uint offset
Current offset in the data/type arrays.
Definition: strings_func.h:68
static StringID MakeStringID(StringTab tab, uint index)
Create a StringID.
Definition: strings_func.h:49
bool HasTypeInformation() const
Does this instance store information about the type of the parameters.
Definition: strings_func.h:149
Functions related to bit mathematics.
StringTab
StringTabs to group StringIDs.
Definition: strings_type.h:30
static StringTab GetStringTab(StringID str)
Extract the StringTab from a StringID.
Definition: strings_func.h:25
const char * GetCurrentLanguageIsoCode()
Get the ISO language code of the currently loaded language.
Definition: strings.cpp:1992
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:249
Settings for the freetype fonts.
Definition: fontcache.h:204
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:1944
uint64 * data
Array with the actual data.
Definition: strings_func.h:64
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:121
static const uint TAB_SIZE_NEWGRF
Number of strings for NewGRFs.
Definition: strings_type.h:54
A searcher for missing glyphs.
Definition: strings_func.h:246
static const uint TAB_SIZE
Number of strings per StringTab.
Definition: strings_type.h:48
int64 GetInt64(WChar type=0)
Read an int64 from the argument array.
Definition: strings.cpp:74
static const uint TAB_SIZE_GAMESCRIPT
Number of strings for GameScripts.
Definition: strings_type.h:51
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:105
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:136
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
Start of GameScript supplied strings.
Definition: strings_type.h:41
End of language files.
Definition: strings_type.h:40
TextDirection
Directions a text can go to.
Definition: strings_type.h:24
uint ConvertDisplaySpeedToKmhishSpeed(uint speed)
Convert the given display speed to the km/h-ish speed.
Definition: strings.cpp:755
WChar GetTypeAtOffset(uint offset) const
Get the type of a specific element.
Definition: strings_func.h:155
static uint64 GetDParam(uint n)
Get the current string parameter at index n from the global string parameter array.
Definition: strings_func.h:231
StringParameters(uint64 *data, uint num_param, WChar *type)
Create a new StringParameters instance.
Definition: strings_func.h:72
StringParameters * parent
If not NULL, this instance references data from this parent instance.
Definition: strings_func.h:63
FontSize
Available font sizes.
Definition: gfx_type.h:203
static const uint TAB_SIZE_BITS
Number of bits for the StringIndex within a StringTab.
Definition: strings_type.h:46
Index of the normal font in the font tables.
Definition: gfx_type.h:204
Start of NewGRF supplied strings.
Definition: strings_type.h:42
Types related to strings.
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:52
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:96
uint64 * GetPointerToOffset(uint offset) const
Get a pointer to a specific element in the data array.
Definition: strings_func.h:142
int32 GetInt32(WChar type=0)
Read an int32 from the argument array.
Definition: strings_func.h:122
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:191
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:201
uint ConvertKmhishSpeedToDisplaySpeed(uint speed)
Convert the given km/h-ish speed to the display speed.
Definition: strings.cpp:745
uint64 * GetDataPointer() const
Get a pointer to the current element in the data array.
Definition: strings_func.h:130