]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
Well, it turns out that we need a different return value for the xhtml
[lyx.git] / src / insets / InsetQuotes.cpp
1 /**
2  * \file InsetQuotes.cpp
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 "BufferView.h"
18 #include "Dimension.h"
19 #include "Language.h"
20 #include "LaTeXFeatures.h"
21 #include "Lexer.h"
22 #include "LyXRC.h"
23 #include "MetricsInfo.h"
24 #include "OutputParams.h"
25
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
28
29 #include "support/debug.h"
30 #include "support/docstring.h"
31 #include "support/docstream.h"
32 #include "support/lstrings.h"
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38
39 namespace {
40
41 /* codes used to read/write quotes to LyX files
42  * e    ``english''
43  * s    ''spanish''
44  * g    ,,german``
45  * p    ,,polish''
46  * f    <<french>>
47  * a    >>danish<<
48  */
49
50 char const * const language_char = "esgpfa";
51 char const * const side_char = "lr" ;
52 char const * const times_char = "sd";
53
54 // List of known quote chars
55 char const * const quote_char = ",'`<>";
56
57 // Unicode characters needed by each quote type
58 char_type const display_quote_char[2][5] = {
59         { 0x201a, 0x2019, 0x2018, 0x2039, 0x203a},
60         { 0x201e, 0x201d, 0x201c, 0x00ab, 0x00bb}
61 };
62
63 // Index of chars used for the quote. Index is [side, language]
64 int quote_index[2][6] = {
65         { 2, 1, 0, 0, 3, 4 },    // "'',,<>"
66         { 1, 1, 2, 1, 4, 3 }     // "`'`'><"
67 };
68
69 // Corresponding LaTeX code, for double and single quotes.
70 char const * const latex_quote_t1[2][5] = {
71         { "\\quotesinglbase ",  "'", "`",
72     "\\guilsinglleft{}", "\\guilsinglright{}" },
73   { ",,", "''", "``", "<<", ">>" }
74 };
75
76 char const * const latex_quote_ot1[2][5] = {
77         { "\\quotesinglbase ",  "'", "`",
78     "\\guilsinglleft{}", "\\guilsinglright{}" },
79   { "\\quotedblbase ", "''", "``",
80     "\\guillemotleft{}", "\\guillemotright{}" }
81 };
82
83 char const * const latex_quote_babel[2][5] = {
84         { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
85   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" }
86 };
87
88 } // namespace anon
89
90
91 InsetQuotes::InsetQuotes(Buffer const & buf, string const & str)
92 {
93         parseString(str);
94         setBuffer(const_cast<Buffer &>(buf));
95 }
96
97 InsetQuotes::InsetQuotes(Buffer const & buf, char_type c)
98         : language_(buf.params().quotes_language), times_(buf.params().quotes_times)
99 {
100         setSide(c);
101         setBuffer(const_cast<Buffer &>(buf));
102 }
103
104
105 InsetQuotes::InsetQuotes(Buffer const & buf, char_type c, QuoteTimes t)
106         : language_(buf.params().quotes_language), times_(t)
107 {
108         setSide(c);
109         setBuffer(const_cast<Buffer &>(buf));
110 }
111
112
113 docstring InsetQuotes::name() const
114 {
115         return from_ascii("Quotes");
116 }
117
118
119 void InsetQuotes::setSide(char_type c)
120 {
121         // Decide whether left or right
122         switch (c) {
123         case ' ':
124         case '(':
125         case '[':
126                 side_ = LeftQuote;   // left quote
127                 break;
128         default:
129                 side_ = RightQuote;  // right quote
130         }
131 }
132
133
134 void InsetQuotes::parseString(string const & s)
135 {
136         string str = s;
137         if (str.length() != 3) {
138                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
139                         " bad string length." << endl;
140                 str = "eld";
141         }
142
143         int i;
144
145         for (i = 0; i < 6; ++i) {
146                 if (str[0] == language_char[i]) {
147                         language_ = QuoteLanguage(i);
148                         break;
149                 }
150         }
151         if (i >= 6) {
152                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
153                         " bad language specification." << endl;
154                 language_ = EnglishQuotes;
155         }
156
157         for (i = 0; i < 2; ++i) {
158                 if (str[1] == side_char[i]) {
159                         side_ = QuoteSide(i);
160                         break;
161                 }
162         }
163         if (i >= 2) {
164                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
165                         " bad side specification." << endl;
166                 side_ = LeftQuote;
167         }
168
169         for (i = 0; i < 2; ++i) {
170                 if (str[2] == times_char[i]) {
171                         times_ = QuoteTimes(i);
172                         break;
173                 }
174         }
175         if (i >= 2) {
176                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
177                         " bad times specification." << endl;
178                 times_ = DoubleQuotes;
179         }
180 }
181
182
183 docstring InsetQuotes::displayString() const
184 {
185         Language const * loclang = buffer().params().language;
186         int const index = quote_index[side_][language_];
187         docstring retdisp = docstring(1, display_quote_char[times_][index]);
188
189         // in french, spaces are added inside double quotes
190         if (times_ == DoubleQuotes && prefixIs(loclang->code(), "fr")) {
191                 if (side_ == LeftQuote)
192                         retdisp += ' ';
193                 else
194                         retdisp.insert(size_t(0), 1, ' ');
195         }
196
197         return retdisp;
198 }
199
200
201 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
202 {
203         FontInfo & font = mi.base.font;
204         frontend::FontMetrics const & fm =
205                 theFontMetrics(font);
206         dim.asc = fm.maxAscent();
207         dim.des = fm.maxDescent();
208         dim.wid = 0;
209
210         // FIXME: should we add a language or a font parameter member?
211         docstring const text = displayString();
212         for (string::size_type i = 0; i < text.length(); ++i) {
213                 if (text[i] == ' ')
214                         dim.wid += fm.width('i');
215                 else if (i == 0 || text[i] != text[i - 1])
216                         dim.wid += fm.width(text[i]);
217                 else
218                         dim.wid += fm.width(',');
219         }
220 }
221
222
223 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
224 {
225         // FIXME: should we add a language or a font parameter member?
226         docstring const text = displayString();
227
228         if (text.length() == 2 && text[0] == text[1]) {
229                 pi.pain.text(x, y, text[0], pi.base.font);
230                 int const t = theFontMetrics(pi.base.font)
231                         .width(',');
232                 pi.pain.text(x + t, y, text[0], pi.base.font);
233         } else {
234                 pi.pain.text(x, y, text, pi.base.font);
235         }
236 }
237
238
239 void InsetQuotes::write(ostream & os) const
240 {
241         string text;
242         text += language_char[language_];
243         text += side_char[side_];
244         text += times_char[times_];
245         os << "Quotes " << text;
246 }
247
248
249 void InsetQuotes::read(Lexer & lex)
250 {
251         lex.setContext("InsetQuotes::read");
252         lex.next();
253         parseString(lex.getString());
254         lex >> "\\end_inset";
255 }
256
257
258 int InsetQuotes::latex(odocstream & os, OutputParams const & runparams) const
259 {
260         const int quoteind = quote_index[side_][language_];
261         string qstr;
262
263         if (language_ == FrenchQuotes && times_ == DoubleQuotes
264             && prefixIs(runparams.local_font->language()->code(), "fr")) {
265                 if (side_ == LeftQuote)
266                         qstr = "\\og "; //the spaces are important here
267                 else
268                         qstr = " \\fg{}"; //and here
269         } else if (lyxrc.fontenc == "T1") {
270                 qstr = latex_quote_t1[times_][quoteind];
271 #ifdef DO_USE_DEFAULT_LANGUAGE
272         } else if (doclang == "default") {
273 #else
274         } else if (!runparams.use_babel) {
275 #endif
276                 qstr = latex_quote_ot1[times_][quoteind];
277         } else {
278                 qstr = latex_quote_babel[times_][quoteind];
279         }
280
281         // Always guard against unfortunate ligatures (!` ?`)
282         if (prefixIs(qstr, "`"))
283                 qstr.insert(0, "{}");
284
285         os << from_ascii(qstr);
286         return 0;
287 }
288
289
290 int InsetQuotes::plaintext(odocstream & os, OutputParams const &) const
291 {
292         docstring const str = displayString();
293         os << str;
294         return str.size();
295 }
296
297
298 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
299 {
300         if (times_ == DoubleQuotes) {
301                 if (side_ == LeftQuote)
302                         os << "&ldquo;";
303                 else
304                         os << "&rdquo;";
305         } else {
306                 if (side_ == LeftQuote)
307                         os << "&lsquo;";
308                 else
309                         os << "&rsquo;";
310         }
311         return 0;
312 }
313
314
315 docstring InsetQuotes::xhtml(odocstream & os, OutputParams const & op) const
316 {
317         docbook(os, op);
318         return docstring();
319 }
320
321
322 void InsetQuotes::tocString(odocstream & os) const
323 {
324         os << displayString();
325 }
326
327
328 void InsetQuotes::validate(LaTeXFeatures & features) const
329 {
330         bool const use_babel = features.useBabel();
331         char type = quote_char[quote_index[side_][language_]];
332
333 #ifdef DO_USE_DEFAULT_LANGUAGE
334         if (features.bufferParams().language->lang() == "default"
335 #else
336         if (!use_babel
337 #endif
338             && lyxrc.fontenc != "T1") {
339                 if (times_ == SingleQuotes)
340                         switch (type) {
341                         case ',': features.require("quotesinglbase"); break;
342                         case '<': features.require("guilsinglleft");  break;
343                         case '>': features.require("guilsinglright"); break;
344                         default: break;
345                         }
346                 else
347                         switch (type) {
348                         case ',': features.require("quotedblbase");   break;
349                         case '<': features.require("guillemotleft");  break;
350                         case '>': features.require("guillemotright"); break;
351                         default: break;
352                         }
353         }
354 }
355
356 } // namespace lyx