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