(svn r28004) -Update from Eints:
[openttd.git] / src / strings_func.h
blob0da711bc4db2f52cbce9ec3ca14be7d4ffd31ed7
1 /* $Id$ */
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 */
10 /** @file strings_func.h Functions related to OTTD's strings. */
12 #ifndef STRINGS_FUNC_H
13 #define STRINGS_FUNC_H
15 #include "strings_type.h"
16 #include "string_type.h"
17 #include "gfx_type.h"
18 #include "core/bitmath_func.hpp"
20 /**
21 * Extract the StringTab from a StringID.
22 * @param str String identifier
23 * @return StringTab from \a str
25 static inline StringTab GetStringTab(StringID str)
27 StringTab result = (StringTab)(str >> TAB_SIZE_BITS);
28 if (result >= TEXT_TAB_NEWGRF_START) return TEXT_TAB_NEWGRF_START;
29 if (result >= TEXT_TAB_GAMESCRIPT_START) return TEXT_TAB_GAMESCRIPT_START;
30 return result;
33 /**
34 * Extract the StringIndex from a StringID.
35 * @param str String identifier
36 * @return StringIndex from \a str
38 static inline uint GetStringIndex(StringID str)
40 return str - (GetStringTab(str) << TAB_SIZE_BITS);
43 /**
44 * Create a StringID
45 * @param tab StringTab
46 * @param index StringIndex
47 * @return StringID composed from \a tab and \a index
49 static inline StringID MakeStringID(StringTab tab, uint index)
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);
59 return (tab << TAB_SIZE_BITS) + index;
62 class StringParameters {
63 StringParameters *parent; ///< If not NULL, this instance references data from this parent instance.
64 uint64 *data; ///< Array with the actual data.
65 WChar *type; ///< Array with type information about the data. Can be NULL when no type information is needed. See #StringControlCode.
67 public:
68 uint offset; ///< Current offset in the data/type arrays.
69 uint num_param; ///< Length of the data array.
71 /** Create a new StringParameters instance. */
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 { }
80 /** Create a new StringParameters instance. */
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)
89 assert_compile(sizeof(data[0]) == sizeof(uint64));
92 /**
93 * Create a new StringParameters instance that can reference part of the data of
94 * the given partent instance.
96 StringParameters(StringParameters &parent, uint size) :
97 parent(&parent),
98 data(parent.data + parent.offset),
99 offset(0),
100 num_param(size)
102 assert(size <= parent.GetDataLeft());
103 if (parent.type == NULL) {
104 this->type = NULL;
105 } else {
106 this->type = parent.type + parent.offset;
110 ~StringParameters()
112 if (this->parent != NULL) {
113 this->parent->offset += this->num_param;
117 void ClearTypeInformation();
119 int64 GetInt64(WChar type = 0);
121 /** Read an int32 from the argument array. @see GetInt64. */
122 int32 GetInt32(WChar type = 0)
124 return (int32)this->GetInt64(type);
127 void ShiftParameters(uint amount);
129 /** Get a pointer to the current element in the data array. */
130 uint64 *GetDataPointer() const
132 return &this->data[this->offset];
135 /** Return the amount of elements which can still be read. */
136 uint GetDataLeft() const
138 return this->num_param - this->offset;
141 /** Get a pointer to a specific element in the data array. */
142 uint64 *GetPointerToOffset(uint offset) const
144 assert(offset < this->num_param);
145 return &this->data[offset];
148 /** Does this instance store information about the type of the parameters. */
149 bool HasTypeInformation() const
151 return this->type != NULL;
154 /** Get the type of a specific element. */
155 WChar GetTypeAtOffset(uint offset) const
157 assert(offset < this->num_param);
158 assert(this->HasTypeInformation());
159 return this->type[offset];
162 void SetParam(uint n, uint64 v)
164 assert(n < this->num_param);
165 this->data[n] = v;
168 uint64 GetParam(uint n) const
170 assert(n < this->num_param);
171 return this->data[n];
174 extern StringParameters _global_string_params;
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);
180 uint ConvertKmhishSpeedToDisplaySpeed(uint speed);
181 uint ConvertDisplaySpeedToKmhishSpeed(uint speed);
183 void InjectDParam(uint amount);
186 * Set a string parameter \a v at index \a n in a given array \a s.
187 * @param s Array of string parameters.
188 * @param n Index of the string parameter.
189 * @param v Value of the string parameter.
191 static inline void SetDParamX(uint64 *s, uint n, uint64 v)
193 s[n] = v;
197 * Set a string parameter \a v at index \a n in the global string parameter array.
198 * @param n Index of the string parameter.
199 * @param v Value of the string parameter.
201 static inline void SetDParam(uint n, uint64 v)
203 _global_string_params.SetParam(n, v);
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);
209 void SetDParamStr(uint n, const char *str);
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);
216 * Get the current string parameter at index \a n from parameter array \a s.
217 * @param s Array of string parameters.
218 * @param n Index of the string parameter.
219 * @return Value of the requested string parameter.
221 static inline uint64 GetDParamX(const uint64 *s, uint n)
223 return s[n];
227 * Get the current string parameter at index \a n from the global string parameter array.
228 * @param n Index of the string parameter.
229 * @return Value of the requested string parameter.
231 static inline uint64 GetDParam(uint n)
233 return _global_string_params.GetParam(n);
236 extern TextDirection _current_text_dir; ///< Text direction of the currently selected language
238 void InitializeLanguagePacks();
239 const char *GetCurrentLanguageIsoCode();
241 int CDECL StringIDSorter(const StringID *a, const StringID *b);
244 * A searcher for missing glyphs.
246 class MissingGlyphSearcher {
247 public:
248 /** Make sure everything gets destructed right. */
249 virtual ~MissingGlyphSearcher() {}
252 * Get the next string to search through.
253 * @return The next string or NULL if there is none.
255 virtual const char *NextString() = 0;
258 * Get the default (font) size of the string.
259 * @return The font size.
261 virtual FontSize DefaultSize() = 0;
264 * Reset the search, i.e. begin from the beginning again.
266 virtual void Reset() = 0;
269 * Whether to search for a monospace font or not.
270 * @return True if searching for monospace.
272 virtual bool Monospace() = 0;
275 * Set the right font names.
276 * @param settings The settings to modify.
277 * @param font_name The new font name.
279 virtual void SetFontNames(struct FreeTypeSettings *settings, const char *font_name) = 0;
281 bool FindMissingGlyphs(const char **str);
284 void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = NULL);
286 #endif /* STRINGS_FUNC_H */