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