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"
28 #include "frontends/FontMetrics.h"
29 #include "frontends/Painter.h"
31 #include "support/debug.h"
32 #include "support/docstring.h"
33 #include "support/docstream.h"
34 #include "support/lstrings.h"
37 using namespace lyx::support;
43 /* codes used to read/write quotes to LyX files
52 char const * const language_char = "esgpfa";
53 char const * const side_char = "lr" ;
54 char const * const times_char = "sd";
56 // List of known quote chars
57 char const * const quote_char = ",'`<>";
59 // Unicode characters needed by each quote type
60 char_type const display_quote_char[2][5] = {
61 { 0x201a, 0x2019, 0x2018, 0x2039, 0x203a},
62 { 0x201e, 0x201d, 0x201c, 0x00ab, 0x00bb}
65 // Index of chars used for the quote. Index is [side, language]
66 int quote_index[2][6] = {
67 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
68 { 1, 1, 2, 1, 4, 3 } // "`'`'><"
71 // Corresponding LaTeX code, for double and single quotes.
72 char const * const latex_quote_t1[2][5] = {
73 { "\\quotesinglbase ", "'", "`",
74 "\\guilsinglleft{}", "\\guilsinglright{}" },
75 { ",,", "''", "``", "<<", ">>" }
78 char const * const latex_quote_ot1[2][5] = {
79 { "\\quotesinglbase ", "'", "`",
80 "\\guilsinglleft{}", "\\guilsinglright{}" },
81 { "\\quotedblbase ", "''", "``",
82 "\\guillemotleft{}", "\\guillemotright{}" }
85 char const * const latex_quote_babel[2][5] = {
86 { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
87 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" }
93 InsetQuotes::InsetQuotes(Buffer * buf, string const & str) : Inset(buf)
98 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, QuoteTimes t)
99 : Inset(buf), times_(t)
102 language_ = buf->params().quotes_language;
104 language_ = EnglishQuotes;
110 docstring InsetQuotes::layoutName() const
112 return from_ascii("Quotes");
116 void InsetQuotes::setSide(char_type c)
118 // Decide whether left or right
123 side_ = LeftQuote; // left quote
126 side_ = RightQuote; // right quote
131 void InsetQuotes::parseString(string const & s)
134 if (str.length() != 3) {
135 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
136 " bad string length." << endl;
142 for (i = 0; i < 6; ++i) {
143 if (str[0] == language_char[i]) {
144 language_ = QuoteLanguage(i);
149 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
150 " bad language specification." << endl;
151 language_ = EnglishQuotes;
154 for (i = 0; i < 2; ++i) {
155 if (str[1] == side_char[i]) {
156 side_ = QuoteSide(i);
161 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
162 " bad side specification." << endl;
166 for (i = 0; i < 2; ++i) {
167 if (str[2] == times_char[i]) {
168 times_ = QuoteTimes(i);
173 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
174 " bad times specification." << endl;
175 times_ = DoubleQuotes;
180 docstring InsetQuotes::displayString() const
182 Language const * loclang =
183 isBufferValid() ? buffer().params().language : 0;
184 int const index = quote_index[side_][language_];
185 docstring retdisp = docstring(1, display_quote_char[times_][index]);
187 // in french, spaces are added inside double quotes
188 // FIXME: this should be done by a separate quote type.
189 if (times_ == DoubleQuotes && loclang && prefixIs(loclang->code(), "fr")) {
190 if (side_ == LeftQuote)
193 retdisp.insert(size_t(0), 1, ' ');
200 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
202 FontInfo & font = mi.base.font;
203 frontend::FontMetrics const & fm =
204 theFontMetrics(font);
205 dim.asc = fm.maxAscent();
206 dim.des = fm.maxDescent();
209 // FIXME: should we add a language or a font parameter member?
210 docstring const text = displayString();
211 for (string::size_type i = 0; i < text.length(); ++i) {
213 dim.wid += fm.width('i');
214 else if (i == 0 || text[i] != text[i - 1])
215 dim.wid += fm.width(text[i]);
217 dim.wid += fm.width(',');
222 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
224 // FIXME: should we add a language or a font parameter member?
225 docstring const text = displayString();
226 FontInfo font = pi.base.font;
227 font.setPaintColor(pi.textColor(font.realColor()));
228 if (text.length() == 2 && text[0] == text[1]) {
229 pi.pain.text(x, y, text[0], font);
230 int const t = theFontMetrics(font)
232 pi.pain.text(x + t, y, text[0], font);
234 pi.pain.text(x, y, text, font);
239 void InsetQuotes::write(ostream & os) const
242 text += language_char[language_];
243 text += side_char[side_];
244 text += times_char[times_];
245 os << "Quotes " << text;
249 void InsetQuotes::read(Lexer & lex)
251 lex.setContext("InsetQuotes::read");
253 parseString(lex.getString());
254 lex >> "\\end_inset";
258 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
260 const int quoteind = quote_index[side_][language_];
263 if (language_ == FrenchQuotes && times_ == DoubleQuotes
264 && prefixIs(runparams.local_font->language()->code(), "fr")
265 && !runparams.use_polyglossia) {
266 if (side_ == LeftQuote)
267 qstr = "\\og "; //the spaces are important here
269 qstr = " \\fg{}"; //and here
270 } else if (lyxrc.fontenc == "T1" && !runparams.use_polyglossia) {
271 qstr = latex_quote_t1[times_][quoteind];
272 #ifdef DO_USE_DEFAULT_LANGUAGE
273 } else if (doclang == "default") {
275 } else if (!runparams.use_babel) {
277 // these are also used by polyglossia
278 qstr = latex_quote_ot1[times_][quoteind];
280 qstr = latex_quote_babel[times_][quoteind];
283 // Always guard against unfortunate ligatures (!` ?`)
284 if (prefixIs(qstr, "`")) {
285 char_type const lastchar = os.lastChar();
286 if (lastchar == '!' || lastchar == '?')
287 qstr.insert(0, "{}");
290 os << from_ascii(qstr);
294 int InsetQuotes::plaintext(odocstringstream & os,
295 OutputParams const &, size_t) const
297 docstring const str = displayString();
303 docstring InsetQuotes::getQuoteEntity() const {
304 if (times_ == DoubleQuotes) {
305 if (side_ == LeftQuote)
306 return from_ascii("“");
308 return from_ascii("”");
310 if (side_ == LeftQuote)
311 return from_ascii("‘");
313 return from_ascii("’");
317 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
319 os << getQuoteEntity();
324 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
326 xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
331 void InsetQuotes::toString(odocstream & os) const
333 os << displayString();
337 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
339 os += displayString();
343 void InsetQuotes::validate(LaTeXFeatures & features) const
345 char type = quote_char[quote_index[side_][language_]];
347 #ifdef DO_USE_DEFAULT_LANGUAGE
348 if (features.bufferParams().language->lang() == "default"
350 if (!features.useBabel()
352 && lyxrc.fontenc != "T1") {
353 if (times_ == SingleQuotes)
355 case ',': features.require("quotesinglbase"); break;
356 case '<': features.require("guilsinglleft"); break;
357 case '>': features.require("guilsinglright"); break;
362 case ',': features.require("quotedblbase"); break;
363 case '<': features.require("guillemotleft"); break;
364 case '>': features.require("guillemotright"); break;