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/FontMetrics.h"
28 #include "frontends/Painter.h"
30 #include "support/lstrings.h"
35 using support::prefixIs;
45 /* codes used to read/write quotes to LyX files
54 char const * const language_char = "esgpfa";
55 char const * const side_char = "lr" ;
56 char const * const times_char = "sd";
58 // List of known quote chars
59 char const * const quote_char = ",'`<>";
61 // Index of chars used for the quote. Index is [side, language]
62 int quote_index[2][6] = {
63 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
64 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
66 // Corresponding LaTeX code, for double and single quotes.
67 char const * const latex_quote_t1[2][5] =
68 { { "\\quotesinglbase ", "'", "`",
69 "\\guilsinglleft{}", "\\guilsinglright{}" },
70 { ",,", "''", "``", "<<", ">>" } };
72 char const * const latex_quote_ot1[2][5] =
73 { { "\\quotesinglbase ", "'", "`",
74 "\\guilsinglleft{}", "\\guilsinglright{}" },
75 { "\\quotedblbase ", "''", "``",
76 "\\guillemotleft{}", "\\guillemotright{}" } };
78 char const * const latex_quote_babel[2][5] =
79 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
80 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
85 InsetQuotes::InsetQuotes(string const & str)
88 setInsetName("InsetQuotes");
92 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
93 : language_(l), side_(s), times_(t)
95 setInsetName("InsetQuotes");
99 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
100 : language_(params.quotes_language), times_(params.quotes_times)
103 setInsetName("InsetQuotes");
107 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
108 : language_(l), times_(t)
111 setInsetName("InsetQuotes");
115 void InsetQuotes::getPosition(char_type c)
117 // Decide whether left or right
119 case ' ': case '(': case '[':
120 side_ = LeftQ; // left quote
123 side_ = RightQ; // right quote
128 void InsetQuotes::parseString(string const & s)
131 if (str.length() != 3) {
132 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
133 " bad string length." << endl;
139 for (i = 0; i < 6; ++i) {
140 if (str[0] == language_char[i]) {
141 language_ = quote_language(i);
146 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
147 " bad language specification." << endl;
148 language_ = EnglishQ;
151 for (i = 0; i < 2; ++i) {
152 if (str[1] == side_char[i]) {
153 side_ = quote_side(i);
158 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
159 " bad side specification." << endl;
163 for (i = 0; i < 2; ++i) {
164 if (str[2] == times_char[i]) {
165 times_ = quote_times(i);
170 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
171 " bad times specification." << endl;
177 string const InsetQuotes::dispString(Language const * loclang) const
180 disp += quote_char[quote_index[side_][language_]];
181 if (times_ == DoubleQ)
184 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
185 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
186 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
189 else if (disp == ">>")
193 // in french, spaces are added inside double quotes
194 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
198 disp.insert(string::size_type(0), 1, ' ');
205 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
207 LyXFont & font = mi.base.font;
208 frontend::FontMetrics const & fm =
209 theFontMetrics(font);
210 dim.asc = fm.maxAscent();
211 dim.des = fm.maxDescent();
214 string const text = dispString(font.language());
215 for (string::size_type i = 0; i < text.length(); ++i) {
217 dim.wid += fm.width('i');
218 else if (i == 0 || text[i] != text[i - 1])
219 dim.wid += fm.width(text[i]);
221 dim.wid += fm.width(',');
228 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
240 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
242 string const text = dispString(pi.base.font.language());
244 if (text.length() == 2 && text[0] == text[1]) {
245 pi.pain.text(x, y, text[0], pi.base.font);
246 int const t = theFontMetrics(pi.base.font)
248 pi.pain.text(x + t, y, text[0], pi.base.font);
250 docstring dtext(text.begin(), text.end());
251 pi.pain.text(x, y, dtext, pi.base.font);
253 setPosCache(pi, x, y);
257 void InsetQuotes::write(Buffer const &, ostream & os) const
260 text += language_char[language_];
261 text += side_char[side_];
262 text += times_char[times_];
263 os << "Quotes " << text;
267 void InsetQuotes::read(Buffer const &, LyXLex & lex)
270 parseString(lex.getString());
272 if (lex.getString() != "\\end_inset") {
273 lex.printError("Missing \\end_inset at this point");
278 int InsetQuotes::latex(Buffer const &, odocstream & os,
279 OutputParams const & runparams) const
281 const int quoteind = quote_index[side_][language_];
284 if (language_ == FrenchQ && times_ == DoubleQ
285 && prefixIs(runparams.local_font->language()->code(), "fr")) {
287 qstr = "\\og "; //the spaces are important here
289 qstr = " \\fg{}"; //and here
290 } else if (lyxrc.fontenc == "T1") {
291 qstr = latex_quote_t1[times_][quoteind];
292 #ifdef DO_USE_DEFAULT_LANGUAGE
293 } else if (doclang == "default") {
295 } else if (!runparams.use_babel) {
297 qstr = latex_quote_ot1[times_][quoteind];
299 qstr = latex_quote_babel[times_][quoteind];
302 // Always guard against unfortunate ligatures (!` ?`)
303 if (prefixIs(qstr, "`"))
304 qstr.insert(0, "{}");
306 os << from_ascii(qstr);
311 int InsetQuotes::plaintext(Buffer const &, odocstream & os,
312 OutputParams const &) const
319 int InsetQuotes::docbook(Buffer const &, odocstream & os,
320 OutputParams const &) const
322 if (times_ == DoubleQ) {
337 int InsetQuotes::textString(Buffer const & buf, odocstream & os,
338 OutputParams const & op) const
340 return plaintext(buf, os, op);
344 void InsetQuotes::validate(LaTeXFeatures & features) const
346 bool const use_babel = features.useBabel();
347 char type = quote_char[quote_index[side_][language_]];
349 #ifdef DO_USE_DEFAULT_LANGUAGE
350 if (features.bufferParams().language->lang() == "default"
354 && lyxrc.fontenc != "T1") {
355 if (times_ == SingleQ)
357 case ',': features.require("quotesinglbase"); break;
358 case '<': features.require("guilsinglleft"); break;
359 case '>': features.require("guilsinglright"); break;
364 case ',': features.require("quotedblbase"); break;
365 case '<': features.require("guillemotleft"); break;
366 case '>': features.require("guillemotright"); break;
373 auto_ptr<InsetBase> InsetQuotes::doClone() const
375 return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
379 InsetBase::Code InsetQuotes::lyxCode() const
381 return InsetBase::QUOTE_CODE;