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