OpenTTD Source 20250716-master-g6b6caa6fa8
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 <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef STRINGS_FUNC_H
11#define STRINGS_FUNC_H
12
13#include "strings_type.h"
14#include "string_type.h"
15#include "gfx_type.h"
16#include "core/bitmath_func.hpp"
18#include "vehicle_type.h"
19
26{
27 StringTab result = (StringTab)(str >> TAB_SIZE_BITS);
28 if (result >= TEXT_TAB_NEWGRF_START) return TEXT_TAB_NEWGRF_START;
30 return result;
31}
32
42
50{
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);
58 }
59 return (tab << TAB_SIZE_BITS) + index.base();
60}
61
66static inline void PrepareArgsForNextRun(std::span<StringParameter> args)
67{
68 for (auto &param : args) param.type = 0;
69}
70
71std::string GetStringWithArgs(StringID string, std::span<StringParameter> args);
72std::string GetString(StringID string);
73std::string_view GetStringPtr(StringID string);
74void AppendStringInPlace(std::string &result, StringID string);
75void AppendStringWithArgsInPlace(std::string &result, StringID string, std::span<StringParameter> params);
76
79
86inline int64_t PackVelocity(uint speed, VehicleType type)
87{
88 /* Vehicle type is a byte, so packed into the top 8 bits of the 64-bit
89 * parameter, although only values from 0-3 are relevant. */
90 return speed | (static_cast<uint64_t>(type) << 56);
91}
92
93uint64_t GetParamMaxValue(uint64_t max_value, uint min_count = 0, FontSize size = FS_NORMAL);
94uint64_t GetParamMaxDigits(uint count, FontSize size = FS_NORMAL);
95
97
99std::string_view GetCurrentLanguageIsoCode();
100std::string_view GetListSeparator();
101std::string_view GetEllipsis();
102
109template <typename... Args>
110auto MakeParameters(Args &&... args)
111{
112 return std::array<StringParameter, sizeof...(args)>({std::forward<StringParameter>(args)...});
113}
114
121template <typename... Args>
122std::string GetString(StringID string, Args &&... args)
123{
124 auto params = MakeParameters(std::forward<Args &&>(args)...);
125 return GetStringWithArgs(string, params);
126}
127
129EncodedString GetEncodedStringWithArgs(StringID str, std::span<const StringParameter> params);
130
138{
139 if (str == INVALID_STRING_ID) return {};
140 return GetEncodedString(str);
141}
142
149template <typename... Args>
150EncodedString GetEncodedString(StringID string, const Args&... args)
151{
152 auto params = MakeParameters(std::forward<const Args&>(args)...);
153 return GetEncodedStringWithArgs(string, params);
154}
155
160public:
162 virtual ~MissingGlyphSearcher() = default;
163
168 virtual std::optional<std::string_view> NextString() = 0;
169
174 virtual FontSize DefaultSize() = 0;
175
179 virtual void Reset() = 0;
180
185 virtual bool Monospace() = 0;
186
193 virtual void SetFontNames(struct FontCacheSettings *settings, std::string_view font_name, const void *os_data = nullptr) = 0;
194
195 bool FindMissingGlyphs();
196};
197
198void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = nullptr);
199
200#endif /* STRINGS_FUNC_H */
Functions related to bit mathematics.
Container for an encoded string, created by GetEncodedString.
A searcher for missing glyphs.
virtual void Reset()=0
Reset the search, i.e.
virtual FontSize DefaultSize()=0
Get the default (font) size of the string.
bool FindMissingGlyphs()
Check whether there are glyphs missing in the current language.
Definition strings.cpp:2279
virtual void SetFontNames(struct FontCacheSettings *settings, std::string_view font_name, const void *os_data=nullptr)=0
Set the right font names.
virtual bool Monospace()=0
Whether to search for a monospace font or not.
virtual std::optional< std::string_view > NextString()=0
Get the next string to search through.
virtual ~MissingGlyphSearcher()=default
Make sure everything gets destructed right.
Concept for unifying the convert through 'base()' behaviour of several 'strong' types.
fluid_settings_t * settings
FluidSynth settings handle.
Types related to the graphics and/or input devices.
FontSize
Available font sizes.
Definition gfx_type.h:250
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:251
Types for strings.
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:337
EncodedString GetEncodedStringWithArgs(StringID str, std::span< const StringParameter > params)
Encode a string with its parameters into an encoded string.
Definition strings.cpp:103
std::string_view GetListSeparator()
Get the list separator string for the current language.
Definition strings.cpp:300
static EncodedString GetEncodedStringIfValid(StringID str)
Encode a string with no parameters into an encoded string, if the string id is valid.
static void PrepareArgsForNextRun(std::span< StringParameter > args)
Prepare the string parameters for the next formatting run, resetting the type information.
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:435
StringTab GetStringTab(StringID str)
Extract the StringTab from a StringID.
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:91
uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type)
Convert the given display speed to the km/h-ish speed.
Definition strings.cpp:1001
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:237
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:425
void CheckForMissingGlyphs(bool base_font=true, MissingGlyphSearcher *search=nullptr)
Check whether the currently loaded language pack uses characters that the currently loaded font does ...
Definition strings.cpp:2364
std::string_view GetCurrentLanguageIsoCode()
Get the ISO language code of the currently loaded language.
Definition strings.cpp:2270
uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type)
Convert the given km/h-ish speed to the display speed.
Definition strings.cpp:991
void InitializeLanguagePacks()
Make a list of the available language packs.
Definition strings.cpp:2222
auto MakeParameters(Args &&... args)
Helper to create the StringParameters with its own buffer with the given parameter values.
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:57
StringID MakeStringID(StringTab tab, StringIndexInTab index)
Create a StringID.
int64_t PackVelocity(uint speed, VehicleType type)
Pack velocity and vehicle type for use with SCC_VELOCITY string parameter.
StringIndexInTab GetStringIndex(StringID str)
Extract the StringIndex from a StringID.
std::string_view GetEllipsis()
Get the ellipsis string for the current language.
Definition strings.cpp:309
uint64_t GetParamMaxDigits(uint count, FontSize size=FS_NORMAL)
Get some number that is suitable for string size computations.
Definition strings.cpp:219
Types related to strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
static const uint TAB_SIZE_NEWGRF
Number of strings for NewGRFs.
static const uint TAB_SIZE_GAMESCRIPT
Number of strings for GameScripts.
static const uint TAB_SIZE_BITS
Number of bits for the StringIndex within a StringTab.
static const uint TAB_SIZE
Number of strings per StringTab.
StringTab
StringTabs to group StringIDs.
@ TEXT_TAB_NEWGRF_START
Start of NewGRF supplied strings.
@ TEXT_TAB_GAMESCRIPT_START
Start of GameScript supplied strings.
@ TEXT_TAB_END
End of language files.
TextDirection
Directions a text can go to.
Settings for the four different fonts.
Definition fontcache.h:205
The data required to format and validate a single parameter of a string.
Templated helper to make a type-safe 'typedef' representing a single POD value.
Types related to vehicles.
VehicleType
Available vehicle types.