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"
33 using lyx::support::prefixIs;
43 /* codes used to read/write quotes to LyX files
52 char const * const language_char = "esgpfa";
53 char const * const side_char = "lr" ;
54 char const * const times_char = "sd";
56 // List of known quote chars
57 char const * const quote_char = ",'`<>";
59 // Index of chars used for the quote. Index is [side, language]
60 int quote_index[2][6] = {
61 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
62 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
64 // Corresponding LaTeX code, for double and single quotes.
65 char const * const latex_quote_t1[2][5] =
66 { { "\\quotesinglbase ", "'", "`",
67 "\\guilsinglleft{}", "\\guilsinglright{}" },
68 { ",,", "''", "``", "<<", ">>" } };
70 char const * const latex_quote_ot1[2][5] =
71 { { "\\quotesinglbase ", "'", "`",
72 "\\guilsinglleft{}", "\\guilsinglright{}" },
73 { "\\quotedblbase ", "''", "``",
74 "\\guillemotleft{}", "\\guillemotright{}" } };
76 char const * const latex_quote_babel[2][5] =
77 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
78 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
83 InsetQuotes::InsetQuotes(string const & str)
89 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
90 : language_(l), side_(s), times_(t)
94 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
95 : language_(params.quotes_language), times_(params.quotes_times)
101 InsetQuotes::InsetQuotes(char c, quote_language l, quote_times t)
102 : language_(l), times_(t)
108 void InsetQuotes::getPosition(char c)
110 // Decide whether left or right
112 case ' ': case '(': case '[':
113 side_ = LeftQ; // left quote
116 side_ = RightQ; // right quote
121 void InsetQuotes::parseString(string const & s)
124 if (str.length() != 3) {
125 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
126 " bad string length." << endl;
132 for (i = 0; i < 6; ++i) {
133 if (str[0] == language_char[i]) {
134 language_ = quote_language(i);
139 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
140 " bad language specification." << endl;
141 language_ = EnglishQ;
144 for (i = 0; i < 2; ++i) {
145 if (str[1] == side_char[i]) {
146 side_ = quote_side(i);
151 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
152 " bad side specification." << endl;
156 for (i = 0; i < 2; ++i) {
157 if (str[2] == times_char[i]) {
158 times_ = quote_times(i);
163 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
164 " bad times specification." << endl;
170 string const InsetQuotes::dispString(Language const * loclang) const
173 disp += quote_char[quote_index[side_][language_]];
174 if (times_ == DoubleQ)
177 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
178 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
179 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
182 else if (disp == ">>")
186 // in french, spaces are added inside double quotes
187 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
191 disp.insert(string::size_type(0), 1, ' ');
198 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
200 LyXFont & font = mi.base.font;
201 dim.asc = font_metrics::maxAscent(font);
202 dim.des = font_metrics::maxDescent(font);
205 string const text = dispString(font.language());
206 for (string::size_type i = 0; i < text.length(); ++i) {
208 dim.wid += font_metrics::width('i', font);
209 else if (i == 0 || text[i] != text[i - 1])
210 dim.wid += font_metrics::width(text[i], font);
212 dim.wid += font_metrics::width(',', font);
219 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
231 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
233 string const text = dispString(pi.base.font.language());
235 if (text.length() == 2 && text[0] == text[1]) {
236 pi.pain.text(x, y, text[0], pi.base.font);
237 int const t = font_metrics::width(',', pi.base.font);
238 pi.pain.text(x + t, y, text[0], pi.base.font);
240 pi.pain.text(x, y, text, pi.base.font);
245 void InsetQuotes::write(Buffer const &, ostream & os) const
248 text += language_char[language_];
249 text += side_char[side_];
250 text += times_char[times_];
251 os << "Quotes " << text;
255 void InsetQuotes::read(Buffer const &, LyXLex & lex)
258 parseString(lex.getString());
260 if (lex.getString() != "\\end_inset") {
261 lex.printError("Missing \\end_inset at this point");
266 int InsetQuotes::latex(Buffer const &, ostream & os,
267 OutputParams const & runparams) const
269 const int quoteind = quote_index[side_][language_];
272 if (language_ == FrenchQ && times_ == DoubleQ
273 && runparams.lang == "frenchb") {
275 qstr = "\\og "; //the spaces are important here
277 qstr = " \\fg{}"; //and here
278 } else if (language_ == FrenchQ && times_ == DoubleQ
279 && runparams.lang == "french") {
281 qstr = "<< "; //the spaces are important here
283 qstr = " >>"; //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, "{}");
305 int InsetQuotes::plaintext(Buffer const &, ostream & os,
306 OutputParams const &) const
313 int InsetQuotes::linuxdoc(Buffer const &, ostream & os,
314 OutputParams const &) const
321 int InsetQuotes::docbook(Buffer const &, ostream & os,
322 OutputParams const &) const
324 if (times_ == DoubleQ) {
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::clone() const
370 return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
374 InsetOld::Code InsetQuotes::lyxCode() const
376 return InsetOld::QUOTE_CODE;