2 * \file InsetQuotes.cpp
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"
17 #include "BufferView.h"
19 #include "Dimension.h"
21 #include "LaTeXFeatures.h"
24 #include "MetricsInfo.h"
25 #include "OutputParams.h"
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
30 #include "support/docstring.h"
31 #include "support/lstrings.h"
36 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 } // "`'`'><"
67 // Corresponding LaTeX code, for double and single quotes.
68 char const * const latex_quote_t1[2][5] = {
69 { "\\quotesinglbase ", "'", "`",
70 "\\guilsinglleft{}", "\\guilsinglright{}" },
71 { ",,", "''", "``", "<<", ">>" }
74 char const * const latex_quote_ot1[2][5] = {
75 { "\\quotesinglbase ", "'", "`",
76 "\\guilsinglleft{}", "\\guilsinglright{}" },
77 { "\\quotedblbase ", "''", "``",
78 "\\guillemotleft{}", "\\guillemotright{}" }
81 char const * const latex_quote_babel[2][5] = {
82 { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
83 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" }
89 InsetQuotes::InsetQuotes(string const & str)
95 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
96 : language_(l), side_(s), times_(t)
101 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
102 : language_(params.quotes_language), times_(params.quotes_times)
108 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
109 : language_(l), times_(t)
115 docstring InsetQuotes::name() const
117 return from_ascii("Quotes");
121 void InsetQuotes::getPosition(char_type c)
123 // Decide whether left or right
128 side_ = LeftQ; // left quote
131 side_ = RightQ; // right quote
136 void InsetQuotes::parseString(string const & s)
139 if (str.length() != 3) {
140 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
141 " bad string length." << endl;
147 for (i = 0; i < 6; ++i) {
148 if (str[0] == language_char[i]) {
149 language_ = quote_language(i);
154 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
155 " bad language specification." << endl;
156 language_ = EnglishQ;
159 for (i = 0; i < 2; ++i) {
160 if (str[1] == side_char[i]) {
161 side_ = quote_side(i);
166 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
167 " bad side specification." << endl;
171 for (i = 0; i < 2; ++i) {
172 if (str[2] == times_char[i]) {
173 times_ = quote_times(i);
178 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
179 " bad times specification." << endl;
185 docstring const InsetQuotes::dispString(Language const * loclang) const
188 disp += quote_char[quote_index[side_][language_]];
189 if (times_ == DoubleQ)
195 retdisp = docstring(1, 0x00ab); //'«';
196 else if (disp == ">>")
197 retdisp = docstring(1, 0x00bb); //'»';
199 // The below are supposed to work, but something fails.
200 else if (disp == ",,")
201 retdisp = docstring(1, 0x201e);
202 else if (disp == "''")
203 retdisp == docstring(1, 0x201d);
204 else if (disp == "``")
205 retdisp == docstring(1, 0x201c);
206 else if (disp == "<")
207 retdisp = docstring(1, 0x2039);
208 else if (disp == ">")
209 retdisp = docstring(1, 0x203a);
210 else if (disp == ",")
211 retdisp = docstring(1, 0x201a);
212 else if (disp == "'")
213 retdisp = docstring(1, 0x2019);
214 else if (disp == "`")
215 retdisp = docstring(1, 0x2018);
218 retdisp = from_ascii(disp);
220 // in french, spaces are added inside double quotes
221 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
225 retdisp.insert(size_t(0), 1, ' ');
232 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
234 FontInfo & font = mi.base.font;
235 frontend::FontMetrics const & fm =
236 theFontMetrics(font);
237 dim.asc = fm.maxAscent();
238 dim.des = fm.maxDescent();
241 // FIXME: should we add a language or a font parameter member?
242 docstring const text = dispString(
243 mi.base.bv->buffer().params().language);
244 for (string::size_type i = 0; i < text.length(); ++i) {
246 dim.wid += fm.width('i');
247 else if (i == 0 || text[i] != text[i - 1])
248 dim.wid += fm.width(text[i]);
250 dim.wid += fm.width(',');
255 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
257 // FIXME: should we add a language or a font parameter member?
258 docstring const text = dispString(
259 pi.base.bv->buffer().params().language);
261 if (text.length() == 2 && text[0] == text[1]) {
262 pi.pain.text(x, y, text[0], pi.base.font);
263 int const t = theFontMetrics(pi.base.font)
265 pi.pain.text(x + t, y, text[0], pi.base.font);
267 pi.pain.text(x, y, text, pi.base.font);
272 void InsetQuotes::write(Buffer const &, ostream & os) const
275 text += language_char[language_];
276 text += side_char[side_];
277 text += times_char[times_];
278 os << "Quotes " << text;
282 void InsetQuotes::read(Buffer const &, Lexer & lex)
285 parseString(lex.getString());
287 if (lex.getString() != "\\end_inset") {
288 lex.printError("Missing \\end_inset at this point");
293 int InsetQuotes::latex(Buffer const &, odocstream & os,
294 OutputParams const & runparams) const
296 const int quoteind = quote_index[side_][language_];
299 if (language_ == FrenchQ && times_ == DoubleQ
300 && prefixIs(runparams.local_font->language()->code(), "fr")) {
302 qstr = "\\og "; //the spaces are important here
304 qstr = " \\fg{}"; //and here
305 } else if (lyxrc.fontenc == "T1") {
306 qstr = latex_quote_t1[times_][quoteind];
307 #ifdef DO_USE_DEFAULT_LANGUAGE
308 } else if (doclang == "default") {
310 } else if (!runparams.use_babel) {
312 qstr = latex_quote_ot1[times_][quoteind];
314 qstr = latex_quote_babel[times_][quoteind];
317 // Always guard against unfortunate ligatures (!` ?`)
318 if (prefixIs(qstr, "`"))
319 qstr.insert(0, "{}");
321 os << from_ascii(qstr);
326 int InsetQuotes::plaintext(Buffer const & buf, odocstream & os,
327 OutputParams const &) const
329 docstring const str = dispString(buf.params().language);
335 int InsetQuotes::docbook(Buffer const &, odocstream & os,
336 OutputParams const &) const
338 if (times_ == DoubleQ) {
353 void InsetQuotes::textString(Buffer const & buf, odocstream & os) const
355 os << dispString(buf.params().language);
359 void InsetQuotes::validate(LaTeXFeatures & features) const
361 bool const use_babel = features.useBabel();
362 char type = quote_char[quote_index[side_][language_]];
364 #ifdef DO_USE_DEFAULT_LANGUAGE
365 if (features.bufferParams().language->lang() == "default"
369 && lyxrc.fontenc != "T1") {
370 if (times_ == SingleQ)
372 case ',': features.require("quotesinglbase"); break;
373 case '<': features.require("guilsinglleft"); break;
374 case '>': features.require("guilsinglright"); break;
379 case ',': features.require("quotedblbase"); break;
380 case '<': features.require("guillemotleft"); break;
381 case '>': features.require("guillemotright"); break;
388 Inset * InsetQuotes::clone() const
390 return new InsetQuotes(language_, side_, times_);