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 "metricsinfo.h"
27 #include "frontends/font_metrics.h"
28 #include "frontends/Painter.h"
29 #include "support/LAssert.h"
30 #include "support/lstrings.h"
32 using namespace lyx::support;
39 // codes used to read/write quotes to LyX files
40 char const * const language_char = "esgpfa";
41 char const * const side_char = "lr" ;
42 char const * const times_char = "sd";
44 // List of known quote chars
45 char const * const quote_char = ",'`<>";
47 // Index of chars used for the quote. Index is [side, language]
48 int quote_index[2][6] = {
49 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
50 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
52 // Corresponding LaTeX code, for double and single quotes.
53 char const * const latex_quote_t1[2][5] =
54 { { "\\quotesinglbase ", "'", "`",
55 "\\guilsinglleft{}", "\\guilsinglright{}" },
56 { ",,", "''", "``", "<<", ">>" } };
58 char const * const latex_quote_ot1[2][5] =
59 { { "\\quotesinglbase ", "'", "`",
60 "\\guilsinglleft{}", "\\guilsinglright{}" },
61 { "\\quotedblbase ", "''", "``",
62 "\\guillemotleft{}", "\\guillemotright{}" } };
64 char const * const latex_quote_babel[2][5] =
65 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
66 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
71 InsetQuotes::InsetQuotes(string const & str)
77 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
78 : language_(l), side_(s), times_(t)
82 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
83 : language_(params.quotes_language), times_(params.quotes_times)
85 // Decide whether left or right
88 #warning eh ? I am lost here ...
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_9
157 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
160 else if (disp == ">>")
164 // in french, spaces are added inside double quotes
165 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
169 disp.insert(string::size_type(0), 1, ' ');
176 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
178 LyXFont & font = mi.base.font;
179 dim.asc = font_metrics::maxAscent(font);
180 dim.des = font_metrics::maxDescent(font);
183 string const text = dispString(font.language());
184 for (string::size_type i = 0; i < text.length(); ++i) {
186 dim.wid += font_metrics::width('i', font);
187 else if (i == 0 || text[i] != text[i - 1])
188 dim.wid += font_metrics::width(text[i], font);
190 dim.wid += font_metrics::width(',', font);
196 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
208 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
210 string const text = dispString(pi.base.font.language());
212 if (text.length() == 2 && text[0] == text[1]) {
213 pi.pain.text(x, y, text[0], pi.base.font);
214 int const t = font_metrics::width(',', pi.base.font);
215 pi.pain.text(x + t, y, text[0], pi.base.font);
217 pi.pain.text(x, y, text, pi.base.font);
222 void InsetQuotes::write(Buffer const *, ostream & os) const
225 text += language_char[language_];
226 text += side_char[side_];
227 text += times_char[times_];
228 os << "Quotes " << text;
232 void InsetQuotes::read(Buffer const *, LyXLex & lex)
235 parseString(lex.getString());
237 if (lex.getString() != "\\end_inset") {
238 lex.printError("Missing \\end_inset at this point");
243 int InsetQuotes::latex(Buffer const * buf, ostream & os,
244 LatexRunParams const & runparams) const
246 // How do we get the local language here??
247 lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
248 Assert(curr_pos != -1);
250 #warning FIXME. We _must_ find another way to get the language. (Lgb)
252 // This cannot be used. (Lgb)
253 string const curr_lang =
254 parOwner()->getFont(buf->params,
255 curr_pos).language()->babel();
257 // And this is not the way... (Lgb)
258 string const curr_lang = buf->params.language->lang();
260 const int quoteind = quote_index[side_][language_];
263 if (language_ == FrenchQ && times_ == DoubleQ
264 && curr_lang == "frenchb") {
266 qstr = "\\og "; //the spaces are important here
268 qstr = " \\fg{}"; //and here
269 } else if (language_ == FrenchQ && times_ == DoubleQ
270 && curr_lang == "french") {
272 qstr = "<< "; //the spaces are important here
274 qstr = " >>"; //and here
275 } else if (lyxrc.fontenc == "T1") {
276 qstr = latex_quote_t1[times_][quoteind];
277 #ifdef DO_USE_DEFAULT_LANGUAGE
278 } else if (doclang == "default") {
280 } else if (!runparams.use_babel) {
282 qstr = latex_quote_ot1[times_][quoteind];
284 qstr = latex_quote_babel[times_][quoteind];
287 // Always guard against unfortunate ligatures (!` ?`)
288 if (prefixIs(qstr, "`"))
289 qstr.insert(0, "{}");
296 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
303 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
310 int InsetQuotes::docbook(Buffer const *, ostream & os, bool) const
312 if (times_ == DoubleQ) {
327 void InsetQuotes::validate(LaTeXFeatures & features) const
329 bool const use_babel = features.useBabel();
330 char type = quote_char[quote_index[side_][language_]];
332 #ifdef DO_USE_DEFAULT_LANGUAGE
333 if (features.bufferParams().language->lang() == "default"
337 && lyxrc.fontenc != "T1") {
338 if (times_ == SingleQ)
340 case ',': features.require("quotesinglbase"); break;
341 case '<': features.require("guilsinglleft"); break;
342 case '>': features.require("guilsinglright"); break;
347 case ',': features.require("quotedblbase"); break;
348 case '<': features.require("guillemotleft"); break;
349 case '>': features.require("guillemotright"); break;
356 InsetBase * InsetQuotes::clone() const
358 return new InsetQuotes(language_, side_, times_);
362 Inset::Code InsetQuotes::lyxCode() const
364 return Inset::QUOTE_CODE;