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
14 #include "insetquotes.h"
16 #include "support/LAssert.h"
17 #include "support/lstrings.h"
18 #include "BufferView.h"
19 #include "LaTeXFeatures.h"
20 #include "frontends/Painter.h"
23 #include "frontends/font_metrics.h"
27 #include "paragraph.h"
33 // Quotes. Used for the various quotes. German, English, French,
34 // Danish, Polish, all either double or single.
38 // codes used to read/write quotes to LyX files
39 char const * const language_char = "esgpfa";
40 char const * const side_char = "lr" ;
41 char const * const times_char = "sd";
43 // List of known quote chars
44 char const * const quote_char = ",'`<>";
46 // Index of chars used for the quote. Index is [side, language]
47 int quote_index[2][6] = {
48 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
49 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
51 // Corresponding LaTeX code, for double and single quotes.
52 char const * const latex_quote_t1[2][5] =
53 { { "\\quotesinglbase ", "'", "`",
54 "\\guilsinglleft{}", "\\guilsinglright{}" },
55 { ",,", "''", "``", "<<", ">>" } };
57 char const * const latex_quote_ot1[2][5] =
58 { { "\\quotesinglbase ", "'", "`",
59 "\\guilsinglleft{}", "\\guilsinglright{}" },
60 { "\\quotedblbase ", "''", "``",
61 "\\guillemotleft{}", "\\guillemotright{}" } };
63 char const * const latex_quote_babel[2][5] =
64 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
65 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
70 InsetQuotes::InsetQuotes(string const & str)
76 InsetQuotes::InsetQuotes(quote_language l,
79 : language_(l), side_(s), times_(t)
83 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
84 : language_(params.quotes_language), times_(params.quotes_times)
86 // Decide whether left or right
89 case Paragraph::META_HFILL:
90 case Paragraph::META_NEWLINE:
91 side_ = LeftQ; // left quote
94 side_ = RightQ; // right quote
99 void InsetQuotes::parseString(string const & s)
102 if (str.length() != 3) {
103 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
104 " bad string length." << endl;
110 for (i = 0; i < 6; ++i) {
111 if (str[0] == language_char[i]) {
112 language_ = quote_language(i);
117 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
118 " bad language specification." << endl;
119 language_ = EnglishQ;
122 for (i = 0; i < 2; ++i) {
123 if (str[1] == side_char[i]) {
124 side_ = quote_side(i);
129 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
130 " bad side specification." << endl;
134 for (i = 0; i < 2; ++i) {
135 if (str[2] == times_char[i]) {
136 times_ = quote_times(i);
141 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
142 " bad times specification." << endl;
148 string const InsetQuotes::dispString(Language const * loclang) const
151 disp += quote_char[quote_index[side_][language_]];
152 if (times_ == DoubleQ)
155 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
156 || lyxrc.font_norm_type == LyXRC::ISO_8859_3
157 || lyxrc.font_norm_type == LyXRC::ISO_8859_4
158 || lyxrc.font_norm_type == LyXRC::ISO_8859_9) {
161 else if (disp == "''")
164 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
165 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
166 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
169 else if (disp == ">>")
173 // in french, spaces are added inside double quotes
174 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
178 disp.insert(string::size_type(0), 1, ' ');
185 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
187 return font_metrics::maxAscent(font);
191 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
193 return font_metrics::maxDescent(font);
197 int InsetQuotes::width(BufferView *, LyXFont const & font) const
199 string const text = dispString(font.language());
202 for (string::size_type i = 0; i < text.length(); ++i) {
204 w += font_metrics::width('i', font);
205 else if (i == 0 || text[i] != text[i - 1])
206 w += font_metrics::width(text[i], font);
208 w += font_metrics::width(',', font);
216 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
228 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
229 int baseline, float & x, bool) const
231 string const text = dispString(font.language());
233 if (text.length() == 2 && text[0] == text[1]) {
234 bv->painter().text(int(x), baseline, text[0], font);
235 int x2 = int(x + font_metrics::width(',', font));
236 bv->painter().text(x2, baseline, text[0], font);
238 bv->painter().text(int(x), baseline, text, font);
239 x += width(bv, font);
243 void InsetQuotes::write(Buffer const *, ostream & os) const
246 text += language_char[language_];
247 text += side_char[side_];
248 text += times_char[times_];
249 os << "Quotes " << text;
253 void InsetQuotes::read(Buffer const *, LyXLex & lex)
256 parseString(lex.getString());
258 if (lex.getString() != "\\end_inset") {
259 lex.printError("Missing \\end_inset at this point");
264 extern bool use_babel;
266 int InsetQuotes::latex(Buffer const * buf, ostream & os,
267 bool /*fragile*/, bool /* free_spc */) const
269 // How do we get the local language here??
270 lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
271 lyx::Assert(curr_pos != -1);
272 string const curr_lang =
273 parOwner()->getFont(buf->params,
274 curr_pos).language()->babel();
276 const int quoteind = quote_index[side_][language_];
279 if (language_ == FrenchQ && times_ == DoubleQ
280 && curr_lang == "frenchb") {
282 qstr = "\\og "; //the spaces are important here
284 qstr = " \\fg{}"; //and here
285 } else if (language_ == FrenchQ && times_ == DoubleQ
286 && curr_lang == "french") {
288 qstr = "<< "; //the spaces are important here
290 qstr = " >>"; //and here
291 } else if (lyxrc.fontenc == "T1") {
292 qstr = latex_quote_t1[times_][quoteind];
293 #ifdef DO_USE_DEFAULT_LANGUAGE
294 } else if (doclang == "default") {
296 } else if (!use_babel) {
298 qstr = latex_quote_ot1[times_][quoteind];
300 qstr = latex_quote_babel[times_][quoteind];
303 // Always guard against unfortunate ligatures (!` ?`)
304 if (prefixIs(qstr, "`"))
305 qstr.insert(0, "{}");
312 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
319 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
326 int InsetQuotes::docbook(Buffer const *, ostream & os, bool) const
328 if (times_ == DoubleQ) {
343 void InsetQuotes::validate(LaTeXFeatures & features) const
345 char type = quote_char[quote_index[side_][language_]];
347 #ifdef DO_USE_DEFAULT_LANGUAGE
348 if (features.bufferParams().language->lang() == "default"
352 && lyxrc.fontenc != "T1") {
353 if (times_ == SingleQ)
355 case ',': features.require("quotesinglbase"); break;
356 case '<': features.require("guilsinglleft"); break;
357 case '>': features.require("guilsinglright"); break;
362 case ',': features.require("quotedblbase"); break;
363 case '<': features.require("guillemotleft"); break;
364 case '>': features.require("guillemotright"); break;
371 Inset * InsetQuotes::clone(Buffer const &, bool) const
373 return new InsetQuotes(language_, side_, times_);
377 Inset::Code InsetQuotes::lyxCode() const
379 return Inset::QUOTE_CODE;