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