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"
20 #include "LaTeXFeatures.h"
23 #include "MetricsInfo.h"
24 #include "OutputParams.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
29 #include "support/docstring.h"
30 #include "support/lstrings.h"
35 using support::prefixIs;
44 /* codes used to read/write quotes to LyX files
53 char const * const language_char = "esgpfa";
54 char const * const side_char = "lr" ;
55 char const * const times_char = "sd";
57 // List of known quote chars
58 char const * const quote_char = ",'`<>";
60 // Index of chars used for the quote. Index is [side, language]
61 int quote_index[2][6] = {
62 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
63 { 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 { ",,", "''", "``", "<<", ">>" }
73 char const * const latex_quote_ot1[2][5] = {
74 { "\\quotesinglbase ", "'", "`",
75 "\\guilsinglleft{}", "\\guilsinglright{}" },
76 { "\\quotedblbase ", "''", "``",
77 "\\guillemotleft{}", "\\guillemotright{}" }
80 char const * const latex_quote_babel[2][5] = {
81 { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
82 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" }
88 InsetQuotes::InsetQuotes(string const & str)
94 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
95 : language_(l), side_(s), times_(t)
100 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
101 : language_(params.quotes_language), times_(params.quotes_times)
107 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
108 : language_(l), times_(t)
114 docstring InsetQuotes::name() const
116 return from_ascii("Quotes");
120 void InsetQuotes::getPosition(char_type c)
122 // Decide whether left or right
127 side_ = LeftQ; // left quote
130 side_ = RightQ; // right quote
135 void InsetQuotes::parseString(string const & s)
138 if (str.length() != 3) {
139 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
140 " bad string length." << endl;
146 for (i = 0; i < 6; ++i) {
147 if (str[0] == language_char[i]) {
148 language_ = quote_language(i);
153 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
154 " bad language specification." << endl;
155 language_ = EnglishQ;
158 for (i = 0; i < 2; ++i) {
159 if (str[1] == side_char[i]) {
160 side_ = quote_side(i);
165 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
166 " bad side specification." << endl;
170 for (i = 0; i < 2; ++i) {
171 if (str[2] == times_char[i]) {
172 times_ = quote_times(i);
177 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
178 " bad times specification." << endl;
184 docstring const InsetQuotes::dispString(Language const * loclang) const
187 disp += quote_char[quote_index[side_][language_]];
188 if (times_ == DoubleQ)
194 retdisp = docstring(1, 0x00ab); //'«';
195 else if (disp == ">>")
196 retdisp = docstring(1, 0x00bb); //'»';
198 // The below are supposed to work, but something fails.
199 else if (disp == ",,")
200 retdisp = docstring(1, 0x201e);
201 else if (disp == "''")
202 retdisp == docstring(1, 0x201d);
203 else if (disp == "``")
204 retdisp == docstring(1, 0x201c);
205 else if (disp == "<")
206 retdisp = docstring(1, 0x2039);
207 else if (disp == ">")
208 retdisp = docstring(1, 0x203a);
209 else if (disp == ",")
210 retdisp = docstring(1, 0x201a);
211 else if (disp == "'")
212 retdisp = docstring(1, 0x2019);
213 else if (disp == "`")
214 retdisp = docstring(1, 0x2018);
217 retdisp = from_ascii(disp);
219 // in french, spaces are added inside double quotes
220 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
224 retdisp.insert(size_t(0), 1, ' ');
231 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
233 FontInfo & font = mi.base.font;
234 frontend::FontMetrics const & fm =
235 theFontMetrics(font);
236 dim.asc = fm.maxAscent();
237 dim.des = fm.maxDescent();
240 // FIXME: should we add a language or a font parameter member?
241 docstring const text = dispString(
242 mi.base.bv->buffer().params().language);
243 for (string::size_type i = 0; i < text.length(); ++i) {
245 dim.wid += fm.width('i');
246 else if (i == 0 || text[i] != text[i - 1])
247 dim.wid += fm.width(text[i]);
249 dim.wid += fm.width(',');
254 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
256 // FIXME: should we add a language or a font parameter member?
257 docstring const text = dispString(
258 pi.base.bv->buffer().params().language);
260 if (text.length() == 2 && text[0] == text[1]) {
261 pi.pain.text(x, y, text[0], pi.base.font);
262 int const t = theFontMetrics(pi.base.font)
264 pi.pain.text(x + t, y, text[0], pi.base.font);
266 pi.pain.text(x, y, text, pi.base.font);
271 void InsetQuotes::write(Buffer const &, ostream & os) const
274 text += language_char[language_];
275 text += side_char[side_];
276 text += times_char[times_];
277 os << "Quotes " << text;
281 void InsetQuotes::read(Buffer const &, Lexer & lex)
284 parseString(lex.getString());
286 if (lex.getString() != "\\end_inset") {
287 lex.printError("Missing \\end_inset at this point");
292 int InsetQuotes::latex(Buffer const &, odocstream & os,
293 OutputParams const & runparams) const
295 const int quoteind = quote_index[side_][language_];
298 if (language_ == FrenchQ && times_ == DoubleQ
299 && prefixIs(runparams.local_font->language()->code(), "fr")) {
301 qstr = "\\og "; //the spaces are important here
303 qstr = " \\fg{}"; //and here
304 } else if (lyxrc.fontenc == "T1") {
305 qstr = latex_quote_t1[times_][quoteind];
306 #ifdef DO_USE_DEFAULT_LANGUAGE
307 } else if (doclang == "default") {
309 } else if (!runparams.use_babel) {
311 qstr = latex_quote_ot1[times_][quoteind];
313 qstr = latex_quote_babel[times_][quoteind];
316 // Always guard against unfortunate ligatures (!` ?`)
317 if (prefixIs(qstr, "`"))
318 qstr.insert(0, "{}");
320 os << from_ascii(qstr);
325 int InsetQuotes::plaintext(Buffer const & buf, odocstream & os,
326 OutputParams const &) const
328 docstring const str = dispString(buf.params().language);
334 int InsetQuotes::docbook(Buffer const &, odocstream & os,
335 OutputParams const &) const
337 if (times_ == DoubleQ) {
352 void InsetQuotes::textString(Buffer const & buf, odocstream & os) const
354 os << dispString(buf.params().language);
358 void InsetQuotes::validate(LaTeXFeatures & features) const
360 bool const use_babel = features.useBabel();
361 char type = quote_char[quote_index[side_][language_]];
363 #ifdef DO_USE_DEFAULT_LANGUAGE
364 if (features.bufferParams().language->lang() == "default"
368 && lyxrc.fontenc != "T1") {
369 if (times_ == SingleQ)
371 case ',': features.require("quotesinglbase"); break;
372 case '<': features.require("guilsinglleft"); break;
373 case '>': features.require("guilsinglright"); break;
378 case ',': features.require("quotedblbase"); break;
379 case '<': features.require("guillemotleft"); break;
380 case '>': features.require("guillemotright"); break;
387 Inset * InsetQuotes::clone() const
389 return new InsetQuotes(language_, side_, times_);