]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
Remove redundant blank
[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_noligatures[2][5] = {
89         { "\\quotesinglbase",  "\\textquoteleft", "\\textquoteright",
90     "\\guilsinglleft", "\\guilsinglright" },
91   { "\\quotedblbase", "\\textquotedblleft", "\\textquotedblright",
92     "\\guillemotleft", "\\guillemotright" }
93 };
94
95 char const * const latex_quote_babel[2][5] = {
96         { "\\glq",  "'", "`", "\\flq", "\\frq" },
97   { "\\glqq", "''", "``", "\\flqq", "\\frqq" }
98 };
99
100 char const * const html_quote[2][5] = {
101         { "&sbquo;",  "&rsquo;", "&lsquo;",
102           "&lsaquo;", "&rsaquo;" },
103   { "&bdquo;", "&rdquo;", "&ldquo;", "&laquo;", "&raquo;" }
104 };
105
106 } // namespace anon
107
108
109 InsetQuotes::InsetQuotes(Buffer * buf, string const & str) : Inset(buf)
110 {
111         parseString(str);
112 }
113
114 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, QuoteTimes t)
115         : Inset(buf), times_(t), pass_thru_(false)
116 {
117         if (buf) {
118                 language_ = buf->params().quotes_language;
119                 fontenc_ = (buf->params().fontenc == "global")
120                         ? lyxrc.fontenc : buf->params().fontenc;
121         } else {
122                 language_ = EnglishQuotes;
123                 fontenc_ = lyxrc.fontenc;
124         }
125
126         setSide(c);
127 }
128
129
130 docstring InsetQuotes::layoutName() const
131 {
132         return from_ascii("Quotes");
133 }
134
135
136 void InsetQuotes::setSide(char_type c)
137 {
138         // Decide whether left or right
139         switch (c) {
140         case ' ':
141         case '(':
142         case '[':
143                 side_ = LeftQuote;   // left quote
144                 break;
145         default:
146                 side_ = RightQuote;  // right quote
147         }
148 }
149
150
151 void InsetQuotes::parseString(string const & s)
152 {
153         string str = s;
154         if (str.length() != 3) {
155                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
156                         " bad string length." << endl;
157                 str = "eld";
158         }
159
160         int i;
161
162         for (i = 0; i < 6; ++i) {
163                 if (str[0] == language_char[i]) {
164                         language_ = QuoteLanguage(i);
165                         break;
166                 }
167         }
168         if (i >= 6) {
169                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
170                         " bad language specification." << endl;
171                 language_ = EnglishQuotes;
172         }
173
174         for (i = 0; i < 2; ++i) {
175                 if (str[1] == side_char[i]) {
176                         side_ = QuoteSide(i);
177                         break;
178                 }
179         }
180         if (i >= 2) {
181                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
182                         " bad side specification." << endl;
183                 side_ = LeftQuote;
184         }
185
186         for (i = 0; i < 2; ++i) {
187                 if (str[2] == times_char[i]) {
188                         times_ = QuoteTimes(i);
189                         break;
190                 }
191         }
192         if (i >= 2) {
193                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
194                         " bad times specification." << endl;
195                 times_ = DoubleQuotes;
196         }
197 }
198
199
200 docstring InsetQuotes::displayString() const
201 {
202         // In PassThru, we use straight quotes
203         if (pass_thru_)
204                 return (times_ == DoubleQuotes) ? from_ascii("\"") : from_ascii("'");
205
206         int const index = quote_index[side_][language_];
207         docstring retdisp = docstring(1, display_quote_char[times_][index]);
208
209         // in French, thin spaces are added inside double guillemets
210         // FIXME: this should be done by a separate quote type.
211         if (prefixIs(context_lang_, "fr")
212             && times_ == DoubleQuotes && language_ == FrenchQuotes) {
213                 // THIN SPACE (U+2009)
214                 char_type const thin_space = 0x2009;
215                 if (side_ == LeftQuote)
216                         retdisp += thin_space;
217                 else
218                         retdisp = thin_space + retdisp;
219         }
220
221         return retdisp;
222 }
223
224
225 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
226 {
227         FontInfo & font = mi.base.font;
228         frontend::FontMetrics const & fm = theFontMetrics(font);
229         dim.asc = fm.maxAscent();
230         dim.des = fm.maxDescent();
231         dim.wid = fm.width(displayString());
232 }
233
234
235 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
236 {
237         FontInfo font = pi.base.font;
238         font.setPaintColor(pi.textColor(font.realColor()));
239         pi.pain.text(x, y, displayString(), font);
240 }
241
242
243 void InsetQuotes::write(ostream & os) const
244 {
245         string text;
246         text += language_char[language_];
247         text += side_char[side_];
248         text += times_char[times_];
249         os << "Quotes " << text;
250 }
251
252
253 void InsetQuotes::read(Lexer & lex)
254 {
255         lex.setContext("InsetQuotes::read");
256         lex.next();
257         parseString(lex.getString());
258         lex >> "\\end_inset";
259 }
260
261
262 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
263 {
264         const int quoteind = quote_index[side_][language_];
265         string qstr;
266
267         // In pass-thru context, we output plain quotes
268         if (runparams.pass_thru)
269                 qstr = (times_ == DoubleQuotes) ? "\"" : "'";
270         else if (language_ == FrenchQuotes && times_ == DoubleQuotes
271             && prefixIs(runparams.local_font->language()->code(), "fr")
272             && !runparams.use_polyglossia) {
273                 // Specific guillemets of French babel
274                 // including correct French spacing
275                 if (side_ == LeftQuote)
276                         qstr = "\\og";
277                 else
278                         qstr = "\\fg";
279         } else if (fontenc_ == "T1"
280                    && !runparams.local_font->language()->internalFontEncoding()
281                    && !runparams.use_polyglossia) {
282                 // Quotation marks for T1 font encoding
283                 // (using ligatures)
284                 qstr = latex_quote_t1[times_][quoteind];
285         } else if (runparams.local_font->language()->internalFontEncoding()) {
286                 // Quotation marks for internal font encodings
287                 // (ligatures not featured)
288                 qstr = latex_quote_noligatures[times_][quoteind];
289 #ifdef DO_USE_DEFAULT_LANGUAGE
290         } else if (doclang == "default") {
291 #else
292         } else if (!runparams.use_babel || runparams.isFullUnicode()) {
293 #endif
294                 // Standard quotation mark macros
295                 // These are also used by polyglossia
296                 // and babel without fontenc (XeTeX/LuaTeX)
297                 qstr = latex_quote_ot1[times_][quoteind];
298         } else {
299                 // Babel shorthand quotation marks (for T1/OT1)
300                 qstr = latex_quote_babel[times_][quoteind];
301         }
302
303         if (!runparams.pass_thru) {
304                 // Always guard against unfortunate ligatures (!` ?` `` '' ,, << >>)
305                 char_type const lastchar = os.lastChar();
306                 if (prefixIs(qstr, "`")) {
307                         if (lastchar == '!' || lastchar == '?')
308                                 qstr.insert(0, "{}");
309                 }
310                 if (contains(from_ascii(",'`<>"), lastchar) && qstr[0] == lastchar)
311                         qstr.insert(0, "{}");
312         }
313
314         os << from_ascii(qstr);
315
316         if (prefixIs(qstr, "\\"))
317                 // properly terminate the command depending on the context
318                 os << termcmd;
319 }
320
321
322 int InsetQuotes::plaintext(odocstringstream & os, 
323         OutputParams const &, size_t) const
324 {
325         docstring const str = displayString();
326         os << str;
327         return str.size();
328 }
329
330
331 docstring InsetQuotes::getQuoteEntity() const {
332         const int quoteind = quote_index[side_][language_];
333         docstring res = from_ascii(html_quote[times_][quoteind]);
334         // in French, thin spaces are added inside double guillemets
335         // FIXME: this should be done by a separate quote type.
336         if (prefixIs(context_lang_, "fr")
337             && times_ == DoubleQuotes && language_ == FrenchQuotes) {
338                 // THIN SPACE (U+2009)
339                 docstring const thin_space = from_ascii("&#x2009;");
340                 if (side_ == LeftQuote)
341                         res += thin_space;
342                 else
343                         res = thin_space + res;
344         }
345         return res;
346 }
347
348
349 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
350 {
351         os << getQuoteEntity();
352         return 0;
353 }
354
355
356 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
357 {
358         xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
359         return docstring();
360 }
361
362
363 void InsetQuotes::toString(odocstream & os) const
364 {
365         os << displayString();
366 }
367
368
369 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
370 {
371         os += displayString();
372 }
373
374
375 void InsetQuotes::updateBuffer(ParIterator const & it, UpdateType /* utype*/)
376 {
377         BufferParams const & bp = buffer().masterBuffer()->params();
378         pass_thru_ = it.paragraph().isPassThru();
379         context_lang_ = it.paragraph().getFontSettings(bp, it.pos()).language()->code();
380 }
381
382
383 void InsetQuotes::validate(LaTeXFeatures & features) const
384 {
385         char type = quote_char[quote_index[side_][language_]];
386
387 #ifdef DO_USE_DEFAULT_LANGUAGE
388         if (features.bufferParams().language->lang() == "default"
389 #else
390         if (!features.useBabel()
391 #endif
392             && !features.usePolyglossia() && fontenc_ != "T1") {
393                 if (times_ == SingleQuotes)
394                         switch (type) {
395                         case ',': features.require("quotesinglbase"); break;
396                         case '<': features.require("guilsinglleft");  break;
397                         case '>': features.require("guilsinglright"); break;
398                         default: break;
399                         }
400                 else
401                         switch (type) {
402                         case ',': features.require("quotedblbase");   break;
403                         case '<': features.require("guillemotleft");  break;
404                         case '>': features.require("guillemotright"); break;
405                         default: break;
406                         }
407         }
408 }
409
410 } // namespace lyx