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