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