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;
107 docstring InsetQuotes::layoutName() const
109 return from_ascii("Quotes");
113 void InsetQuotes::setSide(char_type c)
115 // Decide whether left or right
120 side_ = LeftQuote; // left quote
123 side_ = RightQuote; // right quote
128 void InsetQuotes::parseString(string const & s)
131 if (str.length() != 3) {
132 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
133 " bad string length." << endl;
139 for (i = 0; i < 6; ++i) {
140 if (str[0] == language_char[i]) {
141 language_ = QuoteLanguage(i);
146 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
147 " bad language specification." << endl;
148 language_ = EnglishQuotes;
151 for (i = 0; i < 2; ++i) {
152 if (str[1] == side_char[i]) {
153 side_ = QuoteSide(i);
158 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
159 " bad side specification." << endl;
163 for (i = 0; i < 2; ++i) {
164 if (str[2] == times_char[i]) {
165 times_ = QuoteTimes(i);
170 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
171 " bad times specification." << endl;
172 times_ = DoubleQuotes;
177 docstring InsetQuotes::displayString() const
179 Language const * loclang =
180 isBufferValid() ? buffer().params().language : 0;
181 int const index = quote_index[side_][language_];
182 docstring retdisp = docstring(1, display_quote_char[times_][index]);
184 // in french, spaces are added inside double quotes
185 // FIXME: this should be done by a separate quote type.
186 if (times_ == DoubleQuotes && loclang && prefixIs(loclang->code(), "fr")) {
187 if (side_ == LeftQuote)
190 retdisp.insert(size_t(0), 1, ' ');
197 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
199 FontInfo & font = mi.base.font;
200 frontend::FontMetrics const & fm =
201 theFontMetrics(font);
202 dim.asc = fm.maxAscent();
203 dim.des = fm.maxDescent();
206 // FIXME: should we add a language or a font parameter member?
207 docstring const text = displayString();
208 for (string::size_type i = 0; i < text.length(); ++i) {
210 dim.wid += fm.width('i');
211 else if (i == 0 || text[i] != text[i - 1])
212 dim.wid += fm.width(text[i]);
214 dim.wid += fm.width(',');
219 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
221 // FIXME: should we add a language or a font parameter member?
222 docstring const text = displayString();
223 FontInfo font = pi.base.font;
224 font.setPaintColor(pi.textColor(font.realColor()));
225 if (text.length() == 2 && text[0] == text[1]) {
226 pi.pain.text(x, y, text[0], font);
227 int const t = theFontMetrics(font)
229 pi.pain.text(x + t, y, text[0], font);
231 pi.pain.text(x, y, text, 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 if (language_ == FrenchQuotes && times_ == DoubleQuotes
261 && prefixIs(runparams.local_font->language()->code(), "fr")
262 && !runparams.use_polyglossia) {
263 if (side_ == LeftQuote)
264 qstr = "\\og "; //the spaces are important here
266 qstr = " \\fg{}"; //and here
267 } else if (lyxrc.fontenc == "T1" && !runparams.use_polyglossia) {
268 qstr = latex_quote_t1[times_][quoteind];
269 #ifdef DO_USE_DEFAULT_LANGUAGE
270 } else if (doclang == "default") {
272 } else if (!runparams.use_babel) {
274 // these are also used by polyglossia
275 qstr = latex_quote_ot1[times_][quoteind];
277 qstr = latex_quote_babel[times_][quoteind];
280 // Always guard against unfortunate ligatures (!` ?`)
281 if (prefixIs(qstr, "`")) {
282 char_type const lastchar = os.lastChar();
283 if (lastchar == '!' || lastchar == '?')
284 qstr.insert(0, "{}");
287 os << from_ascii(qstr);
291 int InsetQuotes::plaintext(odocstringstream & os,
292 OutputParams const &, size_t) const
294 docstring const str = displayString();
300 docstring InsetQuotes::getQuoteEntity() const {
301 if (times_ == DoubleQuotes) {
302 if (side_ == LeftQuote)
303 return from_ascii("“");
305 return from_ascii("”");
307 if (side_ == LeftQuote)
308 return from_ascii("‘");
310 return from_ascii("’");
314 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
316 os << getQuoteEntity();
321 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
323 xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
328 void InsetQuotes::toString(odocstream & os) const
330 os << displayString();
334 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
336 os += displayString();
340 void InsetQuotes::validate(LaTeXFeatures & features) const
342 char type = quote_char[quote_index[side_][language_]];
344 #ifdef DO_USE_DEFAULT_LANGUAGE
345 if (features.bufferParams().language->lang() == "default"
347 if (!features.useBabel()
349 && lyxrc.fontenc != "T1") {
350 if (times_ == SingleQuotes)
352 case ',': features.require("quotesinglbase"); break;
353 case '<': features.require("guilsinglleft"); break;
354 case '>': features.require("guilsinglright"); break;
359 case ',': features.require("quotedblbase"); break;
360 case '<': features.require("guillemotleft"); break;
361 case '>': features.require("guillemotright"); break;