]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
Thinko
[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 guillemets
203         // FIXME: this should be done by a separate quote type.
204         if (prefixIs(context_lang_, "fr")
205             && times_ == DoubleQuotes && language_ == FrenchQuotes) {
206                 // THIN SPACE (U+2009)
207                 char_type const thin_space = 0x2009;
208                 if (side_ == LeftQuote)
209                         retdisp += thin_space;
210                 else
211                         retdisp = thin_space + retdisp;
212         }
213
214         return retdisp;
215 }
216
217
218 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
219 {
220         FontInfo & font = mi.base.font;
221         frontend::FontMetrics const & fm = theFontMetrics(font);
222         dim.asc = fm.maxAscent();
223         dim.des = fm.maxDescent();
224         dim.wid = fm.width(displayString());
225 }
226
227
228 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
229 {
230         FontInfo font = pi.base.font;
231         font.setPaintColor(pi.textColor(font.realColor()));
232         pi.pain.text(x, y, displayString(), font);
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         // In some context, we output plain quotes
261         bool const force_plain = 
262                 runparams.pass_thru
263                 || runparams.local_font->language()->lang() == "hebrew";
264         if (force_plain)
265                 qstr = (times_ == DoubleQuotes) ? "\"" : "'";
266         else if (language_ == FrenchQuotes && times_ == DoubleQuotes
267             && prefixIs(runparams.local_font->language()->code(), "fr")
268             && !runparams.use_polyglossia) {
269                 // Specific guillemets of French babel
270                 // including correct French spacing
271                 if (side_ == LeftQuote)
272                         qstr = "\\og";
273                 else
274                         qstr = " \\fg"; // the space is important here
275         } else if (fontenc_ == "T1" && !runparams.use_polyglossia) {
276                 // Quotation marks for T1 font encoding
277                 // (using ligatures)
278                 qstr = latex_quote_t1[times_][quoteind];
279 #ifdef DO_USE_DEFAULT_LANGUAGE
280         } else if (doclang == "default") {
281 #else
282         } else if (!runparams.use_babel || runparams.isFullUnicode()) {
283 #endif
284                 // Standard quotation mark macros
285                 // These are also used by polyglossia
286                 // and babel without fontenc (XeTeX/LuaTeX)
287                 qstr = latex_quote_ot1[times_][quoteind];
288         } else {
289                 // Babel shorthand quotation marks (for T1/OT1)
290                 qstr = latex_quote_babel[times_][quoteind];
291         }
292
293         if (!force_plain) {
294                 // Always guard against unfortunate ligatures (!` ?` `` '' ,, << >>)
295                 char_type const lastchar = os.lastChar();
296                 if (prefixIs(qstr, "`")) {
297                         if (lastchar == '!' || lastchar == '?')
298                                 qstr.insert(0, "{}");
299                 }
300                 if (qstr[0] == lastchar)
301                         qstr.insert(0, "{}");
302         }
303
304         os << from_ascii(qstr);
305         
306         if (prefixIs(qstr, "\\") || prefixIs(qstr, " \\"))
307                 // properly terminate the command depending on the context
308                 os << termcmd;
309 }
310
311
312 int InsetQuotes::plaintext(odocstringstream & os, 
313         OutputParams const &, size_t) const
314 {
315         docstring const str = displayString();
316         os << str;
317         return str.size();
318 }
319
320
321 docstring InsetQuotes::getQuoteEntity() const {
322         const int quoteind = quote_index[side_][language_];
323         docstring res = from_ascii(html_quote[times_][quoteind]);
324         // in French, thin spaces are added inside double guillemets
325         // FIXME: this should be done by a separate quote type.
326         if (prefixIs(context_lang_, "fr")
327             && times_ == DoubleQuotes && language_ == FrenchQuotes) {
328                 // THIN SPACE (U+2009)
329                 docstring const thin_space = from_ascii("&#x2009;");
330                 if (side_ == LeftQuote)
331                         res += thin_space;
332                 else
333                         res = thin_space + res;
334         }
335         return res;
336 }
337
338
339 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
340 {
341         os << getQuoteEntity();
342         return 0;
343 }
344
345
346 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
347 {
348         xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
349         return docstring();
350 }
351
352
353 void InsetQuotes::toString(odocstream & os) const
354 {
355         os << displayString();
356 }
357
358
359 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
360 {
361         os += displayString();
362 }
363
364
365 void InsetQuotes::updateBuffer(ParIterator const & it, UpdateType /* utype*/)
366 {
367         BufferParams const & bp = buffer().masterBuffer()->params();
368         pass_thru_ = it.paragraph().isPassThru();
369         context_lang_ = it.paragraph().getFontSettings(bp, it.pos()).language()->code();
370 }
371
372
373 void InsetQuotes::validate(LaTeXFeatures & features) const
374 {
375         char type = quote_char[quote_index[side_][language_]];
376
377 #ifdef DO_USE_DEFAULT_LANGUAGE
378         if (features.bufferParams().language->lang() == "default"
379 #else
380         if (!features.useBabel()
381 #endif
382             && !features.usePolyglossia() && fontenc_ != "T1") {
383                 if (times_ == SingleQuotes)
384                         switch (type) {
385                         case ',': features.require("quotesinglbase"); break;
386                         case '<': features.require("guilsinglleft");  break;
387                         case '>': features.require("guilsinglright"); break;
388                         default: break;
389                         }
390                 else
391                         switch (type) {
392                         case ',': features.require("quotedblbase");   break;
393                         case '<': features.require("guillemotleft");  break;
394                         case '>': features.require("guillemotright"); break;
395                         default: break;
396                         }
397         }
398 }
399
400 } // namespace lyx