]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
Move depth_type to support/types.h.
[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 "debug.h"
17 #include "language.h"
18 #include "LaTeXFeatures.h"
19 #include "latexrunparams.h"
20 #include "lyxlex.h"
21 #include "lyxrc.h"
22 #include "metricsinfo.h"
23 #include "paragraph.h"
24 #include "paragraph_funcs.h"
25
26 #include "frontends/font_metrics.h"
27 #include "frontends/Painter.h"
28
29 #include "support/LAssert.h"
30 #include "support/lstrings.h"
31
32 using namespace lyx::support;
33
34 using std::endl;
35 using std::auto_ptr;
36 using std::ostream;
37
38
39 namespace {
40
41 /* codes used to read/write quotes to LyX files
42  * e    ``english''
43  * s    ''spanish''
44  * g    ,,german``
45  * p    ,,polish''
46  * f    <<french>>
47  * a    >>danish<<
48  */
49
50 char const * const language_char = "esgpfa";
51 char const * const side_char = "lr" ;
52 char const * const times_char = "sd";
53
54 // List of known quote chars
55 char const * const quote_char = ",'`<>";
56
57 // Index of chars used for the quote. Index is [side, language]
58 int quote_index[2][6] = {
59         { 2, 1, 0, 0, 3, 4 },    // "'',,<>"
60         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
61
62 // Corresponding LaTeX code, for double and single quotes.
63 char const * const latex_quote_t1[2][5] =
64 { { "\\quotesinglbase ",  "'", "`",
65     "\\guilsinglleft{}", "\\guilsinglright{}" },
66   { ",,", "''", "``", "<<", ">>" } };
67
68 char const * const latex_quote_ot1[2][5] =
69 { { "\\quotesinglbase ",  "'", "`",
70     "\\guilsinglleft{}", "\\guilsinglright{}" },
71   { "\\quotedblbase ", "''", "``",
72     "\\guillemotleft{}", "\\guillemotright{}" } };
73
74 char const * const latex_quote_babel[2][5] =
75 { { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
76   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
77
78 } // namespace anon
79
80
81 InsetQuotes::InsetQuotes(string const & str)
82 {
83         parseString(str);
84 }
85
86
87 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
88         : language_(l), side_(s), times_(t)
89 {}
90
91
92 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
93         : language_(params.quotes_language), times_(params.quotes_times)
94 {
95         // Decide whether left or right
96         switch (c) {
97         case ' ': case '(':
98 #warning eh ? I am lost here ...
99         //case Paragraph::META_HFILL:
100         // case Paragraph::META_NEWLINE:
101                 side_ = LeftQ;   // left quote
102                 break;
103         default:
104                 side_ = RightQ;  // right quote
105         }
106 }
107
108
109 void InsetQuotes::parseString(string const & s)
110 {
111         string str(s);
112         if (str.length() != 3) {
113                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
114                         " bad string length." << endl;
115                 str = "eld";
116         }
117
118         int i;
119
120         for (i = 0; i < 6; ++i) {
121                 if (str[0] == language_char[i]) {
122                         language_ = quote_language(i);
123                         break;
124                 }
125         }
126         if (i >= 6) {
127                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
128                         " bad language specification." << endl;
129                 language_ = EnglishQ;
130         }
131
132         for (i = 0; i < 2; ++i) {
133                 if (str[1] == side_char[i]) {
134                         side_ = quote_side(i);
135                         break;
136                 }
137         }
138         if (i >= 2) {
139                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
140                         " bad side specification." << endl;
141                 side_ = LeftQ;
142         }
143
144         for (i = 0; i < 2; ++i) {
145                 if (str[2] == times_char[i]) {
146                         times_ = quote_times(i);
147                         break;
148                 }
149         }
150         if (i >= 2) {
151                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
152                         " bad times specification." << endl;
153                 times_ = DoubleQ;
154         }
155 }
156
157
158 string const InsetQuotes::dispString(Language const * loclang) const
159 {
160         string disp;
161         disp += quote_char[quote_index[side_][language_]];
162         if (times_ == DoubleQ)
163                 disp += disp;
164
165         if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
166             || lyxrc.font_norm_type == LyXRC::ISO_8859_9
167             || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
168                 if (disp == "<<")
169                         disp = '«';
170                 else if (disp == ">>")
171                         disp = '»';
172         }
173
174         // in french, spaces are added inside double quotes
175         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
176                 if (side_ == LeftQ)
177                         disp += ' ';
178                 else
179                         disp.insert(string::size_type(0), 1, ' ');
180         }
181
182         return disp;
183 }
184
185
186 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
187 {
188         LyXFont & font = mi.base.font;
189         dim.asc = font_metrics::maxAscent(font);
190         dim.des = font_metrics::maxDescent(font);
191         dim.wid = 0;
192
193         string const text = dispString(font.language());
194         for (string::size_type i = 0; i < text.length(); ++i) {
195                 if (text[i] == ' ')
196                         dim.wid += font_metrics::width('i', font);
197                 else if (i == 0 || text[i] != text[i - 1])
198                         dim.wid += font_metrics::width(text[i], font);
199                 else
200                         dim.wid += font_metrics::width(',', font);
201         }
202         dim_ = dim;
203 }
204
205
206 #if 0
207 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
208 {
209 #if 1
210         return f;
211 #else
212         LyXFont font(f);
213         return font;
214 #endif
215 }
216 #endif
217
218
219 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
220 {
221         string const text = dispString(pi.base.font.language());
222
223         if (text.length() == 2 && text[0] == text[1]) {
224                 pi.pain.text(x, y, text[0], pi.base.font);
225                 int const t = font_metrics::width(',', pi.base.font);
226                 pi.pain.text(x + t, y, text[0], pi.base.font);
227         } else {
228                 pi.pain.text(x, y, text, pi.base.font);
229         }
230 }
231
232
233 void InsetQuotes::write(Buffer const &, ostream & os) const
234 {
235         string text;
236         text += language_char[language_];
237         text += side_char[side_];
238         text += times_char[times_];
239         os << "Quotes " << text;
240 }
241
242
243 void InsetQuotes::read(Buffer const &, LyXLex & lex)
244 {
245         lex.nextToken();
246         parseString(lex.getString());
247         lex.next();
248         if (lex.getString() != "\\end_inset") {
249                 lex.printError("Missing \\end_inset at this point");
250         }
251 }
252
253
254 int InsetQuotes::latex(Buffer const & buf, ostream & os,
255                        LatexRunParams const & runparams) const
256 {
257         // How do we get the local language here??
258         lyx::pos_type curr_pos = ownerPar(buf, this).getPositionOfInset(this);
259         Assert(curr_pos != -1);
260
261 #warning FIXME. We _must_ find another way to get the language. (Lgb)
262 #if 0
263         // This cannot be used. (Lgb)
264         string const curr_lang =
265                 parOwner()->getFont(buf->params, 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 }