]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
fa2558150fff1829debd9b6e6d5191e1698a5b0a
[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 "debug.h"
18 #include "Language.h"
19 #include "LaTeXFeatures.h"
20 #include "Lexer.h"
21 #include "LyXRC.h"
22 #include "MetricsInfo.h"
23 #include "OutputParams.h"
24
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
27
28 #include "support/lstrings.h"
29
30
31 namespace lyx {
32
33 using support::prefixIs;
34
35 using std::endl;
36 using std::string;
37 using std::ostream;
38
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 // 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 char const * const latex_quote_ot1[2][5] =
70 { { "\\quotesinglbase ",  "'", "`",
71     "\\guilsinglleft{}", "\\guilsinglright{}" },
72   { "\\quotedblbase ", "''", "``",
73     "\\guillemotleft{}", "\\guillemotright{}" } };
74
75 char const * const latex_quote_babel[2][5] =
76 { { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
77   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
78
79 } // namespace anon
80
81
82 InsetQuotes::InsetQuotes(string const & str)
83 {
84         parseString(str);
85 }
86
87
88 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
89         : language_(l), side_(s), times_(t)
90 {
91 }
92
93
94 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
95         : language_(params.quotes_language), times_(params.quotes_times)
96 {
97         getPosition(c);
98 }
99
100
101 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
102         : language_(l), times_(t)
103 {
104         getPosition(c);
105 }
106
107
108 void InsetQuotes::getPosition(char_type c)
109 {
110         // Decide whether left or right
111         switch (c) {
112         case ' ': case '(': case '[':
113                 side_ = LeftQ;   // left quote
114                 break;
115         default:
116                 side_ = RightQ;  // right quote
117         }
118 }
119
120
121 void InsetQuotes::parseString(string const & s)
122 {
123         string str(s);
124         if (str.length() != 3) {
125                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
126                         " bad string length." << endl;
127                 str = "eld";
128         }
129
130         int i;
131
132         for (i = 0; i < 6; ++i) {
133                 if (str[0] == language_char[i]) {
134                         language_ = quote_language(i);
135                         break;
136                 }
137         }
138         if (i >= 6) {
139                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
140                         " bad language specification." << endl;
141                 language_ = EnglishQ;
142         }
143
144         for (i = 0; i < 2; ++i) {
145                 if (str[1] == side_char[i]) {
146                         side_ = quote_side(i);
147                         break;
148                 }
149         }
150         if (i >= 2) {
151                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
152                         " bad side specification." << endl;
153                 side_ = LeftQ;
154         }
155
156         for (i = 0; i < 2; ++i) {
157                 if (str[2] == times_char[i]) {
158                         times_ = quote_times(i);
159                         break;
160                 }
161         }
162         if (i >= 2) {
163                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
164                         " bad times specification." << endl;
165                 times_ = DoubleQ;
166         }
167 }
168
169
170 docstring const InsetQuotes::dispString(Language const * loclang) const
171 {
172         string disp;
173         disp += quote_char[quote_index[side_][language_]];
174         if (times_ == DoubleQ)
175                 disp += disp;
176
177
178         docstring retdisp;
179         if (disp == "<<")
180                 retdisp = docstring(1, 0x00ab); //'«';
181         else if (disp == ">>")
182                 retdisp = docstring(1, 0x00bb); //'»';
183 #if 0
184         // The below are supposed to work, but something fails.
185         else if (disp == ",,")
186                 retdisp = docstring(1, 0x201e);
187         else if (disp == "''")
188                 retdisp == docstring(1, 0x201d);
189         else if (disp == "``")
190                 retdisp == docstring(1, 0x201c);
191         else if (disp == "<")
192                 retdisp = docstring(1, 0x2039);
193         else if (disp == ">")
194                 retdisp = docstring(1, 0x203a);
195         else if (disp == ",")
196                 retdisp = docstring(1, 0x201a);
197         else if (disp == "'")
198                 retdisp = docstring(1, 0x2019);
199         else if (disp == "`")
200                 retdisp = docstring(1, 0x2018);
201 #endif
202         else
203                 retdisp = lyx::from_ascii(disp);
204
205         // in french, spaces are added inside double quotes
206         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
207                 if (side_ == LeftQ)
208                         retdisp += ' ';
209                 else
210                         retdisp.insert(docstring::size_type(0), 1, ' ');
211         }
212
213         return retdisp;
214 }
215
216
217 bool InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
218 {
219         Font & font = mi.base.font;
220         frontend::FontMetrics const & fm =
221                 theFontMetrics(font);
222         dim.asc = fm.maxAscent();
223         dim.des = fm.maxDescent();
224         dim.wid = 0;
225
226         docstring const text = dispString(font.language());
227         for (string::size_type i = 0; i < text.length(); ++i) {
228                 if (text[i] == ' ')
229                         dim.wid += fm.width('i');
230                 else if (i == 0 || text[i] != text[i - 1])
231                         dim.wid += fm.width(text[i]);
232                 else
233                         dim.wid += fm.width(',');
234         }
235         bool const changed = dim_ != dim;
236         dim_ = dim;
237         return changed;
238 }
239
240
241 #if 0
242 Font const InsetQuotes::convertFont(Font const & f) const
243 {
244 #if 1
245         return f;
246 #else
247         Font font(f);
248         return font;
249 #endif
250 }
251 #endif
252
253
254 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
255 {
256         docstring const text = dispString(pi.base.font.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 Inset::Code InsetQuotes::lyxCode() const
392 {
393         return Inset::QUOTE_CODE;
394 }
395
396
397 } // namespace lyx