OpenTTD Source 20241224-master-gf74b0cf984
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
38inline uint GetStringIndex(StringID str)
39{
40 return str - (GetStringTab(str) << TAB_SIZE_BITS);
41}
42
49inline 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
62std::string GetString(StringID string);
63const char *GetStringPtr(StringID string);
64void AppendStringInPlace(std::string &result, StringID string);
65
68
75inline int64_t PackVelocity(uint speed, VehicleType type)
76{
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);
80}
81
82void SetDParam(size_t n, uint64_t v);
83void SetDParamMaxValue(size_t n, uint64_t max_value, uint min_count = 0, FontSize size = FS_NORMAL);
84void SetDParamMaxDigits(size_t n, uint count, FontSize size = FS_NORMAL);
85
86template <typename T, std::enable_if_t<std::is_base_of<StrongTypedefBase, T>::value, int> = 0>
87void SetDParam(size_t n, T v)
88{
89 SetDParam(n, v.base());
90}
91
92template <typename T, std::enable_if_t<std::is_base_of<StrongTypedefBase, T>::value, int> = 0>
93void SetDParamMaxValue(size_t n, T max_value, uint min_count = 0, FontSize size = FS_NORMAL)
94{
95 SetDParamMaxValue(n, max_value.base(), min_count, size);
96}
97
98void SetDParamStr(size_t n, const char *str);
99void SetDParamStr(size_t n, const std::string &str);
100void SetDParamStr(size_t n, std::string &&str);
101
102void CopyInDParam(const std::span<const StringParameterData> backup);
103void CopyOutDParam(std::vector<StringParameterData> &backup, size_t num);
104bool HaveDParamChanged(const std::span<const StringParameterData> backup);
105
106uint64_t GetDParam(size_t n);
107
109
111const char *GetCurrentLanguageIsoCode();
112std::string_view GetListSeparator();
113
118public:
120 virtual ~MissingGlyphSearcher() = default;
121
126 virtual std::optional<std::string_view> NextString() = 0;
127
132 virtual FontSize DefaultSize() = 0;
133
137 virtual void Reset() = 0;
138
143 virtual bool Monospace() = 0;
144
151 virtual void SetFontNames(struct FontCacheSettings *settings, const char *font_name, const void *os_data = nullptr) = 0;
152
153 bool FindMissingGlyphs();
154};
155
156void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = nullptr);
157
158#endif /* STRINGS_FUNC_H */
Functions related to bit mathematics.
A searcher for missing glyphs.
virtual void Reset()=0
Reset the search, i.e.
virtual void SetFontNames(struct FontCacheSettings *settings, const char *font_name, const void *os_data=nullptr)=0
Set the right font names.
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:2192
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.
fluid_settings_t * settings
FluidSynth settings handle.
Types related to the graphics and/or input devices.
FontSize
Available font sizes.
Definition gfx_type.h:208
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:209
Types for strings.
std::string_view GetListSeparator()
Get the list separator string for the current language.
Definition strings.cpp:229
const char * GetCurrentLanguageIsoCode()
Get the ISO language code of the currently loaded language.
Definition strings.cpp:2183
void AppendStringInPlace(std::string &result, StringID string)
Resolve the given StringID and append in place into an existing std::string with all the associated D...
Definition strings.cpp:345
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
StringTab GetStringTab(StringID str)
Extract the StringTab from a StringID.
uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type)
Convert the given display speed to the km/h-ish speed.
Definition strings.cpp:916
uint64_t GetDParam(size_t n)
Get the current string parameter at index n from the global string parameter array.
Definition strings.cpp:114
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition strings.cpp:333
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:2291
void CopyOutDParam(std::vector< StringParameterData > &backup, size_t num)
Copy num string parameters from the global string parameter array to the backup.
Definition strings.cpp:171
void CopyInDParam(const std::span< const StringParameterData > backup)
Copy the parameters from the backup into the global string parameter array.
Definition strings.cpp:159
uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type)
Convert the given km/h-ish speed to the display speed.
Definition strings.cpp:906
void InitializeLanguagePacks()
Make a list of the available language packs.
Definition strings.cpp:2136
bool HaveDParamChanged(const std::span< const StringParameterData > backup)
Checks whether the global string parameters have changed compared to the given backup.
Definition strings.cpp:184
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
int64_t PackVelocity(uint speed, VehicleType type)
Pack velocity and vehicle type for use with SCC_VELOCITY string parameter.
StringID MakeStringID(StringTab tab, uint index)
Create a StringID.
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
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:371
uint GetStringIndex(StringID str)
Extract the StringIndex from a StringID.
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
Types related to strings.
TextDirection
Directions a text can go to.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
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.
Type (helpers) for making a strong typedef that is a distinct type.
Settings for the four different fonts.
Definition fontcache.h:200
Types related to vehicles.
VehicleType
Available vehicle types.