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