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"
24 #include "Paragraph.h"
25 #include "paragraph_funcs.h"
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
30 #include "support/lstrings.h"
35 using support::prefixIs;
45 /* codes used to read/write quotes to LyX files
54 char const * const language_char = "esgpfa";
55 char const * const side_char = "lr" ;
56 char const * const times_char = "sd";
58 // List of known quote chars
59 char const * const quote_char = ",'`<>";
61 // Index of chars used for the quote. Index is [side, language]
62 int quote_index[2][6] = {
63 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
64 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
66 // Corresponding LaTeX code, for double and single quotes.
67 char const * const latex_quote_t1[2][5] =
68 { { "\\quotesinglbase ", "'", "`",
69 "\\guilsinglleft{}", "\\guilsinglright{}" },
70 { ",,", "''", "``", "<<", ">>" } };
72 char const * const latex_quote_ot1[2][5] =
73 { { "\\quotesinglbase ", "'", "`",
74 "\\guilsinglleft{}", "\\guilsinglright{}" },
75 { "\\quotedblbase ", "''", "``",
76 "\\guillemotleft{}", "\\guillemotright{}" } };
78 char const * const latex_quote_babel[2][5] =
79 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
80 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
85 InsetQuotes::InsetQuotes(string const & str)
91 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
92 : language_(l), side_(s), times_(t)
97 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
98 : language_(params.quotes_language), times_(params.quotes_times)
104 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
105 : language_(l), times_(t)
111 void InsetQuotes::getPosition(char_type c)
113 // Decide whether left or right
115 case ' ': case '(': case '[':
116 side_ = LeftQ; // left quote
119 side_ = RightQ; // right quote
124 void InsetQuotes::parseString(string const & s)
127 if (str.length() != 3) {
128 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
129 " bad string length." << endl;
135 for (i = 0; i < 6; ++i) {
136 if (str[0] == language_char[i]) {
137 language_ = quote_language(i);
142 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
143 " bad language specification." << endl;
144 language_ = EnglishQ;
147 for (i = 0; i < 2; ++i) {
148 if (str[1] == side_char[i]) {
149 side_ = quote_side(i);
154 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
155 " bad side specification." << endl;
159 for (i = 0; i < 2; ++i) {
160 if (str[2] == times_char[i]) {
161 times_ = quote_times(i);
166 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
167 " bad times specification." << endl;
173 docstring const InsetQuotes::dispString(Language const * loclang) const
176 disp += quote_char[quote_index[side_][language_]];
177 if (times_ == DoubleQ)
183 retdisp = docstring(1, 0x00ab); //'«';
184 else if (disp == ">>")
185 retdisp = docstring(1, 0x00bb); //'»';
187 // The below are supposed to work, but something fails.
188 else if (disp == ",,")
189 retdisp = docstring(1, 0x201e);
190 else if (disp == "''")
191 retdisp == docstring(1, 0x201d);
192 else if (disp == "``")
193 retdisp == docstring(1, 0x201c);
194 else if (disp == "<")
195 retdisp = docstring(1, 0x2039);
196 else if (disp == ">")
197 retdisp = docstring(1, 0x203a);
198 else if (disp == ",")
199 retdisp = docstring(1, 0x201a);
200 else if (disp == "'")
201 retdisp = docstring(1, 0x2019);
202 else if (disp == "`")
203 retdisp = docstring(1, 0x2018);
206 retdisp = lyx::from_ascii(disp);
208 // in french, spaces are added inside double quotes
209 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
213 retdisp.insert(docstring::size_type(0), 1, ' ');
220 bool InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
222 Font & font = mi.base.font;
223 frontend::FontMetrics const & fm =
224 theFontMetrics(font);
225 dim.asc = fm.maxAscent();
226 dim.des = fm.maxDescent();
229 docstring const text = dispString(font.language());
230 for (string::size_type i = 0; i < text.length(); ++i) {
232 dim.wid += fm.width('i');
233 else if (i == 0 || text[i] != text[i - 1])
234 dim.wid += fm.width(text[i]);
236 dim.wid += fm.width(',');
238 bool const changed = dim_ != dim;
245 Font const InsetQuotes::convertFont(Font const & f) const
257 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
259 docstring const text = dispString(pi.base.font.language());
261 if (text.length() == 2 && text[0] == text[1]) {
262 pi.pain.text(x, y, text[0], pi.base.font);
263 int const t = theFontMetrics(pi.base.font)
265 pi.pain.text(x + t, y, text[0], pi.base.font);
267 pi.pain.text(x, y, text, pi.base.font);
269 setPosCache(pi, x, y);
273 void InsetQuotes::write(Buffer const &, ostream & os) const
276 text += language_char[language_];
277 text += side_char[side_];
278 text += times_char[times_];
279 os << "Quotes " << text;
283 void InsetQuotes::read(Buffer const &, Lexer & lex)
286 parseString(lex.getString());
288 if (lex.getString() != "\\end_inset") {
289 lex.printError("Missing \\end_inset at this point");
294 int InsetQuotes::latex(Buffer const &, odocstream & os,
295 OutputParams const & runparams) const
297 const int quoteind = quote_index[side_][language_];
300 if (language_ == FrenchQ && times_ == DoubleQ
301 && prefixIs(runparams.local_font->language()->code(), "fr")) {
303 qstr = "\\og "; //the spaces are important here
305 qstr = " \\fg{}"; //and here
306 } else if (lyxrc.fontenc == "T1") {
307 qstr = latex_quote_t1[times_][quoteind];
308 #ifdef DO_USE_DEFAULT_LANGUAGE
309 } else if (doclang == "default") {
311 } else if (!runparams.use_babel) {
313 qstr = latex_quote_ot1[times_][quoteind];
315 qstr = latex_quote_babel[times_][quoteind];
318 // Always guard against unfortunate ligatures (!` ?`)
319 if (prefixIs(qstr, "`"))
320 qstr.insert(0, "{}");
322 os << from_ascii(qstr);
327 int InsetQuotes::plaintext(Buffer const & buf, odocstream & os,
328 OutputParams const &) const
330 docstring const str = dispString(buf.params().language);
336 int InsetQuotes::docbook(Buffer const &, odocstream & os,
337 OutputParams const &) const
339 if (times_ == DoubleQ) {
354 void InsetQuotes::textString(Buffer const & buf, odocstream & os) const
356 os << dispString(buf.params().language);
360 void InsetQuotes::validate(LaTeXFeatures & features) const
362 bool const use_babel = features.useBabel();
363 char type = quote_char[quote_index[side_][language_]];
365 #ifdef DO_USE_DEFAULT_LANGUAGE
366 if (features.bufferParams().language->lang() == "default"
370 && lyxrc.fontenc != "T1") {
371 if (times_ == SingleQ)
373 case ',': features.require("quotesinglbase"); break;
374 case '<': features.require("guilsinglleft"); break;
375 case '>': features.require("guilsinglright"); break;
380 case ',': features.require("quotedblbase"); break;
381 case '<': features.require("guillemotleft"); break;
382 case '>': features.require("guillemotright"); break;
389 auto_ptr<Inset> InsetQuotes::doClone() const
391 return auto_ptr<Inset>(new InsetQuotes(language_, side_, times_));
395 Inset::Code InsetQuotes::lyxCode() const
397 return Inset::QUOTE_CODE;