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/>.
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"
16 #include "core/bitmath_func.hpp"
17 #include "core/strong_typedef_type.hpp"
18 #include "vehicle_type.h"
21 * Extract the StringTab from a StringID.
22 * @param str String identifier
23 * @return StringTab from \a str
25 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 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 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 std::string
GetString(StringID string
);
63 const char *GetStringPtr(StringID string
);
64 void AppendStringInPlace(std::string
&result
, StringID string
);
66 uint
ConvertKmhishSpeedToDisplaySpeed(uint speed
, VehicleType type
);
67 uint
ConvertDisplaySpeedToKmhishSpeed(uint speed
, VehicleType type
);
70 * Pack velocity and vehicle type for use with SCC_VELOCITY string parameter.
71 * @param speed Display speed for parameter.
72 * @param type Type of vehicle for parameter.
73 * @return Bit-packed velocity and vehicle type, for use with SetDParam().
75 inline int64_t PackVelocity(uint speed
, VehicleType type
)
77 /* Vehicle type is a byte, so packed into the top 8 bits of the 64-bit
78 * parameter, although only values from 0-3 are relevant. */
79 return speed
| (static_cast<uint64_t>(type
) << 56);
82 void SetDParam(size_t n
, uint64_t v
);
83 void SetDParamMaxValue(size_t n
, uint64_t max_value
, uint min_count
= 0, FontSize size
= FS_NORMAL
);
84 void SetDParamMaxDigits(size_t n
, uint count
, FontSize size
= FS_NORMAL
);
86 template <typename T
, std::enable_if_t
<std::is_base_of
<StrongTypedefBase
, T
>::value
, int> = 0>
87 void SetDParam(size_t n
, T v
)
89 SetDParam(n
, v
.base());
92 template <typename T
, std::enable_if_t
<std::is_base_of
<StrongTypedefBase
, T
>::value
, int> = 0>
93 void SetDParamMaxValue(size_t n
, T max_value
, uint min_count
= 0, FontSize size
= FS_NORMAL
)
95 SetDParamMaxValue(n
, max_value
.base(), min_count
, size
);
98 void SetDParamStr(size_t n
, const char *str
);
99 void SetDParamStr(size_t n
, const std::string
&str
);
100 void SetDParamStr(size_t n
, std::string
&&str
);
102 void CopyInDParam(const std::span
<const StringParameterData
> backup
);
103 void CopyOutDParam(std::vector
<StringParameterData
> &backup
, size_t num
);
104 bool HaveDParamChanged(const std::span
<const StringParameterData
> backup
);
106 uint64_t GetDParam(size_t n
);
108 extern TextDirection _current_text_dir
; ///< Text direction of the currently selected language
110 void InitializeLanguagePacks();
111 const char *GetCurrentLanguageIsoCode();
114 * A searcher for missing glyphs.
116 class MissingGlyphSearcher
{
118 /** Make sure everything gets destructed right. */
119 virtual ~MissingGlyphSearcher() = default;
122 * Get the next string to search through.
123 * @return The next string or nullopt if there is none.
125 virtual std::optional
<std::string_view
> NextString() = 0;
128 * Get the default (font) size of the string.
129 * @return The font size.
131 virtual FontSize
DefaultSize() = 0;
134 * Reset the search, i.e. begin from the beginning again.
136 virtual void Reset() = 0;
139 * Whether to search for a monospace font or not.
140 * @return True if searching for monospace.
142 virtual bool Monospace() = 0;
145 * Set the right font names.
146 * @param settings The settings to modify.
147 * @param font_name The new font name.
148 * @param os_data Opaque pointer to OS-specific data.
150 virtual void SetFontNames(struct FontCacheSettings
*settings
, const char *font_name
, const void *os_data
= nullptr) = 0;
152 bool FindMissingGlyphs();
155 void CheckForMissingGlyphs(bool base_font
= true, MissingGlyphSearcher
*search
= nullptr);
157 #endif /* STRINGS_FUNC_H */