]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
Introduce wide streams. This fixes the remaining problems of plain text
[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/FontMetrics.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         lyx::frontend::FontMetrics const & fm =
203                 theFontMetrics(font);
204         dim.asc = fm.maxAscent();
205         dim.des = fm.maxDescent();
206         dim.wid = 0;
207
208         string const text = dispString(font.language());
209         for (string::size_type i = 0; i < text.length(); ++i) {
210                 if (text[i] == ' ')
211                         dim.wid += fm.width('i');
212                 else if (i == 0 || text[i] != text[i - 1])
213                         dim.wid += fm.width(text[i]);
214                 else
215                         dim.wid += fm.width(',');
216         }
217         dim_ = dim;
218 }
219
220
221 #if 0
222 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
223 {
224 #if 1
225         return f;
226 #else
227         LyXFont font(f);
228         return font;
229 #endif
230 }
231 #endif
232
233
234 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
235 {
236         string const text = dispString(pi.base.font.language());
237
238         if (text.length() == 2 && text[0] == text[1]) {
239                 pi.pain.text(x, y, text[0], pi.base.font);
240                 int const t = theFontMetrics(pi.base.font)
241                         .width(',');
242                 pi.pain.text(x + t, y, text[0], pi.base.font);
243         } else {
244                 docstring dtext(text.begin(), text.end());
245                 pi.pain.text(x, y, dtext, pi.base.font);
246         }
247 }
248
249
250 void InsetQuotes::write(Buffer const &, ostream & os) const
251 {
252         string text;
253         text += language_char[language_];
254         text += side_char[side_];
255         text += times_char[times_];
256         os << "Quotes " << text;
257 }
258
259
260 void InsetQuotes::read(Buffer const &, LyXLex & lex)
261 {
262         lex.next();
263         parseString(lex.getString());
264         lex.next();
265         if (lex.getString() != "\\end_inset") {
266                 lex.printError("Missing \\end_inset at this point");
267         }
268 }
269
270
271 int InsetQuotes::latex(Buffer const &, ostream & os,
272                        OutputParams const & runparams) const
273 {
274         const int quoteind = quote_index[side_][language_];
275         string qstr;
276
277         if (language_ == FrenchQ && times_ == DoubleQ
278             && prefixIs(runparams.local_font->language()->code(), "fr")) {
279                 if (side_ == LeftQ)
280                         qstr = "\\og "; //the spaces are important here
281                 else
282                         qstr = " \\fg{}"; //and here
283         } else if (lyxrc.fontenc == "T1") {
284                 qstr = latex_quote_t1[times_][quoteind];
285 #ifdef DO_USE_DEFAULT_LANGUAGE
286         } else if (doclang == "default") {
287 #else
288         } else if (!runparams.use_babel) {
289 #endif
290                 qstr = latex_quote_ot1[times_][quoteind];
291         } else {
292                 qstr = latex_quote_babel[times_][quoteind];
293         }
294
295         // Always guard against unfortunate ligatures (!` ?`)
296         if (prefixIs(qstr, "`"))
297                 qstr.insert(0, "{}");
298
299         os << qstr;
300         return 0;
301 }
302
303
304 int InsetQuotes::plaintext(Buffer const &, lyx::odocstream & os,
305                        OutputParams const &) const
306 {
307         os << '"';
308         return 0;
309 }
310
311
312 int InsetQuotes::docbook(Buffer const &, ostream & os,
313                          OutputParams const &) const
314 {
315         if (times_ == DoubleQ) {
316                 if (side_ == LeftQ)
317                         os << "&ldquo;";
318                 else
319                         os << "&rdquo;";
320         } else {
321                 if (side_ == LeftQ)
322                         os << "&lsquo;";
323                 else
324                         os << "&rsquo;";
325         }
326         return 0;
327 }
328
329
330 int InsetQuotes::textString(Buffer const & buf, lyx::odocstream & os,
331                        OutputParams const & op) const
332 {
333         return plaintext(buf, os, op);
334 }
335
336
337 void InsetQuotes::validate(LaTeXFeatures & features) const
338 {
339         bool const use_babel = features.useBabel();
340         char type = quote_char[quote_index[side_][language_]];
341
342 #ifdef DO_USE_DEFAULT_LANGUAGE
343         if (features.bufferParams().language->lang() == "default"
344 #else
345         if (!use_babel
346 #endif
347             && lyxrc.fontenc != "T1") {
348                 if (times_ == SingleQ)
349                         switch (type) {
350                                 case ',': features.require("quotesinglbase");  break;
351                         case '<': features.require("guilsinglleft");  break;
352                         case '>': features.require("guilsinglright"); break;
353                         default: break;
354                         }
355                 else
356                         switch (type) {
357                         case ',': features.require("quotedblbase");   break;
358                         case '<': features.require("guillemotleft");  break;
359                         case '>': features.require("guillemotright"); break;
360                         default: break;
361                         }
362         }
363 }
364
365
366 auto_ptr<InsetBase> InsetQuotes::doClone() const
367 {
368         return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
369 }
370
371
372 InsetBase::Code InsetQuotes::lyxCode() const
373 {
374   return InsetBase::QUOTE_CODE;
375 }