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"
19 #include "LaTeXFeatures.h"
22 #include "MetricsInfo.h"
23 #include "OutputParams.h"
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
28 #include "support/lstrings.h"
33 using support::prefixIs;
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 // Index of chars used for the quote. Index is [side, language]
59 int quote_index[2][6] = {
60 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
61 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
63 // Corresponding LaTeX code, for double and single quotes.
64 char const * const latex_quote_t1[2][5] =
65 { { "\\quotesinglbase ", "'", "`",
66 "\\guilsinglleft{}", "\\guilsinglright{}" },
67 { ",,", "''", "``", "<<", ">>" } };
69 char const * const latex_quote_ot1[2][5] =
70 { { "\\quotesinglbase ", "'", "`",
71 "\\guilsinglleft{}", "\\guilsinglright{}" },
72 { "\\quotedblbase ", "''", "``",
73 "\\guillemotleft{}", "\\guillemotright{}" } };
75 char const * const latex_quote_babel[2][5] =
76 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
77 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
82 InsetQuotes::InsetQuotes(string const & str)
88 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
89 : language_(l), side_(s), times_(t)
94 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
95 : language_(params.quotes_language), times_(params.quotes_times)
101 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
102 : language_(l), times_(t)
108 void InsetQuotes::getPosition(char_type c)
110 // Decide whether left or right
112 case ' ': case '(': case '[':
113 side_ = LeftQ; // left quote
116 side_ = RightQ; // right quote
121 void InsetQuotes::parseString(string const & s)
124 if (str.length() != 3) {
125 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
126 " bad string length." << endl;
132 for (i = 0; i < 6; ++i) {
133 if (str[0] == language_char[i]) {
134 language_ = quote_language(i);
139 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
140 " bad language specification." << endl;
141 language_ = EnglishQ;
144 for (i = 0; i < 2; ++i) {
145 if (str[1] == side_char[i]) {
146 side_ = quote_side(i);
151 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
152 " bad side specification." << endl;
156 for (i = 0; i < 2; ++i) {
157 if (str[2] == times_char[i]) {
158 times_ = quote_times(i);
163 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
164 " bad times specification." << endl;
170 docstring const InsetQuotes::dispString(Language const * loclang) const
173 disp += quote_char[quote_index[side_][language_]];
174 if (times_ == DoubleQ)
180 retdisp = docstring(1, 0x00ab); //'«';
181 else if (disp == ">>")
182 retdisp = docstring(1, 0x00bb); //'»';
184 // The below are supposed to work, but something fails.
185 else if (disp == ",,")
186 retdisp = docstring(1, 0x201e);
187 else if (disp == "''")
188 retdisp == docstring(1, 0x201d);
189 else if (disp == "``")
190 retdisp == docstring(1, 0x201c);
191 else if (disp == "<")
192 retdisp = docstring(1, 0x2039);
193 else if (disp == ">")
194 retdisp = docstring(1, 0x203a);
195 else if (disp == ",")
196 retdisp = docstring(1, 0x201a);
197 else if (disp == "'")
198 retdisp = docstring(1, 0x2019);
199 else if (disp == "`")
200 retdisp = docstring(1, 0x2018);
203 retdisp = lyx::from_ascii(disp);
205 // in french, spaces are added inside double quotes
206 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
210 retdisp.insert(docstring::size_type(0), 1, ' ');
217 bool InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
219 Font & font = mi.base.font;
220 frontend::FontMetrics const & fm =
221 theFontMetrics(font);
222 dim.asc = fm.maxAscent();
223 dim.des = fm.maxDescent();
226 docstring const text = dispString(font.language());
227 for (string::size_type i = 0; i < text.length(); ++i) {
229 dim.wid += fm.width('i');
230 else if (i == 0 || text[i] != text[i - 1])
231 dim.wid += fm.width(text[i]);
233 dim.wid += fm.width(',');
235 bool const changed = dim_ != dim;
242 Font const InsetQuotes::convertFont(Font const & f) const
254 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
256 docstring const text = dispString(pi.base.font.language());
258 if (text.length() == 2 && text[0] == text[1]) {
259 pi.pain.text(x, y, text[0], pi.base.font);
260 int const t = theFontMetrics(pi.base.font)
262 pi.pain.text(x + t, y, text[0], pi.base.font);
264 pi.pain.text(x, y, text, pi.base.font);
266 setPosCache(pi, x, y);
270 void InsetQuotes::write(Buffer const &, ostream & os) const
273 text += language_char[language_];
274 text += side_char[side_];
275 text += times_char[times_];
276 os << "Quotes " << text;
280 void InsetQuotes::read(Buffer const &, Lexer & lex)
283 parseString(lex.getString());
285 if (lex.getString() != "\\end_inset") {
286 lex.printError("Missing \\end_inset at this point");
291 int InsetQuotes::latex(Buffer const &, odocstream & os,
292 OutputParams const & runparams) const
294 const int quoteind = quote_index[side_][language_];
297 if (language_ == FrenchQ && times_ == DoubleQ
298 && prefixIs(runparams.local_font->language()->code(), "fr")) {
300 qstr = "\\og "; //the spaces are important here
302 qstr = " \\fg{}"; //and here
303 } else if (lyxrc.fontenc == "T1") {
304 qstr = latex_quote_t1[times_][quoteind];
305 #ifdef DO_USE_DEFAULT_LANGUAGE
306 } else if (doclang == "default") {
308 } else if (!runparams.use_babel) {
310 qstr = latex_quote_ot1[times_][quoteind];
312 qstr = latex_quote_babel[times_][quoteind];
315 // Always guard against unfortunate ligatures (!` ?`)
316 if (prefixIs(qstr, "`"))
317 qstr.insert(0, "{}");
319 os << from_ascii(qstr);
324 int InsetQuotes::plaintext(Buffer const & buf, odocstream & os,
325 OutputParams const &) const
327 docstring const str = dispString(buf.params().language);
333 int InsetQuotes::docbook(Buffer const &, odocstream & os,
334 OutputParams const &) const
336 if (times_ == DoubleQ) {
351 void InsetQuotes::textString(Buffer const & buf, odocstream & os) const
353 os << dispString(buf.params().language);
357 void InsetQuotes::validate(LaTeXFeatures & features) const
359 bool const use_babel = features.useBabel();
360 char type = quote_char[quote_index[side_][language_]];
362 #ifdef DO_USE_DEFAULT_LANGUAGE
363 if (features.bufferParams().language->lang() == "default"
367 && lyxrc.fontenc != "T1") {
368 if (times_ == SingleQ)
370 case ',': features.require("quotesinglbase"); break;
371 case '<': features.require("guilsinglleft"); break;
372 case '>': features.require("guilsinglright"); break;
377 case ',': features.require("quotedblbase"); break;
378 case '<': features.require("guillemotleft"); break;
379 case '>': features.require("guillemotright"); break;
386 Inset * InsetQuotes::clone() const
388 return new InsetQuotes(language_, side_, times_);
392 Inset::Code InsetQuotes::lyxCode() const
394 return Inset::QUOTE_CODE;