]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
Do not output quote TextCommand definitions for polyglossia
[lyx.git] / src / insets / InsetQuotes.cpp
1 /**
2  * \file InsetQuotes.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetQuotes.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "Dimension.h"
19 #include "Font.h"
20 #include "Language.h"
21 #include "LaTeXFeatures.h"
22 #include "Lexer.h"
23 #include "LyXRC.h"
24 #include "MetricsInfo.h"
25 #include "OutputParams.h"
26 #include "output_xhtml.h"
27 #include "Paragraph.h"
28 #include "ParIterator.h"
29 #include "texstream.h"
30
31 #include "frontends/FontMetrics.h"
32 #include "frontends/Painter.h"
33
34 #include "support/debug.h"
35 #include "support/docstring.h"
36 #include "support/docstream.h"
37 #include "support/lstrings.h"
38
39 using namespace std;
40 using namespace lyx::support;
41
42 namespace lyx {
43
44 namespace {
45
46 /* codes used to read/write quotes to LyX files
47  * e    ``english''
48  * s    ''swedish''
49  * g    ,,german``
50  * p    ,,polish''
51  * f    <<french>>
52  * a    >>danish<<
53  */
54
55 char const * const language_char = "esgpfa";
56 char const * const side_char = "lr" ;
57 char const * const times_char = "sd";
58
59 // List of known quote chars
60 char const * const quote_char = ",'`<>";
61
62 // Unicode characters needed by each quote type
63 char_type const display_quote_char[2][5] = {
64         { 0x201a, 0x2019, 0x2018, 0x2039, 0x203a},
65         { 0x201e, 0x201d, 0x201c, 0x00ab, 0x00bb}
66 };
67
68 // Index of chars used for the quote. Index is [side, language]
69 int quote_index[2][6] = {
70         { 2, 1, 0, 0, 3, 4 },    // "'',,<>"
71         { 1, 1, 2, 1, 4, 3 }     // "`'`'><"
72 };
73
74 // Corresponding LaTeX code, for double and single quotes.
75 char const * const latex_quote_t1[2][5] = {
76         { "\\quotesinglbase ",  "'", "`",
77     "\\guilsinglleft{}", "\\guilsinglright{}" },
78   { ",,", "''", "``", "<<", ">>" }
79 };
80
81 char const * const latex_quote_ot1[2][5] = {
82         { "\\quotesinglbase ",  "'", "`",
83     "\\guilsinglleft{}", "\\guilsinglright{}" },
84   { "\\quotedblbase ", "''", "``",
85     "\\guillemotleft{}", "\\guillemotright{}" }
86 };
87
88 char const * const latex_quote_babel[2][5] = {
89         { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
90   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" }
91 };
92
93 char const * const html_quote[2][5] = {
94         { "&sbquo;",  "&rsquo;", "&lsquo;",
95           "&lsaquo;", "&rsaquo;" },
96   { "&bdquo;", "&rdquo;", "&ldquo;", "&laquo;", "&raquo;" }
97 };
98
99 } // namespace anon
100
101
102 InsetQuotes::InsetQuotes(Buffer * buf, string const & str) : Inset(buf)
103 {
104         parseString(str);
105 }
106
107 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, QuoteTimes t)
108         : Inset(buf), times_(t), pass_thru_(false)
109 {
110         if (buf) {
111                 language_ = buf->params().quotes_language;
112                 fontenc_ = (buf->params().fontenc == "global")
113                         ? lyxrc.fontenc : buf->params().fontenc;
114         } else {
115                 language_ = EnglishQuotes;
116                 fontenc_ = lyxrc.fontenc;
117         }
118
119         setSide(c);
120 }
121
122
123 docstring InsetQuotes::layoutName() const
124 {
125         return from_ascii("Quotes");
126 }
127
128
129 void InsetQuotes::setSide(char_type c)
130 {
131         // Decide whether left or right
132         switch (c) {
133         case ' ':
134         case '(':
135         case '[':
136                 side_ = LeftQuote;   // left quote
137                 break;
138         default:
139                 side_ = RightQuote;  // right quote
140         }
141 }
142
143
144 void InsetQuotes::parseString(string const & s)
145 {
146         string str = s;
147         if (str.length() != 3) {
148                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
149                         " bad string length." << endl;
150                 str = "eld";
151         }
152
153         int i;
154
155         for (i = 0; i < 6; ++i) {
156                 if (str[0] == language_char[i]) {
157                         language_ = QuoteLanguage(i);
158                         break;
159                 }
160         }
161         if (i >= 6) {
162                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
163                         " bad language specification." << endl;
164                 language_ = EnglishQuotes;
165         }
166
167         for (i = 0; i < 2; ++i) {
168                 if (str[1] == side_char[i]) {
169                         side_ = QuoteSide(i);
170                         break;
171                 }
172         }
173         if (i >= 2) {
174                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
175                         " bad side specification." << endl;
176                 side_ = LeftQuote;
177         }
178
179         for (i = 0; i < 2; ++i) {
180                 if (str[2] == times_char[i]) {
181                         times_ = QuoteTimes(i);
182                         break;
183                 }
184         }
185         if (i >= 2) {
186                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
187                         " bad times specification." << endl;
188                 times_ = DoubleQuotes;
189         }
190 }
191
192
193 docstring InsetQuotes::displayString() const
194 {
195         // In PassThru and Hebrew, we use straight quotes
196         if (pass_thru_ || context_lang_ == "he_IL")
197                 return (times_ == DoubleQuotes) ? from_ascii("\"") : from_ascii("'");
198
199         int const index = quote_index[side_][language_];
200         docstring retdisp = docstring(1, display_quote_char[times_][index]);
201
202         // in French, thin spaces are added inside double guillemets
203         // FIXME: this should be done by a separate quote type.
204         if (prefixIs(context_lang_, "fr")
205             && times_ == DoubleQuotes && language_ == FrenchQuotes) {
206                 // THIN SPACE (U+2009)
207                 char_type const thin_space = 0x2009;
208                 if (side_ == LeftQuote)
209                         retdisp += thin_space;
210                 else
211                         retdisp = thin_space + retdisp;
212         }
213
214         return retdisp;
215 }
216
217
218 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
219 {
220         FontInfo & font = mi.base.font;
221         frontend::FontMetrics const & fm = theFontMetrics(font);
222         dim.asc = fm.maxAscent();
223         dim.des = fm.maxDescent();
224         dim.wid = fm.width(displayString());
225 }
226
227
228 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
229 {
230         FontInfo font = pi.base.font;
231         font.setPaintColor(pi.textColor(font.realColor()));
232         pi.pain.text(x, y, displayString(), font);
233 }
234
235
236 void InsetQuotes::write(ostream & os) const
237 {
238         string text;
239         text += language_char[language_];
240         text += side_char[side_];
241         text += times_char[times_];
242         os << "Quotes " << text;
243 }
244
245
246 void InsetQuotes::read(Lexer & lex)
247 {
248         lex.setContext("InsetQuotes::read");
249         lex.next();
250         parseString(lex.getString());
251         lex >> "\\end_inset";
252 }
253
254
255 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
256 {
257         const int quoteind = quote_index[side_][language_];
258         string qstr;
259
260         // In some context, we output plain quotes
261         bool const force_plain = 
262                 runparams.pass_thru
263                 || runparams.local_font->language()->lang() == "hebrew";
264         if (force_plain)
265                 qstr = (times_ == DoubleQuotes) ? "\"" : "'";
266         else if (language_ == FrenchQuotes && times_ == DoubleQuotes
267             && prefixIs(runparams.local_font->language()->code(), "fr")
268             && !runparams.use_polyglossia) {
269                 if (side_ == LeftQuote)
270                         qstr = "\\og "; //the spaces are important here
271                 else
272                         qstr = " \\fg{}"; //and here
273         } else if (fontenc_ == "T1" && !runparams.use_polyglossia) {
274                 qstr = latex_quote_t1[times_][quoteind];
275 #ifdef DO_USE_DEFAULT_LANGUAGE
276         } else if (doclang == "default") {
277 #else
278         } else if (!runparams.use_babel) {
279 #endif
280                 // these are also used by polyglossia
281                 qstr = latex_quote_ot1[times_][quoteind];
282         } else {
283                 qstr = latex_quote_babel[times_][quoteind];
284         }
285
286         if (!force_plain) {
287                 // Always guard against unfortunate ligatures (!` ?` `` '' ,, << >>)
288                 char_type const lastchar = os.lastChar();
289                 if (prefixIs(qstr, "`")) {
290                         if (lastchar == '!' || lastchar == '?')
291                                 qstr.insert(0, "{}");
292                 }
293                 if (qstr[1] == lastchar)
294                         qstr.insert(0, "{}");
295         }
296
297         os << from_ascii(qstr);
298 }
299
300
301 int InsetQuotes::plaintext(odocstringstream & os, 
302         OutputParams const &, size_t) const
303 {
304         docstring const str = displayString();
305         os << str;
306         return str.size();
307 }
308
309
310 docstring InsetQuotes::getQuoteEntity() const {
311         const int quoteind = quote_index[side_][language_];
312         return from_ascii(html_quote[times_][quoteind]);
313 }
314
315
316 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
317 {
318         os << getQuoteEntity();
319         return 0;
320 }
321
322
323 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
324 {
325         xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
326         return docstring();
327 }
328
329
330 void InsetQuotes::toString(odocstream & os) const
331 {
332         os << displayString();
333 }
334
335
336 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
337 {
338         os += displayString();
339 }
340
341
342 void InsetQuotes::updateBuffer(ParIterator const & it, UpdateType /* utype*/)
343 {
344         BufferParams const & bp = buffer().masterBuffer()->params();
345         pass_thru_ = it.paragraph().isPassThru();
346         context_lang_ = it.paragraph().getFontSettings(bp, it.pos()).language()->code();
347 }
348
349
350 void InsetQuotes::validate(LaTeXFeatures & features) const
351 {
352         char type = quote_char[quote_index[side_][language_]];
353
354 #ifdef DO_USE_DEFAULT_LANGUAGE
355         if (features.bufferParams().language->lang() == "default"
356 #else
357         if (!features.useBabel()
358 #endif
359             && !features.usePolyglossia() && fontenc_ != "T1") {
360                 if (times_ == SingleQuotes)
361                         switch (type) {
362                         case ',': features.require("quotesinglbase"); break;
363                         case '<': features.require("guilsinglleft");  break;
364                         case '>': features.require("guilsinglright"); break;
365                         default: break;
366                         }
367                 else
368                         switch (type) {
369                         case ',': features.require("quotedblbase");   break;
370                         case '<': features.require("guillemotleft");  break;
371                         case '>': features.require("guillemotright"); break;
372                         default: break;
373                         }
374         }
375 }
376
377 } // namespace lyx