OpenTTD Source  12.0-beta2
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 
71  parent(nullptr),
72  data(data),
73  type(type),
74  offset(0),
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 
95  parent(&parent),
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 
126  uint64 *GetDataPointer() const
127  {
128  return &this->data[this->offset];
129  }
130 
132  uint GetDataLeft() const
133  {
134  return this->num_param - this->offset;
135  }
136 
138  uint64 *GetPointerToOffset(uint offset) const
139  {
140  assert(offset < this->num_param);
141  return &this->data[offset];
142  }
143 
145  bool HasTypeInformation() const
146  {
147  return this->type != nullptr;
148  }
149 
152  {
153  assert(offset < this->num_param);
154  assert(this->HasTypeInformation());
155  return this->type[offset];
156  }
157 
158  void SetParam(uint n, uint64 v)
159  {
160  assert(n < this->num_param);
161  this->data[n] = v;
162  }
163 
164  uint64 GetParam(uint n) const
165  {
166  assert(n < this->num_param);
167  return this->data[n];
168  }
169 };
170 extern StringParameters _global_string_params;
171 
172 char *GetString(char *buffr, StringID string, const char *last);
173 std::string GetString(StringID string);
174 char *GetStringWithArgs(char *buffr, StringID string, StringParameters *args, const char *last, uint case_index = 0, bool game_script = false);
175 const char *GetStringPtr(StringID string);
176 
177 uint ConvertKmhishSpeedToDisplaySpeed(uint speed);
178 uint ConvertDisplaySpeedToKmhishSpeed(uint speed);
179 
186 static inline void SetDParamX(uint64 *s, uint n, uint64 v)
187 {
188  s[n] = v;
189 }
190 
196 static inline void SetDParam(uint n, uint64 v)
197 {
198  _global_string_params.SetParam(n, v);
199 }
200 
201 void SetDParamMaxValue(uint n, uint64 max_value, uint min_count = 0, FontSize size = FS_NORMAL);
202 void SetDParamMaxDigits(uint n, uint count, FontSize size = FS_NORMAL);
203 
204 void SetDParamStr(uint n, const char *str);
205 void SetDParamStr(uint n, const std::string &str);
206 
207 void CopyInDParam(int offs, const uint64 *src, int num);
208 void CopyOutDParam(uint64 *dst, int offs, int num);
209 void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num);
210 
217 static inline uint64 GetDParamX(const uint64 *s, uint n)
218 {
219  return s[n];
220 }
221 
227 static inline uint64 GetDParam(uint n)
228 {
229  return _global_string_params.GetParam(n);
230 }
231 
233 
235 const char *GetCurrentLanguageIsoCode();
236 
237 bool StringIDSorter(const StringID &a, const StringID &b);
238 
243 public:
246 
251  virtual const char *NextString() = 0;
252 
257  virtual FontSize DefaultSize() = 0;
258 
262  virtual void Reset() = 0;
263 
268  virtual bool Monospace() = 0;
269 
276  virtual void SetFontNames(struct FreeTypeSettings *settings, const char *font_name, const void *os_data = nullptr) = 0;
277 
278  bool FindMissingGlyphs();
279 };
280 
281 void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = nullptr);
282 
283 #endif /* STRINGS_FUNC_H */
ConvertKmhishSpeedToDisplaySpeed
uint ConvertKmhishSpeedToDisplaySpeed(uint speed)
Convert the given km/h-ish speed to the display speed.
Definition: strings.cpp:774
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:2012
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:245
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:242
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:296
StringParameters::parent
StringParameters * parent
If not nullptr, this instance references data from this parent instance.
Definition: strings_func.h:61
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition: strings.cpp:284
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:217
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:186
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:151
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:196
ConvertDisplaySpeedToKmhishSpeed
uint ConvertDisplaySpeedToKmhishSpeed(uint speed)
Convert the given display speed to the km/h-ish speed.
Definition: strings.cpp:784
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:221
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
StringParameters::GetDataLeft
uint GetDataLeft() const
Return the amount of elements which can still be read.
Definition: strings_func.h:132
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:2110
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:227
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:138
StringParameters::GetDataPointer
uint64 * GetDataPointer() const
Get a pointer to the current element in the data array.
Definition: strings_func.h:126
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:110
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:2003
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:139
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:1954
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:128
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:94
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:145
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