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