OpenTTD Source: src/strings_func.h Source File

OpenTTD Source 14.0-beta3

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"
17 #include "core/strong_typedef_type.hpp"
18 #include "vehicle_type.h"
19
25 inline StringTab GetStringTab(StringID str)
26 {
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;
31 }
32
38 inline uint GetStringIndex(StringID str)
39 {
40 return str - (GetStringTab(str) << TAB_SIZE_BITS);
41 }
42
49 inline StringID MakeStringID(StringTab tab, uint index)
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;
60 }
61
62 std::string GetString(StringID string);
63 const char *GetStringPtr(StringID string);
64
65 uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type);
66 uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type);
67
74 inline int64_t PackVelocity(uint speed, VehicleType type)
75 {
76 /* Vehicle type is a byte, so packed into the top 8 bits of the 64-bit
77 * parameter, although only values from 0-3 are relevant. */
78 return speed | (static_cast<uint64_t>(type) << 56);
79 }
80
81 void SetDParam(size_t n, uint64_t v);
82 void SetDParamMaxValue(size_t n, uint64_t max_value, uint min_count = 0, FontSize size = FS_NORMAL);
83 void SetDParamMaxDigits(size_t n, uint count, FontSize size = FS_NORMAL);
84
85 template <typename T, std::enable_if_t<std::is_base_of<StrongTypedefBase, T>::value, int> = 0>
86 void SetDParam(size_t n, T v)
87 {
88 SetDParam(n, v.base());
89 }
90
91 template <typename T, std::enable_if_t<std::is_base_of<StrongTypedefBase, T>::value, int> = 0>
92 void SetDParamMaxValue(size_t n, T max_value, uint min_count = 0, FontSize size = FS_NORMAL)
93 {
94 SetDParamMaxValue(n, max_value.base(), min_count, size);
95 }
96
97 void SetDParamStr(size_t n, const char *str);
98 void SetDParamStr(size_t n, const std::string &str);
99 void SetDParamStr(size_t n, std::string &&str);
100
101 void CopyInDParam(const std::span<const StringParameterBackup> backup);
102 void CopyOutDParam(std::vector<StringParameterBackup> &backup, size_t num);
103 bool HaveDParamChanged(const std::vector<StringParameterBackup> &backup);
104
105 uint64_t GetDParam(size_t n);
106
107 extern TextDirection _current_text_dir;
108
109 void InitializeLanguagePacks();
110 const char *GetCurrentLanguageIsoCode();
111
115 class MissingGlyphSearcher {
116 public:
118 virtual ~MissingGlyphSearcher() = default;
119
124 virtual std::optional<std::string_view> NextString() = 0;
125
130 virtual FontSize DefaultSize() = 0;
131
135 virtual void Reset() = 0;
136
141 virtual bool Monospace() = 0;
142
149 virtual void SetFontNames(struct FontCacheSettings *settings, const char *font_name, const void *os_data = nullptr) = 0;
150
151 bool FindMissingGlyphs();
152 };
153
154 void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = nullptr);
155
156 #endif /* STRINGS_FUNC_H */
MissingGlyphSearcher::FindMissingGlyphs
bool FindMissingGlyphs()
Check whether there are glyphs missing in the current language.
Definition: strings.cpp:2166
MissingGlyphSearcher::DefaultSize
virtual FontSize DefaultSize()=0
Get the default (font) size of the string.
SetDParamMaxDigits
void SetDParamMaxDigits(size_t n, uint count, FontSize size=FS_NORMAL)
Set DParam n to some number that is suitable for string size computations.
Definition: strings.cpp:143
MissingGlyphSearcher
A searcher for missing glyphs.
Definition: strings_func.h:115
TEXT_TAB_END
@ TEXT_TAB_END
End of language files.
Definition: strings_type.h:38
TEXT_TAB_NEWGRF_START
@ TEXT_TAB_NEWGRF_START
Start of NewGRF supplied strings.
Definition: strings_type.h:40
HaveDParamChanged
bool HaveDParamChanged(const std::vector< StringParameterBackup > &backup)
Checks whether the global string parameters have changed compared to the given backup.
Definition: strings.cpp:194
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition: strings.cpp:327
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
SetDParamStr
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:352
StringTab
StringTab
StringTabs to group StringIDs.
Definition: strings_type.h:28
MissingGlyphSearcher::Reset
virtual void Reset()=0
Reset the search, i.e.
ConvertKmhishSpeedToDisplaySpeed
uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type)
Convert the given km/h-ish speed to the display speed.
Definition: strings.cpp:894
strings_type.h
TAB_SIZE_NEWGRF
static const uint TAB_SIZE_NEWGRF
Number of strings for NewGRFs.
Definition: strings_type.h:52
strong_typedef_type.hpp
CopyOutDParam
void CopyOutDParam(std::vector< StringParameterBackup > &backup, size_t num)
Copy num string parameters from the global string parameter array to the backup.
Definition: strings.cpp:176
GetStringIndex
uint GetStringIndex(StringID str)
Extract the StringIndex from a StringID.
Definition: strings_func.h:38
MissingGlyphSearcher::NextString
virtual std::optional< std::string_view > NextString()=0
Get the next string to search through.
TAB_SIZE
static const uint TAB_SIZE
Number of strings per StringTab.
Definition: strings_type.h:46
bitmath_func.hpp
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:56
FontCacheSettings
Settings for the four different fonts.
Definition: fontcache.h:216
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:203
GetDParam
uint64_t GetDParam(size_t n)
Get the current string parameter at index n from the global string parameter array.
Definition: strings.cpp:114
CheckForMissingGlyphs
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:2268
TAB_SIZE_GAMESCRIPT
static const uint TAB_SIZE_GAMESCRIPT
Number of strings for GameScripts.
Definition: strings_type.h:49
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
MissingGlyphSearcher::Monospace
virtual bool Monospace()=0
Whether to search for a monospace font or not.
MissingGlyphSearcher::SetFontNames
virtual void SetFontNames(struct FontCacheSettings *settings, const char *font_name, const void *os_data=nullptr)=0
Set the right font names.
SetDParam
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings.cpp:104
string_type.h
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
CopyInDParam
void CopyInDParam(const std::span< const StringParameterBackup > backup)
Copy the parameters from the backup into the global string parameter array.
Definition: strings.cpp:159
vehicle_type.h
GetCurrentLanguageIsoCode
const char * GetCurrentLanguageIsoCode()
Get the ISO language code of the currently loaded language.
Definition: strings.cpp:2157
TextDirection
TextDirection
Directions a text can go to.
Definition: strings_type.h:22
SetDParamMaxValue
void SetDParamMaxValue(size_t n, uint64_t max_value, uint min_count=0, FontSize size=FS_NORMAL)
Set DParam n to some number that is suitable for string size computations.
Definition: strings.cpp:127
TEXT_TAB_GAMESCRIPT_START
@ TEXT_TAB_GAMESCRIPT_START
Start of GameScript supplied strings.
Definition: strings_type.h:39
MakeStringID
StringID MakeStringID(StringTab tab, uint index)
Create a StringID.
Definition: strings_func.h:49
PackVelocity
int64_t PackVelocity(uint speed, VehicleType type)
Pack velocity and vehicle type for use with SCC_VELOCITY string parameter.
Definition: strings_func.h:74
InitializeLanguagePacks
void InitializeLanguagePacks()
Make a list of the available language packs.
Definition: strings.cpp:2110
MissingGlyphSearcher::~MissingGlyphSearcher
virtual ~MissingGlyphSearcher()=default
Make sure everything gets destructed right.
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:202
GetStringTab
StringTab GetStringTab(StringID str)
Extract the StringTab from a StringID.
Definition: strings_func.h:25
gfx_type.h
TAB_SIZE_BITS
static const uint TAB_SIZE_BITS
Number of bits for the StringIndex within a StringTab.
Definition: strings_type.h:44
ConvertDisplaySpeedToKmhishSpeed
uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type)
Convert the given display speed to the km/h-ish speed.
Definition: strings.cpp:904