2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2007-12-21 19:49:27 +00:00
|
|
|
/** @file strings_func.h Functions related to OTTD's strings. */
|
2007-04-04 01:35:16 +00:00
|
|
|
|
2007-12-21 19:49:27 +00:00
|
|
|
#ifndef STRINGS_FUNC_H
|
|
|
|
#define STRINGS_FUNC_H
|
|
|
|
|
|
|
|
#include "strings_type.h"
|
2010-12-09 20:57:52 +00:00
|
|
|
#include "string_type.h"
|
2011-11-19 18:43:00 +00:00
|
|
|
#include "gfx_type.h"
|
2017-02-26 19:39:58 +00:00
|
|
|
#include "core/bitmath_func.hpp"
|
2023-04-08 16:26:13 +00:00
|
|
|
#include "vehicle_type.h"
|
2017-02-26 19:39:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the StringTab from a StringID.
|
|
|
|
* @param str String identifier
|
|
|
|
* @return StringTab from \a str
|
|
|
|
*/
|
2017-02-26 19:40:53 +00:00
|
|
|
static inline StringTab GetStringTab(StringID str)
|
2017-02-26 19:39:58 +00:00
|
|
|
{
|
2017-02-26 19:41:30 +00:00
|
|
|
StringTab result = (StringTab)(str >> TAB_SIZE_BITS);
|
|
|
|
if (result >= TEXT_TAB_NEWGRF_START) return TEXT_TAB_NEWGRF_START;
|
|
|
|
if (result >= TEXT_TAB_GAMESCRIPT_START) return TEXT_TAB_GAMESCRIPT_START;
|
|
|
|
return result;
|
2017-02-26 19:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the StringIndex from a StringID.
|
|
|
|
* @param str String identifier
|
|
|
|
* @return StringIndex from \a str
|
|
|
|
*/
|
|
|
|
static inline uint GetStringIndex(StringID str)
|
|
|
|
{
|
2017-02-26 19:41:30 +00:00
|
|
|
return str - (GetStringTab(str) << TAB_SIZE_BITS);
|
2017-02-26 19:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a StringID
|
|
|
|
* @param tab StringTab
|
|
|
|
* @param index StringIndex
|
|
|
|
* @return StringID composed from \a tab and \a index
|
|
|
|
*/
|
2017-02-26 19:40:53 +00:00
|
|
|
static inline StringID MakeStringID(StringTab tab, uint index)
|
2017-02-26 19:39:58 +00:00
|
|
|
{
|
2017-02-26 19:41:30 +00:00
|
|
|
if (tab == TEXT_TAB_NEWGRF_START) {
|
|
|
|
assert(index < TAB_SIZE_NEWGRF);
|
|
|
|
} else if (tab == TEXT_TAB_GAMESCRIPT_START) {
|
|
|
|
assert(index < TAB_SIZE_GAMESCRIPT);
|
|
|
|
} else {
|
|
|
|
assert(tab < TEXT_TAB_END);
|
|
|
|
assert(index < TAB_SIZE);
|
|
|
|
}
|
|
|
|
return (tab << TAB_SIZE_BITS) + index;
|
2017-02-26 19:39:58 +00:00
|
|
|
}
|
2005-02-06 08:18:00 +00:00
|
|
|
|
2011-03-18 13:34:52 +00:00
|
|
|
class StringParameters {
|
2019-04-10 21:07:06 +00:00
|
|
|
StringParameters *parent; ///< If not nullptr, this instance references data from this parent instance.
|
2011-03-18 13:34:52 +00:00
|
|
|
uint64 *data; ///< Array with the actual data.
|
2019-04-10 21:07:06 +00:00
|
|
|
WChar *type; ///< Array with type information about the data. Can be nullptr when no type information is needed. See #StringControlCode.
|
2011-03-18 13:34:52 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
uint offset; ///< Current offset in the data/type arrays.
|
|
|
|
uint num_param; ///< Length of the data array.
|
|
|
|
|
|
|
|
/** Create a new StringParameters instance. */
|
|
|
|
StringParameters(uint64 *data, uint num_param, WChar *type) :
|
2019-04-10 21:07:06 +00:00
|
|
|
parent(nullptr),
|
2011-03-18 13:34:52 +00:00
|
|
|
data(data),
|
|
|
|
type(type),
|
|
|
|
offset(0),
|
|
|
|
num_param(num_param)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
/** Create a new StringParameters instance. */
|
|
|
|
template <size_t Tnum_param>
|
|
|
|
StringParameters(int64 (&data)[Tnum_param]) :
|
2019-04-10 21:07:06 +00:00
|
|
|
parent(nullptr),
|
2011-03-18 13:34:52 +00:00
|
|
|
data((uint64 *)data),
|
2019-04-10 21:07:06 +00:00
|
|
|
type(nullptr),
|
2011-03-18 13:34:52 +00:00
|
|
|
offset(0),
|
|
|
|
num_param(Tnum_param)
|
|
|
|
{
|
2020-12-27 10:44:22 +00:00
|
|
|
static_assert(sizeof(data[0]) == sizeof(uint64));
|
2011-03-18 13:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new StringParameters instance that can reference part of the data of
|
|
|
|
* the given partent instance.
|
|
|
|
*/
|
|
|
|
StringParameters(StringParameters &parent, uint size) :
|
|
|
|
parent(&parent),
|
|
|
|
data(parent.data + parent.offset),
|
|
|
|
offset(0),
|
|
|
|
num_param(size)
|
|
|
|
{
|
2014-01-12 17:59:43 +00:00
|
|
|
assert(size <= parent.GetDataLeft());
|
2019-04-10 21:07:06 +00:00
|
|
|
if (parent.type == nullptr) {
|
|
|
|
this->type = nullptr;
|
2011-03-18 13:34:52 +00:00
|
|
|
} else {
|
|
|
|
this->type = parent.type + parent.offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~StringParameters()
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->parent != nullptr) {
|
2011-03-18 13:34:52 +00:00
|
|
|
this->parent->offset += this->num_param;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearTypeInformation();
|
|
|
|
|
2012-10-14 15:18:09 +00:00
|
|
|
int64 GetInt64(WChar type = 0);
|
2011-03-18 13:34:52 +00:00
|
|
|
|
|
|
|
/** Read an int32 from the argument array. @see GetInt64. */
|
|
|
|
int32 GetInt32(WChar type = 0)
|
|
|
|
{
|
|
|
|
return (int32)this->GetInt64(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get a pointer to the current element in the data array. */
|
|
|
|
uint64 *GetDataPointer() const
|
|
|
|
{
|
|
|
|
return &this->data[this->offset];
|
|
|
|
}
|
|
|
|
|
2014-01-12 17:59:43 +00:00
|
|
|
/** Return the amount of elements which can still be read. */
|
|
|
|
uint GetDataLeft() const
|
|
|
|
{
|
|
|
|
return this->num_param - this->offset;
|
|
|
|
}
|
|
|
|
|
2011-03-18 13:34:52 +00:00
|
|
|
/** Get a pointer to a specific element in the data array. */
|
|
|
|
uint64 *GetPointerToOffset(uint offset) const
|
|
|
|
{
|
|
|
|
assert(offset < this->num_param);
|
|
|
|
return &this->data[offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Does this instance store information about the type of the parameters. */
|
|
|
|
bool HasTypeInformation() const
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
return this->type != nullptr;
|
2011-03-18 13:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the type of a specific element. */
|
|
|
|
WChar GetTypeAtOffset(uint offset) const
|
|
|
|
{
|
|
|
|
assert(offset < this->num_param);
|
|
|
|
assert(this->HasTypeInformation());
|
|
|
|
return this->type[offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetParam(uint n, uint64 v)
|
|
|
|
{
|
|
|
|
assert(n < this->num_param);
|
|
|
|
this->data[n] = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 GetParam(uint n) const
|
|
|
|
{
|
|
|
|
assert(n < this->num_param);
|
|
|
|
return this->data[n];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
extern StringParameters _global_string_params;
|
|
|
|
|
2021-05-14 16:32:58 +00:00
|
|
|
std::string GetString(StringID string);
|
2023-05-18 16:33:18 +00:00
|
|
|
std::string GetStringWithArgs(StringID string, StringParameters *args);
|
2008-01-29 17:09:00 +00:00
|
|
|
const char *GetStringPtr(StringID string);
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2023-04-08 16:26:13 +00:00
|
|
|
uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type);
|
|
|
|
uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pack velocity and vehicle type for use with SCC_VELOCITY string parameter.
|
|
|
|
* @param speed Display speed for parameter.
|
|
|
|
* @param type Type of vehicle for parameter.
|
|
|
|
* @return Bit-packed velocity and vehicle type, for use with SetDParam().
|
|
|
|
*/
|
|
|
|
static inline int64 PackVelocity(uint speed, VehicleType type)
|
|
|
|
{
|
|
|
|
/* Vehicle type is a byte, so packed into the top 8 bits of the 64-bit
|
|
|
|
* parameter, although only values from 0-3 are relevant. */
|
|
|
|
return speed | (static_cast<uint64>(type) << 56);
|
|
|
|
}
|
2012-02-14 17:03:56 +00:00
|
|
|
|
2009-07-25 07:49:49 +00:00
|
|
|
/**
|
|
|
|
* Set a string parameter \a v at index \a n in a given array \a s.
|
|
|
|
* @param s Array of string parameters.
|
|
|
|
* @param n Index of the string parameter.
|
|
|
|
* @param v Value of the string parameter.
|
|
|
|
*/
|
2007-07-16 09:16:58 +00:00
|
|
|
static inline void SetDParamX(uint64 *s, uint n, uint64 v)
|
|
|
|
{
|
|
|
|
s[n] = v;
|
|
|
|
}
|
|
|
|
|
2009-07-25 07:49:49 +00:00
|
|
|
/**
|
|
|
|
* Set a string parameter \a v at index \a n in the global string parameter array.
|
|
|
|
* @param n Index of the string parameter.
|
|
|
|
* @param v Value of the string parameter.
|
|
|
|
*/
|
2007-07-16 09:16:58 +00:00
|
|
|
static inline void SetDParam(uint n, uint64 v)
|
|
|
|
{
|
2011-03-18 13:34:52 +00:00
|
|
|
_global_string_params.SetParam(n, v);
|
2007-07-16 09:16:58 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 14:33:48 +00:00
|
|
|
void SetDParamMaxValue(uint n, uint64 max_value, uint min_count = 0, FontSize size = FS_NORMAL);
|
|
|
|
void SetDParamMaxDigits(uint n, uint count, FontSize size = FS_NORMAL);
|
2012-12-08 17:18:51 +00:00
|
|
|
|
2007-07-16 09:16:58 +00:00
|
|
|
void SetDParamStr(uint n, const char *str);
|
2021-04-27 18:58:17 +00:00
|
|
|
void SetDParamStr(uint n, const std::string &str);
|
2022-11-05 16:14:10 +00:00
|
|
|
void SetDParamStr(uint n, std::string &&str) = delete; // block passing temporaries to SetDParamStr
|
2007-07-16 09:16:58 +00:00
|
|
|
|
2011-03-18 13:34:52 +00:00
|
|
|
void CopyInDParam(int offs, const uint64 *src, int num);
|
|
|
|
void CopyOutDParam(uint64 *dst, int offs, int num);
|
2011-12-10 16:03:12 +00:00
|
|
|
void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num);
|
2011-03-18 13:34:52 +00:00
|
|
|
|
2009-07-25 07:49:49 +00:00
|
|
|
/**
|
|
|
|
* Get the current string parameter at index \a n from parameter array \a s.
|
|
|
|
* @param s Array of string parameters.
|
|
|
|
* @param n Index of the string parameter.
|
|
|
|
* @return Value of the requested string parameter.
|
|
|
|
*/
|
2007-07-16 09:16:58 +00:00
|
|
|
static inline uint64 GetDParamX(const uint64 *s, uint n)
|
|
|
|
{
|
|
|
|
return s[n];
|
|
|
|
}
|
|
|
|
|
2009-07-25 07:49:49 +00:00
|
|
|
/**
|
|
|
|
* Get the current string parameter at index \a n from the global string parameter array.
|
|
|
|
* @param n Index of the string parameter.
|
|
|
|
* @return Value of the requested string parameter.
|
|
|
|
*/
|
2007-07-16 09:16:58 +00:00
|
|
|
static inline uint64 GetDParam(uint n)
|
|
|
|
{
|
2011-03-18 13:34:52 +00:00
|
|
|
return _global_string_params.GetParam(n);
|
2007-07-16 09:16:58 +00:00
|
|
|
}
|
|
|
|
|
2010-11-13 09:56:25 +00:00
|
|
|
extern TextDirection _current_text_dir; ///< Text direction of the currently selected language
|
2005-02-06 08:18:00 +00:00
|
|
|
|
2007-03-17 22:21:05 +00:00
|
|
|
void InitializeLanguagePacks();
|
2009-10-17 20:34:09 +00:00
|
|
|
const char *GetCurrentLanguageIsoCode();
|
2007-03-17 22:21:05 +00:00
|
|
|
|
2019-04-13 20:53:18 +00:00
|
|
|
bool StringIDSorter(const StringID &a, const StringID &b);
|
2007-06-04 16:07:22 +00:00
|
|
|
|
2011-11-19 18:43:00 +00:00
|
|
|
/**
|
|
|
|
* A searcher for missing glyphs.
|
|
|
|
*/
|
|
|
|
class MissingGlyphSearcher {
|
|
|
|
public:
|
|
|
|
/** Make sure everything gets destructed right. */
|
2023-05-14 21:31:03 +00:00
|
|
|
virtual ~MissingGlyphSearcher() = default;
|
2011-11-19 18:43:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next string to search through.
|
2023-05-09 19:35:50 +00:00
|
|
|
* @return The next string or nullopt if there is none.
|
2011-11-19 18:43:00 +00:00
|
|
|
*/
|
2023-05-09 19:35:50 +00:00
|
|
|
virtual std::optional<std::string_view> NextString() = 0;
|
2011-11-19 18:43:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default (font) size of the string.
|
|
|
|
* @return The font size.
|
|
|
|
*/
|
|
|
|
virtual FontSize DefaultSize() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the search, i.e. begin from the beginning again.
|
|
|
|
*/
|
|
|
|
virtual void Reset() = 0;
|
|
|
|
|
2011-11-20 12:01:42 +00:00
|
|
|
/**
|
|
|
|
* Whether to search for a monospace font or not.
|
|
|
|
* @return True if searching for monospace.
|
|
|
|
*/
|
|
|
|
virtual bool Monospace() = 0;
|
|
|
|
|
2011-11-19 21:02:37 +00:00
|
|
|
/**
|
|
|
|
* Set the right font names.
|
|
|
|
* @param settings The settings to modify.
|
|
|
|
* @param font_name The new font name.
|
2018-11-25 01:02:20 +00:00
|
|
|
* @param os_data Opaque pointer to OS-specific data.
|
2011-11-19 21:02:37 +00:00
|
|
|
*/
|
2022-09-15 17:21:27 +00:00
|
|
|
virtual void SetFontNames(struct FontCacheSettings *settings, const char *font_name, const void *os_data = nullptr) = 0;
|
2011-11-19 21:02:37 +00:00
|
|
|
|
2021-01-12 18:52:30 +00:00
|
|
|
bool FindMissingGlyphs();
|
2011-11-19 18:43:00 +00:00
|
|
|
};
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = nullptr);
|
2007-12-16 18:38:19 +00:00
|
|
|
|
2010-12-22 10:50:32 +00:00
|
|
|
#endif /* STRINGS_FUNC_H */
|