]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
Fix mis-nomer
[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 "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 char const * const html_quote[2][5] = {
92         { "&sbquo;",  "&rsquo;", "&lsquo;",
93           "&lsaquo;", "&rsaquo;" },
94   { "&bdquo;", "&rdquo;", "&ldquo;", "&laquo;", "&raquo;" }
95 };
96
97 } // namespace anon
98
99
100 InsetQuotes::InsetQuotes(Buffer * buf, string const & str) : Inset(buf)
101 {
102         parseString(str);
103 }
104
105 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, QuoteTimes t)
106         : Inset(buf), times_(t)
107 {
108         if (buf) {
109                 language_ = buf->params().quotes_language;
110                 fontenc_ = (buf->params().fontenc == "global")
111                         ? lyxrc.fontenc : buf->params().fontenc;
112         } else {
113                 language_ = EnglishQuotes;
114                 fontenc_ = lyxrc.fontenc;
115         }
116
117         setSide(c);
118 }
119
120
121 docstring InsetQuotes::layoutName() const
122 {
123         return from_ascii("Quotes");
124 }
125
126
127 void InsetQuotes::setSide(char_type c)
128 {
129         // Decide whether left or right
130         switch (c) {
131         case ' ':
132         case '(':
133         case '[':
134                 side_ = LeftQuote;   // left quote
135                 break;
136         default:
137                 side_ = RightQuote;  // right quote
138         }
139 }
140
141
142 void InsetQuotes::parseString(string const & s)
143 {
144         string str = s;
145         if (str.length() != 3) {
146                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
147                         " bad string length." << endl;
148                 str = "eld";
149         }
150
151         int i;
152
153         for (i = 0; i < 6; ++i) {
154                 if (str[0] == language_char[i]) {
155                         language_ = QuoteLanguage(i);
156                         break;
157                 }
158         }
159         if (i >= 6) {
160                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
161                         " bad language specification." << endl;
162                 language_ = EnglishQuotes;
163         }
164
165         for (i = 0; i < 2; ++i) {
166                 if (str[1] == side_char[i]) {
167                         side_ = QuoteSide(i);
168                         break;
169                 }
170         }
171         if (i >= 2) {
172                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
173                         " bad side specification." << endl;
174                 side_ = LeftQuote;
175         }
176
177         for (i = 0; i < 2; ++i) {
178                 if (str[2] == times_char[i]) {
179                         times_ = QuoteTimes(i);
180                         break;
181                 }
182         }
183         if (i >= 2) {
184                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
185                         " bad times specification." << endl;
186                 times_ = DoubleQuotes;
187         }
188 }
189
190
191 // FIXME: should we add a language or a font parameter member?
192 docstring InsetQuotes::displayString() const
193 {
194         Language const * loclang =
195                 isBufferValid() ? buffer().params().language : 0;
196         int const index = quote_index[side_][language_];
197         docstring retdisp = docstring(1, display_quote_char[times_][index]);
198
199         // in french, thin spaces are added inside double quotes
200         // FIXME: this should be done by a separate quote type.
201         if (times_ == DoubleQuotes && loclang && prefixIs(loclang->code(), "fr")) {
202                 // THIN SPACE (U+2009)
203                 char_type const thin_space = 0x2009;
204                 if (side_ == LeftQuote)
205                         retdisp += thin_space;
206                 else
207                         retdisp = thin_space + retdisp;
208         }
209
210         return retdisp;
211 }
212
213
214 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
215 {
216         FontInfo & font = mi.base.font;
217         frontend::FontMetrics const & fm = theFontMetrics(font);
218         dim.asc = fm.maxAscent();
219         dim.des = fm.maxDescent();
220         dim.wid = fm.width(displayString());
221 }
222
223
224 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
225 {
226         FontInfo font = pi.base.font;
227         font.setPaintColor(pi.textColor(font.realColor()));
228         pi.pain.text(x, y, displayString(), font);
229 }
230
231
232 void InsetQuotes::write(ostream & os) const
233 {
234         string text;
235         text += language_char[language_];
236         text += side_char[side_];
237         text += times_char[times_];
238         os << "Quotes " << text;
239 }
240
241
242 void InsetQuotes::read(Lexer & lex)
243 {
244         lex.setContext("InsetQuotes::read");
245         lex.next();
246         parseString(lex.getString());
247         lex >> "\\end_inset";
248 }
249
250
251 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
252 {
253         const int quoteind = quote_index[side_][language_];
254         string qstr;
255
256         if (language_ == FrenchQuotes && times_ == DoubleQuotes
257             && prefixIs(runparams.local_font->language()->code(), "fr")
258             && !runparams.use_polyglossia) {
259                 if (side_ == LeftQuote)
260                         qstr = "\\og "; //the spaces are important here
261                 else
262                         qstr = " \\fg{}"; //and here
263         } else if (fontenc_ == "T1" && !runparams.use_polyglossia) {
264                 qstr = latex_quote_t1[times_][quoteind];
265 #ifdef DO_USE_DEFAULT_LANGUAGE
266         } else if (doclang == "default") {
267 #else
268         } else if (!runparams.use_babel) {
269 #endif
270                 // these are also used by polyglossia
271                 qstr = latex_quote_ot1[times_][quoteind];
272         } else {
273                 qstr = latex_quote_babel[times_][quoteind];
274         }
275
276         // Always guard against unfortunate ligatures (!` ?` `` '' ,, << >>)
277         char_type const lastchar = os.lastChar();
278         if (prefixIs(qstr, "`")) {
279                 if (lastchar == '!' || lastchar == '?')
280                         qstr.insert(0, "{}");
281         }
282         if (qstr[1] == lastchar)
283                 qstr.insert(0, "{}");
284
285         os << from_ascii(qstr);
286 }
287
288
289 int InsetQuotes::plaintext(odocstringstream & os, 
290         OutputParams const &, size_t) const
291 {
292         docstring const str = displayString();
293         os << str;
294         return str.size();
295 }
296
297
298 docstring InsetQuotes::getQuoteEntity() const {
299         const int quoteind = quote_index[side_][language_];
300         return from_ascii(html_quote[times_][quoteind]);
301 }
302
303
304 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
305 {
306         os << getQuoteEntity();
307         return 0;
308 }
309
310
311 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
312 {
313         xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
314         return docstring();
315 }
316
317
318 void InsetQuotes::toString(odocstream & os) const
319 {
320         os << displayString();
321 }
322
323
324 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
325 {
326         os += displayString();
327 }
328
329
330 void InsetQuotes::validate(LaTeXFeatures & features) const
331 {
332         char type = quote_char[quote_index[side_][language_]];
333
334 #ifdef DO_USE_DEFAULT_LANGUAGE
335         if (features.bufferParams().language->lang() == "default"
336 #else
337         if (!features.useBabel()
338 #endif
339             && fontenc_ != "T1") {
340                 if (times_ == SingleQuotes)
341                         switch (type) {
342                         case ',': features.require("quotesinglbase"); break;
343                         case '<': features.require("guilsinglleft");  break;
344                         case '>': features.require("guilsinglright"); break;
345                         default: break;
346                         }
347                 else
348                         switch (type) {
349                         case ',': features.require("quotedblbase");   break;
350                         case '<': features.require("guillemotleft");  break;
351                         case '>': features.require("guillemotright"); break;
352                         default: break;
353                         }
354         }
355 }
356
357 } // namespace lyx