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