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 "texstream.h"
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.h"
32 #include "support/debug.h"
33 #include "support/docstring.h"
34 #include "support/docstream.h"
35 #include "support/lstrings.h"
38 using namespace lyx::support;
44 /* codes used to read/write quotes to LyX files
53 char const * const language_char = "esgpfa";
54 char const * const side_char = "lr" ;
55 char const * const times_char = "sd";
57 // List of known quote chars
58 char const * const quote_char = ",'`<>";
60 // Unicode characters needed by each quote type
61 char_type const display_quote_char[2][5] = {
62 { 0x201a, 0x2019, 0x2018, 0x2039, 0x203a},
63 { 0x201e, 0x201d, 0x201c, 0x00ab, 0x00bb}
66 // Index of chars used for the quote. Index is [side, language]
67 int quote_index[2][6] = {
68 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
69 { 1, 1, 2, 1, 4, 3 } // "`'`'><"
72 // Corresponding LaTeX code, for double and single quotes.
73 char const * const latex_quote_t1[2][5] = {
74 { "\\quotesinglbase ", "'", "`",
75 "\\guilsinglleft{}", "\\guilsinglright{}" },
76 { ",,", "''", "``", "<<", ">>" }
79 char const * const latex_quote_ot1[2][5] = {
80 { "\\quotesinglbase ", "'", "`",
81 "\\guilsinglleft{}", "\\guilsinglright{}" },
82 { "\\quotedblbase ", "''", "``",
83 "\\guillemotleft{}", "\\guillemotright{}" }
86 char const * const latex_quote_babel[2][5] = {
87 { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
88 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" }
94 InsetQuotes::InsetQuotes(Buffer * buf, string const & str) : Inset(buf)
99 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, QuoteTimes t)
100 : Inset(buf), times_(t)
103 language_ = buf->params().quotes_language;
105 language_ = EnglishQuotes;
111 docstring InsetQuotes::layoutName() const
113 return from_ascii("Quotes");
117 void InsetQuotes::setSide(char_type c)
119 // Decide whether left or right
124 side_ = LeftQuote; // left quote
127 side_ = RightQuote; // right quote
132 void InsetQuotes::parseString(string const & s)
135 if (str.length() != 3) {
136 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
137 " bad string length." << endl;
143 for (i = 0; i < 6; ++i) {
144 if (str[0] == language_char[i]) {
145 language_ = QuoteLanguage(i);
150 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
151 " bad language specification." << endl;
152 language_ = EnglishQuotes;
155 for (i = 0; i < 2; ++i) {
156 if (str[1] == side_char[i]) {
157 side_ = QuoteSide(i);
162 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
163 " bad side specification." << endl;
167 for (i = 0; i < 2; ++i) {
168 if (str[2] == times_char[i]) {
169 times_ = QuoteTimes(i);
174 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
175 " bad times specification." << endl;
176 times_ = DoubleQuotes;
181 // FIXME: should we add a language or a font parameter member?
182 docstring InsetQuotes::displayString() const
184 Language const * loclang =
185 isBufferValid() ? buffer().params().language : 0;
186 int const index = quote_index[side_][language_];
187 docstring retdisp = docstring(1, display_quote_char[times_][index]);
189 // in french, thin spaces are added inside double quotes
190 // FIXME: this should be done by a separate quote type.
191 if (times_ == DoubleQuotes && loclang && prefixIs(loclang->code(), "fr")) {
192 // THIN SPACE (U+2009)
193 char_type const thin_space = 0x2009;
194 if (side_ == LeftQuote)
195 retdisp += thin_space;
197 retdisp = thin_space + retdisp;
204 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
206 FontInfo & font = mi.base.font;
207 frontend::FontMetrics const & fm = theFontMetrics(font);
208 dim.asc = fm.maxAscent();
209 dim.des = fm.maxDescent();
210 dim.wid = fm.width(displayString());
214 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
216 FontInfo font = pi.base.font;
217 font.setPaintColor(pi.textColor(font.realColor()));
218 pi.pain.text(x, y, displayString(), font);
222 void InsetQuotes::write(ostream & os) const
225 text += language_char[language_];
226 text += side_char[side_];
227 text += times_char[times_];
228 os << "Quotes " << text;
232 void InsetQuotes::read(Lexer & lex)
234 lex.setContext("InsetQuotes::read");
236 parseString(lex.getString());
237 lex >> "\\end_inset";
241 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
243 const int quoteind = quote_index[side_][language_];
246 if (language_ == FrenchQuotes && times_ == DoubleQuotes
247 && prefixIs(runparams.local_font->language()->code(), "fr")
248 && !runparams.use_polyglossia) {
249 if (side_ == LeftQuote)
250 qstr = "\\og "; //the spaces are important here
252 qstr = " \\fg{}"; //and here
253 } else if (lyxrc.fontenc == "T1" && !runparams.use_polyglossia) {
254 qstr = latex_quote_t1[times_][quoteind];
255 #ifdef DO_USE_DEFAULT_LANGUAGE
256 } else if (doclang == "default") {
258 } else if (!runparams.use_babel) {
260 // these are also used by polyglossia
261 qstr = latex_quote_ot1[times_][quoteind];
263 qstr = latex_quote_babel[times_][quoteind];
266 // Always guard against unfortunate ligatures (!` ?`)
267 if (prefixIs(qstr, "`")) {
268 char_type const lastchar = os.lastChar();
269 if (lastchar == '!' || lastchar == '?')
270 qstr.insert(0, "{}");
273 os << from_ascii(qstr);
277 int InsetQuotes::plaintext(odocstringstream & os,
278 OutputParams const &, size_t) const
280 docstring const str = displayString();
286 docstring InsetQuotes::getQuoteEntity() const {
287 if (times_ == DoubleQuotes) {
288 if (side_ == LeftQuote)
289 return from_ascii("“");
291 return from_ascii("”");
293 if (side_ == LeftQuote)
294 return from_ascii("‘");
296 return from_ascii("’");
300 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
302 os << getQuoteEntity();
307 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
309 xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
314 void InsetQuotes::toString(odocstream & os) const
316 os << displayString();
320 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
322 os += displayString();
326 void InsetQuotes::validate(LaTeXFeatures & features) const
328 char type = quote_char[quote_index[side_][language_]];
330 #ifdef DO_USE_DEFAULT_LANGUAGE
331 if (features.bufferParams().language->lang() == "default"
333 if (!features.useBabel()
335 && lyxrc.fontenc != "T1") {
336 if (times_ == SingleQuotes)
338 case ',': features.require("quotesinglbase"); break;
339 case '<': features.require("guilsinglleft"); break;
340 case '>': features.require("guilsinglright"); break;
345 case ',': features.require("quotedblbase"); break;
346 case '<': features.require("guillemotleft"); break;
347 case '>': features.require("guillemotright"); break;