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"
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 lyx::frontend::FontMetrics const & fm =
203 theFontMetrics(font);
204 dim.asc = fm.maxAscent();
205 dim.des = fm.maxDescent();
208 string const text = dispString(font.language());
209 for (string::size_type i = 0; i < text.length(); ++i) {
211 dim.wid += fm.width('i');
212 else if (i == 0 || text[i] != text[i - 1])
213 dim.wid += fm.width(text[i]);
215 dim.wid += fm.width(',');
222 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
234 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
236 string const text = dispString(pi.base.font.language());
238 if (text.length() == 2 && text[0] == text[1]) {
239 pi.pain.text(x, y, text[0], pi.base.font);
240 int const t = theFontMetrics(pi.base.font)
242 pi.pain.text(x + t, y, text[0], pi.base.font);
244 docstring dtext(text.begin(), text.end());
245 pi.pain.text(x, y, dtext, pi.base.font);
250 void InsetQuotes::write(Buffer const &, ostream & os) const
253 text += language_char[language_];
254 text += side_char[side_];
255 text += times_char[times_];
256 os << "Quotes " << text;
260 void InsetQuotes::read(Buffer const &, LyXLex & lex)
263 parseString(lex.getString());
265 if (lex.getString() != "\\end_inset") {
266 lex.printError("Missing \\end_inset at this point");
271 int InsetQuotes::latex(Buffer const &, ostream & os,
272 OutputParams const & runparams) const
274 const int quoteind = quote_index[side_][language_];
277 if (language_ == FrenchQ && times_ == DoubleQ
278 && prefixIs(runparams.local_font->language()->code(), "fr")) {
280 qstr = "\\og "; //the spaces are important here
282 qstr = " \\fg{}"; //and here
283 } else if (lyxrc.fontenc == "T1") {
284 qstr = latex_quote_t1[times_][quoteind];
285 #ifdef DO_USE_DEFAULT_LANGUAGE
286 } else if (doclang == "default") {
288 } else if (!runparams.use_babel) {
290 qstr = latex_quote_ot1[times_][quoteind];
292 qstr = latex_quote_babel[times_][quoteind];
295 // Always guard against unfortunate ligatures (!` ?`)
296 if (prefixIs(qstr, "`"))
297 qstr.insert(0, "{}");
304 int InsetQuotes::plaintext(Buffer const &, lyx::odocstream & os,
305 OutputParams const &) const
312 int InsetQuotes::docbook(Buffer const &, ostream & os,
313 OutputParams const &) const
315 if (times_ == DoubleQ) {
330 int InsetQuotes::textString(Buffer const & buf, lyx::odocstream & os,
331 OutputParams const & op) const
333 return plaintext(buf, os, op);
337 void InsetQuotes::validate(LaTeXFeatures & features) const
339 bool const use_babel = features.useBabel();
340 char type = quote_char[quote_index[side_][language_]];
342 #ifdef DO_USE_DEFAULT_LANGUAGE
343 if (features.bufferParams().language->lang() == "default"
347 && lyxrc.fontenc != "T1") {
348 if (times_ == SingleQ)
350 case ',': features.require("quotesinglbase"); break;
351 case '<': features.require("guilsinglleft"); break;
352 case '>': features.require("guilsinglright"); break;
357 case ',': features.require("quotedblbase"); break;
358 case '<': features.require("guillemotleft"); break;
359 case '>': features.require("guillemotright"); break;
366 auto_ptr<InsetBase> InsetQuotes::doClone() const
368 return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
372 InsetBase::Code InsetQuotes::lyxCode() const
374 return InsetBase::QUOTE_CODE;