]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
Output docbook as utf8. Probably quite a bit more work needed, but then help form...
[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 using lyx::docstring;
34 using lyx::odocstream;
35 using lyx::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 }
89
90
91 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
92         : language_(l), side_(s), times_(t)
93 {}
94
95
96 InsetQuotes::InsetQuotes(lyx::char_type c, BufferParams const & params)
97         : language_(params.quotes_language), times_(params.quotes_times)
98 {
99         getPosition(c);
100 }
101
102
103 InsetQuotes::InsetQuotes(lyx::char_type c, quote_language l, quote_times t)
104         : language_(l), times_(t)
105 {
106         getPosition(c);
107 }
108
109
110 void InsetQuotes::getPosition(lyx::char_type c)
111 {
112         // Decide whether left or right
113         switch (c) {
114         case ' ': case '(': case '[':
115                 side_ = LeftQ;   // left quote
116                 break;
117         default:
118                 side_ = RightQ;  // right quote
119         }
120 }
121
122
123 void InsetQuotes::parseString(string const & s)
124 {
125         string str(s);
126         if (str.length() != 3) {
127                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
128                         " bad string length." << endl;
129                 str = "eld";
130         }
131
132         int i;
133
134         for (i = 0; i < 6; ++i) {
135                 if (str[0] == language_char[i]) {
136                         language_ = quote_language(i);
137                         break;
138                 }
139         }
140         if (i >= 6) {
141                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
142                         " bad language specification." << endl;
143                 language_ = EnglishQ;
144         }
145
146         for (i = 0; i < 2; ++i) {
147                 if (str[1] == side_char[i]) {
148                         side_ = quote_side(i);
149                         break;
150                 }
151         }
152         if (i >= 2) {
153                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
154                         " bad side specification." << endl;
155                 side_ = LeftQ;
156         }
157
158         for (i = 0; i < 2; ++i) {
159                 if (str[2] == times_char[i]) {
160                         times_ = quote_times(i);
161                         break;
162                 }
163         }
164         if (i >= 2) {
165                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
166                         " bad times specification." << endl;
167                 times_ = DoubleQ;
168         }
169 }
170
171
172 string const InsetQuotes::dispString(Language const * loclang) const
173 {
174         string disp;
175         disp += quote_char[quote_index[side_][language_]];
176         if (times_ == DoubleQ)
177                 disp += disp;
178
179         if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
180             || lyxrc.font_norm_type == LyXRC::ISO_8859_9
181             || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
182                 if (disp == "<<")
183                         disp = '«';
184                 else if (disp == ">>")
185                         disp = '»';
186         }
187
188         // in french, spaces are added inside double quotes
189         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
190                 if (side_ == LeftQ)
191                         disp += ' ';
192                 else
193                         disp.insert(string::size_type(0), 1, ' ');
194         }
195
196         return disp;
197 }
198
199
200 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
201 {
202         LyXFont & font = mi.base.font;
203         lyx::frontend::FontMetrics const & fm =
204                 theFontMetrics(font);
205         dim.asc = fm.maxAscent();
206         dim.des = fm.maxDescent();
207         dim.wid = 0;
208
209         string const text = dispString(font.language());
210         for (string::size_type i = 0; i < text.length(); ++i) {
211                 if (text[i] == ' ')
212                         dim.wid += fm.width('i');
213                 else if (i == 0 || text[i] != text[i - 1])
214                         dim.wid += fm.width(text[i]);
215                 else
216                         dim.wid += fm.width(',');
217         }
218         dim_ = dim;
219 }
220
221
222 #if 0
223 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
224 {
225 #if 1
226         return f;
227 #else
228         LyXFont font(f);
229         return font;
230 #endif
231 }
232 #endif
233
234
235 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
236 {
237         string const text = dispString(pi.base.font.language());
238
239         if (text.length() == 2 && text[0] == text[1]) {
240                 pi.pain.text(x, y, text[0], pi.base.font);
241                 int const t = theFontMetrics(pi.base.font)
242                         .width(',');
243                 pi.pain.text(x + t, y, text[0], pi.base.font);
244         } else {
245                 docstring dtext(text.begin(), text.end());
246                 pi.pain.text(x, y, dtext, pi.base.font);
247         }
248 }
249
250
251 void InsetQuotes::write(Buffer const &, ostream & os) const
252 {
253         string text;
254         text += language_char[language_];
255         text += side_char[side_];
256         text += times_char[times_];
257         os << "Quotes " << text;
258 }
259
260
261 void InsetQuotes::read(Buffer const &, LyXLex & lex)
262 {
263         lex.next();
264         parseString(lex.getString());
265         lex.next();
266         if (lex.getString() != "\\end_inset") {
267                 lex.printError("Missing \\end_inset at this point");
268         }
269 }
270
271
272 int InsetQuotes::latex(Buffer const &, odocstream & os,
273                        OutputParams const & runparams) const
274 {
275         const int quoteind = quote_index[side_][language_];
276         string qstr;
277
278         if (language_ == FrenchQ && times_ == DoubleQ
279             && prefixIs(runparams.local_font->language()->code(), "fr")) {
280                 if (side_ == LeftQ)
281                         qstr = "\\og "; //the spaces are important here
282                 else
283                         qstr = " \\fg{}"; //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 << lyx::from_ascii(qstr);
301         return 0;
302 }
303
304
305 int InsetQuotes::plaintext(Buffer const &, odocstream & os,
306                        OutputParams const &) const
307 {
308         os << '"';
309         return 0;
310 }
311
312
313 int InsetQuotes::docbook(Buffer const &, odocstream & os,
314                          OutputParams const &) const
315 {
316         if (times_ == DoubleQ) {
317                 if (side_ == LeftQ)
318                         os << "&ldquo;";
319                 else
320                         os << "&rdquo;";
321         } else {
322                 if (side_ == LeftQ)
323                         os << "&lsquo;";
324                 else
325                         os << "&rsquo;";
326         }
327         return 0;
328 }
329
330
331 int InsetQuotes::textString(Buffer const & buf, odocstream & os,
332                        OutputParams const & op) const
333 {
334         return plaintext(buf, os, op);
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::doClone() const
368 {
369         return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
370 }
371
372
373 InsetBase::Code InsetQuotes::lyxCode() const
374 {
375   return InsetBase::QUOTE_CODE;
376 }