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