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;
40 // codes used to read/write quotes to LyX files
41 char const * const language_char = "esgpfa";
42 char const * const side_char = "lr" ;
43 char const * const times_char = "sd";
45 // List of known quote chars
46 char const * const quote_char = ",'`<>";
48 // Index of chars used for the quote. Index is [side, language]
49 int quote_index[2][6] = {
50 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
51 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
53 // Corresponding LaTeX code, for double and single quotes.
54 char const * const latex_quote_t1[2][5] =
55 { { "\\quotesinglbase ", "'", "`",
56 "\\guilsinglleft{}", "\\guilsinglright{}" },
57 { ",,", "''", "``", "<<", ">>" } };
59 char const * const latex_quote_ot1[2][5] =
60 { { "\\quotesinglbase ", "'", "`",
61 "\\guilsinglleft{}", "\\guilsinglright{}" },
62 { "\\quotedblbase ", "''", "``",
63 "\\guillemotleft{}", "\\guillemotright{}" } };
65 char const * const latex_quote_babel[2][5] =
66 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
67 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
72 InsetQuotes::InsetQuotes(string const & str)
78 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
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 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
179 LyXFont & font = mi.base.font;
180 dim.asc = font_metrics::maxAscent(font);
181 dim.des = font_metrics::maxDescent(font);
184 string const text = dispString(font.language());
185 for (string::size_type i = 0; i < text.length(); ++i) {
187 dim.wid += font_metrics::width('i', font);
188 else if (i == 0 || text[i] != text[i - 1])
189 dim.wid += font_metrics::width(text[i], font);
191 dim.wid += font_metrics::width(',', font);
198 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
210 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
212 string const text = dispString(pi.base.font.language());
214 if (text.length() == 2 && text[0] == text[1]) {
215 pi.pain.text(x, y, text[0], pi.base.font);
216 int const t = font_metrics::width(',', pi.base.font);
217 pi.pain.text(x + t, y, text[0], pi.base.font);
219 pi.pain.text(x, y, text, pi.base.font);
224 void InsetQuotes::write(Buffer const *, ostream & os) const
227 text += language_char[language_];
228 text += side_char[side_];
229 text += times_char[times_];
230 os << "Quotes " << text;
234 void InsetQuotes::read(Buffer const *, LyXLex & lex)
237 parseString(lex.getString());
239 if (lex.getString() != "\\end_inset") {
240 lex.printError("Missing \\end_inset at this point");
245 int InsetQuotes::latex(Buffer const * buf, ostream & os,
246 LatexRunParams const & runparams) const
248 // How do we get the local language here??
249 lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
250 Assert(curr_pos != -1);
252 #warning FIXME. We _must_ find another way to get the language. (Lgb)
254 // This cannot be used. (Lgb)
255 string const curr_lang =
256 parOwner()->getFont(buf->params,
257 curr_pos).language()->babel();
259 // And this is not the way... (Lgb)
260 string const curr_lang = buf->params.language->lang();
262 const int quoteind = quote_index[side_][language_];
265 if (language_ == FrenchQ && times_ == DoubleQ
266 && curr_lang == "frenchb") {
268 qstr = "\\og "; //the spaces are important here
270 qstr = " \\fg{}"; //and here
271 } else if (language_ == FrenchQ && times_ == DoubleQ
272 && curr_lang == "french") {
274 qstr = "<< "; //the spaces are important here
276 qstr = " >>"; //and here
277 } else if (lyxrc.fontenc == "T1") {
278 qstr = latex_quote_t1[times_][quoteind];
279 #ifdef DO_USE_DEFAULT_LANGUAGE
280 } else if (doclang == "default") {
282 } else if (!runparams.use_babel) {
284 qstr = latex_quote_ot1[times_][quoteind];
286 qstr = latex_quote_babel[times_][quoteind];
289 // Always guard against unfortunate ligatures (!` ?`)
290 if (prefixIs(qstr, "`"))
291 qstr.insert(0, "{}");
298 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
305 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
312 int InsetQuotes::docbook(Buffer const *, ostream & os, bool) const
314 if (times_ == DoubleQ) {
329 void InsetQuotes::validate(LaTeXFeatures & features) const
331 bool const use_babel = features.useBabel();
332 char type = quote_char[quote_index[side_][language_]];
334 #ifdef DO_USE_DEFAULT_LANGUAGE
335 if (features.bufferParams().language->lang() == "default"
339 && lyxrc.fontenc != "T1") {
340 if (times_ == SingleQ)
342 case ',': features.require("quotesinglbase"); break;
343 case '<': features.require("guilsinglleft"); break;
344 case '>': features.require("guilsinglright"); break;
349 case ',': features.require("quotedblbase"); break;
350 case '<': features.require("guillemotleft"); break;
351 case '>': features.require("guillemotright"); break;
358 auto_ptr<InsetBase> InsetQuotes::clone() const
360 return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
364 InsetOld::Code InsetQuotes::lyxCode() const
366 return InsetOld::QUOTE_CODE;