]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
e0083f771edeacb89c7c6cf1666ee9a80924df9d
[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::auto_ptr;
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 bool InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
219 {
220         Font & 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         docstring const text = dispString(font.language());
228         for (string::size_type i = 0; i < text.length(); ++i) {
229                 if (text[i] == ' ')
230                         dim.wid += fm.width('i');
231                 else if (i == 0 || text[i] != text[i - 1])
232                         dim.wid += fm.width(text[i]);
233                 else
234                         dim.wid += fm.width(',');
235         }
236         bool const changed = dim_ != dim;
237         dim_ = dim;
238         return changed;
239 }
240
241
242 #if 0
243 Font const InsetQuotes::convertFont(Font const & f) const
244 {
245 #if 1
246         return f;
247 #else
248         Font font(f);
249         return font;
250 #endif
251 }
252 #endif
253
254
255 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
256 {
257         docstring const text = dispString(pi.base.font.language());
258
259         if (text.length() == 2 && text[0] == text[1]) {
260                 pi.pain.text(x, y, text[0], pi.base.font);
261                 int const t = theFontMetrics(pi.base.font)
262                         .width(',');
263                 pi.pain.text(x + t, y, text[0], pi.base.font);
264         } else {
265                 pi.pain.text(x, y, text, pi.base.font);
266         }
267         setPosCache(pi, x, y);
268 }
269
270
271 void InsetQuotes::write(Buffer const &, ostream & os) const
272 {
273         string text;
274         text += language_char[language_];
275         text += side_char[side_];
276         text += times_char[times_];
277         os << "Quotes " << text;
278 }
279
280
281 void InsetQuotes::read(Buffer const &, Lexer & lex)
282 {
283         lex.next();
284         parseString(lex.getString());
285         lex.next();
286         if (lex.getString() != "\\end_inset") {
287                 lex.printError("Missing \\end_inset at this point");
288         }
289 }
290
291
292 int InsetQuotes::latex(Buffer const &, odocstream & os,
293                        OutputParams const & runparams) const
294 {
295         const int quoteind = quote_index[side_][language_];
296         string qstr;
297
298         if (language_ == FrenchQ && times_ == DoubleQ
299             && prefixIs(runparams.local_font->language()->code(), "fr")) {
300                 if (side_ == LeftQ)
301                         qstr = "\\og "; //the spaces are important here
302                 else
303                         qstr = " \\fg{}"; //and here
304         } else if (lyxrc.fontenc == "T1") {
305                 qstr = latex_quote_t1[times_][quoteind];
306 #ifdef DO_USE_DEFAULT_LANGUAGE
307         } else if (doclang == "default") {
308 #else
309         } else if (!runparams.use_babel) {
310 #endif
311                 qstr = latex_quote_ot1[times_][quoteind];
312         } else {
313                 qstr = latex_quote_babel[times_][quoteind];
314         }
315
316         // Always guard against unfortunate ligatures (!` ?`)
317         if (prefixIs(qstr, "`"))
318                 qstr.insert(0, "{}");
319
320         os << from_ascii(qstr);
321         return 0;
322 }
323
324
325 int InsetQuotes::plaintext(Buffer const & buf, odocstream & os,
326                            OutputParams const &) const
327 {
328         docstring const str = dispString(buf.params().language);
329         os << str;
330         return str.size();
331 }
332
333
334 int InsetQuotes::docbook(Buffer const &, odocstream & os,
335                          OutputParams const &) const
336 {
337         if (times_ == DoubleQ) {
338                 if (side_ == LeftQ)
339                         os << "&ldquo;";
340                 else
341                         os << "&rdquo;";
342         } else {
343                 if (side_ == LeftQ)
344                         os << "&lsquo;";
345                 else
346                         os << "&rsquo;";
347         }
348         return 0;
349 }
350
351
352 void InsetQuotes::textString(Buffer const & buf, odocstream & os) const
353 {
354         os << dispString(buf.params().language);
355 }
356
357
358 void InsetQuotes::validate(LaTeXFeatures & features) const
359 {
360         bool const use_babel = features.useBabel();
361         char type = quote_char[quote_index[side_][language_]];
362
363 #ifdef DO_USE_DEFAULT_LANGUAGE
364         if (features.bufferParams().language->lang() == "default"
365 #else
366         if (!use_babel
367 #endif
368             && lyxrc.fontenc != "T1") {
369                 if (times_ == SingleQ)
370                         switch (type) {
371                                 case ',': features.require("quotesinglbase");  break;
372                         case '<': features.require("guilsinglleft");  break;
373                         case '>': features.require("guilsinglright"); break;
374                         default: break;
375                         }
376                 else
377                         switch (type) {
378                         case ',': features.require("quotedblbase");   break;
379                         case '<': features.require("guillemotleft");  break;
380                         case '>': features.require("guillemotright"); break;
381                         default: break;
382                         }
383         }
384 }
385
386
387 auto_ptr<Inset> InsetQuotes::doClone() const
388 {
389         return auto_ptr<Inset>(new InsetQuotes(language_, side_, times_));
390 }
391
392
393 Inset::Code InsetQuotes::lyxCode() const
394 {
395         return Inset::QUOTE_CODE;
396 }
397
398
399 } // namespace lyx