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 : Inset(buf), times_(t), pass_thru_(false)
118 language_ = buf->params().quotes_language;
119 fontenc_ = (buf->params().fontenc == "global")
120 ? lyxrc.fontenc : buf->params().fontenc;
122 language_ = EnglishQuotes;
123 fontenc_ = lyxrc.fontenc;
130 docstring InsetQuotes::layoutName() const
132 return from_ascii("Quotes");
136 void InsetQuotes::setSide(char_type c)
138 // Decide whether left or right
143 side_ = LeftQuote; // left quote
146 side_ = RightQuote; // right quote
151 void InsetQuotes::parseString(string const & s)
154 if (str.length() != 3) {
155 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
156 " bad string length." << endl;
162 for (i = 0; i < 6; ++i) {
163 if (str[0] == language_char[i]) {
164 language_ = QuoteLanguage(i);
169 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
170 " bad language specification." << endl;
171 language_ = EnglishQuotes;
174 for (i = 0; i < 2; ++i) {
175 if (str[1] == side_char[i]) {
176 side_ = QuoteSide(i);
181 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
182 " bad side specification." << endl;
186 for (i = 0; i < 2; ++i) {
187 if (str[2] == times_char[i]) {
188 times_ = QuoteTimes(i);
193 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
194 " bad times specification." << endl;
195 times_ = DoubleQuotes;
200 docstring InsetQuotes::displayString() const
202 // In PassThru, we use straight quotes
204 return (times_ == DoubleQuotes) ? from_ascii("\"") : from_ascii("'");
206 int const index = quote_index[side_][language_];
207 docstring retdisp = docstring(1, display_quote_char[times_][index]);
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;
218 retdisp = thin_space + retdisp;
225 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
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());
235 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
237 FontInfo font = pi.base.font;
238 font.setPaintColor(pi.textColor(font.realColor()));
239 pi.pain.text(x, y, displayString(), font);
243 void InsetQuotes::write(ostream & os) const
246 text += language_char[language_];
247 text += side_char[side_];
248 text += times_char[times_];
249 os << "Quotes " << text;
253 void InsetQuotes::read(Lexer & lex)
255 lex.setContext("InsetQuotes::read");
257 parseString(lex.getString());
258 lex >> "\\end_inset";
262 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
264 const int quoteind = quote_index[side_][language_];
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)
278 // the preceding space is important here
279 // FIXME: really? (doesn't make a difference
280 // with recent babel-french)
282 } else if (fontenc_ == "T1"
283 && !runparams.local_font->language()->internalFontEncoding()
284 && !runparams.use_polyglossia) {
285 // Quotation marks for T1 font encoding
287 qstr = latex_quote_t1[times_][quoteind];
288 } else if (runparams.local_font->language()->internalFontEncoding()) {
289 // Quotation marks for internal font encodings
290 // (ligatures not featured)
291 qstr = latex_quote_noligatures[times_][quoteind];
292 #ifdef DO_USE_DEFAULT_LANGUAGE
293 } else if (doclang == "default") {
295 } else if (!runparams.use_babel || runparams.isFullUnicode()) {
297 // Standard quotation mark macros
298 // These are also used by polyglossia
299 // and babel without fontenc (XeTeX/LuaTeX)
300 qstr = latex_quote_ot1[times_][quoteind];
302 // Babel shorthand quotation marks (for T1/OT1)
303 qstr = latex_quote_babel[times_][quoteind];
306 if (!runparams.pass_thru) {
307 // Always guard against unfortunate ligatures (!` ?` `` '' ,, << >>)
308 char_type const lastchar = os.lastChar();
309 if (prefixIs(qstr, "`")) {
310 if (lastchar == '!' || lastchar == '?')
311 qstr.insert(0, "{}");
313 if (qstr[0] == lastchar)
314 qstr.insert(0, "{}");
317 os << from_ascii(qstr);
319 // FIXME Check if the space in " \\fg" is really needed (see above)
320 if (prefixIs(qstr, "\\") || prefixIs(qstr, " \\"))
321 // properly terminate the command depending on the context
326 int InsetQuotes::plaintext(odocstringstream & os,
327 OutputParams const &, size_t) const
329 docstring const str = displayString();
335 docstring InsetQuotes::getQuoteEntity() const {
336 const int quoteind = quote_index[side_][language_];
337 docstring res = from_ascii(html_quote[times_][quoteind]);
338 // in French, thin spaces are added inside double guillemets
339 // FIXME: this should be done by a separate quote type.
340 if (prefixIs(context_lang_, "fr")
341 && times_ == DoubleQuotes && language_ == FrenchQuotes) {
342 // THIN SPACE (U+2009)
343 docstring const thin_space = from_ascii(" ");
344 if (side_ == LeftQuote)
347 res = thin_space + res;
353 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
355 os << getQuoteEntity();
360 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
362 xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
367 void InsetQuotes::toString(odocstream & os) const
369 os << displayString();
373 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
375 os += displayString();
379 void InsetQuotes::updateBuffer(ParIterator const & it, UpdateType /* utype*/)
381 BufferParams const & bp = buffer().masterBuffer()->params();
382 pass_thru_ = it.paragraph().isPassThru();
383 context_lang_ = it.paragraph().getFontSettings(bp, it.pos()).language()->code();
387 void InsetQuotes::validate(LaTeXFeatures & features) const
389 char type = quote_char[quote_index[side_][language_]];
391 #ifdef DO_USE_DEFAULT_LANGUAGE
392 if (features.bufferParams().language->lang() == "default"
394 if (!features.useBabel()
396 && !features.usePolyglossia() && fontenc_ != "T1") {
397 if (times_ == SingleQuotes)
399 case ',': features.require("quotesinglbase"); break;
400 case '<': features.require("guilsinglleft"); break;
401 case '>': features.require("guilsinglright"); break;
406 case ',': features.require("quotedblbase"); break;
407 case '<': features.require("guillemotleft"); break;
408 case '>': features.require("guillemotright"); break;