]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
* src/paragraph_funcs.cpp (breakParagraph): change parameter 'flag' to
[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 void 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 }
236
237
238 #if 0
239 Font const InsetQuotes::convertFont(Font const & f) const
240 {
241 #if 1
242         return f;
243 #else
244         Font font(f);
245         return font;
246 #endif
247 }
248 #endif
249
250
251 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
252 {
253         docstring const text = dispString(pi.base.font.language());
254
255         if (text.length() == 2 && text[0] == text[1]) {
256                 pi.pain.text(x, y, text[0], pi.base.font);
257                 int const t = theFontMetrics(pi.base.font)
258                         .width(',');
259                 pi.pain.text(x + t, y, text[0], pi.base.font);
260         } else {
261                 pi.pain.text(x, y, text, pi.base.font);
262         }
263 }
264
265
266 void InsetQuotes::write(Buffer const &, ostream & os) const
267 {
268         string text;
269         text += language_char[language_];
270         text += side_char[side_];
271         text += times_char[times_];
272         os << "Quotes " << text;
273 }
274
275
276 void InsetQuotes::read(Buffer const &, Lexer & lex)
277 {
278         lex.next();
279         parseString(lex.getString());
280         lex.next();
281         if (lex.getString() != "\\end_inset") {
282                 lex.printError("Missing \\end_inset at this point");
283         }
284 }
285
286
287 int InsetQuotes::latex(Buffer const &, odocstream & os,
288                        OutputParams const & runparams) const
289 {
290         const int quoteind = quote_index[side_][language_];
291         string qstr;
292
293         if (language_ == FrenchQ && times_ == DoubleQ
294             && prefixIs(runparams.local_font->language()->code(), "fr")) {
295                 if (side_ == LeftQ)
296                         qstr = "\\og "; //the spaces are important here
297                 else
298                         qstr = " \\fg{}"; //and here
299         } else if (lyxrc.fontenc == "T1") {
300                 qstr = latex_quote_t1[times_][quoteind];
301 #ifdef DO_USE_DEFAULT_LANGUAGE
302         } else if (doclang == "default") {
303 #else
304         } else if (!runparams.use_babel) {
305 #endif
306                 qstr = latex_quote_ot1[times_][quoteind];
307         } else {
308                 qstr = latex_quote_babel[times_][quoteind];
309         }
310
311         // Always guard against unfortunate ligatures (!` ?`)
312         if (prefixIs(qstr, "`"))
313                 qstr.insert(0, "{}");
314
315         os << from_ascii(qstr);
316         return 0;
317 }
318
319
320 int InsetQuotes::plaintext(Buffer const & buf, odocstream & os,
321                            OutputParams const &) const
322 {
323         docstring const str = dispString(buf.params().language);
324         os << str;
325         return str.size();
326 }
327
328
329 int InsetQuotes::docbook(Buffer const &, odocstream & os,
330                          OutputParams const &) const
331 {
332         if (times_ == DoubleQ) {
333                 if (side_ == LeftQ)
334                         os << "&ldquo;";
335                 else
336                         os << "&rdquo;";
337         } else {
338                 if (side_ == LeftQ)
339                         os << "&lsquo;";
340                 else
341                         os << "&rsquo;";
342         }
343         return 0;
344 }
345
346
347 void InsetQuotes::textString(Buffer const & buf, odocstream & os) const
348 {
349         os << dispString(buf.params().language);
350 }
351
352
353 void InsetQuotes::validate(LaTeXFeatures & features) const
354 {
355         bool const use_babel = features.useBabel();
356         char type = quote_char[quote_index[side_][language_]];
357
358 #ifdef DO_USE_DEFAULT_LANGUAGE
359         if (features.bufferParams().language->lang() == "default"
360 #else
361         if (!use_babel
362 #endif
363             && lyxrc.fontenc != "T1") {
364                 if (times_ == SingleQ)
365                         switch (type) {
366                                 case ',': features.require("quotesinglbase");  break;
367                         case '<': features.require("guilsinglleft");  break;
368                         case '>': features.require("guilsinglright"); break;
369                         default: break;
370                         }
371                 else
372                         switch (type) {
373                         case ',': features.require("quotedblbase");   break;
374                         case '<': features.require("guillemotleft");  break;
375                         case '>': features.require("guillemotright"); break;
376                         default: break;
377                         }
378         }
379 }
380
381
382 Inset * InsetQuotes::clone() const
383 {
384         return new InsetQuotes(language_, side_, times_);
385 }
386
387
388 Inset::Code InsetQuotes::lyxCode() const
389 {
390         return Inset::QUOTE_CODE;
391 }
392
393
394 } // namespace lyx