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