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