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_babel[2][5] = {
89 { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
90 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" }
93 char const * const html_quote[2][5] = {
94 { "‚", "’", "‘",
95 "‹", "›" },
96 { "„", "”", "“", "«", "»" }
102 InsetQuotes::InsetQuotes(Buffer * buf, string const & str) : Inset(buf)
107 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, QuoteTimes t)
108 : Inset(buf), times_(t), pass_thru_(false)
111 language_ = buf->params().quotes_language;
112 fontenc_ = (buf->params().fontenc == "global")
113 ? lyxrc.fontenc : buf->params().fontenc;
115 language_ = EnglishQuotes;
116 fontenc_ = lyxrc.fontenc;
123 docstring InsetQuotes::layoutName() const
125 return from_ascii("Quotes");
129 void InsetQuotes::setSide(char_type c)
131 // Decide whether left or right
136 side_ = LeftQuote; // left quote
139 side_ = RightQuote; // right quote
144 void InsetQuotes::parseString(string const & s)
147 if (str.length() != 3) {
148 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
149 " bad string length." << endl;
155 for (i = 0; i < 6; ++i) {
156 if (str[0] == language_char[i]) {
157 language_ = QuoteLanguage(i);
162 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
163 " bad language specification." << endl;
164 language_ = EnglishQuotes;
167 for (i = 0; i < 2; ++i) {
168 if (str[1] == side_char[i]) {
169 side_ = QuoteSide(i);
174 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
175 " bad side specification." << endl;
179 for (i = 0; i < 2; ++i) {
180 if (str[2] == times_char[i]) {
181 times_ = QuoteTimes(i);
186 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
187 " bad times specification." << endl;
188 times_ = DoubleQuotes;
193 docstring InsetQuotes::displayString() const
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("'");
199 int const index = quote_index[side_][language_];
200 docstring retdisp = docstring(1, display_quote_char[times_][index]);
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;
211 retdisp = thin_space + retdisp;
218 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
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());
228 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
230 FontInfo font = pi.base.font;
231 font.setPaintColor(pi.textColor(font.realColor()));
232 pi.pain.text(x, y, displayString(), font);
236 void InsetQuotes::write(ostream & os) const
239 text += language_char[language_];
240 text += side_char[side_];
241 text += times_char[times_];
242 os << "Quotes " << text;
246 void InsetQuotes::read(Lexer & lex)
248 lex.setContext("InsetQuotes::read");
250 parseString(lex.getString());
251 lex >> "\\end_inset";
255 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
257 const int quoteind = quote_index[side_][language_];
260 // In some context, we output plain quotes
261 bool const force_plain =
263 || runparams.local_font->language()->lang() == "hebrew";
265 qstr = (times_ == DoubleQuotes) ? "\"" : "'";
266 else if (language_ == FrenchQuotes && times_ == DoubleQuotes
267 && prefixIs(runparams.local_font->language()->code(), "fr")
268 && !runparams.use_polyglossia) {
269 if (side_ == LeftQuote)
270 qstr = "\\og "; //the spaces are important here
272 qstr = " \\fg{}"; //and here
273 } else if (fontenc_ == "T1" && !runparams.use_polyglossia) {
274 qstr = latex_quote_t1[times_][quoteind];
275 #ifdef DO_USE_DEFAULT_LANGUAGE
276 } else if (doclang == "default") {
278 } else if (!runparams.use_babel) {
280 // these are also used by polyglossia
281 qstr = latex_quote_ot1[times_][quoteind];
283 qstr = latex_quote_babel[times_][quoteind];
287 // Always guard against unfortunate ligatures (!` ?` `` '' ,, << >>)
288 char_type const lastchar = os.lastChar();
289 if (prefixIs(qstr, "`")) {
290 if (lastchar == '!' || lastchar == '?')
291 qstr.insert(0, "{}");
293 if (qstr[1] == lastchar)
294 qstr.insert(0, "{}");
297 os << from_ascii(qstr);
301 int InsetQuotes::plaintext(odocstringstream & os,
302 OutputParams const &, size_t) const
304 docstring const str = displayString();
310 docstring InsetQuotes::getQuoteEntity() const {
311 const int quoteind = quote_index[side_][language_];
312 docstring res = from_ascii(html_quote[times_][quoteind]);
313 // in French, thin spaces are added inside double guillemets
314 // FIXME: this should be done by a separate quote type.
315 if (prefixIs(context_lang_, "fr")
316 && times_ == DoubleQuotes && language_ == FrenchQuotes) {
317 // THIN SPACE (U+2009)
318 docstring const thin_space = from_ascii(" ");
319 if (side_ == LeftQuote)
322 res = thin_space + res;
328 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
330 os << getQuoteEntity();
335 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
337 xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
342 void InsetQuotes::toString(odocstream & os) const
344 os << displayString();
348 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
350 os += displayString();
354 void InsetQuotes::updateBuffer(ParIterator const & it, UpdateType /* utype*/)
356 BufferParams const & bp = buffer().masterBuffer()->params();
357 pass_thru_ = it.paragraph().isPassThru();
358 context_lang_ = it.paragraph().getFontSettings(bp, it.pos()).language()->code();
362 void InsetQuotes::validate(LaTeXFeatures & features) const
364 char type = quote_char[quote_index[side_][language_]];
366 #ifdef DO_USE_DEFAULT_LANGUAGE
367 if (features.bufferParams().language->lang() == "default"
369 if (!features.useBabel()
371 && !features.usePolyglossia() && fontenc_ != "T1") {
372 if (times_ == SingleQuotes)
374 case ',': features.require("quotesinglbase"); break;
375 case '<': features.require("guilsinglleft"); break;
376 case '>': features.require("guilsinglright"); break;
381 case ',': features.require("quotedblbase"); break;
382 case '<': features.require("guillemotleft"); break;
383 case '>': features.require("guillemotright"); break;