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 lyx::support::prefixIs;
42 /* codes used to read/write quotes to LyX files
51 char const * const language_char = "esgpfa";
52 char const * const side_char = "lr" ;
53 char const * const times_char = "sd";
55 // List of known quote chars
56 char const * const quote_char = ",'`<>";
58 // Index of chars used for the quote. Index is [side, language]
59 int quote_index[2][6] = {
60 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
61 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
63 // Corresponding LaTeX code, for double and single quotes.
64 char const * const latex_quote_t1[2][5] =
65 { { "\\quotesinglbase ", "'", "`",
66 "\\guilsinglleft{}", "\\guilsinglright{}" },
67 { ",,", "''", "``", "<<", ">>" } };
69 char const * const latex_quote_ot1[2][5] =
70 { { "\\quotesinglbase ", "'", "`",
71 "\\guilsinglleft{}", "\\guilsinglright{}" },
72 { "\\quotedblbase ", "''", "``",
73 "\\guillemotleft{}", "\\guillemotright{}" } };
75 char const * const latex_quote_babel[2][5] =
76 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
77 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
82 InsetQuotes::InsetQuotes(string const & str)
88 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
89 : language_(l), side_(s), times_(t)
93 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
94 : language_(params.quotes_language), times_(params.quotes_times)
96 // Decide whether left or right
99 #warning eh ? I am lost here ...
100 //case Paragraph::META_HFILL:
101 // case Paragraph::META_NEWLINE:
102 side_ = LeftQ; // left quote
105 side_ = RightQ; // right quote
110 void InsetQuotes::parseString(string const & s)
113 if (str.length() != 3) {
114 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
115 " bad string length." << endl;
121 for (i = 0; i < 6; ++i) {
122 if (str[0] == language_char[i]) {
123 language_ = quote_language(i);
128 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
129 " bad language specification." << endl;
130 language_ = EnglishQ;
133 for (i = 0; i < 2; ++i) {
134 if (str[1] == side_char[i]) {
135 side_ = quote_side(i);
140 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
141 " bad side specification." << endl;
145 for (i = 0; i < 2; ++i) {
146 if (str[2] == times_char[i]) {
147 times_ = quote_times(i);
152 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
153 " bad times specification." << endl;
159 string const InsetQuotes::dispString(Language const * loclang) const
162 disp += quote_char[quote_index[side_][language_]];
163 if (times_ == DoubleQ)
166 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
167 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
168 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
171 else if (disp == ">>")
175 // in french, spaces are added inside double quotes
176 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
180 disp.insert(string::size_type(0), 1, ' ');
187 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
189 LyXFont & font = mi.base.font;
190 dim.asc = font_metrics::maxAscent(font);
191 dim.des = font_metrics::maxDescent(font);
194 string const text = dispString(font.language());
195 for (string::size_type i = 0; i < text.length(); ++i) {
197 dim.wid += font_metrics::width('i', font);
198 else if (i == 0 || text[i] != text[i - 1])
199 dim.wid += font_metrics::width(text[i], font);
201 dim.wid += font_metrics::width(',', font);
208 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
220 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
222 string const text = dispString(pi.base.font.language());
224 if (text.length() == 2 && text[0] == text[1]) {
225 pi.pain.text(x, y, text[0], pi.base.font);
226 int const t = font_metrics::width(',', pi.base.font);
227 pi.pain.text(x + t, y, text[0], pi.base.font);
229 pi.pain.text(x, y, text, pi.base.font);
234 void InsetQuotes::write(Buffer const &, ostream & os) const
237 text += language_char[language_];
238 text += side_char[side_];
239 text += times_char[times_];
240 os << "Quotes " << text;
244 void InsetQuotes::read(Buffer const &, LyXLex & lex)
247 parseString(lex.getString());
249 if (lex.getString() != "\\end_inset") {
250 lex.printError("Missing \\end_inset at this point");
255 int InsetQuotes::latex(Buffer const & buf, ostream & os,
256 LatexRunParams const & runparams) const
258 // How do we get the local language here??
259 lyx::pos_type curr_pos = ownerPar(buf, this).getPositionOfInset(this);
260 BOOST_ASSERT(curr_pos != -1);
262 #warning FIXME. We _must_ find another way to get the language. (Lgb)
264 // This cannot be used. (Lgb)
265 string const curr_lang =
266 parOwner()->getFont(buf->params, curr_pos).language()->babel();
268 // And this is not the way... (Lgb)
269 string const curr_lang = buf.params().language->lang();
271 const int quoteind = quote_index[side_][language_];
274 if (language_ == FrenchQ && times_ == DoubleQ
275 && curr_lang == "frenchb") {
277 qstr = "\\og "; //the spaces are important here
279 qstr = " \\fg{}"; //and here
280 } else if (language_ == FrenchQ && times_ == DoubleQ
281 && curr_lang == "french") {
283 qstr = "<< "; //the spaces are important here
285 qstr = " >>"; //and here
286 } else if (lyxrc.fontenc == "T1") {
287 qstr = latex_quote_t1[times_][quoteind];
288 #ifdef DO_USE_DEFAULT_LANGUAGE
289 } else if (doclang == "default") {
291 } else if (!runparams.use_babel) {
293 qstr = latex_quote_ot1[times_][quoteind];
295 qstr = latex_quote_babel[times_][quoteind];
298 // Always guard against unfortunate ligatures (!` ?`)
299 if (prefixIs(qstr, "`"))
300 qstr.insert(0, "{}");
307 int InsetQuotes::ascii(Buffer const &, ostream & os, int) const
314 int InsetQuotes::linuxdoc(Buffer const &, ostream & os) const
321 int InsetQuotes::docbook(Buffer const &, ostream & os, bool) const
323 if (times_ == DoubleQ) {
338 void InsetQuotes::validate(LaTeXFeatures & features) const
340 bool const use_babel = features.useBabel();
341 char type = quote_char[quote_index[side_][language_]];
343 #ifdef DO_USE_DEFAULT_LANGUAGE
344 if (features.bufferParams().language->lang() == "default"
348 && lyxrc.fontenc != "T1") {
349 if (times_ == SingleQ)
351 case ',': features.require("quotesinglbase"); break;
352 case '<': features.require("guilsinglleft"); break;
353 case '>': features.require("guilsinglright"); break;
358 case ',': features.require("quotedblbase"); break;
359 case '<': features.require("guillemotleft"); break;
360 case '>': features.require("guillemotright"); break;
367 auto_ptr<InsetBase> InsetQuotes::clone() const
369 return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
373 InsetOld::Code InsetQuotes::lyxCode() const
375 return InsetOld::QUOTE_CODE;