]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
Fix some conversion problems spotted by MSVC warnings
[lyx.git] / src / insets / insetquotes.C
1 /**
2  * \file insetquotes.C
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 "lyxlex.h"
21 #include "lyxrc.h"
22 #include "metricsinfo.h"
23 #include "outputparams.h"
24 #include "paragraph.h"
25 #include "paragraph_funcs.h"
26
27 #include "frontends/font_metrics.h"
28 #include "frontends/Painter.h"
29
30 #include "support/lstrings.h"
31
32
33 using lyx::docstring;
34 using lyx::support::prefixIs;
35
36 using std::endl;
37 using std::string;
38 using std::auto_ptr;
39 using std::ostream;
40
41
42 namespace {
43
44 /* codes used to read/write quotes to LyX files
45  * e    ``english''
46  * s    ''spanish''
47  * g    ,,german``
48  * p    ,,polish''
49  * f    <<french>>
50  * a    >>danish<<
51  */
52
53 char const * const language_char = "esgpfa";
54 char const * const side_char = "lr" ;
55 char const * const times_char = "sd";
56
57 // List of known quote chars
58 char const * const quote_char = ",'`<>";
59
60 // Index of chars used for the quote. Index is [side, language]
61 int quote_index[2][6] = {
62         { 2, 1, 0, 0, 3, 4 },    // "'',,<>"
63         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
64
65 // Corresponding LaTeX code, for double and single quotes.
66 char const * const latex_quote_t1[2][5] =
67 { { "\\quotesinglbase ",  "'", "`",
68     "\\guilsinglleft{}", "\\guilsinglright{}" },
69   { ",,", "''", "``", "<<", ">>" } };
70
71 char const * const latex_quote_ot1[2][5] =
72 { { "\\quotesinglbase ",  "'", "`",
73     "\\guilsinglleft{}", "\\guilsinglright{}" },
74   { "\\quotedblbase ", "''", "``",
75     "\\guillemotleft{}", "\\guillemotright{}" } };
76
77 char const * const latex_quote_babel[2][5] =
78 { { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
79   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
80
81 } // namespace anon
82
83
84 InsetQuotes::InsetQuotes(string const & str)
85 {
86         parseString(str);
87 }
88
89
90 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
91         : language_(l), side_(s), times_(t)
92 {}
93
94
95 InsetQuotes::InsetQuotes(lyx::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(lyx::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(lyx::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 string 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         if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
179             || lyxrc.font_norm_type == LyXRC::ISO_8859_9
180             || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
181                 if (disp == "<<")
182                         disp = '«';
183                 else if (disp == ">>")
184                         disp = '»';
185         }
186
187         // in french, spaces are added inside double quotes
188         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
189                 if (side_ == LeftQ)
190                         disp += ' ';
191                 else
192                         disp.insert(string::size_type(0), 1, ' ');
193         }
194
195         return disp;
196 }
197
198
199 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
200 {
201         LyXFont & font = mi.base.font;
202         dim.asc = font_metrics::maxAscent(font);
203         dim.des = font_metrics::maxDescent(font);
204         dim.wid = 0;
205
206         string const text = dispString(font.language());
207         for (string::size_type i = 0; i < text.length(); ++i) {
208                 if (text[i] == ' ')
209                         dim.wid += font_metrics::width('i', font);
210                 else if (i == 0 || text[i] != text[i - 1])
211                         dim.wid += font_metrics::width(text[i], font);
212                 else
213                         dim.wid += font_metrics::width(',', font);
214         }
215         dim_ = dim;
216 }
217
218
219 #if 0
220 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
221 {
222 #if 1
223         return f;
224 #else
225         LyXFont font(f);
226         return font;
227 #endif
228 }
229 #endif
230
231
232 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
233 {
234         string const text = dispString(pi.base.font.language());
235
236         if (text.length() == 2 && text[0] == text[1]) {
237                 pi.pain.text(x, y, text[0], pi.base.font);
238                 int const t = font_metrics::width(',', pi.base.font);
239                 pi.pain.text(x + t, y, text[0], pi.base.font);
240         } else {
241                 docstring dtext(text.begin(), text.end());
242                 pi.pain.text(x, y, dtext, pi.base.font);
243         }
244 }
245
246
247 void InsetQuotes::write(Buffer const &, ostream & os) const
248 {
249         string text;
250         text += language_char[language_];
251         text += side_char[side_];
252         text += times_char[times_];
253         os << "Quotes " << text;
254 }
255
256
257 void InsetQuotes::read(Buffer const &, LyXLex & lex)
258 {
259         lex.next();
260         parseString(lex.getString());
261         lex.next();
262         if (lex.getString() != "\\end_inset") {
263                 lex.printError("Missing \\end_inset at this point");
264         }
265 }
266
267
268 int InsetQuotes::latex(Buffer const &, ostream & os,
269                        OutputParams const & runparams) const
270 {
271         const int quoteind = quote_index[side_][language_];
272         string qstr;
273
274         if (language_ == FrenchQ && times_ == DoubleQ
275             && prefixIs(runparams.local_font->language()->code(), "fr")) {
276                 if (side_ == LeftQ)
277                         qstr = "\\og "; //the spaces are important here
278                 else
279                         qstr = " \\fg{}"; //and here
280         } else if (lyxrc.fontenc == "T1") {
281                 qstr = latex_quote_t1[times_][quoteind];
282 #ifdef DO_USE_DEFAULT_LANGUAGE
283         } else if (doclang == "default") {
284 #else
285         } else if (!runparams.use_babel) {
286 #endif
287                 qstr = latex_quote_ot1[times_][quoteind];
288         } else {
289                 qstr = latex_quote_babel[times_][quoteind];
290         }
291
292         // Always guard against unfortunate ligatures (!` ?`)
293         if (prefixIs(qstr, "`"))
294                 qstr.insert(0, "{}");
295
296         os << qstr;
297         return 0;
298 }
299
300
301 int InsetQuotes::plaintext(Buffer const &, ostream & os,
302                        OutputParams const &) const
303 {
304         os << '"';
305         return 0;
306 }
307
308
309 int InsetQuotes::docbook(Buffer const &, ostream & os,
310                          OutputParams const &) const
311 {
312         if (times_ == DoubleQ) {
313                 if (side_ == LeftQ)
314                         os << "&ldquo;";
315                 else
316                         os << "&rdquo;";
317         } else {
318                 if (side_ == LeftQ)
319                         os << "&lsquo;";
320                 else
321                         os << "&rsquo;";
322         }
323         return 0;
324 }
325
326
327 int InsetQuotes::textString(Buffer const & buf, ostream & os,
328                        OutputParams const & op) const
329 {
330         return plaintext(buf, os, op);
331 }
332
333
334 void InsetQuotes::validate(LaTeXFeatures & features) const
335 {
336         bool const use_babel = features.useBabel();
337         char type = quote_char[quote_index[side_][language_]];
338
339 #ifdef DO_USE_DEFAULT_LANGUAGE
340         if (features.bufferParams().language->lang() == "default"
341 #else
342         if (!use_babel
343 #endif
344             && lyxrc.fontenc != "T1") {
345                 if (times_ == SingleQ)
346                         switch (type) {
347                                 case ',': features.require("quotesinglbase");  break;
348                         case '<': features.require("guilsinglleft");  break;
349                         case '>': features.require("guilsinglright"); break;
350                         default: break;
351                         }
352                 else
353                         switch (type) {
354                         case ',': features.require("quotedblbase");   break;
355                         case '<': features.require("guillemotleft");  break;
356                         case '>': features.require("guillemotright"); break;
357                         default: break;
358                         }
359         }
360 }
361
362
363 auto_ptr<InsetBase> InsetQuotes::doClone() const
364 {
365         return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
366 }
367
368
369 InsetBase::Code InsetQuotes::lyxCode() const
370 {
371   return InsetBase::QUOTE_CODE;
372 }