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