4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file newgrf_text.h Header of Action 04 "universal holder" structure and functions */
18 #include "strings_type.h"
19 #include "core/flexarray.h"
20 #include "core/smallvec_type.hpp"
21 #include "table/control_codes.h"
23 /** This character, the thorn ('รพ'), indicates a unicode string to NFO. */
24 static const WChar NFO_UTF8_IDENTIFIER
= 0x00DE;
28 * Element of the linked list.
29 * Each of those elements represent the string,
30 * but according to a different lang.
32 struct GRFText
: FlexArray
<char> {
34 const size_t len
; ///< The length of the stored string, used for copying.
35 char text
[]; ///< The actual (translated) text.
39 * Actually construct the GRFText.
40 * @param text The text to store in this GRFText.
41 * @param len The length of the text to store.
43 GRFText (const char *text
, size_t len
) : len (len
)
45 /* We need to use memcpy instead of strcpy due to
46 * the possibility of "choice lists" and therefore
47 * intermediate string terminators. */
48 memcpy (this->text
, text
, len
);
52 * Allocate and assign a new GRFText with the given text.
53 * As these strings can have string terminations in them, e.g.
54 * due to "choice lists" we (sometimes) cannot rely on detecting
55 * the length by means of strlen. Also, if the length is already
56 * known not scanning the whole string is more efficient.
57 * @param text The text to store in the new GRFText.
58 * @param len The length of the text.
60 static GRFText
*create (const char *text
, size_t len
)
62 return new (len
) GRFText (text
, len
);
66 static GRFText
*create (const char *text
, uint32 grfid
, byte langid
,
69 /** Create a GRFText for a given string. */
70 static GRFText
*create (const char *text
)
72 return create (text
, strlen(text
) + 1);
76 * Create a copy of this GRFText.
77 * @return an exact copy of the given text.
79 GRFText
*clone (void) const
81 return GRFText::create (this->text
, this->len
);
86 StringID
AddGRFString(uint32 grfid
, uint16 stringid
, byte langid
, bool new_scheme
, bool allow_newlines
, const char *text_to_add
, StringID def_string
);
87 StringID
GetGRFStringID(uint32 grfid
, StringID stringid
);
88 const char *GetGRFStringPtr(uint16 stringid
);
89 void CleanUpStrings();
90 void SetCurrentGrfLangID(byte language_id
);
92 char *TranslateTTDPatchCodes (uint32 grfid
, uint8 language_id
,
93 bool allow_newlines
, const char *str
,
94 StringControlCode byte80
= SCC_NEWGRF_PRINT_WORD_STRING_ID
);
96 bool CheckGrfLangID(byte lang_id
, byte grf_version
);
98 void StartTextRefStackUsage(const GRFFile
*grffile
, byte numEntries
, const uint32
*values
= NULL
);
99 void StopTextRefStackUsage();
100 void RewindTextRefStack();
101 bool UsingNewGRFTextStack();
102 struct TextRefStack
*CreateTextRefStackBackup();
103 void RestoreTextRefStackBackup(struct TextRefStack
*backup
);
104 uint
RemapNewGRFStringControlCode (uint scc
, stringb
*buf
, const char **str
, int64
*argv
, uint argv_size
, bool modify_argv
);
106 /** Mapping of language data between a NewGRF and OpenTTD. */
108 /** Mapping between NewGRF and OpenTTD IDs. */
110 byte newgrf_id
; ///< NewGRF's internal ID for a case/gender.
111 byte openttd_id
; ///< OpenTTD's internal ID for a case/gender.
114 /* We need a vector and can't use SmallMap due to the fact that for "setting" a
115 * gender of a string or requesting a case for a substring we want to map from
116 * the NewGRF's internal ID to OpenTTD's ID whereas for the choice lists we map
117 * the genders/cases/plural OpenTTD IDs to the NewGRF's internal IDs. In this
118 * case a NewGRF developer/translator might want a different translation for
119 * both cases. Thus we are basically implementing a multi-map. */
120 SmallVector
<Mapping
, 1> gender_map
; ///< Mapping of NewGRF and OpenTTD IDs for genders.
121 SmallVector
<Mapping
, 1> case_map
; ///< Mapping of NewGRF and OpenTTD IDs for cases.
122 int plural_form
; ///< The plural form used for this language.
124 int GetMapping(int newgrf_id
, bool gender
) const;
125 int GetReverseMapping(int openttd_id
, bool gender
) const;
126 static const LanguageMap
*GetLanguageMap(uint32 grfid
, uint8 language_id
);
129 /** Map of GRFText objects by langid. */
130 struct GRFTextMap
: private std::map
<byte
, GRFText
*> {
131 /** Default constructor. */
132 GRFTextMap() : std::map
<byte
, GRFText
*> ()
136 GRFTextMap (const GRFTextMap
&other
);
140 const GRFText
*get_current (void) const;
143 * Get a C-string from this GRFText-list. If there is a translation
144 * for the current language it is returned, otherwise the default
145 * translation is returned. If there is neither a default nor a
146 * translation for the current language NULL is returned.
148 const char *get_string (void) const
150 const GRFText
*t
= this->get_current();
151 return (t
!= NULL
) ? t
->text
: NULL
;
154 void add (byte langid
, GRFText
*text
);
155 void add (byte langid
, uint32 grfid
, bool allow_newlines
, const char *text
);
158 * Add a GRFText to this list. The text should not contain any
159 * text-codes. The text will be added as a 'default language'-text.
160 * @param text The text to add to the list.
162 void add_default (const char *text
)
164 this->add (0x7F, GRFText::create (text
));
168 #endif /* NEWGRF_TEXT_H */