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"
37 // codes used to read/write quotes to LyX files
38 char const * const language_char = "esgpfa";
39 char const * const side_char = "lr" ;
40 char const * const times_char = "sd";
42 // List of known quote chars
43 char const * const quote_char = ",'`<>";
45 // Index of chars used for the quote. Index is [side, language]
46 int quote_index[2][6] = {
47 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
48 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
50 // Corresponding LaTeX code, for double and single quotes.
51 char const * const latex_quote_t1[2][5] =
52 { { "\\quotesinglbase ", "'", "`",
53 "\\guilsinglleft{}", "\\guilsinglright{}" },
54 { ",,", "''", "``", "<<", ">>" } };
56 char const * const latex_quote_ot1[2][5] =
57 { { "\\quotesinglbase ", "'", "`",
58 "\\guilsinglleft{}", "\\guilsinglright{}" },
59 { "\\quotedblbase ", "''", "``",
60 "\\guillemotleft{}", "\\guillemotright{}" } };
62 char const * const latex_quote_babel[2][5] =
63 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
64 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
69 InsetQuotes::InsetQuotes(string const & str)
75 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
76 : language_(l), side_(s), times_(t)
80 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
81 : language_(params.quotes_language), times_(params.quotes_times)
83 // Decide whether left or right
86 #warning eh ? I am lost here ...
87 //case Paragraph::META_HFILL:
88 // case Paragraph::META_NEWLINE:
89 side_ = LeftQ; // left quote
92 side_ = RightQ; // right quote
97 void InsetQuotes::parseString(string const & s)
100 if (str.length() != 3) {
101 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
102 " bad string length." << endl;
108 for (i = 0; i < 6; ++i) {
109 if (str[0] == language_char[i]) {
110 language_ = quote_language(i);
115 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
116 " bad language specification." << endl;
117 language_ = EnglishQ;
120 for (i = 0; i < 2; ++i) {
121 if (str[1] == side_char[i]) {
122 side_ = quote_side(i);
127 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
128 " bad side specification." << endl;
132 for (i = 0; i < 2; ++i) {
133 if (str[2] == times_char[i]) {
134 times_ = quote_times(i);
139 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
140 " bad times specification." << endl;
146 string const InsetQuotes::dispString(Language const * loclang) const
149 disp += quote_char[quote_index[side_][language_]];
150 if (times_ == DoubleQ)
153 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
154 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
155 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
158 else if (disp == ">>")
162 // in french, spaces are added inside double quotes
163 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
167 disp.insert(string::size_type(0), 1, ' ');
174 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
176 LyXFont & font = mi.base.font;
177 dim.asc = font_metrics::maxAscent(font);
178 dim.des = font_metrics::maxDescent(font);
181 string const text = dispString(font.language());
182 for (string::size_type i = 0; i < text.length(); ++i) {
184 dim.wid += font_metrics::width('i', font);
185 else if (i == 0 || text[i] != text[i - 1])
186 dim.wid += font_metrics::width(text[i], font);
188 dim.wid += font_metrics::width(',', font);
194 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
206 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
208 string const text = dispString(pi.base.font.language());
210 if (text.length() == 2 && text[0] == text[1]) {
211 pi.pain.text(x, y, text[0], pi.base.font);
212 int const t = font_metrics::width(',', pi.base.font);
213 pi.pain.text(x + t, y, text[0], pi.base.font);
215 pi.pain.text(x, y, text, pi.base.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 void InsetQuotes::validate(LaTeXFeatures & features) const
327 bool const use_babel = features.useBabel();
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() const
356 return new InsetQuotes(language_, side_, times_);
360 Inset::Code InsetQuotes::lyxCode() const
362 return Inset::QUOTE_CODE;