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