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 "BufferView.h"
18 #include "dimension.h"
20 #include "LaTeXFeatures.h"
21 #include "latexrunparams.h"
25 #include "paragraph.h"
26 #include "frontends/font_metrics.h"
27 #include "frontends/Painter.h"
28 #include "support/LAssert.h"
29 #include "support/lstrings.h"
36 // codes used to read/write quotes to LyX files
37 char const * const language_char = "esgpfa";
38 char const * const side_char = "lr" ;
39 char const * const times_char = "sd";
41 // List of known quote chars
42 char const * const quote_char = ",'`<>";
44 // Index of chars used for the quote. Index is [side, language]
45 int quote_index[2][6] = {
46 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
47 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
49 // Corresponding LaTeX code, for double and single quotes.
50 char const * const latex_quote_t1[2][5] =
51 { { "\\quotesinglbase ", "'", "`",
52 "\\guilsinglleft{}", "\\guilsinglright{}" },
53 { ",,", "''", "``", "<<", ">>" } };
55 char const * const latex_quote_ot1[2][5] =
56 { { "\\quotesinglbase ", "'", "`",
57 "\\guilsinglleft{}", "\\guilsinglright{}" },
58 { "\\quotedblbase ", "''", "``",
59 "\\guillemotleft{}", "\\guillemotright{}" } };
61 char const * const latex_quote_babel[2][5] =
62 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
63 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
68 InsetQuotes::InsetQuotes(string const & str)
74 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
75 : language_(l), side_(s), times_(t)
79 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
80 : language_(params.quotes_language), times_(params.quotes_times)
82 // Decide whether left or right
85 #warning eh ? I am lost here ...
86 //case Paragraph::META_HFILL:
87 // case Paragraph::META_NEWLINE:
88 side_ = LeftQ; // left quote
91 side_ = RightQ; // right quote
96 void InsetQuotes::parseString(string const & s)
99 if (str.length() != 3) {
100 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
101 " bad string length." << endl;
107 for (i = 0; i < 6; ++i) {
108 if (str[0] == language_char[i]) {
109 language_ = quote_language(i);
114 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
115 " bad language specification." << endl;
116 language_ = EnglishQ;
119 for (i = 0; i < 2; ++i) {
120 if (str[1] == side_char[i]) {
121 side_ = quote_side(i);
126 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
127 " bad side specification." << endl;
131 for (i = 0; i < 2; ++i) {
132 if (str[2] == times_char[i]) {
133 times_ = quote_times(i);
138 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
139 " bad times specification." << endl;
145 string const InsetQuotes::dispString(Language const * loclang) const
148 disp += quote_char[quote_index[side_][language_]];
149 if (times_ == DoubleQ)
152 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
153 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
154 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
157 else if (disp == ">>")
161 // in french, spaces are added inside double quotes
162 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
166 disp.insert(string::size_type(0), 1, ' ');
173 void InsetQuotes::dimension(BufferView *, LyXFont const & font,
174 Dimension & dim) const
176 dim.a = font_metrics::maxAscent(font);
177 dim.d = font_metrics::maxDescent(font);
180 string const text = dispString(font.language());
181 for (string::size_type i = 0; i < text.length(); ++i) {
183 dim.w += font_metrics::width('i', font);
184 else if (i == 0 || text[i] != text[i - 1])
185 dim.w += font_metrics::width(text[i], font);
187 dim.w += font_metrics::width(',', font);
193 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
205 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
206 int baseline, float & x) const
208 string const text = dispString(font.language());
210 if (text.length() == 2 && text[0] == text[1]) {
211 bv->painter().text(int(x), baseline, text[0], font);
212 int x2 = int(x + font_metrics::width(',', font));
213 bv->painter().text(x2, baseline, text[0], font);
215 bv->painter().text(int(x), baseline, text, font);
216 x += width(bv, font);
220 void InsetQuotes::write(Buffer const *, ostream & os) const
223 text += language_char[language_];
224 text += side_char[side_];
225 text += times_char[times_];
226 os << "Quotes " << text;
230 void InsetQuotes::read(Buffer const *, LyXLex & lex)
233 parseString(lex.getString());
235 if (lex.getString() != "\\end_inset") {
236 lex.printError("Missing \\end_inset at this point");
241 int InsetQuotes::latex(Buffer const * buf, ostream & os,
242 LatexRunParams const & runparams) const
244 // How do we get the local language here??
245 lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
246 lyx::Assert(curr_pos != -1);
248 #warning FIXME. We _must_ find another way to get the language. (Lgb)
250 // This cannot be used. (Lgb)
251 string const curr_lang =
252 parOwner()->getFont(buf->params,
253 curr_pos).language()->babel();
255 // And this is not the way... (Lgb)
256 string const curr_lang = buf->params.language->lang();
258 const int quoteind = quote_index[side_][language_];
261 if (language_ == FrenchQ && times_ == DoubleQ
262 && curr_lang == "frenchb") {
264 qstr = "\\og "; //the spaces are important here
266 qstr = " \\fg{}"; //and here
267 } else if (language_ == FrenchQ && times_ == DoubleQ
268 && curr_lang == "french") {
270 qstr = "<< "; //the spaces are important here
272 qstr = " >>"; //and here
273 } else if (lyxrc.fontenc == "T1") {
274 qstr = latex_quote_t1[times_][quoteind];
275 #ifdef DO_USE_DEFAULT_LANGUAGE
276 } else if (doclang == "default") {
278 } else if (!runparams.use_babel) {
280 qstr = latex_quote_ot1[times_][quoteind];
282 qstr = latex_quote_babel[times_][quoteind];
285 // Always guard against unfortunate ligatures (!` ?`)
286 if (prefixIs(qstr, "`"))
287 qstr.insert(0, "{}");
294 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
301 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
308 int InsetQuotes::docbook(Buffer const *, ostream & os, bool) const
310 if (times_ == DoubleQ) {
325 extern bool use_babel;
327 void InsetQuotes::validate(LaTeXFeatures & features) const
329 char type = quote_char[quote_index[side_][language_]];
331 #ifdef DO_USE_DEFAULT_LANGUAGE
332 if (features.bufferParams().language->lang() == "default"
336 && lyxrc.fontenc != "T1") {
337 if (times_ == SingleQ)
339 case ',': features.require("quotesinglbase"); break;
340 case '<': features.require("guilsinglleft"); break;
341 case '>': features.require("guilsinglright"); break;
346 case ',': features.require("quotedblbase"); break;
347 case '<': features.require("guillemotleft"); break;
348 case '>': features.require("guillemotright"); break;
355 Inset * InsetQuotes::clone(Buffer const &, bool) const
357 return new InsetQuotes(language_, side_, times_);
361 Inset::Code InsetQuotes::lyxCode() const
363 return Inset::QUOTE_CODE;