xoreos  0.0.5
language.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cassert>
26 
27 #include "src/common/ustring.h"
30 
31 #include "src/aurora/language.h"
33 
35 
36 namespace Aurora {
37 
39  _currentLanguageText(kLanguageInvalid), _currentLanguageVoice(kLanguageInvalid),
40  _currentGender(kLanguageGenderMale) {
41 
42 }
43 
45 }
46 
48  _langByID.clear();
49  _langByLang.clear();
50 
53 
55 }
56 
58  Declaration declaration;
59 
60  declaration.language = language;
61  declaration.id = id;
62  declaration.encoding = encoding;
63 
64  declaration.encodingLocString = encoding;
65 
66  addLanguage(declaration);
67 }
68 
70  Common::Encoding encodingLocString) {
71  Declaration declaration;
72 
73  declaration.language = language;
74  declaration.id = id;
75  declaration.encoding = encoding;
76 
77  declaration.encodingLocString = encodingLocString;
78 
79  addLanguage(declaration);
80 }
81 
82 void LanguageManager::addLanguage(const Declaration &languageDeclaration) {
83  if (languageDeclaration.id != kLanguageInvalid)
84  _langByID[languageDeclaration.id] = languageDeclaration;
85 
86  if (languageDeclaration.language != kLanguageInvalid)
87  _langByLang[languageDeclaration.language] = languageDeclaration;
88 }
89 
90 void LanguageManager::addLanguages(const Declaration *languageDeclarations, size_t count) {
91  while (count-- > 0)
92  addLanguage(*languageDeclarations++);
93 }
94 
95 std::vector<Language> LanguageManager::getLanguages() const {
96  std::vector<Language> languages;
97 
98  for (LanguageByLanguage::const_iterator l = _langByLang.begin(); l != _langByLang.end(); ++l)
99  languages.push_back(l->first);
100 
101  return languages;
102 }
103 
105  LanguageByLanguage::const_iterator l = _langByLang.find(language);
106  if (l != _langByLang.end())
107  return &l->second;
108 
109  return 0;
110 }
111 
113  LanguageByID::const_iterator l = _langByID.find(id);
114  if (l != _langByID.end())
115  return &l->second;
116 
117  return 0;
118 }
119 
121  const Declaration *l = find(language);
122  if (!l)
123  return kLanguageInvalid;
124 
125  return l->id;
126 }
127 
129  const Declaration *l = find(language);
130  if (!l)
131  return kLanguageInvalid;
132 
133  if (gender == kLanguageGenderCurrent)
134  gender = getCurrentGender();
135 
136  return convertLanguageIDToGendered(l->id, gender);
137 }
138 
140  const Declaration *l = find(languageID);
141  if (!l)
142  return kLanguageInvalid;
143 
144  return l->language;
145 }
146 
148  gender = getLanguageGender(languageID);
149 
150  const Declaration *l = find(convertLanguageIDToUngendered(languageID));
151  if (!l)
152  return kLanguageInvalid;
153 
154  return l->language;
155 }
156 
158  LanguageGender gender;
159  return getLanguage(languageID, gender);
160 }
161 
163  const Declaration *l = find(language);
164  if (!l)
166 
167  return l->encoding;
168 }
169 
171  const Declaration *l = find(language);
172  if (!l)
174 
175  return l->encodingLocString;
176 }
177 
179  setCurrentLanguageText(language);
180  setCurrentLanguageVoice(language);
181 }
182 
183 void LanguageManager::setCurrentLanguage(Language languageText, Language languageVoice) {
184  setCurrentLanguageText(languageText);
185  setCurrentLanguageVoice(languageVoice);
186 }
187 
189  _currentGender = gender;
190 }
191 
193  return _currentLanguageText;
194 }
195 
197  return _currentLanguageVoice;
198 }
199 
201  _currentLanguageText = language;
202 }
203 
205  _currentLanguageVoice = language;
206 }
207 
209  return _currentGender;
210 }
211 
214 }
215 
218 }
219 
221  assert(((size_t) gender) < kLanguageGenderMAX);
222 
223  if (languageID == kLanguageInvalid)
224  return kLanguageInvalid;
225 
226  // In gendered language use, the ID is:
227  // - ID * 2 + 0 for male
228  // - ID * 2 + 1 for female
229 
230  return languageID * 2 + ((size_t) gender);
231 }
232 
234  if (languageID == kLanguageInvalid)
235  return kLanguageInvalid;
236 
237  return languageID / 2;
238 }
239 
241  if (languageID == kLanguageInvalid)
242  return kLanguageGenderMale;
243 
244  return (LanguageGender) (languageID % 2);
245 }
246 
248  if (languageID == kLanguageInvalid)
249  return kLanguageInvalid;
250 
251  return languageID ^ 1;
252 }
253 
255  if (language == kLanguageChinese)
256  return "Chinese";
257 
258  if (((uint32) language >= kLanguageMAX))
259  return "Invalid";
260 
261  return kLanguageNames[language];
262 }
263 
265  if (str.empty())
266  return kLanguageInvalid;
267 
268  Common::UString lowerStr = str.toLower();
269 
270  for (size_t i = 0; i < ARRAYSIZE(kLanguageStrings); i++) {
271  for (size_t j = 0; j < ARRAYSIZE(kLanguageStrings[i].strings); j++) {
272  if (!kLanguageStrings[i].strings[j])
273  break;
274 
275  if (lowerStr == kLanguageStrings[i].strings[j])
276  return kLanguageStrings[i].language;
277  }
278  }
279 
280  return kLanguageInvalid;
281 }
282 
285 
286  output.reserve(stream.size());
287 
288  int state = 0;
289 
290  std::vector<byte> collect;
291  collect.reserve(6);
292 
293  byte color[3];
294 
295  byte b;
296  while (stream.read(&b, 1) == 1) {
297  if (state == 0) {
298  if (b == '<') {
299  collect.push_back(b);
300  state = 1;
301  } else
302  output.writeByte(b);
303 
304  continue;
305  }
306 
307  if (state == 1) {
308  if (b == 'c') {
309  collect.push_back(b);
310  state = 2;
311  } else {
312  output.write(&collect[0], collect.size());
313  output.writeByte(b);
314  collect.clear();
315  state = 0;
316  }
317 
318  continue;
319  }
320 
321  if ((state == 2) || (state == 3) || (state == 4)) {
322  collect.push_back(b);
323  color[state - 2] = b;
324  state++;
325 
326  continue;
327  }
328 
329  if (state == 5) {
330  if (b == '>') {
331  Common::UString c = Common::UString::format("<c%02X%02X%02X%02X>",
332  (uint8) color[0], (uint8) color[1], (uint8) color[2], (uint8) 0xFF);
333 
334  output.writeString(c);
335  collect.clear();
336  state = 0;
337 
338  } else {
339  output.write(&collect[0], collect.size());
340  output.writeByte(b);
341  collect.clear();
342  state = 0;
343  }
344 
345  continue;
346  }
347  }
348 
349  return new Common::MemoryReadStream(output.getData(), output.size(), true);
350 }
351 
352 } // End of namespace Aurora
LanguageGender
Definition: language.h:67
Language _currentLanguageVoice
Definition: language.h:265
void setCurrentLanguageText(Language language)
Change the current language, for text only.
Definition: language.cpp:200
void setCurrentLanguage(Language language)
Change the current language, setting both text and voice to the same language.
Definition: language.cpp:178
A stream that dynamically grows as it&#39;s written to.
const Declaration * find(Language language) const
Definition: language.cpp:104
A class holding an UTF-8 string.
Definition: ustring.h:48
void writeString(const UString &str)
Write the given string to the stream, encoded as UTF-8.
Definition: writestream.cpp:94
uint8_t uint8
Definition: types.h:200
LanguageByLanguage _langByLang
Definition: language.h:262
Implementing the reading stream interfaces for plain memory blocks.
The global language manager.
Definition: language.h:114
#define ARRAYSIZE(x)
Macro which determines the number of entries in a fixed size array.
Definition: util.h:131
Language
Definition: language.h:46
static uint32 convertLanguageIDToGendered(uint32 languageID, LanguageGender gender)
Convert an ungendered language ID to a gendered language ID.
Definition: language.cpp:220
uint32 getLanguageID(Language language) const
Construct the internal language ID for an ungendered use of a language.
Definition: language.cpp:120
static Common::MemoryReadStream * preParseColorCodes(Common::SeekableReadStream &stream)
Pre-parse and fix color codes found in UI and dialogue strings in Aurora games.
Definition: language.cpp:283
LanguageGender getCurrentGender() const
Return the gender modulating the current language.
Definition: language.cpp:208
Common::Encoding getEncodingLocString(Language language) const
Return the encoding used for the given language, for reading an embedded LocString string...
Definition: language.cpp:170
size_t size() const
Return the number of bytes written to this stream in total.
Language getCurrentLanguageText() const
Return the current language for text.
Definition: language.cpp:192
Language getCurrentLanguageVoice() const
Return the current language for voices.
Definition: language.cpp:196
static Common::UString getLanguageName(Language language)
Return the human readable name of a language.
Definition: language.cpp:254
static LanguageGender getLanguageGender(uint32 languageID)
Return the gender of this gendered language ID.
Definition: language.cpp:240
static const LanguageStrings kLanguageStrings[]
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Definition: ustring.cpp:718
Language getLanguage(uint32 languageID) const
Decode the internal language ID for an ungendered use of a language.
Definition: language.cpp:139
Language <-> Language name mappings.
Implementing the writing stream interfaces for memory blocks.
#define DECLARE_SINGLETON(T)
Note that you need to use this macro from the global namespace.
Definition: singleton.h:122
void addLanguages(const Declaration *languageDeclarations, size_t count)
Add several supported language for the current game to the LanguageManager, together with their inter...
Definition: language.cpp:90
Types and functions related to language.
Language _currentLanguageText
Definition: language.h:264
virtual size_t read(void *dataPtr, size_t dataSize)=0
Read data from the stream.
Simple memory based &#39;stream&#39;, which implements the ReadStream interface for a plain memory block...
Definition: memreadstream.h:66
Encoding
Definition: encoding.h:37
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
static uint32 swapLanguageGender(uint32 languageID)
Swap the gender of this gendered language ID.
Definition: language.cpp:247
void setCurrentLanguageVoice(Language language)
Change the current language, for voice only.
Definition: language.cpp:204
void writeByte(byte value)
Definition: writestream.h:88
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
Common::Encoding getEncoding(Language language) const
Return the encoding used for the given language.
Definition: language.cpp:162
void addLanguage(Language language, uint32 id, Common::Encoding encoding)
Add a supported language for the current game to the LanguageManager, together with its internal (ung...
Definition: language.cpp:57
Common::Encoding getCurrentEncodingLocString() const
Return the encoding for the current text language, for reading an embedded LocString string...
Definition: language.cpp:216
static const char *const kLanguageNames[]
Unicode string handling.
std::vector< Language > getLanguages() const
Return all declared supported languages for the current game.
Definition: language.cpp:95
LanguageGender _currentGender
Definition: language.h:266
size_t write(const void *dataPtr, size_t dataSize)
Write data into the stream.
Pseudo value that means the current language gender.
Definition: language.h:72
UString toLower() const
Return a lowercased copy of the string.
Definition: ustring.cpp:481
Common::Encoding getCurrentEncoding() const
Return the encoding for the current text language.
Definition: language.cpp:212
uint32_t uint32
Definition: types.h:204
Pseudo value that means either traditional or simplified Chinese.
Definition: language.h:63
Common::Encoding encodingLocString
Definition: language.h:121
Language getLanguageGendered(uint32 languageID) const
Decode the internal language ID for a gendered use of a language (and ignore the language gender)...
Definition: language.cpp:157
void clear()
Clear all managed languages from the LanguageManager.
Definition: language.cpp:47
static Language parseLanguage(const Common::UString &str)
Parse this string into a language.
Definition: language.cpp:264
LanguageByID _langByID
Definition: language.h:261
Interface for a seekable & readable data stream.
Definition: readstream.h:265
static uint32 convertLanguageIDToUngendered(uint32 languageID)
Convert a gendered language ID to an ungendered language ID.
Definition: language.cpp:233
uint8 byte
Definition: types.h:209
void setCurrentGender(LanguageGender gender)
Change the gender modulating the current language.
Definition: language.cpp:188