Add: INR currency (#8136)
[openttd-github.git] / src / strings_func.h
blob6ef36dafc62ff7c36ccc4927326148d7213d6937
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 */
8 /** @file strings_func.h Functions related to OTTD's strings. */
10 #ifndef STRINGS_FUNC_H
11 #define STRINGS_FUNC_H
13 #include "strings_type.h"
14 #include "string_type.h"
15 #include "gfx_type.h"
16 #include "core/bitmath_func.hpp"
18 /**
19 * Extract the StringTab from a StringID.
20 * @param str String identifier
21 * @return StringTab from \a str
23 static inline StringTab GetStringTab(StringID str)
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;
31 /**
32 * Extract the StringIndex from a StringID.
33 * @param str String identifier
34 * @return StringIndex from \a str
36 static inline uint GetStringIndex(StringID str)
38 return str - (GetStringTab(str) << TAB_SIZE_BITS);
41 /**
42 * Create a StringID
43 * @param tab StringTab
44 * @param index StringIndex
45 * @return StringID composed from \a tab and \a index
47 static inline StringID MakeStringID(StringTab tab, uint index)
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);
57 return (tab << TAB_SIZE_BITS) + index;
60 class StringParameters {
61 StringParameters *parent; ///< If not nullptr, this instance references data from this parent instance.
62 uint64 *data; ///< Array with the actual data.
63 WChar *type; ///< Array with type information about the data. Can be nullptr when no type information is needed. See #StringControlCode.
65 public:
66 uint offset; ///< Current offset in the data/type arrays.
67 uint num_param; ///< Length of the data array.
69 /** Create a new StringParameters instance. */
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 { }
78 /** Create a new StringParameters instance. */
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)
87 assert_compile(sizeof(data[0]) == sizeof(uint64));
90 /**
91 * Create a new StringParameters instance that can reference part of the data of
92 * the given partent instance.
94 StringParameters(StringParameters &parent, uint size) :
95 parent(&parent),
96 data(parent.data + parent.offset),
97 offset(0),
98 num_param(size)
100 assert(size <= parent.GetDataLeft());
101 if (parent.type == nullptr) {
102 this->type = nullptr;
103 } else {
104 this->type = parent.type + parent.offset;
108 ~StringParameters()
110 if (this->parent != nullptr) {
111 this->parent->offset += this->num_param;
115 void ClearTypeInformation();
117 int64 GetInt64(WChar type = 0);
119 /** Read an int32 from the argument array. @see GetInt64. */
120 int32 GetInt32(WChar type = 0)
122 return (int32)this->GetInt64(type);
125 void ShiftParameters(uint amount);
127 /** Get a pointer to the current element in the data array. */
128 uint64 *GetDataPointer() const
130 return &this->data[this->offset];
133 /** Return the amount of elements which can still be read. */
134 uint GetDataLeft() const
136 return this->num_param - this->offset;
139 /** Get a pointer to a specific element in the data array. */
140 uint64 *GetPointerToOffset(uint offset) const
142 assert(offset < this->num_param);
143 return &this->data[offset];
146 /** Does this instance store information about the type of the parameters. */
147 bool HasTypeInformation() const
149 return this->type != nullptr;
152 /** Get the type of a specific element. */
153 WChar GetTypeAtOffset(uint offset) const
155 assert(offset < this->num_param);
156 assert(this->HasTypeInformation());
157 return this->type[offset];
160 void SetParam(uint n, uint64 v)
162 assert(n < this->num_param);
163 this->data[n] = v;
166 uint64 GetParam(uint n) const
168 assert(n < this->num_param);
169 return this->data[n];
172 extern StringParameters _global_string_params;
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);
178 uint ConvertKmhishSpeedToDisplaySpeed(uint speed);
179 uint ConvertDisplaySpeedToKmhishSpeed(uint speed);
181 void InjectDParam(uint amount);
184 * Set a string parameter \a v at index \a n in a given array \a s.
185 * @param s Array of string parameters.
186 * @param n Index of the string parameter.
187 * @param v Value of the string parameter.
189 static inline void SetDParamX(uint64 *s, uint n, uint64 v)
191 s[n] = v;
195 * Set a string parameter \a v at index \a n in the global string parameter array.
196 * @param n Index of the string parameter.
197 * @param v Value of the string parameter.
199 static inline void SetDParam(uint n, uint64 v)
201 _global_string_params.SetParam(n, v);
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);
207 void SetDParamStr(uint n, const char *str);
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);
214 * Get the current string parameter at index \a n from parameter array \a s.
215 * @param s Array of string parameters.
216 * @param n Index of the string parameter.
217 * @return Value of the requested string parameter.
219 static inline uint64 GetDParamX(const uint64 *s, uint n)
221 return s[n];
225 * Get the current string parameter at index \a n from the global string parameter array.
226 * @param n Index of the string parameter.
227 * @return Value of the requested string parameter.
229 static inline uint64 GetDParam(uint n)
231 return _global_string_params.GetParam(n);
234 extern TextDirection _current_text_dir; ///< Text direction of the currently selected language
236 void InitializeLanguagePacks();
237 const char *GetCurrentLanguageIsoCode();
239 bool StringIDSorter(const StringID &a, const StringID &b);
242 * A searcher for missing glyphs.
244 class MissingGlyphSearcher {
245 public:
246 /** Make sure everything gets destructed right. */
247 virtual ~MissingGlyphSearcher() {}
250 * Get the next string to search through.
251 * @return The next string or nullptr if there is none.
253 virtual const char *NextString() = 0;
256 * Get the default (font) size of the string.
257 * @return The font size.
259 virtual FontSize DefaultSize() = 0;
262 * Reset the search, i.e. begin from the beginning again.
264 virtual void Reset() = 0;
267 * Whether to search for a monospace font or not.
268 * @return True if searching for monospace.
270 virtual bool Monospace() = 0;
273 * Set the right font names.
274 * @param settings The settings to modify.
275 * @param font_name The new font name.
276 * @param os_data Opaque pointer to OS-specific data.
278 virtual void SetFontNames(struct FreeTypeSettings *settings, const char *font_name, const void *os_data = nullptr) = 0;
280 bool FindMissingGlyphs(const char **str);
283 void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = nullptr);
285 #endif /* STRINGS_FUNC_H */