OpenTTD Source: src/strings_func.h Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef STRINGS_FUNC_H
11#define STRINGS_FUNC_H
12
13#include "strings_type.h"
14#include "gfx_type.h"
15#include "vehicle_type.h"
16
22 inline StringTab GetStringTab(StringID str)
23{
24 StringTab result = (StringTab)(str >> TAB_SIZE_BITS);
25 if (result >= TEXT_TAB_NEWGRF_START) return TEXT_TAB_NEWGRF_START;
26 if (result >= TEXT_TAB_GAMESCRIPT_START) return TEXT_TAB_GAMESCRIPT_START;
27 return result;
28}
29
35 inline StringIndexInTab GetStringIndex(StringID str)
36{
37 return StringIndexInTab{str - (GetStringTab(str) << TAB_SIZE_BITS)};
38}
39
46 inline StringID MakeStringID(StringTab tab, StringIndexInTab index)
47{
48 if (tab == TEXT_TAB_NEWGRF_START) {
49 assert(index < TAB_SIZE_NEWGRF);
50 } else if (tab == TEXT_TAB_GAMESCRIPT_START) {
51 assert(index < TAB_SIZE_GAMESCRIPT);
52 } else {
53 assert(tab < TEXT_TAB_END);
54 assert(index < TAB_SIZE);
55 }
56 return (tab << TAB_SIZE_BITS) + index.base();
57}
58
63 static inline void PrepareArgsForNextRun(std::span<StringParameter> args)
64{
65 for (auto &param : args) param.type = 0;
66}
67
68std::string GetStringWithArgs(StringID string, std::span<StringParameter> args);
69std::string GetString(StringID string);
70std::string_view GetStringPtr(StringID string);
71void AppendStringInPlace(std::string &result, StringID string);
72void AppendStringWithArgsInPlace(std::string &result, StringID string, std::span<StringParameter> params);
73
74uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type);
75uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type);
76
83 inline int64_t PackVelocity(uint speed, VehicleType type)
84{
85 /* Vehicle type is a byte, so packed into the top 8 bits of the 64-bit
86 * parameter, although only values from 0-3 are relevant. */
87 return speed | (static_cast<uint64_t>(type) << 56);
88}
89
90uint64_t GetParamMaxValue(uint64_t max_value, uint min_count = 0, FontSize size = FS_NORMAL);
91uint64_t GetParamMaxDigits(uint count, FontSize size = FS_NORMAL);
92
93extern TextDirection _current_text_dir;
94
95void InitializeLanguagePacks();
96std::string_view GetCurrentLanguageIsoCode();
97std::string_view GetListSeparator();
98std::string_view GetEllipsis();
99
106template <typename... Args>
107 auto MakeParameters(Args &&... args)
108{
109 return std::array<StringParameter, sizeof...(args)>({std::forward<StringParameter>(args)...});
110}
111
118template <typename... Args>
119std::string GetString(StringID string, Args &&... args)
120{
121 auto params = MakeParameters(std::forward<Args &&>(args)...);
122 return GetStringWithArgs(string, params);
123}
124
125 EncodedString GetEncodedString(StringID str);
126 EncodedString GetEncodedStringWithArgs(StringID str, std::span<const StringParameter> params);
127
134 static inline EncodedString GetEncodedStringIfValid(StringID str)
135{
136 if (str == INVALID_STRING_ID) return {};
137 return GetEncodedString(str);
138}
139
146template <typename... Args>
147 EncodedString GetEncodedString(StringID string, const Args&... args)
148{
149 auto params = MakeParameters(std::forward<const Args&>(args)...);
150 return GetEncodedStringWithArgs(string, params);
151}
152
156 class MissingGlyphSearcher {
157public:
159 virtual ~MissingGlyphSearcher() = default;
160
165 virtual std::optional<std::string_view> NextString() = 0;
166
171 virtual FontSize DefaultSize() = 0;
172
176 virtual void Reset() = 0;
177
182 virtual bool Monospace() = 0;
183
190 virtual void SetFontNames(struct FontCacheSettings *settings, std::string_view font_name, const void *os_data = nullptr) = 0;
191
192 bool FindMissingGlyphs();
193};
194
195void CheckForMissingGlyphs(MissingGlyphSearcher *search = nullptr);
196
197#endif /* STRINGS_FUNC_H */
EncodedString
Container for an encoded string, created by GetEncodedString.
Definition strings_type.h:98
MissingGlyphSearcher
A searcher for missing glyphs.
Definition strings_func.h:156
MissingGlyphSearcher::Reset
virtual void Reset()=0
Reset the search, i.e.
MissingGlyphSearcher::DefaultSize
virtual FontSize DefaultSize()=0
Get the default (font) size of the string.
MissingGlyphSearcher::FindMissingGlyphs
bool FindMissingGlyphs()
Check whether there are glyphs missing in the current language.
Definition strings.cpp:2306
MissingGlyphSearcher::SetFontNames
virtual void SetFontNames(struct FontCacheSettings *settings, std::string_view font_name, const void *os_data=nullptr)=0
Set the right font names.
MissingGlyphSearcher::Monospace
virtual bool Monospace()=0
Whether to search for a monospace font or not.
MissingGlyphSearcher::NextString
virtual std::optional< std::string_view > NextString()=0
Get the next string to search through.
MissingGlyphSearcher::~MissingGlyphSearcher
virtual ~MissingGlyphSearcher()=default
Make sure everything gets destructed right.
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition fluidsynth.cpp:25
gfx_type.h
Types related to the graphics and/or input devices.
FontSize
FontSize
Available font sizes.
Definition gfx_type.h:248
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:249
GetStringWithArgs
void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index, bool game_script)
Get a parsed string with most special stringcodes replaced by the string parameters.
Definition strings.cpp:336
GetEncodedStringWithArgs
EncodedString GetEncodedStringWithArgs(StringID str, std::span< const StringParameter > params)
Encode a string with its parameters into an encoded string.
Definition strings.cpp:102
GetListSeparator
std::string_view GetListSeparator()
Get the list separator string for the current language.
Definition strings.cpp:299
GetEncodedStringIfValid
static EncodedString GetEncodedStringIfValid(StringID str)
Encode a string with no parameters into an encoded string, if the string id is valid.
Definition strings_func.h:134
PrepareArgsForNextRun
static void PrepareArgsForNextRun(std::span< StringParameter > args)
Prepare the string parameters for the next formatting run, resetting the type information.
Definition strings_func.h:63
AppendStringInPlace
void AppendStringInPlace(std::string &result, StringID string)
Resolve the given StringID and append in place into an existing std::string with formatting but no pa...
Definition strings.cpp:434
GetStringTab
StringTab GetStringTab(StringID str)
Extract the StringTab from a StringID.
Definition strings_func.h:22
GetEncodedString
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:90
ConvertDisplaySpeedToKmhishSpeed
uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type)
Convert the given display speed to the km/h-ish speed.
Definition strings.cpp:1001
GetParamMaxValue
uint64_t GetParamMaxValue(uint64_t max_value, uint min_count=0, FontSize size=FS_NORMAL)
Get some number that is suitable for string size computations.
Definition strings.cpp:236
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
GetCurrentLanguageIsoCode
std::string_view GetCurrentLanguageIsoCode()
Get the ISO language code of the currently loaded language.
Definition strings.cpp:2297
CheckForMissingGlyphs
void CheckForMissingGlyphs(MissingGlyphSearcher *search=nullptr)
Check whether the currently loaded language pack uses characters that the currently loaded font does ...
Definition strings.cpp:2390
ConvertKmhishSpeedToDisplaySpeed
uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type)
Convert the given km/h-ish speed to the display speed.
Definition strings.cpp:991
InitializeLanguagePacks
void InitializeLanguagePacks()
Make a list of the available language packs.
Definition strings.cpp:2249
MakeParameters
auto MakeParameters(Args &&... args)
Helper to create the StringParameters with its own buffer with the given parameter values.
Definition strings_func.h:107
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
MakeStringID
StringID MakeStringID(StringTab tab, StringIndexInTab index)
Create a StringID.
Definition strings_func.h:46
PackVelocity
int64_t PackVelocity(uint speed, VehicleType type)
Pack velocity and vehicle type for use with SCC_VELOCITY string parameter.
Definition strings_func.h:83
GetStringIndex
StringIndexInTab GetStringIndex(StringID str)
Extract the StringIndex from a StringID.
Definition strings_func.h:35
GetEllipsis
std::string_view GetEllipsis()
Get the ellipsis string for the current language.
Definition strings.cpp:308
GetParamMaxDigits
uint64_t GetParamMaxDigits(uint count, FontSize size=FS_NORMAL)
Get some number that is suitable for string size computations.
Definition strings.cpp:218
strings_type.h
Types related to strings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition strings_type.h:20
TAB_SIZE_NEWGRF
static const uint TAB_SIZE_NEWGRF
Number of strings for NewGRFs.
Definition strings_type.h:58
TAB_SIZE_GAMESCRIPT
static const uint TAB_SIZE_GAMESCRIPT
Number of strings for GameScripts.
Definition strings_type.h:55
TAB_SIZE_BITS
static const uint TAB_SIZE_BITS
Number of bits for the StringIndex within a StringTab.
Definition strings_type.h:50
TAB_SIZE
static const uint TAB_SIZE
Number of strings per StringTab.
Definition strings_type.h:52
StringTab
StringTab
StringTabs to group StringIDs.
Definition strings_type.h:31
TEXT_TAB_NEWGRF_START
@ TEXT_TAB_NEWGRF_START
Start of NewGRF supplied strings.
Definition strings_type.h:43
TEXT_TAB_GAMESCRIPT_START
@ TEXT_TAB_GAMESCRIPT_START
Start of GameScript supplied strings.
Definition strings_type.h:42
TEXT_TAB_END
@ TEXT_TAB_END
End of language files.
Definition strings_type.h:41
TextDirection
TextDirection
Directions a text can go to.
Definition strings_type.h:25
FontCacheSettings
Settings for the four different fonts.
Definition fontcache.h:180
StringParameter
The data required to format and validate a single parameter of a string.
Definition strings_type.h:78
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:130
vehicle_type.h
Types related to vehicles.
VehicleType
VehicleType
Available vehicle types.
Definition vehicle_type.h:22