]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
two-phase-drawing for InsetText & InsetTabular
[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         dim_ = dim;
193 }
194
195
196 #if 0
197 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
198 {
199 #if 1
200         return f;
201 #else
202         LyXFont font(f);
203         return font;
204 #endif
205 }
206 #endif
207
208
209 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
210 {
211         string const text = dispString(pi.base.font.language());
212
213         if (text.length() == 2 && text[0] == text[1]) {
214                 pi.pain.text(x, y, text[0], pi.base.font);
215                 int const t = font_metrics::width(',', pi.base.font);
216                 pi.pain.text(x + t, y, text[0], pi.base.font);
217         } else {
218                 pi.pain.text(x, y, text, pi.base.font);
219         }
220 }
221
222
223 void InsetQuotes::write(Buffer const *, ostream & os) const
224 {
225         string text;
226         text += language_char[language_];
227         text += side_char[side_];
228         text += times_char[times_];
229         os << "Quotes " << text;
230 }
231
232
233 void InsetQuotes::read(Buffer const *, LyXLex & lex)
234 {
235         lex.nextToken();
236         parseString(lex.getString());
237         lex.next();
238         if (lex.getString() != "\\end_inset") {
239                 lex.printError("Missing \\end_inset at this point");
240         }
241 }
242
243
244 int InsetQuotes::latex(Buffer const * buf, ostream & os,
245                        LatexRunParams const & runparams) const
246 {
247         // How do we get the local language here??
248         lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
249         Assert(curr_pos != -1);
250
251 #warning FIXME. We _must_ find another way to get the language. (Lgb)
252 #if 0
253         // This cannot be used. (Lgb)
254         string const curr_lang =
255                 parOwner()->getFont(buf->params,
256                                     curr_pos).language()->babel();
257 #else
258         // And this is not the way... (Lgb)
259         string const curr_lang = buf->params.language->lang();
260 #endif
261         const int quoteind = quote_index[side_][language_];
262         string qstr;
263
264         if (language_ == FrenchQ && times_ == DoubleQ
265             && curr_lang == "frenchb") {
266                 if (side_ == LeftQ)
267                         qstr = "\\og "; //the spaces are important here
268                 else
269                         qstr = " \\fg{}"; //and here
270         } else if (language_ == FrenchQ && times_ == DoubleQ
271                    && curr_lang == "french") {
272                 if (side_ == LeftQ)
273                         qstr = "<< "; //the spaces are important here
274                 else
275                         qstr = " >>"; //and here
276         } else if (lyxrc.fontenc == "T1") {
277                 qstr = latex_quote_t1[times_][quoteind];
278 #ifdef DO_USE_DEFAULT_LANGUAGE
279         } else if (doclang == "default") {
280 #else
281         } else if (!runparams.use_babel) {
282 #endif
283                 qstr = latex_quote_ot1[times_][quoteind];
284         } else {
285                 qstr = latex_quote_babel[times_][quoteind];
286         }
287
288         // Always guard against unfortunate ligatures (!` ?`)
289         if (prefixIs(qstr, "`"))
290                 qstr.insert(0, "{}");
291
292         os << qstr;
293         return 0;
294 }
295
296
297 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
298 {
299         os << '"';
300         return 0;
301 }
302
303
304 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
305 {
306         os << '"';
307         return 0;
308 }
309
310
311 int InsetQuotes::docbook(Buffer const *, ostream & os, bool) const
312 {
313         if (times_ == DoubleQ) {
314                 if (side_ == LeftQ)
315                         os << "&ldquo;";
316                 else
317                         os << "&rdquo;";
318         } else {
319                 if (side_ == LeftQ)
320                         os << "&lsquo;";
321                 else
322                         os << "&rsquo;";
323         }
324         return 0;
325 }
326
327
328 void InsetQuotes::validate(LaTeXFeatures & features) const
329 {
330         bool const use_babel = features.useBabel();
331         char type = quote_char[quote_index[side_][language_]];
332
333 #ifdef DO_USE_DEFAULT_LANGUAGE
334         if (features.bufferParams().language->lang() == "default"
335 #else
336         if (!use_babel
337 #endif
338             && lyxrc.fontenc != "T1") {
339                 if (times_ == SingleQ)
340                         switch (type) {
341                                 case ',': features.require("quotesinglbase");  break;
342                         case '<': features.require("guilsinglleft");  break;
343                         case '>': features.require("guilsinglright"); break;
344                         default: break;
345                         }
346                 else
347                         switch (type) {
348                         case ',': features.require("quotedblbase");   break;
349                         case '<': features.require("guillemotleft");  break;
350                         case '>': features.require("guillemotright"); break;
351                         default: break;
352                         }
353         }
354 }
355
356
357 InsetBase * InsetQuotes::clone() const
358 {
359         return new InsetQuotes(language_, side_, times_);
360 }
361
362
363 Inset::Code InsetQuotes::lyxCode() const
364 {
365   return Inset::QUOTE_CODE;
366 }