2 * \file InsetQuotes.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jean-Marc Lasgouttes
8 * Full author contact details are available in file CREDITS.
13 #include "InsetQuotes.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "Dimension.h"
21 #include "LaTeXFeatures.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"
31 #include "frontends/FontMetrics.h"
32 #include "frontends/Painter.h"
34 #include "support/debug.h"
35 #include "support/docstring.h"
36 #include "support/docstream.h"
37 #include "support/lstrings.h"
40 using namespace lyx::support;
46 /* codes used to read/write quotes to LyX files
55 char const * const language_char = "esgpfa";
56 char const * const side_char = "lr" ;
57 char const * const times_char = "sd";
59 // List of known quote chars
60 char const * const quote_char = ",'`<>";
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}
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 } // "`'`'><"
74 // Corresponding LaTeX code, for double and single quotes.
75 char const * const latex_quote_t1[2][5] = {
76 { "\\quotesinglbase", "'", "`",
77 "\\guilsinglleft", "\\guilsinglright" },
78 { ",,", "''", "``", "<<", ">>" }
81 char const * const latex_quote_ot1[2][5] = {
82 { "\\quotesinglbase", "'", "`",
83 "\\guilsinglleft", "\\guilsinglright" },
84 { "\\quotedblbase", "''", "``",
85 "\\guillemotleft", "\\guillemotright" }
88 char const * const latex_quote_noligatures[2][5] = {
89 { "\\quotesinglbase", "\\textquoteleft", "\\textquoteright",
90 "\\guilsinglleft", "\\guilsinglright" },
91 { "\\quotedblbase", "\\textquotedblleft", "\\textquotedblright",
92 "\\guillemotleft", "\\guillemotright" }
95 char const * const latex_quote_babel[2][5] = {
96 { "\\glq", "'", "`", "\\flq", "\\frq" },
97 { "\\glqq", "''", "``", "\\flqq", "\\frqq" }
100 char const * const html_quote[2][5] = {
101 { "‚", "’", "‘",
102 "‹", "›" },
103 { "„", "”", "“", "«", "»" }
109 InsetQuotes::InsetQuotes(Buffer * buf, string const & str) : Inset(buf)
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)
119 language_ = l.empty() ? buf->params().quotes_language : getLanguage(l);
120 fontenc_ = (buf->params().fontenc == "global")
121 ? lyxrc.fontenc : buf->params().fontenc;
123 language_ = l.empty() ? EnglishQuotes : getLanguage(l);
124 fontenc_ = lyxrc.fontenc;
129 else if (s == "right")
136 docstring InsetQuotes::layoutName() const
138 return from_ascii("Quotes");
142 void InsetQuotes::setSide(char_type c)
144 // Decide whether left or right
149 side_ = LeftQuote; // left quote
152 side_ = RightQuote; // right quote
157 void InsetQuotes::parseString(string const & s)
160 if (str.length() != 3) {
161 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
162 " bad string length." << endl;
168 for (i = 0; i < 6; ++i) {
169 if (str[0] == language_char[i]) {
170 language_ = QuoteLanguage(i);
175 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
176 " bad language specification." << endl;
177 language_ = EnglishQuotes;
180 for (i = 0; i < 2; ++i) {
181 if (str[1] == side_char[i]) {
182 side_ = QuoteSide(i);
187 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
188 " bad side specification." << endl;
192 for (i = 0; i < 2; ++i) {
193 if (str[2] == times_char[i]) {
194 times_ = QuoteTimes(i);
199 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
200 " bad times specification." << endl;
201 times_ = DoubleQuotes;
205 InsetQuotes::QuoteLanguage InsetQuotes::getLanguage(string const & s)
207 QuoteLanguage ql = EnglishQuotes;
211 else if (s == "swedish")
213 else if (s == "german")
215 else if (s == "polish")
217 else if (s == "french")
219 else if (s == "danish")
226 docstring InsetQuotes::displayString() const
228 // In PassThru, we use straight quotes
230 return (times_ == DoubleQuotes) ? from_ascii("\"") : from_ascii("'");
232 int const index = quote_index[side_][language_];
233 docstring retdisp = docstring(1, display_quote_char[times_][index]);
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;
244 retdisp = thin_space + retdisp;
251 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
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());
261 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
263 FontInfo font = pi.base.font;
264 font.setPaintColor(pi.textColor(font.realColor()));
265 pi.pain.text(x, y, displayString(), font);
269 void InsetQuotes::write(ostream & os) const
272 text += language_char[language_];
273 text += side_char[side_];
274 text += times_char[times_];
275 os << "Quotes " << text;
279 void InsetQuotes::read(Lexer & lex)
281 lex.setContext("InsetQuotes::read");
283 parseString(lex.getString());
284 lex >> "\\end_inset";
288 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
290 const int quoteind = quote_index[side_][language_];
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]);
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");
308 qstr = from_ascii("\\fg");
309 } else if (fontenc_ == "T1"
310 && !runparams.local_font->language()->internalFontEncoding()) {
311 // Quotation marks for T1 font encoding
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") {
321 } else if (!runparams.use_babel || runparams.isFullUnicode()) {
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]);
328 // Babel shorthand quotation marks (for T1/OT1)
329 qstr = from_ascii(latex_quote_babel[times_][quoteind]);
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 == '?'))
339 // ``` ''' ,,, <<< >>>
340 // => `{}`` '{}'' ,{},, <{}<< >{}>>
341 if (contains(from_ascii(",'`<>"), lastchar)
342 && prefixIs(qstr, lastchar))
348 if (prefixIs(qstr, from_ascii("\\")))
349 // properly terminate the command depending on the context
354 int InsetQuotes::plaintext(odocstringstream & os,
355 OutputParams const &, size_t) const
357 docstring const str = displayString();
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(" ");
372 if (side_ == LeftQuote)
375 res = thin_space + res;
381 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
383 os << getQuoteEntity();
388 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
390 xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
395 void InsetQuotes::toString(odocstream & os) const
397 os << displayString();
401 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
403 os += displayString();
407 void InsetQuotes::updateBuffer(ParIterator const & it, UpdateType /* utype*/)
409 BufferParams const & bp = buffer().masterBuffer()->params();
410 pass_thru_ = it.paragraph().isPassThru();
411 context_lang_ = it.paragraph().getFontSettings(bp, it.pos()).language()->code();
415 void InsetQuotes::validate(LaTeXFeatures & features) const
417 char type = quote_char[quote_index[side_][language_]];
419 #ifdef DO_USE_DEFAULT_LANGUAGE
420 if (features.bufferParams().language->lang() == "default"
422 if (!features.useBabel()
424 && !features.usePolyglossia() && fontenc_ != "T1") {
425 if (times_ == SingleQuotes)
427 case ',': features.require("quotesinglbase"); break;
428 case '<': features.require("guilsinglleft"); break;
429 case '>': features.require("guilsinglright"); break;
434 case ',': features.require("quotedblbase"); break;
435 case '<': features.require("guillemotleft"); break;
436 case '>': features.require("guillemotright"); break;