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 #warning eh ? I am lost here ...
90 //case Paragraph::META_HFILL:
91 // case Paragraph::META_NEWLINE:
92 side_ = LeftQ; // left quote
95 side_ = RightQ; // right quote
100 void InsetQuotes::parseString(string const & s)
103 if (str.length() != 3) {
104 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
105 " bad string length." << endl;
111 for (i = 0; i < 6; ++i) {
112 if (str[0] == language_char[i]) {
113 language_ = quote_language(i);
118 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
119 " bad language specification." << endl;
120 language_ = EnglishQ;
123 for (i = 0; i < 2; ++i) {
124 if (str[1] == side_char[i]) {
125 side_ = quote_side(i);
130 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
131 " bad side specification." << endl;
135 for (i = 0; i < 2; ++i) {
136 if (str[2] == times_char[i]) {
137 times_ = quote_times(i);
142 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
143 " bad times specification." << endl;
149 string const InsetQuotes::dispString(Language const * loclang) const
152 disp += quote_char[quote_index[side_][language_]];
153 if (times_ == DoubleQ)
156 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
157 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
158 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
161 else if (disp == ">>")
165 // in french, spaces are added inside double quotes
166 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
170 disp.insert(string::size_type(0), 1, ' ');
177 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
179 return font_metrics::maxAscent(font);
183 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
185 return font_metrics::maxDescent(font);
189 int InsetQuotes::width(BufferView *, LyXFont const & font) const
191 string const text = dispString(font.language());
194 for (string::size_type i = 0; i < text.length(); ++i) {
196 w += font_metrics::width('i', font);
197 else if (i == 0 || text[i] != text[i - 1])
198 w += font_metrics::width(text[i], font);
200 w += font_metrics::width(',', font);
208 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
220 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
221 int baseline, float & x) const
223 string const text = dispString(font.language());
225 if (text.length() == 2 && text[0] == text[1]) {
226 bv->painter().text(int(x), baseline, text[0], font);
227 int x2 = int(x + font_metrics::width(',', font));
228 bv->painter().text(x2, baseline, text[0], font);
230 bv->painter().text(int(x), baseline, text, font);
231 x += width(bv, font);
235 void InsetQuotes::write(Buffer const *, ostream & os) const
238 text += language_char[language_];
239 text += side_char[side_];
240 text += times_char[times_];
241 os << "Quotes " << text;
245 void InsetQuotes::read(Buffer const *, LyXLex & lex)
248 parseString(lex.getString());
250 if (lex.getString() != "\\end_inset") {
251 lex.printError("Missing \\end_inset at this point");
256 extern bool use_babel;
258 int InsetQuotes::latex(Buffer const * buf, ostream & os,
259 bool /*fragile*/, bool /* free_spc */) const
261 // How do we get the local language here??
262 lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
263 lyx::Assert(curr_pos != -1);
264 string const curr_lang =
265 parOwner()->getFont(buf->params,
266 curr_pos).language()->babel();
268 const int quoteind = quote_index[side_][language_];
271 if (language_ == FrenchQ && times_ == DoubleQ
272 && curr_lang == "frenchb") {
274 qstr = "\\og "; //the spaces are important here
276 qstr = " \\fg{}"; //and here
277 } else if (language_ == FrenchQ && times_ == DoubleQ
278 && curr_lang == "french") {
280 qstr = "<< "; //the spaces are important here
282 qstr = " >>"; //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 (!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::ascii(Buffer const *, ostream & os, int) const
311 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
318 int InsetQuotes::docbook(Buffer const *, ostream & os, bool) const
320 if (times_ == DoubleQ) {
335 void InsetQuotes::validate(LaTeXFeatures & features) const
337 char type = quote_char[quote_index[side_][language_]];
339 #ifdef DO_USE_DEFAULT_LANGUAGE
340 if (features.bufferParams().language->lang() == "default"
344 && lyxrc.fontenc != "T1") {
345 if (times_ == SingleQ)
347 case ',': features.require("quotesinglbase"); break;
348 case '<': features.require("guilsinglleft"); break;
349 case '>': features.require("guilsinglright"); break;
354 case ',': features.require("quotedblbase"); break;
355 case '<': features.require("guillemotleft"); break;
356 case '>': features.require("guillemotright"); break;
363 Inset * InsetQuotes::clone(Buffer const &, bool) const
365 return new InsetQuotes(language_, side_, times_);
369 Inset::Code InsetQuotes::lyxCode() const
371 return Inset::QUOTE_CODE;