OpenTTD Source  20240915-master-g3784a3d3d6
strings_func.h File Reference
#include "strings_type.h"
#include "string_type.h"
#include "gfx_type.h"
#include "core/bitmath_func.hpp"
#include "core/strong_typedef_type.hpp"
#include "vehicle_type.h"

Go to the source code of this file.

Data Structures

class  MissingGlyphSearcher
 A searcher for missing glyphs. More...
 

Functions

StringTab GetStringTab (StringID str)
 Extract the StringTab from a StringID. More...
 
uint GetStringIndex (StringID str)
 Extract the StringIndex from a StringID. More...
 
StringID MakeStringID (StringTab tab, uint index)
 Create a StringID. More...
 
std::string GetString (StringID string)
 Resolve the given StringID into a std::string with all the associated DParam lookups and formatting. More...
 
const char * GetStringPtr (StringID string)
 
uint ConvertKmhishSpeedToDisplaySpeed (uint speed, VehicleType type)
 Convert the given km/h-ish speed to the display speed. More...
 
uint ConvertDisplaySpeedToKmhishSpeed (uint speed, VehicleType type)
 Convert the given display speed to the km/h-ish speed. More...
 
int64_t PackVelocity (uint speed, VehicleType type)
 Pack velocity and vehicle type for use with SCC_VELOCITY string parameter. More...
 
void SetDParam (size_t n, uint64_t v)
 Set a string parameter v at index n in the global string parameter array. More...
 
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. More...
 
void SetDParamMaxDigits (size_t n, uint count, FontSize size=FS_NORMAL)
 Set DParam n to some number that is suitable for string size computations. More...
 
template<typename T , std::enable_if_t< std::is_base_of< StrongTypedefBase, T >::value, int > = 0>
void SetDParam (size_t n, T v)
 
template<typename T , std::enable_if_t< std::is_base_of< StrongTypedefBase, T >::value, int > = 0>
void SetDParamMaxValue (size_t n, T max_value, uint min_count=0, FontSize size=FS_NORMAL)
 
void SetDParamStr (size_t n, const char *str)
 This function is used to "bind" a C string to a OpenTTD dparam slot. More...
 
void SetDParamStr (size_t n, const std::string &str)
 This function is used to "bind" the C string of a std::string to a OpenTTD dparam slot. More...
 
void SetDParamStr (size_t n, std::string &&str)
 This function is used to "bind" the std::string to a OpenTTD dparam slot. More...
 
void CopyInDParam (const std::span< const StringParameterData > backup)
 Copy the parameters from the backup into the global string parameter array. More...
 
void CopyOutDParam (std::vector< StringParameterData > &backup, size_t num)
 Copy num string parameters from the global string parameter array to the backup. More...
 
bool HaveDParamChanged (const std::span< const StringParameterData > backup)
 Checks whether the global string parameters have changed compared to the given backup. More...
 
uint64_t GetDParam (size_t n)
 Get the current string parameter at index n from the global string parameter array. More...
 
void InitializeLanguagePacks ()
 Make a list of the available language packs. More...
 
const char * GetCurrentLanguageIsoCode ()
 Get the ISO language code of the currently loaded language. More...
 
void CheckForMissingGlyphs (bool base_font=true, MissingGlyphSearcher *search=nullptr)
 Check whether the currently loaded language pack uses characters that the currently loaded font does not support. More...
 

Variables

TextDirection _current_text_dir
 Text direction of the currently selected language.
 

Detailed Description

Functions related to OTTD's strings.

Definition in file strings_func.h.

Function Documentation

◆ CheckForMissingGlyphs()

void CheckForMissingGlyphs ( bool  base_font,
MissingGlyphSearcher searcher 
)

Check whether the currently loaded language pack uses characters that the currently loaded font does not support.

If this is the case an error message will be shown in English. The error message will not be localized because that would mean it might use characters that are not in the font, which is the whole reason this check has been added.

Parameters
base_fontWhether to look at the base font as well.
searcherThe methods to use to search for strings to check. If nullptr the loaded language pack searcher is used.

Definition at line 2249 of file strings.cpp.

References MissingGlyphSearcher::FindMissingGlyphs().

Referenced by TextfileWindow::LoadText().

◆ ConvertDisplaySpeedToKmhishSpeed()

uint ConvertDisplaySpeedToKmhishSpeed ( uint  speed,
VehicleType  type 
)

