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/Application.h"
28 #include "frontends/FontLoader.h"
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.h"
32 #include "support/lstrings.h"
36 using lyx::support::prefixIs;
46 /* codes used to read/write quotes to LyX files
55 char const * const language_char = "esgpfa";
56 char const * const side_char = "lr" ;
57 char const * const times_char = "sd";
59 // List of known quote chars
60 char const * const quote_char = ",'`<>";
62 // Index of chars used for the quote. Index is [side, language]
63 int quote_index[2][6] = {
64 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
65 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
67 // Corresponding LaTeX code, for double and single quotes.
68 char const * const latex_quote_t1[2][5] =
69 { { "\\quotesinglbase ", "'", "`",
70 "\\guilsinglleft{}", "\\guilsinglright{}" },
71 { ",,", "''", "``", "<<", ">>" } };
73 char const * const latex_quote_ot1[2][5] =
74 { { "\\quotesinglbase ", "'", "`",
75 "\\guilsinglleft{}", "\\guilsinglright{}" },
76 { "\\quotedblbase ", "''", "``",
77 "\\guillemotleft{}", "\\guillemotright{}" } };
79 char const * const latex_quote_babel[2][5] =
80 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
81 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
86 InsetQuotes::InsetQuotes(string const & str)
92 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
93 : language_(l), side_(s), times_(t)
97 InsetQuotes::InsetQuotes(lyx::char_type c, BufferParams const & params)
98 : language_(params.quotes_language), times_(params.quotes_times)
104 InsetQuotes::InsetQuotes(lyx::char_type c, quote_language l, quote_times t)
105 : language_(l), times_(t)
111 void InsetQuotes::getPosition(lyx::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 string const InsetQuotes::dispString(Language const * loclang) const
176 disp += quote_char[quote_index[side_][language_]];
177 if (times_ == DoubleQ)
180 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
181 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
182 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
185 else if (disp == ">>")
189 // in french, spaces are added inside double quotes
190 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
194 disp.insert(string::size_type(0), 1, ' ');
201 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
203 LyXFont & font = mi.base.font;
204 lyx::frontend::FontMetrics const & fm =
205 theApp->fontLoader().metrics(font);
206 dim.asc = fm.maxAscent();
207 dim.des = fm.maxDescent();
210 string const text = dispString(font.language());
211 for (string::size_type i = 0; i < text.length(); ++i) {
213 dim.wid += fm.width('i');
214 else if (i == 0 || text[i] != text[i - 1])
215 dim.wid += fm.width(text[i]);
217 dim.wid += fm.width(',');
224 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
236 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
238 string const text = dispString(pi.base.font.language());
240 if (text.length() == 2 && text[0] == text[1]) {
241 pi.pain.text(x, y, text[0], pi.base.font);
242 int const t = theApp->fontLoader().metrics(pi.base.font)
244 pi.pain.text(x + t, y, text[0], pi.base.font);
246 docstring dtext(text.begin(), text.end());
247 pi.pain.text(x, y, dtext, pi.base.font);
252 void InsetQuotes::write(Buffer const &, ostream & os) const
255 text += language_char[language_];
256 text += side_char[side_];
257 text += times_char[times_];
258 os << "Quotes " << text;
262 void InsetQuotes::read(Buffer const &, LyXLex & lex)
265 parseString(lex.getString());
267 if (lex.getString() != "\\end_inset") {
268 lex.printError("Missing \\end_inset at this point");
273 int InsetQuotes::latex(Buffer const &, ostream & os,
274 OutputParams const & runparams) const
276 const int quoteind = quote_index[side_][language_];
279 if (language_ == FrenchQ && times_ == DoubleQ
280 && prefixIs(runparams.local_font->language()->code(), "fr")) {
282 qstr = "\\og "; //the spaces are important here
284 qstr = " \\fg{}"; //and here
285 } else if (lyxrc.fontenc == "T1") {
286 qstr = latex_quote_t1[times_][quoteind];
287 #ifdef DO_USE_DEFAULT_LANGUAGE
288 } else if (doclang == "default") {
290 } else if (!runparams.use_babel) {
292 qstr = latex_quote_ot1[times_][quoteind];
294 qstr = latex_quote_babel[times_][quoteind];
297 // Always guard against unfortunate ligatures (!` ?`)
298 if (prefixIs(qstr, "`"))
299 qstr.insert(0, "{}");
306 int InsetQuotes::plaintext(Buffer const &, ostream & os,
307 OutputParams const &) const
314 int InsetQuotes::docbook(Buffer const &, ostream & os,
315 OutputParams const &) const
317 if (times_ == DoubleQ) {
332 int InsetQuotes::textString(Buffer const & buf, ostream & os,
333 OutputParams const & op) const
335 return plaintext(buf, os, op);
339 void InsetQuotes::validate(LaTeXFeatures & features) const
341 bool const use_babel = features.useBabel();
342 char type = quote_char[quote_index[side_][language_]];
344 #ifdef DO_USE_DEFAULT_LANGUAGE
345 if (features.bufferParams().language->lang() == "default"
349 && lyxrc.fontenc != "T1") {
350 if (times_ == SingleQ)
352 case ',': features.require("quotesinglbase"); break;
353 case '<': features.require("guilsinglleft"); break;
354 case '>': features.require("guilsinglright"); break;
359 case ',': features.require("quotedblbase"); break;
360 case '<': features.require("guillemotleft"); break;
361 case '>': features.require("guillemotright"); break;
368 auto_ptr<InsetBase> InsetQuotes::doClone() const
370 return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
374 InsetBase::Code InsetQuotes::lyxCode() const
376 return InsetBase::QUOTE_CODE;