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"
20 #include "LaTeXFeatures.h"
23 #include "MetricsInfo.h"
24 #include "OutputParams.h"
25 #include "output_xhtml.h"
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
30 #include "support/debug.h"
31 #include "support/docstring.h"
32 #include "support/docstream.h"
33 #include "support/lstrings.h"
36 using namespace lyx::support;
42 /* codes used to read/write quotes to LyX files
51 char const * const language_char = "esgpfa";
52 char const * const side_char = "lr" ;
53 char const * const times_char = "sd";
55 // List of known quote chars
56 char const * const quote_char = ",'`<>";
58 // Unicode characters needed by each quote type
59 char_type const display_quote_char[2][5] = {
60 { 0x201a, 0x2019, 0x2018, 0x2039, 0x203a},
61 { 0x201e, 0x201d, 0x201c, 0x00ab, 0x00bb}
64 // Index of chars used for the quote. Index is [side, language]
65 int quote_index[2][6] = {
66 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
67 { 1, 1, 2, 1, 4, 3 } // "`'`'><"
70 // Corresponding LaTeX code, for double and single quotes.
71 char const * const latex_quote_t1[2][5] = {
72 { "\\quotesinglbase ", "'", "`",
73 "\\guilsinglleft{}", "\\guilsinglright{}" },
74 { ",,", "''", "``", "<<", ">>" }
77 char const * const latex_quote_ot1[2][5] = {
78 { "\\quotesinglbase ", "'", "`",
79 "\\guilsinglleft{}", "\\guilsinglright{}" },
80 { "\\quotedblbase ", "''", "``",
81 "\\guillemotleft{}", "\\guillemotright{}" }
84 char const * const latex_quote_babel[2][5] = {
85 { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
86 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" }
92 InsetQuotes::InsetQuotes(Buffer * buf, string const & str) : Inset(buf)
97 InsetQuotes::InsetQuotes(Buffer * buf, char_type c) : Inset(buf)
100 language_ = buf->params().quotes_language;
101 times_ = buf->params().quotes_times;
107 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, QuoteTimes t)
108 : Inset(buf), times_(t)
111 language_ = buf->params().quotes_language;
116 docstring InsetQuotes::name() const
118 return from_ascii("Quotes");
122 void InsetQuotes::setSide(char_type c)
124 // Decide whether left or right
129 side_ = LeftQuote; // left quote
132 side_ = RightQuote; // right quote
137 void InsetQuotes::parseString(string const & s)
140 if (str.length() != 3) {
141 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
142 " bad string length." << endl;
148 for (i = 0; i < 6; ++i) {
149 if (str[0] == language_char[i]) {
150 language_ = QuoteLanguage(i);
155 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
156 " bad language specification." << endl;
157 language_ = EnglishQuotes;
160 for (i = 0; i < 2; ++i) {
161 if (str[1] == side_char[i]) {
162 side_ = QuoteSide(i);
167 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
168 " bad side specification." << endl;
172 for (i = 0; i < 2; ++i) {
173 if (str[2] == times_char[i]) {
174 times_ = QuoteTimes(i);
179 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
180 " bad times specification." << endl;
181 times_ = DoubleQuotes;
186 docstring InsetQuotes::displayString() const
188 Language const * loclang = buffer().params().language;
189 int const index = quote_index[side_][language_];
190 docstring retdisp = docstring(1, display_quote_char[times_][index]);
192 // in french, spaces are added inside double quotes
193 if (times_ == DoubleQuotes && prefixIs(loclang->code(), "fr")) {
194 if (side_ == LeftQuote)
197 retdisp.insert(size_t(0), 1, ' ');
204 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
206 FontInfo & font = mi.base.font;
207 frontend::FontMetrics const & fm =
208 theFontMetrics(font);
209 dim.asc = fm.maxAscent();
210 dim.des = fm.maxDescent();
213 // FIXME: should we add a language or a font parameter member?
214 docstring const text = displayString();
215 for (string::size_type i = 0; i < text.length(); ++i) {
217 dim.wid += fm.width('i');
218 else if (i == 0 || text[i] != text[i - 1])
219 dim.wid += fm.width(text[i]);
221 dim.wid += fm.width(',');
226 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
228 // FIXME: should we add a language or a font parameter member?
229 docstring const text = displayString();
231 if (text.length() == 2 && text[0] == text[1]) {
232 pi.pain.text(x, y, text[0], pi.base.font);
233 int const t = theFontMetrics(pi.base.font)
235 pi.pain.text(x + t, y, text[0], pi.base.font);
237 pi.pain.text(x, y, text, pi.base.font);
242 void InsetQuotes::write(ostream & os) const
245 text += language_char[language_];
246 text += side_char[side_];
247 text += times_char[times_];
248 os << "Quotes " << text;
252 void InsetQuotes::read(Lexer & lex)
254 lex.setContext("InsetQuotes::read");
256 parseString(lex.getString());
257 lex >> "\\end_inset";
261 int InsetQuotes::latex(odocstream & os, OutputParams const & runparams) const
263 const int quoteind = quote_index[side_][language_];
266 if (language_ == FrenchQuotes && times_ == DoubleQuotes
267 && prefixIs(runparams.local_font->language()->code(), "fr")) {
268 if (side_ == LeftQuote)
269 qstr = "\\og "; //the spaces are important here
271 qstr = " \\fg{}"; //and here
272 } else if (lyxrc.fontenc == "T1") {
273 qstr = latex_quote_t1[times_][quoteind];
274 #ifdef DO_USE_DEFAULT_LANGUAGE
275 } else if (doclang == "default") {
277 } else if (!runparams.use_babel) {
279 qstr = latex_quote_ot1[times_][quoteind];
281 qstr = latex_quote_babel[times_][quoteind];
284 // Always guard against unfortunate ligatures (!` ?`)
285 if (prefixIs(qstr, "`"))
286 qstr.insert(0, "{}");
288 os << from_ascii(qstr);
293 int InsetQuotes::plaintext(odocstream & os, OutputParams const &) const
295 docstring const str = displayString();
301 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
303 if (times_ == DoubleQuotes) {
304 if (side_ == LeftQuote)
309 if (side_ == LeftQuote)
318 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const & op) const
320 docbook(xs.os(), op);
325 void InsetQuotes::tocString(odocstream & os) const
327 os << displayString();
331 void InsetQuotes::validate(LaTeXFeatures & features) const
333 bool const use_babel = features.useBabel();
334 char type = quote_char[quote_index[side_][language_]];
336 #ifdef DO_USE_DEFAULT_LANGUAGE
337 if (features.bufferParams().language->lang() == "default"
341 && lyxrc.fontenc != "T1") {
342 if (times_ == SingleQuotes)
344 case ',': features.require("quotesinglbase"); break;
345 case '<': features.require("guilsinglleft"); break;
346 case '>': features.require("guilsinglright"); break;
351 case ',': features.require("quotedblbase"); break;
352 case '<': features.require("guillemotleft"); break;
353 case '>': features.require("guillemotright"); break;