Convert the given display speed to the km/h-ish speed.

Parameters
speedthe speed to convert
Returns
the converted speed.

Definition at line 889 of file strings.cpp.

References Units::c, UnitConversion::FromDisplay(), and GetVelocityUnits().

◆ ConvertKmhishSpeedToDisplaySpeed()

uint ConvertKmhishSpeedToDisplaySpeed ( uint  speed,
VehicleType  type 
)

Convert the given km/h-ish speed to the display speed.

Parameters
speedthe speed to convert
Returns
the converted speed.

Definition at line 879 of file strings.cpp.

References Units::c, GetVelocityUnits(), and UnitConversion::ToDisplay().

◆ CopyInDParam()

void CopyInDParam ( const std::span< const StringParameterData >  backup)

Copy the parameters from the backup into the global string parameter array.

Parameters
backupThe backup to copy from.

Definition at line 159 of file strings.cpp.

◆ CopyOutDParam()

void CopyOutDParam ( std::vector< StringParameterData > &  backup,
size_t  num 
)

Copy num string parameters from the global string parameter array to the backup.

Parameters
backupThe backup to write to.
numNumber of string parameters to copy.

Definition at line 171 of file strings.cpp.

Referenced by NewsItem::NewsItem().

◆ GetCurrentLanguageIsoCode()

const char* GetCurrentLanguageIsoCode ( )

Get the ISO language code of the currently loaded language.

Returns
the ISO code.

Definition at line 2141 of file strings.cpp.

◆ GetDParam()

uint64_t GetDParam ( size_t  n)

Get the current string parameter at index n from the global string parameter array.

Parameters
nIndex of the string parameter.
Returns
Value of the requested string parameter.

Definition at line 114 of file strings.cpp.

◆ GetString()

◆ GetStringIndex()

uint GetStringIndex ( StringID  str)
inline

Extract the StringIndex from a StringID.

Parameters
strString identifier
Returns
StringIndex from str

Definition at line 38 of file strings_func.h.

References GetStringTab(), and TAB_SIZE_BITS.

Referenced by GetStringWithArgs().

◆ GetStringTab()

StringTab GetStringTab ( StringID  str)
inline

Extract the StringTab from a StringID.

Parameters
strString identifier
Returns
StringTab from str

Definition at line 25 of file strings_func.h.

Referenced by CopyFromOldName(), GetStringIndex(), and GetStringWithArgs().

◆ HaveDParamChanged()

bool HaveDParamChanged ( const std::span< const StringParameterData >  backup)

Checks whether the global string parameters have changed compared to the given backup.

Parameters
backupThe backup to check against.
Returns
True when the parameters have changed, otherwise false.

Definition at line 184 of file strings.cpp.

◆ InitializeLanguagePacks()

void InitializeLanguagePacks ( )

Make a list of the available language packs.

Put the data in _languages list.

< Matching the language in the configuration file or the current locale

< Using pt_PT for pt_BR locale when pt_BR is not available

< Fallback when no locale-matching language has been found

Definition at line 2094 of file strings.cpp.

◆ MakeStringID()

StringID MakeStringID ( StringTab  tab,
uint  index 
)
inline

Create a StringID.

Parameters
tabStringTab
indexStringIndex
Returns
StringID composed from tab and index

Definition at line 49 of file strings_func.h.

References TAB_SIZE, TAB_SIZE_BITS, TAB_SIZE_GAMESCRIPT, TAB_SIZE_NEWGRF, TEXT_TAB_END, TEXT_TAB_GAMESCRIPT_START, and TEXT_TAB_NEWGRF_START.

◆ PackVelocity()

int64_t PackVelocity ( uint  speed,
VehicleType  type 
)
inline

Pack velocity and vehicle type for use with SCC_VELOCITY string parameter.

Parameters
speedDisplay speed for parameter.
typeType of vehicle for parameter.
Returns
Bit-packed velocity and vehicle type, for use with SetDParam().

Definition at line 74 of file strings_func.h.

Referenced by BuildBridgeWindow::GetBridgeSelectString().

◆ SetDParam()

void SetDParam ( size_t  n,
uint64_t  v 
)

Set a string parameter v at index n in the global string parameter array.

Parameters
nIndex of the string parameter.
vValue of the string parameter.

Definition at line 104 of file strings.cpp.

