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