]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
Fix text frame drawing.
[lyx.git] / src / insets / InsetQuotes.cpp
1 /**
2  * \file InsetQuotes.cpp
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 "BufferParams.h"
17 #include "debug.h"
18 #include "Language.h"
19 #include "LaTeXFeatures.h"
20 #include "Lexer.h"
21 #include "LyXRC.h"
22 #include "MetricsInfo.h"
23 #include "OutputParams.h"
24
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
27
28 #include "support/lstrings.h"
29
30
31 namespace lyx {
32
33 using support::prefixIs;
34
35 using std::endl;
36 using std::string;
37 using std::ostream;
38
39
40 namespace {
41
42 /* codes used to read/write quotes to LyX files
43  * e    ``english''
44  * s    ''spanish''
45  * g    ,,german``
46  * p    ,,polish''
47  * f    <<french>>
48  * a    >>danish<<
49  */
50
51 char const * const language_char = "esgpfa";
52 char const * const side_char = "lr" ;
53 char const * const times_char = "sd";
54
55 // List of known quote chars
56 char const * const quote_char = ",'`<>";
57
58 // Index of chars used for the quote. Index is [side, language]
59 int quote_index[2][6] = {
60         { 2, 1, 0, 0, 3, 4 },    // "'',,<>"
61         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
62
63 // Corresponding LaTeX code, for double and single quotes.
64 char const * const latex_quote_t1[2][5] =
65 { { "\\quotesinglbase ",  "'", "`",
66     "\\guilsinglleft{}", "\\guilsinglright{}" },
67   { ",,", "''", "``", "<<", ">>" } };
68
69 char const * const latex_quote_ot1[2][5] =
70 { { "\\quotesinglbase ",  "'", "`",
71     "\\guilsinglleft{}", "\\guilsinglright{}" },
72   { "\\quotedblbase ", "''", "``",
73     "\\guillemotleft{}", "\\guillemotright{}" } };
74
75 char const * const latex_quote_babel[2][5] =
76 { { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
77   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
78
79 } // namespace anon
80
81
82 InsetQuotes::InsetQuotes(string const & str)
83 {
84         parseString(str);
85 }
86
87
88 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
89         : language_(l), side_(s), times_(t)
90 {
91 }
92
93
94 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
95         : language_(params.quotes_language), times_(params.quotes_times)
96 {
97         getPosition(c);
98 }
99
100
101 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
102         : language_(l), times_(t)
103 {
104         getPosition(c);
105 }
106
107
108 void InsetQuotes::getPosition(char_type c)
109 {
110         // Decide whether left or right
111         switch (c) {
112         case ' ': case '(': case '[':
113                 side_ = LeftQ;   // left quote
114                 break;
115         default:
116                 side_ = RightQ;  // right quote
117         }
118 }
119
120
121 void InsetQuotes::parseString(string const & s)
122 {
123         string str(s);
124         if (str.length() != 3) {
125                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
126                         " bad string length." << endl;
127                 str = "eld";
128         }
129
130         int i;
131
132         for (i = 0; i < 6; ++i) {
133                 if (str[0] == language_char[i]) {
134                         language_ = quote_language(i);
135                         break;
136                 }
137         }
138         if (i >= 6) {
139                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
140                         " bad language specification." << endl;
141                 language_ = EnglishQ;
142         }
143
144         for (i = 0; i < 2; ++i) {
145                 if (str[1] == side_char[i]) {
146                         side_ = quote_side(i);
147                         break;
148                 }
149         }
150         if (i >= 2) {
151                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
152                         " bad side specification." << endl;
153                 side_ = LeftQ;
154         }
155
156         for (i = 0; i < 2; ++i) {
157                 if (str[2] == times_char[i]) {
158                         times_ = quote_times(i);
159                         break;
160                 }
161         }
162         if (i >= 2) {
163                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
164                         " bad times specification." << endl;
165                 times_ = DoubleQ;
166         }
167 }
168
169
170 docstring const InsetQuotes::dispString(Language const * loclang) const
171 {
172         string disp;
173         disp += quote_char[quote_index[side_][language_]];
174         if (times_ == DoubleQ)
175                 disp += disp;
176
177
178         docstring retdisp;
179         if (disp == "<<")
180                 retdisp = docstring(1, 0x00ab); //'«';
181         else if (disp == ">>")
182                 retdisp = docstring(1, 0x00bb); //'»';
183 #if 0
184         // The below are supposed to work, but something fails.
185         else if (disp == ",,")
186                 retdisp = docstring(1, 0x201e);
187         else if (disp == "''")
188                 retdisp == docstring(1, 0x201d);
189         else if (disp == "``")
190                 retdisp == docstring(1, 0x201c);
191         else if (disp == "<")
192                 retdisp = docstring(1, 0x2039);
193         else if (disp == ">")
194                 retdisp = docstring(1, 0x203a);
195         else if (disp == ",")
196                 retdisp = docstring(1, 0x201a);
197         else if (disp == "'")
198                 retdisp = docstring(1, 0x2019);
199         else if (disp == "`")
200                 retdisp = docstring(1, 0x2018);
201 #endif
202         else
203                 retdisp = lyx::from_ascii(disp);
204
205         // in french, spaces are added inside double quotes
206         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
207                 if (side_ == LeftQ)
208                         retdisp += ' ';
209                 else
210                         retdisp.insert(docstring::size_type(0), 1, ' ');
211         }
212
213         return retdisp;
214 }
215
216
217 bool InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
218 {
219         Font & font = mi.base.font;
220         frontend::FontMetrics const & fm =
221                 theFontMetrics(font);
222         dim.asc = fm.maxAscent();
223         dim.des = fm.maxDescent();
224         dim.wid = 0;
225
226         docstring const text = dispString(font.language());
227         for (string::size_type i = 0; i < text.length(); ++i) {
228                 if (text[i] == ' ')
229                         dim.wid += fm.width('i');
230                 else if (i == 0 || text[i] != text[i - 1])
231                         dim.wid += fm.width(text[i]);
232                 else
233                         dim.wid += fm.width(',');
234         }
235         bool const changed = dim_ != dim;
236         dim_ = dim;
237         return changed;
238 }
239
240
241 #if 0
242 Font const InsetQuotes::convertFont(Font const & f) const
243 {
244 #if 1
245         return f;
246 #else
247         Font font(f);
248         return font;
249 #endif
250 }
251 #endif
252
253
254 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
255 {
256         docstring const text = dispString(pi.base.font.language());
257
258         if (text.length() == 2 && text[0] == text[1]) {
259                 pi.pain.text(x, y, text[0], pi.base.font);
260                 int const t = theFontMetrics(pi.base.font)
261                         .width(',');
262                 pi.pain.text(x + t, y, text[0], pi.base.font);
263         } else {
264                 pi.pain.text(x, y, text, pi.base.font);
265         }
266         setPosCache(pi, x, y);
267 }
268
269
270 void InsetQuotes::write(Buffer const &, ostream & os) const
271 {
272         string text;
273         text += language_char[language_];
274         text += side_char[side_];
275         text += times_char[times_];
276         os << "Quotes " << text;
277 }
278
279
280 void InsetQuotes::read(Buffer const &, Lexer & lex)
281 {
282         lex.next();
283         parseString(lex.getString());
284         lex.next();
285         if (lex.getString() != "\\end_inset") {
286                 lex.printError("Missing \\end_inset at this point");
287         }
288 }
289
290
291 int InsetQuotes::latex(Buffer const &, odocstream & os,
292                        OutputParams const & runparams) const
293 {
294         const int quoteind = quote_index[side_][language_];
295         string qstr;
296
297         if (language_ == FrenchQ && times_ == DoubleQ
298             && prefixIs(runparams.local_font->language()->code(), "fr")) {
299                 if (side_ == LeftQ)
300                         qstr = "\\og "; //the spaces are important here
301                 else
302                         qstr = " \\fg{}"; //and here
303         } else if (lyxrc.fontenc == "T1") {
304                 qstr = latex_quote_t1[times_][quoteind];
305 #ifdef DO_USE_DEFAULT_LANGUAGE
306         } else if (doclang == "default") {
307 #else
308         } else if (!runparams.use_babel) {
309 #endif
310                 qstr = latex_quote_ot1[times_][quoteind];
311         } else {
312                 qstr = latex_quote_babel[times_][quoteind];
313         }
314
315         // Always guard against unfortunate ligatures (!` ?`)
316         if (prefixIs(qstr, "`"))
317                 qstr.insert(0, "{}");
318
319         os << from_ascii(qstr);
320         return 0;
321 }
322
323
324 int InsetQuotes::plaintext(Buffer const & buf, odocstream & os,
325                            OutputParams const &) const
326 {
327         docstring const str = dispString(buf.params().language);
328         os << str;
329         return str.size();
330 }
331
332
333 int InsetQuotes::docbook(Buffer const &, odocstream & os,
334                          OutputParams const &) const
335 {
336         if (times_ == DoubleQ) {
337                 if (side_ == LeftQ)
338                         os << "&ldquo;";
339                 else
340                         os << "&rdquo;";
341         } else {
342                 if (side_ == LeftQ)
343                         os << "&lsquo;";
344                 else
345                         os << "&rsquo;";
346         }
347         return 0;
348 }
349
350
351 void InsetQuotes::textString(Buffer const & buf, odocstream & os) const
352 {
353         os << dispString(buf.params().language);
354 }
355
356
357 void InsetQuotes::validate(LaTeXFeatures & features) const
358 {
359         bool const use_babel = features.useBabel();
360         char type = quote_char[quote_index[side_][language_]];
361
362 #ifdef DO_USE_DEFAULT_LANGUAGE
363         if (features.bufferParams().language->lang() == "default"
364 #else
365         if (!use_babel
366 #endif
367             && lyxrc.fontenc != "T1") {
368                 if (times_ == SingleQ)
369                         switch (type) {
370                                 case ',': features.require("quotesinglbase");  break;
371                         case '<': features.require("guilsinglleft");  break;
372                         case '>': features.require("guilsinglright"); break;
373                         default: break;
374                         }
375                 else
376                         switch (type) {
377                         case ',': features.require("quotedblbase");   break;
378                         case '<': features.require("guillemotleft");  break;
379                         case '>': features.require("guillemotright"); break;
380                         default: break;
381                         }
382         }
383 }
384
385
386 Inset * InsetQuotes::clone() const
387 {
388         return new InsetQuotes(language_, side_, times_);
389 }
390
391
392 Inset::Code InsetQuotes::lyxCode() const
393 {
394         return Inset::QUOTE_CODE;
395 }
396
397
398 } // namespace lyx