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