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 "bufferparams.h"
19 #include "LaTeXFeatures.h"
22 #include "metricsinfo.h"
23 #include "outputparams.h"
24 #include "paragraph.h"
25 #include "paragraph_funcs.h"
27 #include "frontends/font_metrics.h"
28 #include "frontends/Painter.h"
30 #include "support/lstrings.h"
33 using lyx::support::prefixIs;
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)
101 InsetQuotes::InsetQuotes(char c, quote_language l, quote_times t)
102 : language_(l), times_(t)
108 void InsetQuotes::getPosition(char c)
110 // Decide whether left or right
113 // FIXME: what about '['? (jspitzm)
114 side_ = LeftQ; // left quote
117 side_ = RightQ; // right quote
122 void InsetQuotes::parseString(string const & s)
125 if (str.length() != 3) {
126 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
127 " bad string length." << endl;
133 for (i = 0; i < 6; ++i) {
134 if (str[0] == language_char[i]) {
135 language_ = quote_language(i);
140 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
141 " bad language specification." << endl;
142 language_ = EnglishQ;
145 for (i = 0; i < 2; ++i) {
146 if (str[1] == side_char[i]) {
147 side_ = quote_side(i);
152 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
153 " bad side specification." << endl;
157 for (i = 0; i < 2; ++i) {
158 if (str[2] == times_char[i]) {
159 times_ = quote_times(i);
164 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
165 " bad times specification." << endl;
171 string const InsetQuotes::dispString(Language const * loclang) const
174 disp += quote_char[quote_index[side_][language_]];
175 if (times_ == DoubleQ)
178 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
179 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
180 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
183 else if (disp == ">>")
187 // in french, spaces are added inside double quotes
188 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
192 disp.insert(string::size_type(0), 1, ' ');
199 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
201 LyXFont & font = mi.base.font;
202 dim.asc = font_metrics::maxAscent(font);
203 dim.des = font_metrics::maxDescent(font);
206 string const text = dispString(font.language());
207 for (string::size_type i = 0; i < text.length(); ++i) {
209 dim.wid += font_metrics::width('i', font);
210 else if (i == 0 || text[i] != text[i - 1])
211 dim.wid += font_metrics::width(text[i], font);
213 dim.wid += font_metrics::width(',', font);
220 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
232 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
234 string const text = dispString(pi.base.font.language());
236 if (text.length() == 2 && text[0] == text[1]) {
237 pi.pain.text(x, y, text[0], pi.base.font);
238 int const t = font_metrics::width(',', pi.base.font);
239 pi.pain.text(x + t, y, text[0], pi.base.font);
241 pi.pain.text(x, y, text, pi.base.font);
246 void InsetQuotes::write(Buffer const &, ostream & os) const
249 text += language_char[language_];
250 text += side_char[side_];
251 text += times_char[times_];
252 os << "Quotes " << text;
256 void InsetQuotes::read(Buffer const &, LyXLex & lex)
259 parseString(lex.getString());
261 if (lex.getString() != "\\end_inset") {
262 lex.printError("Missing \\end_inset at this point");
267 int InsetQuotes::latex(Buffer const & buf, ostream & os,
268 OutputParams const & runparams) const
270 // How do we get the local language here??
271 lyx::pos_type curr_pos = ownerPar(buf, this).getPositionOfInset(this);
272 BOOST_ASSERT(curr_pos != -1);
274 #warning FIXME. We _must_ find another way to get the language. (Lgb)
276 // This cannot be used. (Lgb)
277 string const curr_lang =
278 parOwner()->getFont(buf->params, curr_pos).language()->babel();
280 // And this is not the way... (Lgb)
281 string const curr_lang = buf.params().language->lang();
283 const int quoteind = quote_index[side_][language_];
286 if (language_ == FrenchQ && times_ == DoubleQ
287 && curr_lang == "frenchb") {
289 qstr = "\\og "; //the spaces are important here
291 qstr = " \\fg{}"; //and here
292 } else if (language_ == FrenchQ && times_ == DoubleQ
293 && curr_lang == "french") {
295 qstr = "<< "; //the spaces are important here
297 qstr = " >>"; //and here
298 } else if (lyxrc.fontenc == "T1") {
299 qstr = latex_quote_t1[times_][quoteind];
300 #ifdef DO_USE_DEFAULT_LANGUAGE
301 } else if (doclang == "default") {
303 } else if (!runparams.use_babel) {
305 qstr = latex_quote_ot1[times_][quoteind];
307 qstr = latex_quote_babel[times_][quoteind];
310 // Always guard against unfortunate ligatures (!` ?`)
311 if (prefixIs(qstr, "`"))
312 qstr.insert(0, "{}");
319 int InsetQuotes::plaintext(Buffer const &, ostream & os,
320 OutputParams const &) const
327 int InsetQuotes::linuxdoc(Buffer const &, ostream & os,
328 OutputParams const &) const
335 int InsetQuotes::docbook(Buffer const &, ostream & os,
336 OutputParams const &) const
338 if (times_ == DoubleQ) {
353 void InsetQuotes::validate(LaTeXFeatures & features) const
355 bool const use_babel = features.useBabel();
356 char type = quote_char[quote_index[side_][language_]];
358 #ifdef DO_USE_DEFAULT_LANGUAGE
359 if (features.bufferParams().language->lang() == "default"
363 && lyxrc.fontenc != "T1") {
364 if (times_ == SingleQ)
366 case ',': features.require("quotesinglbase"); break;
367 case '<': features.require("guilsinglleft"); break;
368 case '>': features.require("guilsinglright"); break;
373 case ',': features.require("quotedblbase"); break;
374 case '<': features.require("guillemotleft"); break;
375 case '>': features.require("guillemotright"); break;
382 auto_ptr<InsetBase> InsetQuotes::clone() const
384 return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
388 InsetOld::Code InsetQuotes::lyxCode() const
390 return InsetOld::QUOTE_CODE;