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)
96 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
97 : language_(params.quotes_language), times_(params.quotes_times)
103 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
104 : language_(l), times_(t)
110 void InsetQuotes::getPosition(char_type c)
112 // Decide whether left or right
114 case ' ': case '(': case '[':
115 side_ = LeftQ; // left quote
118 side_ = RightQ; // right quote
123 void InsetQuotes::parseString(string const & s)
126 if (str.length() != 3) {
127 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
128 " bad string length." << endl;
134 for (i = 0; i < 6; ++i) {
135 if (str[0] == language_char[i]) {
136 language_ = quote_language(i);
141 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
142 " bad language specification." << endl;
143 language_ = EnglishQ;
146 for (i = 0; i < 2; ++i) {
147 if (str[1] == side_char[i]) {
148 side_ = quote_side(i);
153 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
154 " bad side specification." << endl;
158 for (i = 0; i < 2; ++i) {
159 if (str[2] == times_char[i]) {
160 times_ = quote_times(i);
165 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
166 " bad times specification." << endl;
172 string const InsetQuotes::dispString(Language const * loclang) const
175 disp += quote_char[quote_index[side_][language_]];
176 if (times_ == DoubleQ)
179 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
180 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
181 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
184 else if (disp == ">>")
188 // in french, spaces are added inside double quotes
189 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
193 disp.insert(string::size_type(0), 1, ' ');
200 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
202 LyXFont & font = mi.base.font;
203 frontend::FontMetrics const & fm =
204 theFontMetrics(font);
205 dim.asc = fm.maxAscent();
206 dim.des = fm.maxDescent();
209 string const text = dispString(font.language());
210 for (string::size_type i = 0; i < text.length(); ++i) {
212 dim.wid += fm.width('i');
213 else if (i == 0 || text[i] != text[i - 1])
214 dim.wid += fm.width(text[i]);
216 dim.wid += fm.width(',');
223 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
235 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
237 string const text = dispString(pi.base.font.language());
239 if (text.length() == 2 && text[0] == text[1]) {
240 pi.pain.text(x, y, text[0], pi.base.font);
241 int const t = theFontMetrics(pi.base.font)
243 pi.pain.text(x + t, y, text[0], pi.base.font);
245 docstring dtext(text.begin(), text.end());
246 pi.pain.text(x, y, dtext, pi.base.font);
251 void InsetQuotes::write(Buffer const &, ostream & os) const
254 text += language_char[language_];
255 text += side_char[side_];
256 text += times_char[times_];
257 os << "Quotes " << text;
261 void InsetQuotes::read(Buffer const &, LyXLex & lex)
264 parseString(lex.getString());
266 if (lex.getString() != "\\end_inset") {
267 lex.printError("Missing \\end_inset at this point");
272 int InsetQuotes::latex(Buffer const &, odocstream & os,
273 OutputParams const & runparams) const
275 const int quoteind = quote_index[side_][language_];
278 if (language_ == FrenchQ && times_ == DoubleQ
279 && prefixIs(runparams.local_font->language()->code(), "fr")) {
281 qstr = "\\og "; //the spaces are important here
283 qstr = " \\fg{}"; //and here
284 } else if (lyxrc.fontenc == "T1") {
285 qstr = latex_quote_t1[times_][quoteind];
286 #ifdef DO_USE_DEFAULT_LANGUAGE
287 } else if (doclang == "default") {
289 } else if (!runparams.use_babel) {
291 qstr = latex_quote_ot1[times_][quoteind];
293 qstr = latex_quote_babel[times_][quoteind];
296 // Always guard against unfortunate ligatures (!` ?`)
297 if (prefixIs(qstr, "`"))
298 qstr.insert(0, "{}");
300 os << from_ascii(qstr);
305 int InsetQuotes::plaintext(Buffer const &, odocstream & os,
306 OutputParams const &) const
313 int InsetQuotes::docbook(Buffer const &, odocstream & os,
314 OutputParams const &) const
316 if (times_ == DoubleQ) {
331 int InsetQuotes::textString(Buffer const & buf, odocstream & os,
332 OutputParams const & op) const
334 return plaintext(buf, os, op);
338 void InsetQuotes::validate(LaTeXFeatures & features) const
340 bool const use_babel = features.useBabel();
341 char type = quote_char[quote_index[side_][language_]];
343 #ifdef DO_USE_DEFAULT_LANGUAGE
344 if (features.bufferParams().language->lang() == "default"
348 && lyxrc.fontenc != "T1") {
349 if (times_ == SingleQ)
351 case ',': features.require("quotesinglbase"); break;
352 case '<': features.require("guilsinglleft"); break;
353 case '>': features.require("guilsinglright"); break;
358 case ',': features.require("quotedblbase"); break;
359 case '<': features.require("guillemotleft"); break;
360 case '>': features.require("guillemotright"); break;
367 auto_ptr<InsetBase> InsetQuotes::doClone() const
369 return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
373 InsetBase::Code InsetQuotes::lyxCode() const
375 return InsetBase::QUOTE_CODE;