]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
use more unicode in math
[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 namespace lyx {
34
35 using support::prefixIs;
36
37 using std::endl;
38 using std::string;
39 using std::auto_ptr;
40 using std::ostream;
41
42
43 namespace {
44
45 /* codes used to read/write quotes to LyX files
46  * e    ``english''
47  * s    ''spanish''
48  * g    ,,german``
49  * p    ,,polish''
50  * f    <<french>>
51  * a    >>danish<<
52  */
53
54 char const * const language_char = "esgpfa";
55 char const * const side_char = "lr" ;
56 char const * const times_char = "sd";
57
58 // List of known quote chars
59 char const * const quote_char = ",'`<>";
60
61 // Index of chars used for the quote. Index is [side, language]
62 int quote_index[2][6] = {
63         { 2, 1, 0, 0, 3, 4 },    // "'',,<>"
64         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
65
66 // Corresponding LaTeX code, for double and single quotes.
67 char const * const latex_quote_t1[2][5] =
68 { { "\\quotesinglbase ",  "'", "`",
69     "\\guilsinglleft{}", "\\guilsinglright{}" },
70   { ",,", "''", "``", "<<", ">>" } };
71
72 char const * const latex_quote_ot1[2][5] =
73 { { "\\quotesinglbase ",  "'", "`",
74     "\\guilsinglleft{}", "\\guilsinglright{}" },
75   { "\\quotedblbase ", "''", "``",
76     "\\guillemotleft{}", "\\guillemotright{}" } };
77
78 char const * const latex_quote_babel[2][5] =
79 { { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
80   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
81
82 } // namespace anon
83
84
85 InsetQuotes::InsetQuotes(string const & str)
86 {
87         parseString(str);
88         setInsetName(from_utf8("InsetQuotes"));
89 }
90
91
92 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
93         : language_(l), side_(s), times_(t)
94 {
95         setInsetName(from_utf8("InsetQuotes"));
96 }
97
98
99 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
100         : language_(params.quotes_language), times_(params.quotes_times)
101 {
102         getPosition(c);
103         setInsetName(from_utf8("InsetQuotes"));
104 }
105
106
107 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
108         : language_(l), times_(t)
109 {
110         getPosition(c);
111         setInsetName(from_utf8("InsetQuotes"));
112 }
113
114
115 void InsetQuotes::getPosition(char_type c)
116 {
117         // Decide whether left or right
118         switch (c) {
119         case ' ': case '(': case '[':
120                 side_ = LeftQ;   // left quote
121                 break;
122         default:
123                 side_ = RightQ;  // right quote
124         }
125 }
126
127
128 void InsetQuotes::parseString(string const & s)
129 {
130         string str(s);
131         if (str.length() != 3) {
132                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
133                         " bad string length." << endl;
134                 str = "eld";
135         }
136
137         int i;
138
139         for (i = 0; i < 6; ++i) {
140                 if (str[0] == language_char[i]) {
141                         language_ = quote_language(i);
142                         break;
143                 }
144         }
145         if (i >= 6) {
146                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
147                         " bad language specification." << endl;
148                 language_ = EnglishQ;
149         }
150
151         for (i = 0; i < 2; ++i) {
152                 if (str[1] == side_char[i]) {
153                         side_ = quote_side(i);
154                         break;
155                 }
156         }
157         if (i >= 2) {
158                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
159                         " bad side specification." << endl;
160                 side_ = LeftQ;
161         }
162
163         for (i = 0; i < 2; ++i) {
164                 if (str[2] == times_char[i]) {
165                         times_ = quote_times(i);
166                         break;
167                 }
168         }
169         if (i >= 2) {
170                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
171                         " bad times specification." << endl;
172                 times_ = DoubleQ;
173         }
174 }
175
176
177 docstring const InsetQuotes::dispString(Language const * loclang) const
178 {
179         string disp;
180         disp += quote_char[quote_index[side_][language_]];
181         if (times_ == DoubleQ)
182                 disp += disp;
183
184
185         docstring retdisp;
186         if (disp == "<<")
187                 retdisp = docstring(1, 0x00ab); //'«';
188         else if (disp == ">>")
189                 retdisp = docstring(1, 0x00bb); //'»';
190 #if 0
191         // The below are supposed to work, but something fails.
192         else if (disp == ",,")
193                 retdisp = docstring(1, 0x201e);
194         else if (disp == "''")
195                 retdisp == docstring(1, 0x201d);
196         else if (disp == "``")
197                 retdisp == docstring(1, 0x201c);
198         else if (disp == "<")
199                 retdisp = docstring(1, 0x2039);
200         else if (disp == ">")
201                 retdisp = docstring(1, 0x203a);
202         else if (disp == ",")
203                 retdisp = docstring(1, 0x201a);
204         else if (disp == "'")
205                 retdisp = docstring(1, 0x2019);
206         else if (disp == "`")
207                 retdisp = docstring(1, 0x2018);
208 #endif
209         else
210                 retdisp = lyx::from_ascii(disp);
211         
212         // in french, spaces are added inside double quotes
213         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
214                 if (side_ == LeftQ)
215                         retdisp += ' ';
216                 else
217                         retdisp.insert(docstring::size_type(0), 1, ' ');
218         }
219
220         return retdisp;
221 }
222
223
224 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
225 {
226         LyXFont & font = mi.base.font;
227         frontend::FontMetrics const & fm =
228                 theFontMetrics(font);
229         dim.asc = fm.maxAscent();
230         dim.des = fm.maxDescent();
231         dim.wid = 0;
232
233         docstring const text = dispString(font.language());
234         for (string::size_type i = 0; i < text.length(); ++i) {
235                 if (text[i] == ' ')
236                         dim.wid += fm.width('i');
237                 else if (i == 0 || text[i] != text[i - 1])
238                         dim.wid += fm.width(text[i]);
239                 else
240                         dim.wid += fm.width(',');
241         }
242         dim_ = dim;
243 }
244
245
246 #if 0
247 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
248 {
249 #if 1
250         return f;
251 #else
252         LyXFont font(f);
253         return font;
254 #endif
255 }
256 #endif
257
258
259 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
260 {
261         docstring const text = dispString(pi.base.font.language());
262
263         if (text.length() == 2 && text[0] == text[1]) {
264                 pi.pain.text(x, y, text[0], pi.base.font);
265                 int const t = theFontMetrics(pi.base.font)
266                         .width(',');
267                 pi.pain.text(x + t, y, text[0], pi.base.font);
268         } else {
269                 pi.pain.text(x, y, text, pi.base.font);
270         }
271         setPosCache(pi, x, y);
272 }
273
274
275 void InsetQuotes::write(Buffer const &, ostream & os) const
276 {
277         string text;
278         text += language_char[language_];
279         text += side_char[side_];
280         text += times_char[times_];
281         os << "Quotes " << text;
282 }
283
284
285 void InsetQuotes::read(Buffer const &, LyXLex & lex)
286 {
287         lex.next();
288         parseString(lex.getString());
289         lex.next();
290         if (lex.getString() != "\\end_inset") {
291                 lex.printError("Missing \\end_inset at this point");
292         }
293 }
294
295
296 int InsetQuotes::latex(Buffer const &, odocstream & os,
297                        OutputParams const & runparams) const
298 {
299         const int quoteind = quote_index[side_][language_];
300         string qstr;
301
302         if (language_ == FrenchQ && times_ == DoubleQ
303             && prefixIs(runparams.local_font->language()->code(), "fr")) {
304                 if (side_ == LeftQ)
305                         qstr = "\\og "; //the spaces are important here
306                 else
307                         qstr = " \\fg{}"; //and here
308         } else if (lyxrc.fontenc == "T1") {
309                 qstr = latex_quote_t1[times_][quoteind];
310 #ifdef DO_USE_DEFAULT_LANGUAGE
311         } else if (doclang == "default") {
312 #else
313         } else if (!runparams.use_babel) {
314 #endif
315                 qstr = latex_quote_ot1[times_][quoteind];
316         } else {
317                 qstr = latex_quote_babel[times_][quoteind];
318         }
319
320         // Always guard against unfortunate ligatures (!` ?`)
321         if (prefixIs(qstr, "`"))
322                 qstr.insert(0, "{}");
323
324         os << from_ascii(qstr);
325         return 0;
326 }
327
328
329 int InsetQuotes::plaintext(Buffer const &, odocstream & os,
330                        OutputParams const &) const
331 {
332         os << '"';
333         return 0;
334 }
335
336
337 int InsetQuotes::docbook(Buffer const &, odocstream & os,
338                          OutputParams const &) const
339 {
340         if (times_ == DoubleQ) {
341                 if (side_ == LeftQ)
342                         os << "&ldquo;";
343                 else
344                         os << "&rdquo;";
345         } else {
346                 if (side_ == LeftQ)
347                         os << "&lsquo;";
348                 else
349                         os << "&rsquo;";
350         }
351         return 0;
352 }
353
354
355 int InsetQuotes::textString(Buffer const & buf, odocstream & os,
356                        OutputParams const & op) const
357 {
358         return plaintext(buf, os, op);
359 }
360
361
362 void InsetQuotes::validate(LaTeXFeatures & features) const
363 {
364         bool const use_babel = features.useBabel();
365         char type = quote_char[quote_index[side_][language_]];
366
367 #ifdef DO_USE_DEFAULT_LANGUAGE
368         if (features.bufferParams().language->lang() == "default"
369 #else
370         if (!use_babel
371 #endif
372             && lyxrc.fontenc != "T1") {
373                 if (times_ == SingleQ)
374                         switch (type) {
375                                 case ',': features.require("quotesinglbase");  break;
376                         case '<': features.require("guilsinglleft");  break;
377                         case '>': features.require("guilsinglright"); break;
378                         default: break;
379                         }
380                 else
381                         switch (type) {
382                         case ',': features.require("quotedblbase");   break;
383                         case '<': features.require("guillemotleft");  break;
384                         case '>': features.require("guillemotright"); break;
385                         default: break;
386                         }
387         }
388 }
389
390
391 auto_ptr<InsetBase> InsetQuotes::doClone() const
392 {
393         return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
394 }
395
396
397 InsetBase::Code InsetQuotes::lyxCode() const
398 {
399         return InsetBase::QUOTE_CODE;
400 }
401
402
403 } // namespace lyx