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"
15 #include "BufferView.h"
16 #include "LaTeXFeatures.h"
17 #include "frontends/Painter.h"
20 #include "dimension.h"
21 #include "frontends/font_metrics.h"
25 #include "paragraph.h"
27 #include "support/LAssert.h"
28 #include "support/lstrings.h"
35 // codes used to read/write quotes to LyX files
36 char const * const language_char = "esgpfa";
37 char const * const side_char = "lr" ;
38 char const * const times_char = "sd";
40 // List of known quote chars
41 char const * const quote_char = ",'`<>";
43 // Index of chars used for the quote. Index is [side, language]
44 int quote_index[2][6] = {
45 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
46 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
48 // Corresponding LaTeX code, for double and single quotes.
49 char const * const latex_quote_t1[2][5] =
50 { { "\\quotesinglbase ", "'", "`",
51 "\\guilsinglleft{}", "\\guilsinglright{}" },
52 { ",,", "''", "``", "<<", ">>" } };
54 char const * const latex_quote_ot1[2][5] =
55 { { "\\quotesinglbase ", "'", "`",
56 "\\guilsinglleft{}", "\\guilsinglright{}" },
57 { "\\quotedblbase ", "''", "``",
58 "\\guillemotleft{}", "\\guillemotright{}" } };
60 char const * const latex_quote_babel[2][5] =
61 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
62 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
67 InsetQuotes::InsetQuotes(string const & str)
73 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
74 : language_(l), side_(s), times_(t)
78 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
79 : language_(params.quotes_language), times_(params.quotes_times)
81 // Decide whether left or right
84 #warning eh ? I am lost here ...
85 //case Paragraph::META_HFILL:
86 // case Paragraph::META_NEWLINE:
87 side_ = LeftQ; // left quote
90 side_ = RightQ; // right quote
95 void InsetQuotes::parseString(string const & s)
98 if (str.length() != 3) {
99 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
100 " bad string length." << endl;
106 for (i = 0; i < 6; ++i) {
107 if (str[0] == language_char[i]) {
108 language_ = quote_language(i);
113 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
114 " bad language specification." << endl;
115 language_ = EnglishQ;
118 for (i = 0; i < 2; ++i) {
119 if (str[1] == side_char[i]) {
120 side_ = quote_side(i);
125 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
126 " bad side specification." << endl;
130 for (i = 0; i < 2; ++i) {
131 if (str[2] == times_char[i]) {
132 times_ = quote_times(i);
137 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
138 " bad times specification." << endl;
144 string const InsetQuotes::dispString(Language const * loclang) const
147 disp += quote_char[quote_index[side_][language_]];
148 if (times_ == DoubleQ)
151 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
152 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
153 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
156 else if (disp == ">>")
160 // in french, spaces are added inside double quotes
161 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
165 disp.insert(string::size_type(0), 1, ' ');
172 void InsetQuotes::dimension(BufferView *, LyXFont const & font,
173 Dimension & dim) const
175 dim.a = font_metrics::maxAscent(font);
176 dim.d = font_metrics::maxDescent(font);
179 string const text = dispString(font.language());
180 for (string::size_type i = 0; i < text.length(); ++i) {
182 dim.w += font_metrics::width('i', font);
183 else if (i == 0 || text[i] != text[i - 1])
184 dim.w += font_metrics::width(text[i], font);
186 dim.w += font_metrics::width(',', font);
192 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
204 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
205 int baseline, float & x) const
207 string const text = dispString(font.language());
209 if (text.length() == 2 && text[0] == text[1]) {
210 bv->painter().text(int(x), baseline, text[0], font);
211 int x2 = int(x + font_metrics::width(',', font));
212 bv->painter().text(x2, baseline, text[0], font);
214 bv->painter().text(int(x), baseline, text, font);
215 x += width(bv, font);
219 void InsetQuotes::write(Buffer const *, ostream & os) const
222 text += language_char[language_];
223 text += side_char[side_];
224 text += times_char[times_];
225 os << "Quotes " << text;
229 void InsetQuotes::read(Buffer const *, LyXLex & lex)
232 parseString(lex.getString());
234 if (lex.getString() != "\\end_inset") {
235 lex.printError("Missing \\end_inset at this point");
240 extern bool use_babel;
242 int InsetQuotes::latex(Buffer const * buf, ostream & os,
243 bool /*fragile*/, bool /* free_spc */) const
245 // How do we get the local language here??
246 lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
247 lyx::Assert(curr_pos != -1);
249 #warning FIXME. We _must_ find another way to get the language. (Lgb)
251 // This cannot be used. (Lgb)
252 string const curr_lang =
253 parOwner()->getFont(buf->params,
254 curr_pos).language()->babel();
256 // And this is not the way... (Lgb)
257 string const curr_lang = buf->params.language->lang();
259 const int quoteind = quote_index[side_][language_];
262 if (language_ == FrenchQ && times_ == DoubleQ
263 && curr_lang == "frenchb") {
265 qstr = "\\og "; //the spaces are important here
267 qstr = " \\fg{}"; //and here
268 } else if (language_ == FrenchQ && times_ == DoubleQ
269 && curr_lang == "french") {
271 qstr = "<< "; //the spaces are important here
273 qstr = " >>"; //and here
274 } else if (lyxrc.fontenc == "T1") {
275 qstr = latex_quote_t1[times_][quoteind];
276 #ifdef DO_USE_DEFAULT_LANGUAGE
277 } else if (doclang == "default") {
279 } else if (!use_babel) {
281 qstr = latex_quote_ot1[times_][quoteind];
283 qstr = latex_quote_babel[times_][quoteind];
286 // Always guard against unfortunate ligatures (!` ?`)
287 if (prefixIs(qstr, "`"))
288 qstr.insert(0, "{}");
295 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
302 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
309 int InsetQuotes::docbook(Buffer const *, ostream & os, bool) const
311 if (times_ == DoubleQ) {
326 void InsetQuotes::validate(LaTeXFeatures & features) const
328 char type = quote_char[quote_index[side_][language_]];
330 #ifdef DO_USE_DEFAULT_LANGUAGE
331 if (features.bufferParams().language->lang() == "default"
335 && lyxrc.fontenc != "T1") {
336 if (times_ == SingleQ)
338 case ',': features.require("quotesinglbase"); break;
339 case '<': features.require("guilsinglleft"); break;
340 case '>': features.require("guilsinglright"); break;
345 case ',': features.require("quotedblbase"); break;
346 case '<': features.require("guillemotleft"); break;
347 case '>': features.require("guillemotright"); break;
354 Inset * InsetQuotes::clone(Buffer const &, bool) const
356 return new InsetQuotes(language_, side_, times_);
360 Inset::Code InsetQuotes::lyxCode() const
362 return Inset::QUOTE_CODE;