]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
26abda076c8eb81175b3345ec10fe3a3f279b841
[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(QuoteLanguage l, QuoteSide s, QuoteTimes t)
92         : language_(l), side_(s), times_(t)
93 {
94 }
95
96
97 InsetQuotes::InsetQuotes(Buffer const & buf, char_type c)
98         : language_(buf.params().quotes_language), times_(buf.params().quotes_times)
99 {
100         setSide(c);
101 }
102
103
104 InsetQuotes::InsetQuotes(char_type c, QuoteLanguage l, QuoteTimes t)
105         : language_(l), times_(t)
106 {
107         setSide(c);
108 }
109
110
111 docstring InsetQuotes::name() const
112 {
113         return from_ascii("Quotes");
114 }
115
116
117 void InsetQuotes::setSide(char_type c)
118 {
119         // Decide whether left or right
120         switch (c) {
121         case ' ':
122         case '(':
123         case '[':
124                 side_ = LeftQuote;   // left quote
125                 break;
126         default:
127                 side_ = RightQuote;  // 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_ = QuoteLanguage(i);
146                         break;
147                 }
148         }
149         if (i >= 6) {
150                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
151                         " bad language specification." << endl;
152                 language_ = EnglishQuotes;
153         }
154
155         for (i = 0; i < 2; ++i) {
156                 if (str[1] == side_char[i]) {
157                         side_ = QuoteSide(i);
158                         break;
159                 }
160         }
161         if (i >= 2) {
162                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
163                         " bad side specification." << endl;
164                 side_ = LeftQuote;
165         }
166
167         for (i = 0; i < 2; ++i) {
168                 if (str[2] == times_char[i]) {
169                         times_ = QuoteTimes(i);
170                         break;
171                 }
172         }
173         if (i >= 2) {
174                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
175                         " bad times specification." << endl;
176                 times_ = DoubleQuotes;
177         }
178 }
179
180
181 docstring InsetQuotes::displayString() const
182 {
183         Language const * loclang = buffer().params().language;
184         string disp;
185         disp += quote_char[quote_index[side_][language_]];
186         if (times_ == DoubleQuotes)
187                 disp += disp;
188
189
190         docstring retdisp;
191         if (disp == "<<")
192                 retdisp = docstring(1, 0x00ab); //'«';
193         else if (disp == ">>")
194                 retdisp = docstring(1, 0x00bb); //'»';
195 #if 0
196         // The below are supposed to work, but something fails.
197         else if (disp == ",,")
198                 retdisp = docstring(1, 0x201e);
199         else if (disp == "''")
200                 retdisp == docstring(1, 0x201d);
201         else if (disp == "``")
202                 retdisp == docstring(1, 0x201c);
203         else if (disp == "<")
204                 retdisp = docstring(1, 0x2039);
205         else if (disp == ">")
206                 retdisp = docstring(1, 0x203a);
207         else if (disp == ",")
208                 retdisp = docstring(1, 0x201a);
209         else if (disp == "'")
210                 retdisp = docstring(1, 0x2019);
211         else if (disp == "`")
212                 retdisp = docstring(1, 0x2018);
213 #endif
214         else
215                 retdisp = from_ascii(disp);
216
217         // in french, spaces are added inside double quotes
218         if (times_ == DoubleQuotes && prefixIs(loclang->code(), "fr")) {
219                 if (side_ == LeftQuote)
220                         retdisp += ' ';
221                 else
222                         retdisp.insert(size_t(0), 1, ' ');
223         }
224
225         return retdisp;
226 }
227
228
229 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
230 {
231         FontInfo & font = mi.base.font;
232         frontend::FontMetrics const & fm =
233                 theFontMetrics(font);
234         dim.asc = fm.maxAscent();
235         dim.des = fm.maxDescent();
236         dim.wid = 0;
237
238         // FIXME: should we add a language or a font parameter member?
239         docstring const text = displayString();
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 = displayString();
255
256         if (text.length() == 2 && text[0] == text[1]) {
257                 pi.pain.text(x, y, text[0], pi.base.font);
258                 int const t = theFontMetrics(pi.base.font)
259                         .width(',');
260                 pi.pain.text(x + t, y, text[0], pi.base.font);
261         } else {
262                 pi.pain.text(x, y, text, pi.base.font);
263         }
264 }
265
266
267 void InsetQuotes::write(ostream & os) const
268 {
269         string text;
270         text += language_char[language_];
271         text += side_char[side_];
272         text += times_char[times_];
273         os << "Quotes " << text;
274 }
275
276
277 void InsetQuotes::read(Lexer & lex)
278 {
279         lex.setContext("InsetQuotes::read");
280         lex.next();
281         parseString(lex.getString());
282         lex >> "\\end_inset";
283 }
284
285
286 int InsetQuotes::latex(odocstream & os, OutputParams const & runparams) const
287 {
288         const int quoteind = quote_index[side_][language_];
289         string qstr;
290
291         if (language_ == FrenchQuotes && times_ == DoubleQuotes
292             && prefixIs(runparams.local_font->language()->code(), "fr")) {
293                 if (side_ == LeftQuote)
294                         qstr = "\\og "; //the spaces are important here
295                 else
296                         qstr = " \\fg{}"; //and here
297         } else if (lyxrc.fontenc == "T1") {
298                 qstr = latex_quote_t1[times_][quoteind];
299 #ifdef DO_USE_DEFAULT_LANGUAGE
300         } else if (doclang == "default") {
301 #else
302         } else if (!runparams.use_babel) {
303 #endif
304                 qstr = latex_quote_ot1[times_][quoteind];
305         } else {
306                 qstr = latex_quote_babel[times_][quoteind];
307         }
308
309         // Always guard against unfortunate ligatures (!` ?`)
310         if (prefixIs(qstr, "`"))
311                 qstr.insert(0, "{}");
312
313         os << from_ascii(qstr);
314         return 0;
315 }
316
317
318 int InsetQuotes::plaintext(odocstream & os, OutputParams const &) const
319 {
320         docstring const str = displayString();
321         os << str;
322         return str.size();
323 }
324
325
326 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
327 {
328         if (times_ == DoubleQuotes) {
329                 if (side_ == LeftQuote)
330                         os << "&ldquo;";
331                 else
332                         os << "&rdquo;";
333         } else {
334                 if (side_ == LeftQuote)
335                         os << "&lsquo;";
336                 else
337                         os << "&rsquo;";
338         }
339         return 0;
340 }
341
342
343 void InsetQuotes::textString(odocstream & os) const
344 {
345         os << displayString();
346 }
347
348
349 void InsetQuotes::validate(LaTeXFeatures & features) const
350 {
351         bool const use_babel = features.useBabel();
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 (!use_babel
358 #endif
359             && lyxrc.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