]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
fix #832
[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
14 #include "insetquotes.h"
15
16 #include "support/LAssert.h"
17 #include "support/lstrings.h"
18 #include "BufferView.h"
19 #include "LaTeXFeatures.h"
20 #include "frontends/Painter.h"
21 #include "buffer.h"
22 #include "debug.h"
23 #include "frontends/font_metrics.h"
24 #include "language.h"
25 #include "lyxfont.h"
26 #include "lyxrc.h"
27 #include "paragraph.h"
28 #include "lyxlex.h"
29
30 using std::ostream;
31 using std::endl;
32
33 // Quotes. Used for the various quotes. German, English, French,
34 // Danish, Polish, all either double or single.
35
36 namespace {
37
38 // codes used to read/write quotes to LyX files
39 char const * const language_char = "esgpfa";
40 char const * const side_char = "lr" ;
41 char const * const times_char = "sd";
42
43 // List of known quote chars
44 char const * const quote_char = ",'`<>";
45
46 // Index of chars used for the quote. Index is [side, language]
47 int quote_index[2][6] = {
48         { 2, 1, 0, 0, 3, 4 },    // "'',,<>"
49         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
50
51 // Corresponding LaTeX code, for double and single quotes.
52 char const * const latex_quote_t1[2][5] =
53 { { "\\quotesinglbase ",  "'", "`",
54     "\\guilsinglleft{}", "\\guilsinglright{}" },
55   { ",,", "''", "``", "<<", ">>" } };
56
57 char const * const latex_quote_ot1[2][5] =
58 { { "\\quotesinglbase ",  "'", "`",
59     "\\guilsinglleft{}", "\\guilsinglright{}" },
60   { "\\quotedblbase ", "''", "``",
61     "\\guillemotleft{}", "\\guillemotright{}" } };
62
63 char const * const latex_quote_babel[2][5] =
64 { { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
65   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
66
67 } // namespace anon
68
69
70 InsetQuotes::InsetQuotes(string const & str)
71 {
72         parseString(str);
73 }
74
75
76 InsetQuotes::InsetQuotes(quote_language l,
77                          quote_side s,
78                          quote_times t)
79         : language_(l), side_(s), times_(t)
80 {}
81
82
83 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
84         : language_(params.quotes_language), times_(params.quotes_times)
85 {
86         // Decide whether left or right
87         switch (c) {
88         case ' ': case '(':
89 #warning eh ? I am lost here ...
90         //case Paragraph::META_HFILL:
91         // case Paragraph::META_NEWLINE:
92                 side_ = LeftQ;   // left quote
93                 break;
94         default:
95                 side_ = RightQ;  // right quote
96         }
97 }
98
99
100 void InsetQuotes::parseString(string const & s)
101 {
102         string str(s);
103         if (str.length() != 3) {
104                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
105                         " bad string length." << endl;
106                 str = "eld";
107         }
108
109         int i;
110
111         for (i = 0; i < 6; ++i) {
112                 if (str[0] == language_char[i]) {
113                         language_ = quote_language(i);
114                         break;
115                 }
116         }
117         if (i >= 6) {
118                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
119                         " bad language specification." << endl;
120                 language_ = EnglishQ;
121         }
122
123         for (i = 0; i < 2; ++i) {
124                 if (str[1] == side_char[i]) {
125                         side_ = quote_side(i);
126                         break;
127                 }
128         }
129         if (i >= 2) {
130                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
131                         " bad side specification." << endl;
132                 side_ = LeftQ;
133         }
134
135         for (i = 0; i < 2; ++i) {
136                 if (str[2] == times_char[i]) {
137                         times_ = quote_times(i);
138                         break;
139                 }
140         }
141         if (i >= 2) {
142                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
143                         " bad times specification." << endl;
144                 times_ = DoubleQ;
145         }
146 }
147
148
149 string const InsetQuotes::dispString(Language const * loclang) const
150 {
151         string disp;
152         disp += quote_char[quote_index[side_][language_]];
153         if (times_ == DoubleQ)
154                 disp += disp;
155
156         if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
157             || lyxrc.font_norm_type == LyXRC::ISO_8859_9
158             || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
159                 if (disp == "<<")
160                         disp = '«';
161                 else if (disp == ">>")
162                         disp = '»';
163         }
164
165         // in french, spaces are added inside double quotes
166         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
167                 if (side_ == LeftQ)
168                         disp += ' ';
169                 else
170                         disp.insert(string::size_type(0), 1, ' ');
171         }
172
173         return disp;
174 }
175
176
177 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
178 {
179         return font_metrics::maxAscent(font);
180 }
181
182
183 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
184 {
185         return font_metrics::maxDescent(font);
186 }
187
188
189 int InsetQuotes::width(BufferView *, LyXFont const & font) const
190 {
191         string const text = dispString(font.language());
192         int w = 0;
193
194         for (string::size_type i = 0; i < text.length(); ++i) {
195                 if (text[i] == ' ')
196                         w += font_metrics::width('i', font);
197                 else if (i == 0 || text[i] != text[i - 1])
198                         w += font_metrics::width(text[i], font);
199                 else
200                         w += font_metrics::width(',', font);
201         }
202
203         return w;
204 }
205
206
207 #if 0
208 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
209 {
210 #if 1
211         return f;
212 #else
213         LyXFont font(f);
214         return font;
215 #endif
216 }
217 #endif
218
219
220 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
221                        int baseline, float & x) const
222 {
223         string const text = dispString(font.language());
224
225         if (text.length() == 2 && text[0] == text[1]) {
226                 bv->painter().text(int(x), baseline, text[0], font);
227                 int x2 = int(x + font_metrics::width(',', font));
228                 bv->painter().text(x2, baseline, text[0], font);
229         } else
230                 bv->painter().text(int(x), baseline, text, font);
231         x += width(bv, font);
232 }
233
234
235 void InsetQuotes::write(Buffer const *, ostream & os) const
236 {
237         string text;
238         text += language_char[language_];
239         text += side_char[side_];
240         text += times_char[times_];
241         os << "Quotes " << text;
242 }
243
244
245 void InsetQuotes::read(Buffer const *, LyXLex & lex)
246 {
247         lex.nextToken();
248         parseString(lex.getString());
249         lex.next();
250         if (lex.getString() != "\\end_inset") {
251                 lex.printError("Missing \\end_inset at this point");
252         }
253 }
254
255
256 extern bool use_babel;
257
258 int InsetQuotes::latex(Buffer const * buf, ostream & os,
259                        bool /*fragile*/, bool /* free_spc */) const
260 {
261         // How do we get the local language here??
262         lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
263         lyx::Assert(curr_pos != -1);
264
265 #warning FIXME. We _must_ find another way to get the language. (Lgb)
266 #if 0
267         // This cannot be used. (Lgb)
268         string const curr_lang =
269                 parOwner()->getFont(buf->params,
270                                     curr_pos).language()->babel();
271 #else
272         // And this is not the way... (Lgb)
273         string const curr_lang = buf->params.language->lang();
274 #endif
275         const int quoteind = quote_index[side_][language_];
276         string qstr;
277
278         if (language_ == FrenchQ && times_ == DoubleQ
279             && curr_lang == "frenchb") {
280                 if (side_ == LeftQ)
281                         qstr = "\\og "; //the spaces are important here
282                 else
283                         qstr = " \\fg{}"; //and here
284         } else if (language_ == FrenchQ && times_ == DoubleQ
285                    && curr_lang == "french") {
286                 if (side_ == LeftQ)
287                         qstr = "<< "; //the spaces are important here
288                 else
289                         qstr = " >>"; //and here
290         } else if (lyxrc.fontenc == "T1") {
291                 qstr = latex_quote_t1[times_][quoteind];
292 #ifdef DO_USE_DEFAULT_LANGUAGE
293         } else if (doclang == "default") {
294 #else
295         } else if (!use_babel) {
296 #endif
297                 qstr = latex_quote_ot1[times_][quoteind];
298         } else {
299                 qstr = latex_quote_babel[times_][quoteind];
300         }
301
302         // Always guard against unfortunate ligatures (!` ?`)
303         if (prefixIs(qstr, "`"))
304                 qstr.insert(0, "{}");
305
306         os << qstr;
307         return 0;
308 }
309
310
311 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
312 {
313         os << '"';
314         return 0;
315 }
316
317
318 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
319 {
320         os << '"';
321         return 0;
322 }
323
324
325 int InsetQuotes::docbook(Buffer const *, ostream & os, bool) const
326 {
327         if (times_ == DoubleQ) {
328                 if (side_ == LeftQ)
329                         os << "&ldquo;";
330                 else
331                         os << "&rdquo;";
332         } else {
333                 if (side_ == LeftQ)
334                         os << "&lsquo;";
335                 else
336                         os << "&rsquo;";
337         }
338         return 0;
339 }
340
341
342 void InsetQuotes::validate(LaTeXFeatures & features) const
343 {
344         char type = quote_char[quote_index[side_][language_]];
345
346 #ifdef DO_USE_DEFAULT_LANGUAGE
347         if (features.bufferParams().language->lang() == "default"
348 #else
349         if (!use_babel
350 #endif
351             && lyxrc.fontenc != "T1") {
352                 if (times_ == SingleQ)
353                         switch (type) {
354                         case ',': features.require("quotesinglbase");  break;
355                         case '<': features.require("guilsinglleft");  break;
356                         case '>': features.require("guilsinglright"); break;
357                         default: break;
358                         }
359                 else
360                         switch (type) {
361                         case ',': features.require("quotedblbase");   break;
362                         case '<': features.require("guillemotleft");  break;
363                         case '>': features.require("guillemotright"); break;
364                         default: break;
365                         }
366         }
367 }
368
369
370 Inset * InsetQuotes::clone(Buffer const &, bool) const
371 {
372   return new InsetQuotes(language_, side_, times_);
373 }
374
375
376 Inset::Code InsetQuotes::lyxCode() const
377 {
378   return Inset::QUOTE_CODE;
379 }