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