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 "paragraph_funcs.h"
29 #include "frontends/font_metrics.h"
30 #include "frontends/Painter.h"
32 #include "support/LAssert.h"
33 #include "support/lstrings.h"
35 using namespace lyx::support;
43 /* codes used to read/write quotes to LyX files
52 char const * const language_char = "esgpfa";
53 char const * const side_char = "lr" ;
54 char const * const times_char = "sd";
56 // List of known quote chars
57 char const * const quote_char = ",'`<>";
59 // Index of chars used for the quote. Index is [side, language]
60 int quote_index[2][6] = {
61 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
62 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
64 // Corresponding LaTeX code, for double and single quotes.
65 char const * const latex_quote_t1[2][5] =
66 { { "\\quotesinglbase ", "'", "`",
67 "\\guilsinglleft{}", "\\guilsinglright{}" },
68 { ",,", "''", "``", "<<", ">>" } };
70 char const * const latex_quote_ot1[2][5] =
71 { { "\\quotesinglbase ", "'", "`",
72 "\\guilsinglleft{}", "\\guilsinglright{}" },
73 { "\\quotedblbase ", "''", "``",
74 "\\guillemotleft{}", "\\guillemotright{}" } };
76 char const * const latex_quote_babel[2][5] =
77 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
78 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
83 InsetQuotes::InsetQuotes(string const & str)
89 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
90 : language_(l), side_(s), times_(t)
94 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
95 : language_(params.quotes_language), times_(params.quotes_times)
97 // Decide whether left or right
100 #warning eh ? I am lost here ...
101 //case Paragraph::META_HFILL:
102 // case Paragraph::META_NEWLINE:
103 side_ = LeftQ; // left quote
106 side_ = RightQ; // right quote
111 void InsetQuotes::parseString(string const & s)
114 if (str.length() != 3) {
115 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
116 " bad string length." << endl;
122 for (i = 0; i < 6; ++i) {
123 if (str[0] == language_char[i]) {
124 language_ = quote_language(i);
129 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
130 " bad language specification." << endl;
131 language_ = EnglishQ;
134 for (i = 0; i < 2; ++i) {
135 if (str[1] == side_char[i]) {
136 side_ = quote_side(i);
141 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
142 " bad side specification." << endl;
146 for (i = 0; i < 2; ++i) {
147 if (str[2] == times_char[i]) {
148 times_ = quote_times(i);
153 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
154 " bad times specification." << endl;
160 string const InsetQuotes::dispString(Language const * loclang) const
163 disp += quote_char[quote_index[side_][language_]];
164 if (times_ == DoubleQ)
167 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
168 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
169 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
172 else if (disp == ">>")
176 // in french, spaces are added inside double quotes
177 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
181 disp.insert(string::size_type(0), 1, ' ');
188 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
190 LyXFont & font = mi.base.font;
191 dim.asc = font_metrics::maxAscent(font);
192 dim.des = font_metrics::maxDescent(font);
195 string const text = dispString(font.language());
196 for (string::size_type i = 0; i < text.length(); ++i) {
198 dim.wid += font_metrics::width('i', font);
199 else if (i == 0 || text[i] != text[i - 1])
200 dim.wid += font_metrics::width(text[i], font);
202 dim.wid += font_metrics::width(',', font);
209 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
221 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
223 string const text = dispString(pi.base.font.language());
225 if (text.length() == 2 && text[0] == text[1]) {
226 pi.pain.text(x, y, text[0], pi.base.font);
227 int const t = font_metrics::width(',', pi.base.font);
228 pi.pain.text(x + t, y, text[0], pi.base.font);
230 pi.pain.text(x, y, text, pi.base.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 int InsetQuotes::latex(Buffer const & buf, ostream & os,
257 LatexRunParams const & runparams) const
259 // How do we get the local language here??
260 lyx::pos_type curr_pos = ownerPar(buf, this).getPositionOfInset(this);
261 Assert(curr_pos != -1);
263 #warning FIXME. We _must_ find another way to get the language. (Lgb)
265 // This cannot be used. (Lgb)
266 string const curr_lang =
267 parOwner()->getFont(buf->params, curr_pos).language()->babel();
269 // And this is not the way... (Lgb)
270 string const curr_lang = buf.params.language->lang();
272 const int quoteind = quote_index[side_][language_];
275 if (language_ == FrenchQ && times_ == DoubleQ
276 && curr_lang == "frenchb") {
278 qstr = "\\og "; //the spaces are important here
280 qstr = " \\fg{}"; //and here
281 } else if (language_ == FrenchQ && times_ == DoubleQ
282 && curr_lang == "french") {
284 qstr = "<< "; //the spaces are important here
286 qstr = " >>"; //and here
287 } else if (lyxrc.fontenc == "T1") {
288 qstr = latex_quote_t1[times_][quoteind];
289 #ifdef DO_USE_DEFAULT_LANGUAGE
290 } else if (doclang == "default") {
292 } else if (!runparams.use_babel) {
294 qstr = latex_quote_ot1[times_][quoteind];
296 qstr = latex_quote_babel[times_][quoteind];
299 // Always guard against unfortunate ligatures (!` ?`)
300 if (prefixIs(qstr, "`"))
301 qstr.insert(0, "{}");
308 int InsetQuotes::ascii(Buffer const &, ostream & os, int) const
315 int InsetQuotes::linuxdoc(Buffer const &, ostream & os) const
322 int InsetQuotes::docbook(Buffer const &, ostream & os, bool) const
324 if (times_ == DoubleQ) {
339 void InsetQuotes::validate(LaTeXFeatures & features) const
341 bool const use_babel = features.useBabel();
342 char type = quote_char[quote_index[side_][language_]];
344 #ifdef DO_USE_DEFAULT_LANGUAGE
345 if (features.bufferParams().language->lang() == "default"
349 && lyxrc.fontenc != "T1") {
350 if (times_ == SingleQ)
352 case ',': features.require("quotesinglbase"); break;
353 case '<': features.require("guilsinglleft"); break;
354 case '>': features.require("guilsinglright"); break;
359 case ',': features.require("quotedblbase"); break;
360 case '<': features.require("guillemotleft"); break;
361 case '>': features.require("guillemotright"); break;
368 auto_ptr<InsetBase> InsetQuotes::clone() const
370 return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
374 InsetOld::Code InsetQuotes::lyxCode() const
376 return InsetOld::QUOTE_CODE;