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