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/font_metrics.h"
28 #include "frontends/Painter.h"
30 #include "support/lstrings.h"
34 using lyx::support::prefixIs;
44 /* codes used to read/write quotes to LyX files
53 char const * const language_char = "esgpfa";
54 char const * const side_char = "lr" ;
55 char const * const times_char = "sd";
57 // List of known quote chars
58 char const * const quote_char = ",'`<>";
60 // Index of chars used for the quote. Index is [side, language]
61 int quote_index[2][6] = {
62 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
63 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
65 // Corresponding LaTeX code, for double and single quotes.
66 char const * const latex_quote_t1[2][5] =
67 { { "\\quotesinglbase ", "'", "`",
68 "\\guilsinglleft{}", "\\guilsinglright{}" },
69 { ",,", "''", "``", "<<", ">>" } };
71 char const * const latex_quote_ot1[2][5] =
72 { { "\\quotesinglbase ", "'", "`",
73 "\\guilsinglleft{}", "\\guilsinglright{}" },
74 { "\\quotedblbase ", "''", "``",
75 "\\guillemotleft{}", "\\guillemotright{}" } };
77 char const * const latex_quote_babel[2][5] =
78 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
79 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
84 InsetQuotes::InsetQuotes(string const & str)
90 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
91 : language_(l), side_(s), times_(t)
95 InsetQuotes::InsetQuotes(lyx::char_type c, BufferParams const & params)
96 : language_(params.quotes_language), times_(params.quotes_times)
102 InsetQuotes::InsetQuotes(lyx::char_type c, quote_language l, quote_times t)
103 : language_(l), times_(t)
109 void InsetQuotes::getPosition(lyx::char_type c)
111 // Decide whether left or right
113 case ' ': case '(': case '[':
114 side_ = LeftQ; // left quote
117 side_ = RightQ; // right quote
122 void InsetQuotes::parseString(string const & s)
125 if (str.length() != 3) {
126 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
127 " bad string length." << endl;
133 for (i = 0; i < 6; ++i) {
134 if (str[0] == language_char[i]) {
135 language_ = quote_language(i);
140 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
141 " bad language specification." << endl;
142 language_ = EnglishQ;
145 for (i = 0; i < 2; ++i) {
146 if (str[1] == side_char[i]) {
147 side_ = quote_side(i);
152 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
153 " bad side specification." << endl;
157 for (i = 0; i < 2; ++i) {
158 if (str[2] == times_char[i]) {
159 times_ = quote_times(i);
164 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
165 " bad times specification." << endl;
171 string const InsetQuotes::dispString(Language const * loclang) const
174 disp += quote_char[quote_index[side_][language_]];
175 if (times_ == DoubleQ)
178 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
179 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
180 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
183 else if (disp == ">>")
187 // in french, spaces are added inside double quotes
188 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
192 disp.insert(string::size_type(0), 1, ' ');
199 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
201 LyXFont & font = mi.base.font;
202 dim.asc = font_metrics::maxAscent(font);
203 dim.des = font_metrics::maxDescent(font);
206 string const text = dispString(font.language());
207 for (string::size_type i = 0; i < text.length(); ++i) {
209 dim.wid += font_metrics::width('i', font);
210 else if (i == 0 || text[i] != text[i - 1])
211 dim.wid += font_metrics::width(text[i], font);
213 dim.wid += font_metrics::width(',', font);
220 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
232 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
234 string const text = dispString(pi.base.font.language());
236 if (text.length() == 2 && text[0] == text[1]) {
237 pi.pain.text(x, y, text[0], pi.base.font);
238 int const t = font_metrics::width(',', pi.base.font);
239 pi.pain.text(x + t, y, text[0], pi.base.font);
241 docstring dtext(text.begin(), text.end());
242 pi.pain.text(x, y, dtext, pi.base.font);
247 void InsetQuotes::write(Buffer const &, ostream & os) const
250 text += language_char[language_];
251 text += side_char[side_];
252 text += times_char[times_];
253 os << "Quotes " << text;
257 void InsetQuotes::read(Buffer const &, LyXLex & lex)
260 parseString(lex.getString());
262 if (lex.getString() != "\\end_inset") {
263 lex.printError("Missing \\end_inset at this point");
268 int InsetQuotes::latex(Buffer const &, ostream & os,
269 OutputParams const & runparams) const
271 const int quoteind = quote_index[side_][language_];
274 if (language_ == FrenchQ && times_ == DoubleQ
275 && prefixIs(runparams.local_font->language()->code(), "fr")) {
277 qstr = "\\og "; //the spaces are important here
279 qstr = " \\fg{}"; //and here
280 } else if (lyxrc.fontenc == "T1") {
281 qstr = latex_quote_t1[times_][quoteind];
282 #ifdef DO_USE_DEFAULT_LANGUAGE
283 } else if (doclang == "default") {
285 } else if (!runparams.use_babel) {
287 qstr = latex_quote_ot1[times_][quoteind];
289 qstr = latex_quote_babel[times_][quoteind];
292 // Always guard against unfortunate ligatures (!` ?`)
293 if (prefixIs(qstr, "`"))
294 qstr.insert(0, "{}");
301 int InsetQuotes::plaintext(Buffer const &, ostream & os,
302 OutputParams const &) const
309 int InsetQuotes::docbook(Buffer const &, ostream & os,
310 OutputParams const &) const
312 if (times_ == DoubleQ) {
327 int InsetQuotes::textString(Buffer const & buf, ostream & os,
328 OutputParams const & op) const
330 return plaintext(buf, os, op);
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_ == SingleQ)
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;
363 auto_ptr<InsetBase> InsetQuotes::doClone() const
365 return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
369 InsetBase::Code InsetQuotes::lyxCode() const
371 return InsetBase::QUOTE_CODE;