]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
- InsetQuotes updates the inset cache
[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("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("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("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("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 string 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         if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
185             || lyxrc.font_norm_type == LyXRC::ISO_8859_9
186             || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
187                 if (disp == "<<")
188                         disp = '«';
189                 else if (disp == ">>")
190                         disp = '»';
191         }
192
193         // in french, spaces are added inside double quotes
194         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
195                 if (side_ == LeftQ)
196                         disp += ' ';
197                 else
198                         disp.insert(string::size_type(0), 1, ' ');
199         }
200
201         return disp;
202 }
203
204
205 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
206 {
207         LyXFont & font = mi.base.font;
208         frontend::FontMetrics const & fm =
209                 theFontMetrics(font);
210         dim.asc = fm.maxAscent();
211         dim.des = fm.maxDescent();
212         dim.wid = 0;
213
214         string const text = dispString(font.language());
215         for (string::size_type i = 0; i < text.length(); ++i) {
216                 if (text[i] == ' ')
217                         dim.wid += fm.width('i');
218                 else if (i == 0 || text[i] != text[i - 1])
219                         dim.wid += fm.width(text[i]);
220                 else
221                         dim.wid += fm.width(',');
222         }
223         dim_ = dim;
224 }
225
226
227 #if 0
228 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
229 {
230 #if 1
231         return f;
232 #else
233         LyXFont font(f);
234         return font;
235 #endif
236 }
237 #endif
238
239
240 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
241 {
242         string const text = dispString(pi.base.font.language());
243
244         if (text.length() == 2 && text[0] == text[1]) {
245                 pi.pain.text(x, y, text[0], pi.base.font);
246                 int const t = theFontMetrics(pi.base.font)
247                         .width(',');
248                 pi.pain.text(x + t, y, text[0], pi.base.font);
249         } else {
250                 docstring dtext(text.begin(), text.end());
251                 pi.pain.text(x, y, dtext, pi.base.font);
252         }
253         setPosCache(pi, x, y);
254 }
255
256
257 void InsetQuotes::write(Buffer const &, ostream & os) const
258 {
259         string text;
260         text += language_char[language_];
261         text += side_char[side_];
262         text += times_char[times_];
263         os << "Quotes " << text;
264 }
265
266
267 void InsetQuotes::read(Buffer const &, LyXLex & lex)
268 {
269         lex.next();
270         parseString(lex.getString());
271         lex.next();
272         if (lex.getString() != "\\end_inset") {
273                 lex.printError("Missing \\end_inset at this point");
274         }
275 }
276
277
278 int InsetQuotes::latex(Buffer const &, odocstream & os,
279                        OutputParams const & runparams) const
280 {
281         const int quoteind = quote_index[side_][language_];
282         string qstr;
283
284         if (language_ == FrenchQ && times_ == DoubleQ
285             && prefixIs(runparams.local_font->language()->code(), "fr")) {
286                 if (side_ == LeftQ)
287                         qstr = "\\og "; //the spaces are important here
288                 else
289                         qstr = " \\fg{}"; //and here
290         } else if (lyxrc.fontenc == "T1") {
291                 qstr = latex_quote_t1[times_][quoteind];
292 #ifdef DO_USE_DEFAULT_LANGUAGE
293         } else if (doclang == "default") {
294 #else
295         } else if (!runparams.use_babel) {
296 #endif
297                 qstr = latex_quote_ot1[times_][quoteind];
298         } else {
299                 qstr = latex_quote_babel[times_][quoteind];
300         }
301
302         // Always guard against unfortunate ligatures (!` ?`)
303         if (prefixIs(qstr, "`"))
304                 qstr.insert(0, "{}");
305
306         os << from_ascii(qstr);
307         return 0;
308 }
309
310
311 int InsetQuotes::plaintext(Buffer const &, odocstream & os,
312                        OutputParams const &) const
313 {
314         os << '"';
315         return 0;
316 }
317
318
319 int InsetQuotes::docbook(Buffer const &, odocstream & os,
320                          OutputParams const &) const
321 {
322         if (times_ == DoubleQ) {
323                 if (side_ == LeftQ)
324                         os << "&ldquo;";
325                 else
326                         os << "&rdquo;";
327         } else {
328                 if (side_ == LeftQ)
329                         os << "&lsquo;";
330                 else
331                         os << "&rsquo;";
332         }
333         return 0;
334 }
335
336
337 int InsetQuotes::textString(Buffer const & buf, odocstream & os,
338                        OutputParams const & op) const
339 {
340         return plaintext(buf, os, op);
341 }
342
343
344 void InsetQuotes::validate(LaTeXFeatures & features) const
345 {
346         bool const use_babel = features.useBabel();
347         char type = quote_char[quote_index[side_][language_]];
348
349 #ifdef DO_USE_DEFAULT_LANGUAGE
350         if (features.bufferParams().language->lang() == "default"
351 #else
352         if (!use_babel
353 #endif
354             && lyxrc.fontenc != "T1") {
355                 if (times_ == SingleQ)
356                         switch (type) {
357                                 case ',': features.require("quotesinglbase");  break;
358                         case '<': features.require("guilsinglleft");  break;
359                         case '>': features.require("guilsinglright"); break;
360                         default: break;
361                         }
362                 else
363                         switch (type) {
364                         case ',': features.require("quotedblbase");   break;
365                         case '<': features.require("guillemotleft");  break;
366                         case '>': features.require("guillemotright"); break;
367                         default: break;
368                         }
369         }
370 }
371
372
373 auto_ptr<InsetBase> InsetQuotes::doClone() const
374 {
375         return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
376 }
377
378
379 InsetBase::Code InsetQuotes::lyxCode() const
380 {
381   return InsetBase::QUOTE_CODE;
382 }
383
384
385 } // namespace lyx