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"
20 #include "latexrunparams.h"
23 #include "metricsinfo.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"
32 using namespace lyx::support;
41 /* codes used to read/write quotes to LyX files
50 char const * const language_char = "esgpfa";
51 char const * const side_char = "lr" ;
52 char const * const times_char = "sd";
54 // List of known quote chars
55 char const * const quote_char = ",'`<>";
57 // Index of chars used for the quote. Index is [side, language]
58 int quote_index[2][6] = {
59 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
60 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
62 // Corresponding LaTeX code, for double and single quotes.
63 char const * const latex_quote_t1[2][5] =
64 { { "\\quotesinglbase ", "'", "`",
65 "\\guilsinglleft{}", "\\guilsinglright{}" },
66 { ",,", "''", "``", "<<", ">>" } };
68 char const * const latex_quote_ot1[2][5] =
69 { { "\\quotesinglbase ", "'", "`",
70 "\\guilsinglleft{}", "\\guilsinglright{}" },
71 { "\\quotedblbase ", "''", "``",
72 "\\guillemotleft{}", "\\guillemotright{}" } };
74 char const * const latex_quote_babel[2][5] =
75 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
76 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
81 InsetQuotes::InsetQuotes(string const & str)
87 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
88 : language_(l), side_(s), times_(t)
92 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
93 : language_(params.quotes_language), times_(params.quotes_times)
95 // Decide whether left or right
98 #warning eh ? I am lost here ...
99 //case Paragraph::META_HFILL:
100 // case Paragraph::META_NEWLINE:
101 side_ = LeftQ; // left quote
104 side_ = RightQ; // right quote
109 void InsetQuotes::parseString(string const & s)
112 if (str.length() != 3) {
113 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
114 " bad string length." << endl;
120 for (i = 0; i < 6; ++i) {
121 if (str[0] == language_char[i]) {
122 language_ = quote_language(i);
127 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
128 " bad language specification." << endl;
129 language_ = EnglishQ;
132 for (i = 0; i < 2; ++i) {
133 if (str[1] == side_char[i]) {
134 side_ = quote_side(i);
139 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
140 " bad side specification." << endl;
144 for (i = 0; i < 2; ++i) {
145 if (str[2] == times_char[i]) {
146 times_ = quote_times(i);
151 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
152 " bad times specification." << endl;
158 string const InsetQuotes::dispString(Language const * loclang) const
161 disp += quote_char[quote_index[side_][language_]];
162 if (times_ == DoubleQ)
165 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
166 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
167 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
170 else if (disp == ">>")
174 // in french, spaces are added inside double quotes
175 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
179 disp.insert(string::size_type(0), 1, ' ');
186 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
188 LyXFont & font = mi.base.font;
189 dim.asc = font_metrics::maxAscent(font);
190 dim.des = font_metrics::maxDescent(font);
193 string const text = dispString(font.language());
194 for (string::size_type i = 0; i < text.length(); ++i) {
196 dim.wid += font_metrics::width('i', font);
197 else if (i == 0 || text[i] != text[i - 1])
198 dim.wid += font_metrics::width(text[i], font);
200 dim.wid += font_metrics::width(',', font);
207 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
219 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
221 string const text = dispString(pi.base.font.language());
223 if (text.length() == 2 && text[0] == text[1]) {
224 pi.pain.text(x, y, text[0], pi.base.font);
225 int const t = font_metrics::width(',', pi.base.font);
226 pi.pain.text(x + t, y, text[0], pi.base.font);
228 pi.pain.text(x, y, text, pi.base.font);
233 void InsetQuotes::write(Buffer const &, ostream & os) const
236 text += language_char[language_];
237 text += side_char[side_];
238 text += times_char[times_];
239 os << "Quotes " << text;
243 void InsetQuotes::read(Buffer const &, LyXLex & lex)
246 parseString(lex.getString());
248 if (lex.getString() != "\\end_inset") {
249 lex.printError("Missing \\end_inset at this point");
254 int InsetQuotes::latex(Buffer const & buf, ostream & os,
255 LatexRunParams const & runparams) const
257 // How do we get the local language here??
258 lyx::pos_type curr_pos = ownerPar(buf, this).getPositionOfInset(this);
259 BOOST_ASSERT(curr_pos != -1);
261 #warning FIXME. We _must_ find another way to get the language. (Lgb)
263 // This cannot be used. (Lgb)
264 string const curr_lang =
265 parOwner()->getFont(buf->params, curr_pos).language()->babel();
267 // And this is not the way... (Lgb)
268 string const curr_lang = buf.params().language->lang();
270 const int quoteind = quote_index[side_][language_];
273 if (language_ == FrenchQ && times_ == DoubleQ
274 && curr_lang == "frenchb") {
276 qstr = "\\og "; //the spaces are important here
278 qstr = " \\fg{}"; //and here
279 } else if (language_ == FrenchQ && times_ == DoubleQ
280 && curr_lang == "french") {
282 qstr = "<< "; //the spaces are important here
284 qstr = " >>"; //and here
285 } else if (lyxrc.fontenc == "T1") {
286 qstr = latex_quote_t1[times_][quoteind];
287 #ifdef DO_USE_DEFAULT_LANGUAGE
288 } else if (doclang == "default") {
290 } else if (!runparams.use_babel) {
292 qstr = latex_quote_ot1[times_][quoteind];
294 qstr = latex_quote_babel[times_][quoteind];
297 // Always guard against unfortunate ligatures (!` ?`)
298 if (prefixIs(qstr, "`"))
299 qstr.insert(0, "{}");
306 int InsetQuotes::ascii(Buffer const &, ostream & os, int) const
313 int InsetQuotes::linuxdoc(Buffer const &, ostream & os) const
320 int InsetQuotes::docbook(Buffer const &, ostream & os, bool) const
322 if (times_ == DoubleQ) {
337 void InsetQuotes::validate(LaTeXFeatures & features) const
339 bool const use_babel = features.useBabel();
340 char type = quote_char[quote_index[side_][language_]];
342 #ifdef DO_USE_DEFAULT_LANGUAGE
343 if (features.bufferParams().language->lang() == "default"
347 && lyxrc.fontenc != "T1") {
348 if (times_ == SingleQ)
350 case ',': features.require("quotesinglbase"); break;
351 case '<': features.require("guilsinglleft"); break;
352 case '>': features.require("guilsinglright"); break;
357 case ',': features.require("quotedblbase"); break;
358 case '<': features.require("guillemotleft"); break;
359 case '>': features.require("guillemotright"); break;
366 auto_ptr<InsetBase> InsetQuotes::clone() const
368 return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
372 InsetOld::Code InsetQuotes::lyxCode() const
374 return InsetOld::QUOTE_CODE;