OpenTTD Source  1.11.2
newgrf_text.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 NEWGRF_TEXT_H
11 #define NEWGRF_TEXT_H
12 
13 #include "string_type.h"
14 #include "strings_type.h"
15 #include "core/smallvec_type.hpp"
16 #include "table/control_codes.h"
17 #include <utility>
18 #include <vector>
19 #include <string>
20 
22 static const WChar NFO_UTF8_IDENTIFIER = 0x00DE;
23 
25 struct GRFText {
26  byte langid;
27  std::string text;
28 };
29 
31 typedef std::vector<GRFText> GRFTextList;
33 typedef std::shared_ptr<GRFTextList> GRFTextWrapper;
34 
35 StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid, bool new_scheme, bool allow_newlines, const char *text_to_add, StringID def_string);
36 StringID GetGRFStringID(uint32 grfid, StringID stringid);
37 const char *GetGRFStringFromGRFText(const GRFTextList &text_list);
38 const char *GetGRFStringFromGRFText(const GRFTextWrapper &text);
39 const char *GetGRFStringPtr(uint16 stringid);
40 void CleanUpStrings();
41 void SetCurrentGrfLangID(byte language_id);
42 std::string TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, bool allow_newlines, const std::string &str, StringControlCode byte80 = SCC_NEWGRF_PRINT_WORD_STRING_ID);
43 void AddGRFTextToList(GRFTextList &list, byte langid, uint32 grfid, bool allow_newlines, const char *text_to_add);
44 void AddGRFTextToList(GRFTextWrapper &list, byte langid, uint32 grfid, bool allow_newlines, const char *text_to_add);
45 void AddGRFTextToList(GRFTextWrapper &list, const char *text_to_add);
46 
47 bool CheckGrfLangID(byte lang_id, byte grf_version);
48 
49 void StartTextRefStackUsage(const struct GRFFile *grffile, byte numEntries, const uint32 *values = nullptr);
51 void RewindTextRefStack();
54 void RestoreTextRefStackBackup(struct TextRefStack *backup);
55 uint RemapNewGRFStringControlCode(uint scc, char *buf_start, char **buff, const char **str, int64 *argv, uint argv_size, bool modify_argv);
56 
58 struct LanguageMap {
60  struct Mapping {
61  byte newgrf_id;
62  byte openttd_id;
63  };
64 
65  /* We need a vector and can't use SmallMap due to the fact that for "setting" a
66  * gender of a string or requesting a case for a substring we want to map from
67  * the NewGRF's internal ID to OpenTTD's ID whereas for the choice lists we map
68  * the genders/cases/plural OpenTTD IDs to the NewGRF's internal IDs. In this
69  * case a NewGRF developer/translator might want a different translation for
70  * both cases. Thus we are basically implementing a multi-map. */
71  std::vector<Mapping> gender_map;
72  std::vector<Mapping> case_map;
74 
75  int GetMapping(int newgrf_id, bool gender) const;
76  int GetReverseMapping(int openttd_id, bool gender) const;
77  static const LanguageMap *GetLanguageMap(uint32 grfid, uint8 language_id);
78 };
79 
80 #endif /* NEWGRF_TEXT_H */
GRFText
A GRF text with associated language ID.
Definition: newgrf_text.h:25
LanguageMap::case_map
std::vector< Mapping > case_map
Mapping of NewGRF and OpenTTD IDs for cases.
Definition: newgrf_text.h:72
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
GRFTextList
std::vector< GRFText > GRFTextList
A GRF text with a list of translations.
Definition: newgrf_text.h:31
LanguageMap::plural_form
int plural_form
The plural form used for this language.
Definition: newgrf_text.h:73
smallvec_type.hpp
AddGRFString
StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid, bool new_scheme, bool allow_newlines, const char *text_to_add, StringID def_string)
Add the new read string into our structure.
Definition: newgrf_text.cpp:552
GetGRFStringFromGRFText
const char * GetGRFStringFromGRFText(const GRFTextList &text_list)
Get a C-string from a GRFText-list.
Definition: newgrf_text.cpp:621
StartTextRefStackUsage
void StartTextRefStackUsage(const GRFFile *grffile, byte numEntries, const uint32 *values)
Start using the TTDP compatible string code parsing.
Definition: newgrf_text.cpp:822
strings_type.h
GRFText::text
std::string text
The actual (translated) text.
Definition: newgrf_text.h:27
control_codes.h
NFO_UTF8_IDENTIFIER
static const WChar NFO_UTF8_IDENTIFIER
This character, the thorn ('รพ'), indicates a unicode string to NFO.
Definition: newgrf_text.h:22
SCC_NEWGRF_PRINT_WORD_STRING_ID
@ SCC_NEWGRF_PRINT_WORD_STRING_ID
81: Read 2 bytes from the stack as String ID
Definition: control_codes.h:129
LanguageMap::GetLanguageMap
static const LanguageMap * GetLanguageMap(uint32 grfid, uint8 language_id)
Get the language map associated with a given NewGRF and language.
Definition: newgrf.cpp:2574
LanguageMap::Mapping::openttd_id
byte openttd_id
OpenTTD's internal ID for a case/gender.
Definition: newgrf_text.h:62
LanguageMap::gender_map
std::vector< Mapping > gender_map
Mapping of NewGRF and OpenTTD IDs for genders.
Definition: newgrf_text.h:71
CleanUpStrings
void CleanUpStrings()
House cleaning.
Definition: newgrf_text.cpp:696
RestoreTextRefStackBackup
void RestoreTextRefStackBackup(struct TextRefStack *backup)
Restore a copy of the text stack to the used stack.
Definition: newgrf_text.cpp:798
string_type.h
UsingNewGRFTextStack
bool UsingNewGRFTextStack()
Check whether the NewGRF text stack is in use.
Definition: newgrf_text.cpp:780
LanguageMap::Mapping
Mapping between NewGRF and OpenTTD IDs.
Definition: newgrf_text.h:60
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
LanguageMap
Mapping of language data between a NewGRF and OpenTTD.
Definition: newgrf_text.h:58
SetCurrentGrfLangID
void SetCurrentGrfLangID(byte language_id)
Equivalence Setter function between game and newgrf langID.
Definition: newgrf_text.cpp:673
LanguageMap::GetMapping
int GetMapping(int newgrf_id, bool gender) const
Get the mapping from the NewGRF supplied ID to OpenTTD's internal ID.
Definition: newgrf_text.cpp:88
CreateTextRefStackBackup
struct TextRefStack * CreateTextRefStackBackup()
Create a backup of the current NewGRF text stack.
Definition: newgrf_text.cpp:789
StringControlCode
StringControlCode
List of string control codes used for string formatting, displaying, and by strgen to generate the la...
Definition: control_codes.h:17
StopTextRefStackUsage
void StopTextRefStackUsage()
Stop using the TTDP compatible string code parsing.
Definition: newgrf_text.cpp:839
AddGRFTextToList
void AddGRFTextToList(GRFTextList &list, byte langid, uint32 grfid, bool allow_newlines, const char *text_to_add)
Add a string to a GRFText list.
Definition: newgrf_text.cpp:517
GRFText::langid
byte langid
The language associated with this GRFText.
Definition: newgrf_text.h:26
TranslateTTDPatchCodes
std::string TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, bool allow_newlines, const std::string &str, StringControlCode byte80=SCC_NEWGRF_PRINT_WORD_STRING_ID)
Translate TTDPatch string codes into something OpenTTD can handle (better).
Definition: newgrf_text.cpp:241
GetGRFStringID
StringID GetGRFStringID(uint32 grfid, StringID stringid)
Returns the index for this stringid associated with its grfID.
Definition: newgrf_text.cpp:602
LanguageMap::GetReverseMapping
int GetReverseMapping(int openttd_id, bool gender) const
Get the mapping from OpenTTD's internal ID to the NewGRF supplied ID.
Definition: newgrf_text.cpp:103
GetGRFStringPtr
const char * GetGRFStringPtr(uint16 stringid)
Get a C-string from a stringid set by a newgrf.
Definition: newgrf_text.cpp:654
GRFTextWrapper
std::shared_ptr< GRFTextList > GRFTextWrapper
Reference counted wrapper around a GRFText pointer.
Definition: newgrf_text.h:33
RemapNewGRFStringControlCode
uint RemapNewGRFStringControlCode(uint scc, char *buf_start, char **buff, const char **str, int64 *argv, uint argv_size, bool modify_argv)
FormatString for NewGRF specific "magic" string control codes.
Definition: newgrf_text.cpp:859
LanguageMap::Mapping::newgrf_id
byte newgrf_id
NewGRF's internal ID for a case/gender.
Definition: newgrf_text.h:61
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
TextRefStack
Definition: newgrf_text.cpp:709