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(QuoteLanguage l, QuoteSide s, QuoteTimes t)
92 : language_(l), side_(s), times_(t)
97 InsetQuotes::InsetQuotes(Buffer const & buf, char_type c)
98 : language_(buf.params().quotes_language), times_(buf.params().quotes_times)
104 InsetQuotes::InsetQuotes(char_type c, QuoteLanguage l, QuoteTimes t)
105 : language_(l), times_(t)
111 docstring InsetQuotes::name() const
113 return from_ascii("Quotes");
117 void InsetQuotes::setSide(char_type c)
119 // Decide whether left or right
124 side_ = LeftQuote; // left quote
127 side_ = RightQuote; // 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_ = QuoteLanguage(i);
150 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
151 " bad language specification." << endl;
152 language_ = EnglishQuotes;
155 for (i = 0; i < 2; ++i) {
156 if (str[1] == side_char[i]) {
157 side_ = QuoteSide(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_ = QuoteTimes(i);
174 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
175 " bad times specification." << endl;
176 times_ = DoubleQuotes;
181 docstring InsetQuotes::displayString() const
183 Language const * loclang = buffer().params().language;
185 disp += quote_char[quote_index[side_][language_]];
186 if (times_ == DoubleQuotes)
192 retdisp = docstring(1, 0x00ab); //'«';
193 else if (disp == ">>")
194 retdisp = docstring(1, 0x00bb); //'»';
196 // The below are supposed to work, but something fails.
197 else if (disp == ",,")
198 retdisp = docstring(1, 0x201e);
199 else if (disp == "''")
200 retdisp == docstring(1, 0x201d);
201 else if (disp == "``")
202 retdisp == docstring(1, 0x201c);
203 else if (disp == "<")
204 retdisp = docstring(1, 0x2039);
205 else if (disp == ">")
206 retdisp = docstring(1, 0x203a);
207 else if (disp == ",")
208 retdisp = docstring(1, 0x201a);
209 else if (disp == "'")
210 retdisp = docstring(1, 0x2019);
211 else if (disp == "`")
212 retdisp = docstring(1, 0x2018);
215 retdisp = from_ascii(disp);
217 // in french, spaces are added inside double quotes
218 if (times_ == DoubleQuotes && prefixIs(loclang->code(), "fr")) {
219 if (side_ == LeftQuote)
222 retdisp.insert(size_t(0), 1, ' ');
229 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
231 FontInfo & font = mi.base.font;
232 frontend::FontMetrics const & fm =
233 theFontMetrics(font);
234 dim.asc = fm.maxAscent();
235 dim.des = fm.maxDescent();
238 // FIXME: should we add a language or a font parameter member?
239 docstring const text = displayString();
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 = displayString();
256 if (text.length() == 2 && text[0] == text[1]) {
257 pi.pain.text(x, y, text[0], pi.base.font);
258 int const t = theFontMetrics(pi.base.font)
260 pi.pain.text(x + t, y, text[0], pi.base.font);
262 pi.pain.text(x, y, text, pi.base.font);
267 void InsetQuotes::write(ostream & os) const
270 text += language_char[language_];
271 text += side_char[side_];
272 text += times_char[times_];
273 os << "Quotes " << text;
277 void InsetQuotes::read(Lexer & lex)
280 parseString(lex.getString());
282 if (lex.getString() != "\\end_inset") {
283 lex.printError("Missing \\end_inset at this point");
288 int InsetQuotes::latex(odocstream & os, OutputParams const & runparams) const
290 const int quoteind = quote_index[side_][language_];
293 if (language_ == FrenchQuotes && times_ == DoubleQuotes
294 && prefixIs(runparams.local_font->language()->code(), "fr")) {
295 if (side_ == LeftQuote)
296 qstr = "\\og "; //the spaces are important here
298 qstr = " \\fg{}"; //and here
299 } else if (lyxrc.fontenc == "T1") {
300 qstr = latex_quote_t1[times_][quoteind];
301 #ifdef DO_USE_DEFAULT_LANGUAGE
302 } else if (doclang == "default") {
304 } else if (!runparams.use_babel) {
306 qstr = latex_quote_ot1[times_][quoteind];
308 qstr = latex_quote_babel[times_][quoteind];
311 // Always guard against unfortunate ligatures (!` ?`)
312 if (prefixIs(qstr, "`"))
313 qstr.insert(0, "{}");
315 os << from_ascii(qstr);
320 int InsetQuotes::plaintext(odocstream & os, OutputParams const &) const
322 docstring const str = displayString();
328 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
330 if (times_ == DoubleQuotes) {
331 if (side_ == LeftQuote)
336 if (side_ == LeftQuote)
345 void InsetQuotes::textString(odocstream & os) const
347 os << displayString();
351 void InsetQuotes::validate(LaTeXFeatures & features) const
353 bool const use_babel = features.useBabel();
354 char type = quote_char[quote_index[side_][language_]];
356 #ifdef DO_USE_DEFAULT_LANGUAGE
357 if (features.bufferParams().language->lang() == "default"
361 && lyxrc.fontenc != "T1") {
362 if (times_ == SingleQuotes)
364 case ',': features.require("quotesinglbase"); break;
365 case '<': features.require("guilsinglleft"); break;
366 case '>': features.require("guilsinglright"); break;
371 case ',': features.require("quotedblbase"); break;
372 case '<': features.require("guillemotleft"); break;
373 case '>': features.require("guillemotright"); break;