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