]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
changelogs
[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::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 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
95         : language_(params.quotes_language), times_(params.quotes_times)
96 {
97         getPosition(c);
98 }
99
100
101 InsetQuotes::InsetQuotes(char c, quote_language l, quote_times t)
102         : language_(l), times_(t)
103 {
104         getPosition(c);
105 }
106
107
108 void InsetQuotes::getPosition(char 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 string 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         if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
178             || lyxrc.font_norm_type == LyXRC::ISO_8859_9
179             || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
180                 if (disp == "<<")
181                         disp = '«';
182                 else if (disp == ">>")
183                         disp = '»';
184         }
185
186         // in french, spaces are added inside double quotes
187         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
188                 if (side_ == LeftQ)
189                         disp += ' ';
190                 else
191                         disp.insert(string::size_type(0), 1, ' ');
192         }
193
194         return disp;
195 }
196
197
198 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
199 {
200         LyXFont & font = mi.base.font;
201         dim.asc = font_metrics::maxAscent(font);
202         dim.des = font_metrics::maxDescent(font);
203         dim.wid = 0;
204
205         string const text = dispString(font.language());
206         for (string::size_type i = 0; i < text.length(); ++i) {
207                 if (text[i] == ' ')
208                         dim.wid += font_metrics::width('i', font);
209                 else if (i == 0 || text[i] != text[i - 1])
210                         dim.wid += font_metrics::width(text[i], font);
211                 else
212                         dim.wid += font_metrics::width(',', font);
213         }
214         dim_ = dim;
215 }
216
217
218 #if 0
219 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
220 {
221 #if 1
222         return f;
223 #else
224         LyXFont font(f);
225         return font;
226 #endif
227 }
228 #endif
229
230
231 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
232 {
233         string const text = dispString(pi.base.font.language());
234
235         if (text.length() == 2 && text[0] == text[1]) {
236                 pi.pain.text(x, y, text[0], pi.base.font);
237                 int const t = font_metrics::width(',', pi.base.font);
238                 pi.pain.text(x + t, y, text[0], pi.base.font);
239         } else {
240                 pi.pain.text(x, y, text, pi.base.font);
241         }
242 }
243
244
245 void InsetQuotes::write(Buffer const &, ostream & os) const
246 {
247         string text;
248         text += language_char[language_];
249         text += side_char[side_];
250         text += times_char[times_];
251         os << "Quotes " << text;
252 }
253
254
255 void InsetQuotes::read(Buffer const &, LyXLex & lex)
256 {
257         lex.next();
258         parseString(lex.getString());
259         lex.next();
260         if (lex.getString() != "\\end_inset") {
261                 lex.printError("Missing \\end_inset at this point");
262         }
263 }
264
265
266 int InsetQuotes::latex(Buffer const &, ostream & os,
267                        OutputParams const & runparams) const
268 {
269         const int quoteind = quote_index[side_][language_];
270         string qstr;
271
272         if (language_ == FrenchQ && times_ == DoubleQ
273             && runparams.local_language == "frenchb") {
274                 if (side_ == LeftQ)
275                         qstr = "\\og "; //the spaces are important here
276                 else
277                         qstr = " \\fg{}"; //and here
278         } else if (language_ == FrenchQ && times_ == DoubleQ
279                    && runparams.local_language == "french") {
280                 if (side_ == LeftQ)
281                         qstr = "<< "; //the spaces are important here
282                 else
283                         qstr = " >>"; //and here
284         } else if (lyxrc.fontenc == "T1") {
285                 qstr = latex_quote_t1[times_][quoteind];
286 #ifdef DO_USE_DEFAULT_LANGUAGE
287         } else if (doclang == "default") {
288 #else
289         } else if (!runparams.use_babel) {
290 #endif
291                 qstr = latex_quote_ot1[times_][quoteind];
292         } else {
293                 qstr = latex_quote_babel[times_][quoteind];
294         }
295
296         // Always guard against unfortunate ligatures (!` ?`)
297         if (prefixIs(qstr, "`"))
298                 qstr.insert(0, "{}");
299
300         os << qstr;
301         return 0;
302 }
303
304
305 int InsetQuotes::plaintext(Buffer const &, ostream & os,
306                        OutputParams const &) const
307 {
308         os << '"';
309         return 0;
310 }
311
312
313 int InsetQuotes::linuxdoc(Buffer const &, ostream & os,
314                           OutputParams const &) const
315 {
316         os << '"';
317         return 0;
318 }
319
320
321 int InsetQuotes::docbook(Buffer const &, ostream & os,
322                          OutputParams const &) const
323 {
324         if (times_ == DoubleQ) {
325                 if (side_ == LeftQ)
326                         os << "&ldquo;";
327                 else
328                         os << "&rdquo;";
329         } else {
330                 if (side_ == LeftQ)
331                         os << "&lsquo;";
332                 else
333                         os << "&rsquo;";
334         }
335         return 0;
336 }
337
338
339 void InsetQuotes::validate(LaTeXFeatures & features) const
340 {
341         bool const use_babel = features.useBabel();
342         char type = quote_char[quote_index[side_][language_]];
343
344 #ifdef DO_USE_DEFAULT_LANGUAGE
345         if (features.bufferParams().language->lang() == "default"
346 #else
347         if (!use_babel
348 #endif
349             && lyxrc.fontenc != "T1") {
350                 if (times_ == SingleQ)
351                         switch (type) {
352                                 case ',': features.require("quotesinglbase");  break;
353                         case '<': features.require("guilsinglleft");  break;
354                         case '>': features.require("guilsinglright"); break;
355                         default: break;
356                         }
357                 else
358                         switch (type) {
359                         case ',': features.require("quotedblbase");   break;
360                         case '<': features.require("guillemotleft");  break;
361                         case '>': features.require("guillemotright"); break;
362                         default: break;
363                         }
364         }
365 }
366
367
368 auto_ptr<InsetBase> InsetQuotes::doClone() const
369 {
370         return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
371 }
372
373
374 InsetOld::Code InsetQuotes::lyxCode() const
375 {
376   return InsetOld::QUOTE_CODE;
377 }