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"
18 #include "Dimension.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/debug.h"
30 #include "support/docstring.h"
31 #include "support/docstream.h"
32 #include "support/lstrings.h"
35 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 } // "`'`'><"
63 // Corresponding LaTeX code, for double and single quotes.
64 char const * const latex_quote_t1[2][5] = {
65 { "\\quotesinglbase ", "'", "`",
66 "\\guilsinglleft{}", "\\guilsinglright{}" },
67 { ",,", "''", "``", "<<", ">>" }
70 char const * const latex_quote_ot1[2][5] = {
71 { "\\quotesinglbase ", "'", "`",
72 "\\guilsinglleft{}", "\\guilsinglright{}" },
73 { "\\quotedblbase ", "''", "``",
74 "\\guillemotleft{}", "\\guillemotright{}" }
77 char const * const latex_quote_babel[2][5] = {
78 { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
79 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" }
85 InsetQuotes::InsetQuotes(string const & str)
91 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
92 : language_(l), side_(s), times_(t)
97 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
98 : language_(params.quotes_language), times_(params.quotes_times)
104 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
105 : language_(l), times_(t)
111 docstring InsetQuotes::name() const
113 return from_ascii("Quotes");
117 void InsetQuotes::getPosition(char_type c)
119 // Decide whether left or right
124 side_ = LeftQ; // left quote
127 side_ = RightQ; // right quote
132 void InsetQuotes::parseString(string const & s)
135 if (str.length() != 3) {
136 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
137 " bad string length." << endl;
143 for (i = 0; i < 6; ++i) {
144 if (str[0] == language_char[i]) {
145 language_ = quote_language(i);
150 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
151 " bad language specification." << endl;
152 language_ = EnglishQ;
155 for (i = 0; i < 2; ++i) {
156 if (str[1] == side_char[i]) {
157 side_ = quote_side(i);
162 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
163 " bad side specification." << endl;
167 for (i = 0; i < 2; ++i) {
168 if (str[2] == times_char[i]) {
169 times_ = quote_times(i);
174 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
175 " bad times specification." << endl;
181 docstring const InsetQuotes::dispString(Language const * loclang) const
184 disp += quote_char[quote_index[side_][language_]];
185 if (times_ == DoubleQ)
191 retdisp = docstring(1, 0x00ab); //'«';
192 else if (disp == ">>")
193 retdisp = docstring(1, 0x00bb); //'»';
195 // The below are supposed to work, but something fails.
196 else if (disp == ",,")
197 retdisp = docstring(1, 0x201e);
198 else if (disp == "''")
199 retdisp == docstring(1, 0x201d);
200 else if (disp == "``")
201 retdisp == docstring(1, 0x201c);
202 else if (disp == "<")
203 retdisp = docstring(1, 0x2039);
204 else if (disp == ">")
205 retdisp = docstring(1, 0x203a);
206 else if (disp == ",")
207 retdisp = docstring(1, 0x201a);
208 else if (disp == "'")
209 retdisp = docstring(1, 0x2019);
210 else if (disp == "`")
211 retdisp = docstring(1, 0x2018);
214 retdisp = from_ascii(disp);
216 // in french, spaces are added inside double quotes
217 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
221 retdisp.insert(size_t(0), 1, ' ');
228 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
230 FontInfo & font = mi.base.font;
231 frontend::FontMetrics const & fm =
232 theFontMetrics(font);
233 dim.asc = fm.maxAscent();
234 dim.des = fm.maxDescent();
237 // FIXME: should we add a language or a font parameter member?
238 docstring const text = dispString(
239 mi.base.bv->buffer().params().language);
240 for (string::size_type i = 0; i < text.length(); ++i) {
242 dim.wid += fm.width('i');
243 else if (i == 0 || text[i] != text[i - 1])
244 dim.wid += fm.width(text[i]);
246 dim.wid += fm.width(',');
251 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
253 // FIXME: should we add a language or a font parameter member?
254 docstring const text = dispString(
255 pi.base.bv->buffer().params().language);
257 if (text.length() == 2 && text[0] == text[1]) {
258 pi.pain.text(x, y, text[0], pi.base.font);
259 int const t = theFontMetrics(pi.base.font)
261 pi.pain.text(x + t, y, text[0], pi.base.font);
263 pi.pain.text(x, y, text, pi.base.font);
268 void InsetQuotes::write(Buffer const &, ostream & os) const
271 text += language_char[language_];
272 text += side_char[side_];
273 text += times_char[times_];
274 os << "Quotes " << text;
278 void InsetQuotes::read(Buffer const &, Lexer & lex)
281 parseString(lex.getString());
283 if (lex.getString() != "\\end_inset") {
284 lex.printError("Missing \\end_inset at this point");
289 int InsetQuotes::latex(Buffer const &, odocstream & os,
290 OutputParams const & runparams) const
292 const int quoteind = quote_index[side_][language_];
295 if (language_ == FrenchQ && times_ == DoubleQ
296 && prefixIs(runparams.local_font->language()->code(), "fr")) {
298 qstr = "\\og "; //the spaces are important here
300 qstr = " \\fg{}"; //and here
301 } else if (lyxrc.fontenc == "T1") {
302 qstr = latex_quote_t1[times_][quoteind];
303 #ifdef DO_USE_DEFAULT_LANGUAGE
304 } else if (doclang == "default") {
306 } else if (!runparams.use_babel) {
308 qstr = latex_quote_ot1[times_][quoteind];
310 qstr = latex_quote_babel[times_][quoteind];
313 // Always guard against unfortunate ligatures (!` ?`)
314 if (prefixIs(qstr, "`"))
315 qstr.insert(0, "{}");
317 os << from_ascii(qstr);
322 int InsetQuotes::plaintext(Buffer const & buf, odocstream & os,
323 OutputParams const &) const
325 docstring const str = dispString(buf.params().language);
331 int InsetQuotes::docbook(Buffer const &, odocstream & os,
332 OutputParams const &) const
334 if (times_ == DoubleQ) {
349 void InsetQuotes::textString(Buffer const & buf, odocstream & os) const
351 os << dispString(buf.params().language);
355 void InsetQuotes::validate(LaTeXFeatures & features) const
357 bool const use_babel = features.useBabel();
358 char type = quote_char[quote_index[side_][language_]];
360 #ifdef DO_USE_DEFAULT_LANGUAGE
361 if (features.bufferParams().language->lang() == "default"
365 && lyxrc.fontenc != "T1") {
366 if (times_ == SingleQ)
368 case ',': features.require("quotesinglbase"); break;
369 case '<': features.require("guilsinglleft"); break;
370 case '>': features.require("guilsinglright"); break;
375 case ',': features.require("quotedblbase"); break;
376 case '<': features.require("guillemotleft"); break;
377 case '>': features.require("guillemotright"); break;
384 Inset * InsetQuotes::clone() const
386 return new InsetQuotes(language_, side_, times_);