]> git.lyx.org Git - lyx.git/blob - src/Language.cpp
Remove Language::fontenc()
[lyx.git] / src / Language.cpp
1 /**
2  * \file Language.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Jürgen Spitzmüller
9  * \author Dekel Tsur
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "Language.h"
17
18 #include "Encoding.h"
19 #include "LaTeXFonts.h"
20 #include "Lexer.h"
21 #include "LyXRC.h"
22
23 #include "support/debug.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26 #include "support/lassert.h"
27 #include "support/lstrings.h"
28 #include "support/Messages.h"
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34
35 Languages languages;
36 Language const * ignore_language = 0;
37 Language const * default_language = 0;
38 Language const * latex_language = 0;
39 Language const * reset_language = 0;
40
41
42 bool Language::isPolyglossiaExclusive() const
43 {
44         return babel().empty() && !polyglossia().empty() && requires().empty();
45 }
46
47
48 bool Language::isBabelExclusive() const
49 {
50         return !babel().empty() && polyglossia().empty() && requires().empty();
51 }
52
53
54 docstring const Language::translateLayout(string const & m) const
55 {
56         if (m.empty())
57                 return docstring();
58
59         if (!isAscii(m)) {
60                 lyxerr << "Warning: not translating `" << m
61                        << "' because it is not pure ASCII.\n";
62                 return from_utf8(m);
63         }
64
65         TranslationMap::const_iterator it = layoutTranslations_.find(m);
66         if (it != layoutTranslations_.end())
67                 return it->second;
68
69         docstring t = from_ascii(m);
70         cleanTranslation(t);
71         return t;
72 }
73
74
75 string Language::fontenc(BufferParams const & params) const
76 {
77         // Determine optimal font encoding
78         // We check whether the used rm font supports an encoding our language supports
79         for (auto & fe : fontenc_) {
80                 LaTeXFont const & lf = theLaTeXFonts().getLaTeXFont(from_ascii(params.fontsRoman()));
81                 // ASCII means: support all T* encodings plus OT1
82                 if (fe == "ASCII") {
83                         vector<string> const lfe = lf.fontencs();
84                         for (auto & afe : lfe) {
85                                 if (afe == "OT1" || prefixIs(afe, "T"))
86                                         // we found a suitable one; return that.
87                                         return afe;
88                         }
89                 }
90                 // For other encodings, just check whether the font supports it
91                 if (lf.hasFontenc(fe))
92                         return fe;
93         }
94         // We did not find a suitable one; just take the first in the list,
95         // the priorized one (which is "T1" for ASCII).
96         return fontenc_.front() == "ASCII" ? "T1" : fontenc_.front();
97 }
98
99
100 bool Language::readLanguage(Lexer & lex)
101 {
102         enum LanguageTags {
103                 LA_AS_BABELOPTS = 1,
104                 LA_BABELNAME,
105                 LA_ENCODING,
106                 LA_END,
107                 LA_FONTENC,
108                 LA_GUINAME,
109                 LA_HAS_GUI_SUPPORT,
110                 LA_INTERNAL_ENC,
111                 LA_LANG_CODE,
112                 LA_LANG_VARIETY,
113                 LA_POLYGLOSSIANAME,
114                 LA_POLYGLOSSIAOPTS,
115                 LA_POSTBABELPREAMBLE,
116                 LA_PREBABELPREAMBLE,
117                 LA_PROVIDES,
118                 LA_REQUIRES,
119                 LA_QUOTESTYLE,
120                 LA_RTL
121         };
122
123         // Keep these sorted alphabetically!
124         LexerKeyword languageTags[] = {
125                 { "asbabeloptions",       LA_AS_BABELOPTS },
126                 { "babelname",            LA_BABELNAME },
127                 { "encoding",             LA_ENCODING },
128                 { "end",                  LA_END },
129                 { "fontencoding",         LA_FONTENC },
130                 { "guiname",              LA_GUINAME },
131                 { "hasguisupport",        LA_HAS_GUI_SUPPORT },
132                 { "internalencoding",     LA_INTERNAL_ENC },
133                 { "langcode",             LA_LANG_CODE },
134                 { "langvariety",          LA_LANG_VARIETY },
135                 { "polyglossianame",      LA_POLYGLOSSIANAME },
136                 { "polyglossiaopts",      LA_POLYGLOSSIAOPTS },
137                 { "postbabelpreamble",    LA_POSTBABELPREAMBLE },
138                 { "prebabelpreamble",     LA_PREBABELPREAMBLE },
139                 { "provides",             LA_PROVIDES },
140                 { "quotestyle",           LA_QUOTESTYLE },
141                 { "requires",             LA_REQUIRES },
142                 { "rtl",                  LA_RTL }
143         };
144
145         bool error = false;
146         bool finished = false;
147         lex.pushTable(languageTags);
148         // parse style section
149         while (!finished && lex.isOK() && !error) {
150                 int le = lex.lex();
151                 // See comment in LyXRC.cpp.
152                 switch (le) {
153                 case Lexer::LEX_FEOF:
154                         continue;
155
156                 case Lexer::LEX_UNDEF: // parse error
157                         lex.printError("Unknown language tag `$$Token'");
158                         error = true;
159                         continue;
160
161                 default:
162                         break;
163                 }
164                 switch (static_cast<LanguageTags>(le)) {
165                 case LA_END: // end of structure
166                         finished = true;
167                         break;
168                 case LA_AS_BABELOPTS:
169                         lex >> as_babel_options_;
170                         break;
171                 case LA_BABELNAME:
172                         lex >> babel_;
173                         break;
174                 case LA_POLYGLOSSIANAME:
175                         lex >> polyglossia_name_;
176                         break;
177                 case LA_POLYGLOSSIAOPTS:
178                         lex >> polyglossia_opts_;
179                         break;
180                 case LA_QUOTESTYLE:
181                         lex >> quote_style_;
182                         break;
183                 case LA_ENCODING:
184                         lex >> encodingStr_;
185                         break;
186                 case LA_FONTENC: {
187                         lex.eatLine();
188                         vector<string> const fe =
189                                 getVectorFromString(lex.getString(true), "|");
190                         fontenc_.insert(fontenc_.end(), fe.begin(), fe.end());
191                         break;
192                 }
193                 case LA_GUINAME:
194                         lex >> display_;
195                         break;
196                 case LA_HAS_GUI_SUPPORT:
197                         lex >> has_gui_support_;
198                         break;
199                 case LA_INTERNAL_ENC:
200                         lex >> internal_enc_;
201                         break;
202                 case LA_LANG_CODE:
203                         lex >> code_;
204                         break;
205                 case LA_LANG_VARIETY:
206                         lex >> variety_;
207                         break;
208                 case LA_POSTBABELPREAMBLE:
209                         babel_postsettings_ =
210                                 lex.getLongString(from_ascii("EndPostBabelPreamble"));
211                         break;
212                 case LA_PREBABELPREAMBLE:
213                         babel_presettings_ =
214                                 lex.getLongString(from_ascii("EndPreBabelPreamble"));
215                         break;
216                 case LA_REQUIRES:
217                         lex >> requires_;
218                         break;
219                 case LA_PROVIDES:
220                         lex >> provides_;
221                         break;
222                 case LA_RTL:
223                         lex >> rightToLeft_;
224                         break;
225                 }
226         }
227         lex.popTable();
228         return finished && !error;
229 }
230
231
232 bool Language::read(Lexer & lex)
233 {
234         as_babel_options_ = 0;
235         encoding_ = 0;
236         internal_enc_ = 0;
237         rightToLeft_ = 0;
238
239         if (!lex.next()) {
240                 lex.printError("No name given for language: `$$Token'.");
241                 return false;
242         }
243
244         lang_ = lex.getString();
245         LYXERR(Debug::INFO, "Reading language " << lang_);
246         if (!readLanguage(lex)) {
247                 LYXERR0("Error parsing language `" << lang_ << '\'');
248                 return false;
249         }
250
251         encoding_ = encodings.fromLyXName(encodingStr_);
252         if (!encoding_ && !encodingStr_.empty()) {
253                 encoding_ = encodings.fromLyXName("iso8859-1");
254                 LYXERR0("Unknown encoding " << encodingStr_);
255         }
256         if (fontenc_.empty())
257                 fontenc_.push_back("ASCII");
258         return true;
259 }
260
261
262 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
263 {
264         TranslationMap::const_iterator const end = trans.end();
265         for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
266                 if (replace
267                         || layoutTranslations_.find(it->first) == layoutTranslations_.end())
268                         layoutTranslations_[it->first] = it->second;
269         }
270 }
271
272
273 void Languages::read(FileName const & filename)
274 {
275         Lexer lex;
276         lex.setFile(filename);
277         lex.setContext("Languages::read");
278         while (lex.isOK()) {
279                 int le = lex.lex();
280                 switch (le) {
281                 case Lexer::LEX_FEOF:
282                         continue;
283
284                 default:
285                         break;
286                 }
287                 if (lex.getString() != "Language") {
288                         lex.printError("Unknown Language tag `$$Token'");
289                         continue;
290                 }
291                 Language l;
292                 l.read(lex);
293                 if (!lex)
294                         break;
295                 if (l.lang() == "latex") {
296                         // Check if latex language was not already defined.
297                         LASSERT(latex_language == 0, continue);
298                         static const Language latex_lang = l;
299                         latex_language = &latex_lang;
300                 } else if (l.lang() == "ignore") {
301                         // Check if ignore language was not already defined.
302                         LASSERT(ignore_language == 0, continue);
303                         static const Language ignore_lang = l;
304                         ignore_language = &ignore_lang;
305                 } else
306                         languagelist[l.lang()] = l;
307         }
308
309         default_language = getLanguage("english");
310         if (!default_language) {
311                 LYXERR0("Default language \"english\" not found!");
312                 default_language = &(*languagelist.begin()).second;
313                 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
314         }
315
316         // Read layout translations
317         FileName const path = libFileSearch(string(), "layouttranslations");
318         readLayoutTranslations(path);
319 }
320
321
322 namespace {
323
324 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
325 {
326         while (lex.isOK()) {
327                 if (lex.checkFor("End"))
328                         break;
329                 if (!lex.next(true))
330                         return false;
331                 string const key = lex.getString();
332                 if (!lex.next(true))
333                         return false;
334                 docstring const val = lex.getDocString();
335                 trans[key] = val;
336         }
337         return true;
338 }
339
340
341 enum Match {
342         NoMatch,
343         ApproximateMatch,
344         ExactMatch
345 };
346
347
348 Match match(string const & code, Language const & lang)
349 {
350         // we need to mimic gettext: code can be a two-letter code, which
351         // should match all variants, e.g. "de" should match "de_DE",
352         // "de_AT" etc.
353         // special case for chinese:
354         // simplified  => code == "zh_CN", langcode == "zh_CN"
355         // traditional => code == "zh_TW", langcode == "zh_CN"
356         string const variety = lang.variety();
357         string const langcode = variety.empty() ?
358                                 lang.code() : lang.code() + '_' + variety;
359         string const name = lang.lang();
360         if ((code == langcode && name != "chinese-traditional")
361                 || (code == "zh_TW"  && name == "chinese-traditional"))
362                 return ExactMatch;
363         if ((code.size() == 2) && (langcode.size() > 2)
364                 && (code + '_' == langcode.substr(0, 3)))
365                 return ApproximateMatch;
366         return NoMatch;
367 }
368
369 } // namespace
370
371
372 void Languages::readLayoutTranslations(support::FileName const & filename)
373 {
374         Lexer lex;
375         lex.setFile(filename);
376         lex.setContext("Languages::read");
377
378         // 1) read all translations (exact and approximate matches) into trans
379         typedef std::map<string, Language::TranslationMap> TransMap;
380         TransMap trans;
381         LanguageList::iterator const lbeg = languagelist.begin();
382         LanguageList::iterator const lend = languagelist.end();
383         while (lex.isOK()) {
384                 if (!lex.checkFor("Translation")) {
385                         if (lex.isOK())
386                                 lex.printError("Unknown layout translation tag `$$Token'");
387                         break;
388                 }
389                 if (!lex.next(true))
390                         break;
391                 string const code = lex.getString();
392                 bool found = false;
393                 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
394                         if (match(code, lit->second) != NoMatch) {
395                                 found = true;
396                                 break;
397                         }
398                 }
399                 if (!found) {
400                         lex.printError("Unknown language `" + code + "'");
401                         break;
402                 }
403                 if (!readTranslations(lex, trans[code])) {
404                         lex.printError("Could not read layout translations for language `"
405                                 + code + "'");
406                         break;
407                 }
408         }
409
410         // 2) merge all translations into the languages
411         // exact translations overwrite approximate ones
412         TransMap::const_iterator const tbeg = trans.begin();
413         TransMap::const_iterator const tend = trans.end();
414         for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
415                 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
416                         Match const m = match(tit->first, lit->second);
417                         if (m == NoMatch)
418                                 continue;
419                         lit->second.readLayoutTranslations(tit->second,
420                                                            m == ExactMatch);
421                 }
422         }
423
424 }
425
426
427 Language const * Languages::getLanguage(string const & language) const
428 {
429         if (language == "reset")
430                 return reset_language;
431         if (language == "ignore")
432                 return ignore_language;
433         const_iterator it = languagelist.find(language);
434         return it == languagelist.end() ? reset_language : &it->second;
435 }
436
437
438 } // namespace lyx