Referenced by AdvertiseIndustryOpening(), AgeVehicle(), AircraftHandleDestTooFar(), Subsidy::AwardTo(), BuildReplacementVehicle(), CheckCompanyHasMoney(), ClientNetworkGameSocketHandler::CheckConnection(), CheckEngines(), CheckforTownRating(), CheckIndustries(), CheckTownRoadTypes(), CheckTrainCollision(), CheckTrainsLengths(), CmdCompanyCtrl(), CmdGiveMoney(), CmdIncreaseLoan(), CompanyCheckBankrupt(), CompanyNewsInformation::CompanyNewsInformation(), CrashAirplane(), StationViewWindow::DrawAcceptedCargo(), TimetableWindow::DrawArrivalDeparturePanel(), StationViewWindow::DrawCargoRatings(), DrawPrice(), TownAuthorityWindow::DrawRatings(), BuildVehicleWindow::FilterByText(), FloodVehicle(), GenerateDefaultSaveName(), BuildBridgeWindow::GetBridgeSelectString(), RefitWindow::GetCapacityString(), IndustryDirectoryWindow::GetIndustryString(), BaseSet< GraphicsSet, MAX_GFT, true >::GetListLabel(), BaseGraphWindow::GetYLabelWidth(), GRFLoadConfig(), Vehicle::HandlePathfindingResult(), CommandHelperBase::InternalExecuteProcessResult(), BuildIndustryWindow::MakeCargoListString(), MakeScreenshotWithConfirm(), NetworkHandlePauseChange(), NewVehicleAvailable(), SelectCompanyManagerFaceWindow::OnInit(), GameSettingsWindow::OnPaint(), SelectGameWindow::OnResize(), CompanyWindow::OnResize(), StoryBookWindow::RefreshSelectedPage(), RoadVehArrivesAt(), SaveHighScoreValueNetwork(), ServerNetworkAdminSocketHandler::SendCompanyInfo(), ServerNetworkAdminSocketHandler::SendCompanyUpdate(), SetDParamMaxDigits(), SetDParamsForOwnedBy(), SelectCompanyManagerFaceWindow::SetFaceStringParameters(), NIHelper::SetObjectAtStringParameters(), NIHelper::SetSimpleStringParameters(), SettingsValueAbsolute(), SetupSubsidyDecodeParam(), IntSettingDesc::SetValueDParams(), ShipArrivesAt(), ShowNewGrfVehicleError(), ShowRejectOrAcceptNews(), ToolbarScenDatePanel(), TownActionRoadRebuild(), TrainEnterStation(), UpdateFillingPercent(), SettingEntry::UpdateFilterState(), Sign::UpdateVirtCoord(), Waypoint::UpdateVirtCoord(), and Town::UpdateVirtCoord().

◆ SetDParamMaxDigits()

void SetDParamMaxDigits ( size_t  n,
uint  count,
FontSize  size 
)

Set DParam n to some number that is suitable for string size computations.

Parameters
nIndex of the string parameter.
countNumber of digits which shall be displayable.
sizeFont of the number

Definition at line 143 of file strings.cpp.

References GetBroadestDigit(), and SetDParam().

Referenced by GetUnitNumberWidth(), and SetDParamMaxValue().

◆ SetDParamMaxValue()

void SetDParamMaxValue ( size_t  n,
uint64_t  max_value,
uint  min_count,
FontSize  size 
)

Set DParam n to some number that is suitable for string size computations.

Parameters
nIndex of the string parameter.
max_valueThe biggest value which shall be displayed. For the result only the number of digits of max_value matter.
min_countMinimum number of digits independent of max.
sizeFont of the number

Definition at line 127 of file strings.cpp.

References SetDParamMaxDigits().

Referenced by TimetableWindow::DrawTimetablePanel().

◆ SetDParamStr() [1/3]

◆ SetDParamStr() [2/3]

void SetDParamStr ( size_t  n,
const std::string &  str 
)

This function is used to "bind" the C string of a std::string to a OpenTTD dparam slot.

The caller has to ensure that the std::string reference remains valid while the string is shown.

Parameters
nslot of the string
strstring to bind

Definition at line 355 of file strings.cpp.

◆ SetDParamStr() [3/3]

void SetDParamStr ( size_t  n,
std::string &&  str 
)

This function is used to "bind" the std::string to a OpenTTD dparam slot.

Contrary to the other SetDParamStr functions, this moves the string into the parameter slot.

Parameters
nslot of the string
strstring to bind

Definition at line 367 of file strings.cpp.