1 /* $Id: strings_func.h 26238 2014-01-12 17:59:43Z frosch $ */
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/>.
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"
18 #include "core/bitmath_func.hpp"
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
;
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
);
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
);
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 nullptr, 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 nullptr when no type information is needed. See #StringControlCode.
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
) :
80 /** Create a new StringParameters instance. */
81 template <size_t Tnum_param
>
82 StringParameters(int64 (&data
)[Tnum_param
]) :
89 assert_compile(sizeof(data
[0]) == sizeof(uint64
));
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
) :
98 data(parent
.data
+ parent
.offset
),
102 assert(size
<= parent
.GetDataLeft());
103 if (parent
.type
== nullptr) {
104 this->type
= nullptr;
106 this->type
= parent
.type
+ parent
.offset
;
112 if (this->parent
!= nullptr) {
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
!= nullptr;
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
);
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
)
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
);
215 void MakeCamelCase(char line
[]);
218 * Get the current string parameter at index \a n from parameter array \a s.
219 * @param s Array of string parameters.
220 * @param n Index of the string parameter.
221 * @return Value of the requested string parameter.
223 static inline uint64
GetDParamX(const uint64
*s
, uint n
)
229 * Get the current string parameter at index \a n from the global string parameter array.
230 * @param n Index of the string parameter.
231 * @return Value of the requested string parameter.
233 static inline uint64
GetDParam(uint n
)
235 return _global_string_params
.GetParam(n
);
238 extern TextDirection _current_text_dir
; ///< Text direction of the currently selected language
240 void InitializeLanguagePacks();
241 const char *GetCurrentLanguageIsoCode();
243 int CDECL
StringIDSorter(const StringID
*a
, const StringID
*b
);
246 * A searcher for missing glyphs.
248 class MissingGlyphSearcher
{
250 /** Make sure everything gets destructed right. */
251 virtual ~MissingGlyphSearcher() {}
254 * Get the next string to search through.
255 * @return The next string or nullptr if there is none.
257 virtual const char *NextString() = 0;
260 * Get the default (font) size of the string.
261 * @return The font size.
263 virtual FontSize
DefaultSize() = 0;
266 * Reset the search, i.e. begin from the beginning again.
268 virtual void Reset() = 0;
271 * Whether to search for a monospace font or not.
272 * @return True if searching for monospace.
274 virtual bool Monospace() = 0;
277 * Set the right font names.
278 * @param settings The settings to modify.
279 * @param font_name The new font name.
281 virtual void SetFontNames(struct FreeTypeSettings
*settings
, const char *font_name
) = 0;
283 bool FindMissingGlyphs(const char **str
);
286 void CheckForMissingGlyphs(bool base_font
= true, MissingGlyphSearcher
*search
= nullptr);
288 #endif /* STRINGS_FUNC_H */