]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
1cf4d5bb8a6125bb35df9531992f408dabfa9c1e
[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.nextToken();
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 & buf, ostream & os,
267                        OutputParams const & runparams) const
268 {
269         // How do we get the local language here??
270         lyx::pos_type curr_pos = ownerPar(buf, this).getPositionOfInset(this);
271         BOOST_ASSERT(curr_pos != -1);
272
273 #ifdef WITH_WARNINGS
274 #warning FIXME. We _must_ find another way to get the language. (Lgb)
275 #endif
276 #if 0
277         // This cannot be used. (Lgb)
278         string const curr_lang =
279                 parOwner()->getFont(buf->params, curr_pos).language()->babel();
280 #else
281         // And this is not the way... (Lgb)
282         string const curr_lang = buf.params().language->lang();
283 #endif
284         const int quoteind = quote_index[side_][language_];
285         string qstr;
286
287         if (language_ == FrenchQ && times_ == DoubleQ
288             && curr_lang == "frenchb") {
289                 if (side_ == LeftQ)
290                         qstr = "\\og "; //the spaces are important here
291                 else
292                         qstr = " \\fg{}"; //and here
293         } else if (language_ == FrenchQ && times_ == DoubleQ
294                    && curr_lang == "french") {
295                 if (side_ == LeftQ)
296                         qstr = "<< "; //the spaces are important here
297                 else
298                         qstr = " >>"; //and here
299         } else if (lyxrc.fontenc == "T1") {
300                 qstr = latex_quote_t1[times_][quoteind];
301 #ifdef DO_USE_DEFAULT_LANGUAGE
302         } else if (doclang == "default") {
303 #else
304         } else if (!runparams.use_babel) {
305 #endif
306                 qstr = latex_quote_ot1[times_][quoteind];
307         } else {
308                 qstr = latex_quote_babel[times_][quoteind];
309         }
310
311         // Always guard against unfortunate ligatures (!` ?`)
312         if (prefixIs(qstr, "`"))
313                 qstr.insert(0, "{}");
314
315         os << qstr;
316         return 0;
317 }
318
319
320 int InsetQuotes::plaintext(Buffer const &, ostream & os,
321                        OutputParams const &) const
322 {
323         os << '"';
324         return 0;
325 }
326
327
328 int InsetQuotes::linuxdoc(Buffer const &, ostream & os,
329                           OutputParams const &) const
330 {
331         os << '"';
332         return 0;
333 }
334
335
336 int InsetQuotes::docbook(Buffer const &, ostream & os,
337                          OutputParams const &) const
338 {
339         if (times_ == DoubleQ) {
340                 if (side_ == LeftQ)
341                         os << "&ldquo;";
342                 else
343                         os << "&rdquo;";
344         } else {
345                 if (side_ == LeftQ)
346                         os << "&lsquo;";
347                 else
348                         os << "&rsquo;";
349         }
350         return 0;
351 }
352
353
354 void InsetQuotes::validate(LaTeXFeatures & features) const
355 {
356         bool const use_babel = features.useBabel();
357         char type = quote_char[quote_index[side_][language_]];
358
359 #ifdef DO_USE_DEFAULT_LANGUAGE
360         if (features.bufferParams().language->lang() == "default"
361 #else
362         if (!use_babel
363 #endif
364             && lyxrc.fontenc != "T1") {
365                 if (times_ == SingleQ)
366                         switch (type) {
367                                 case ',': features.require("quotesinglbase");  break;
368                         case '<': features.require("guilsinglleft");  break;
369                         case '>': features.require("guilsinglright"); break;
370                         default: break;
371                         }
372                 else
373                         switch (type) {
374                         case ',': features.require("quotedblbase");   break;
375                         case '<': features.require("guillemotleft");  break;
376                         case '>': features.require("guillemotright"); break;
377                         default: break;
378                         }
379         }
380 }
381
382
383 auto_ptr<InsetBase> InsetQuotes::clone() const
384 {
385         return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
386 }
387
388
389 InsetOld::Code InsetQuotes::lyxCode() const
390 {
391   return InsetOld::QUOTE_CODE;
392 }