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) : Inset(buf)
101 language_ = buf->params().quotes_language;
102 times_ = buf->params().quotes_times;
108 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, QuoteTimes t)
109 : Inset(buf), times_(t)
112 language_ = buf->params().quotes_language;
117 docstring InsetQuotes::name() const
119 return from_ascii("Quotes");
123 void InsetQuotes::setSide(char_type c)
125 // Decide whether left or right
130 side_ = LeftQuote; // left quote
133 side_ = RightQuote; // right quote
138 void InsetQuotes::parseString(string const & s)
141 if (str.length() != 3) {
142 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
143 " bad string length." << endl;
149 for (i = 0; i < 6; ++i) {
150 if (str[0] == language_char[i]) {
151 language_ = QuoteLanguage(i);
156 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
157 " bad language specification." << endl;
158 language_ = EnglishQuotes;
161 for (i = 0; i < 2; ++i) {
162 if (str[1] == side_char[i]) {
163 side_ = QuoteSide(i);
168 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
169 " bad side specification." << endl;
173 for (i = 0; i < 2; ++i) {
174 if (str[2] == times_char[i]) {
175 times_ = QuoteTimes(i);
180 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
181 " bad times specification." << endl;
182 times_ = DoubleQuotes;
187 docstring InsetQuotes::displayString() const
189 Language const * loclang =
190 isBufferValid() ? buffer().params().language : 0;
191 int const index = quote_index[side_][language_];
192 docstring retdisp = docstring(1, display_quote_char[times_][index]);
194 // in french, spaces are added inside double quotes
195 // FIXME: this should be done by a separate quote type.
196 if (times_ == DoubleQuotes && loclang && prefixIs(loclang->code(), "fr")) {
197 if (side_ == LeftQuote)
200 retdisp.insert(size_t(0), 1, ' ');
207 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
209 FontInfo & font = mi.base.font;
210 frontend::FontMetrics const & fm =
211 theFontMetrics(font);
212 dim.asc = fm.maxAscent();
213 dim.des = fm.maxDescent();
216 // FIXME: should we add a language or a font parameter member?
217 docstring const text = displayString();
218 for (string::size_type i = 0; i < text.length(); ++i) {
220 dim.wid += fm.width('i');
221 else if (i == 0 || text[i] != text[i - 1])
222 dim.wid += fm.width(text[i]);
224 dim.wid += fm.width(',');
229 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
231 // FIXME: should we add a language or a font parameter member?
232 docstring const text = displayString();
234 if (text.length() == 2 && text[0] == text[1]) {
235 pi.pain.text(x, y, text[0], pi.base.font);
236 int const t = theFontMetrics(pi.base.font)
238 pi.pain.text(x + t, y, text[0], pi.base.font);
240 pi.pain.text(x, y, text, pi.base.font);
245 void InsetQuotes::write(ostream & os) const
248 text += language_char[language_];
249 text += side_char[side_];
250 text += times_char[times_];
251 os << "Quotes " << text;
255 void InsetQuotes::read(Lexer & lex)
257 lex.setContext("InsetQuotes::read");
259 parseString(lex.getString());
260 lex >> "\\end_inset";
264 int InsetQuotes::latex(odocstream & os, OutputParams const & runparams) const
266 const int quoteind = quote_index[side_][language_];
269 if (language_ == FrenchQuotes && times_ == DoubleQuotes
270 && prefixIs(runparams.local_font->language()->code(), "fr")) {
271 if (side_ == LeftQuote)
272 qstr = "\\og "; //the spaces are important here
274 qstr = " \\fg{}"; //and here
275 } else if (lyxrc.fontenc == "T1") {
276 qstr = latex_quote_t1[times_][quoteind];
277 #ifdef DO_USE_DEFAULT_LANGUAGE
278 } else if (doclang == "default") {
280 } else if (!runparams.use_babel) {
282 qstr = latex_quote_ot1[times_][quoteind];
284 qstr = latex_quote_babel[times_][quoteind];
287 // Always guard against unfortunate ligatures (!` ?`)
288 if (prefixIs(qstr, "`"))
289 qstr.insert(0, "{}");
291 os << from_ascii(qstr);
296 int InsetQuotes::plaintext(odocstream & os, OutputParams const &) const
298 docstring const str = displayString();
304 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
306 if (times_ == DoubleQuotes) {
307 if (side_ == LeftQuote)
312 if (side_ == LeftQuote)
321 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const & op) const
323 docbook(xs.os(), op);
328 void InsetQuotes::tocString(odocstream & os) const
330 os << displayString();
334 void InsetQuotes::validate(LaTeXFeatures & features) const
336 bool const use_babel = features.useBabel();
337 char type = quote_char[quote_index[side_][language_]];
339 #ifdef DO_USE_DEFAULT_LANGUAGE
340 if (features.bufferParams().language->lang() == "default"
344 && lyxrc.fontenc != "T1") {
345 if (times_ == SingleQuotes)
347 case ',': features.require("quotesinglbase"); break;
348 case '<': features.require("guilsinglleft"); break;
349 case '>': features.require("guilsinglright"); break;
354 case ',': features.require("quotedblbase"); break;
355 case '<': features.require("guillemotleft"); break;
356 case '>': features.require("guillemotright"); break;