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 // Unicode characters needed by each quote type
58 char_type const display_quote_char[2][5] = {
59 { 0x201a, 0x2019, 0x2018, 0x2039, 0x203a},
60 { 0x201e, 0x201d, 0x201c, 0x00ab, 0x00bb}
63 // Index of chars used for the quote. Index is [side, language]
64 int quote_index[2][6] = {
65 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
66 { 1, 1, 2, 1, 4, 3 } // "`'`'><"
69 // Corresponding LaTeX code, for double and single quotes.
70 char const * const latex_quote_t1[2][5] = {
71 { "\\quotesinglbase ", "'", "`",
72 "\\guilsinglleft{}", "\\guilsinglright{}" },
73 { ",,", "''", "``", "<<", ">>" }
76 char const * const latex_quote_ot1[2][5] = {
77 { "\\quotesinglbase ", "'", "`",
78 "\\guilsinglleft{}", "\\guilsinglright{}" },
79 { "\\quotedblbase ", "''", "``",
80 "\\guillemotleft{}", "\\guillemotright{}" }
83 char const * const latex_quote_babel[2][5] = {
84 { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
85 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" }
91 InsetQuotes::InsetQuotes(Buffer const & buf, string const & str)
94 setBuffer(const_cast<Buffer &>(buf));
97 InsetQuotes::InsetQuotes(Buffer const & buf, char_type c)
98 : language_(buf.params().quotes_language), times_(buf.params().quotes_times)
101 setBuffer(const_cast<Buffer &>(buf));
105 InsetQuotes::InsetQuotes(Buffer const & buf, char_type c, QuoteTimes t)
106 : language_(buf.params().quotes_language), times_(t)
109 setBuffer(const_cast<Buffer &>(buf));
113 docstring InsetQuotes::name() const
115 return from_ascii("Quotes");
119 void InsetQuotes::setSide(char_type c)
121 // Decide whether left or right
126 side_ = LeftQuote; // left quote
129 side_ = RightQuote; // right quote
134 void InsetQuotes::parseString(string const & s)
137 if (str.length() != 3) {
138 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
139 " bad string length." << endl;
145 for (i = 0; i < 6; ++i) {
146 if (str[0] == language_char[i]) {
147 language_ = QuoteLanguage(i);
152 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
153 " bad language specification." << endl;
154 language_ = EnglishQuotes;
157 for (i = 0; i < 2; ++i) {
158 if (str[1] == side_char[i]) {
159 side_ = QuoteSide(i);
164 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
165 " bad side specification." << endl;
169 for (i = 0; i < 2; ++i) {
170 if (str[2] == times_char[i]) {
171 times_ = QuoteTimes(i);
176 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
177 " bad times specification." << endl;
178 times_ = DoubleQuotes;
183 docstring InsetQuotes::displayString() const
185 Language const * loclang = buffer().params().language;
186 int const index = quote_index[side_][language_];
187 docstring retdisp = docstring(1, display_quote_char[times_][index]);
189 // in french, spaces are added inside double quotes
190 if (times_ == DoubleQuotes && prefixIs(loclang->code(), "fr")) {
191 if (side_ == LeftQuote)
194 retdisp.insert(size_t(0), 1, ' ');
201 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
203 FontInfo & font = mi.base.font;
204 frontend::FontMetrics const & fm =
205 theFontMetrics(font);
206 dim.asc = fm.maxAscent();
207 dim.des = fm.maxDescent();
210 // FIXME: should we add a language or a font parameter member?
211 docstring const text = displayString();
212 for (string::size_type i = 0; i < text.length(); ++i) {
214 dim.wid += fm.width('i');
215 else if (i == 0 || text[i] != text[i - 1])
216 dim.wid += fm.width(text[i]);
218 dim.wid += fm.width(',');
223 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
225 // FIXME: should we add a language or a font parameter member?
226 docstring const text = displayString();
228 if (text.length() == 2 && text[0] == text[1]) {
229 pi.pain.text(x, y, text[0], pi.base.font);
230 int const t = theFontMetrics(pi.base.font)
232 pi.pain.text(x + t, y, text[0], pi.base.font);
234 pi.pain.text(x, y, text, pi.base.font);
239 void InsetQuotes::write(ostream & os) const
242 text += language_char[language_];
243 text += side_char[side_];
244 text += times_char[times_];
245 os << "Quotes " << text;
249 void InsetQuotes::read(Lexer & lex)
251 lex.setContext("InsetQuotes::read");
253 parseString(lex.getString());
254 lex >> "\\end_inset";
258 int InsetQuotes::latex(odocstream & os, OutputParams const & runparams) const
260 const int quoteind = quote_index[side_][language_];
263 if (language_ == FrenchQuotes && times_ == DoubleQuotes
264 && prefixIs(runparams.local_font->language()->code(), "fr")) {
265 if (side_ == LeftQuote)
266 qstr = "\\og "; //the spaces are important here
268 qstr = " \\fg{}"; //and here
269 } else if (lyxrc.fontenc == "T1") {
270 qstr = latex_quote_t1[times_][quoteind];
271 #ifdef DO_USE_DEFAULT_LANGUAGE
272 } else if (doclang == "default") {
274 } else if (!runparams.use_babel) {
276 qstr = latex_quote_ot1[times_][quoteind];
278 qstr = latex_quote_babel[times_][quoteind];
281 // Always guard against unfortunate ligatures (!` ?`)
282 if (prefixIs(qstr, "`"))
283 qstr.insert(0, "{}");
285 os << from_ascii(qstr);
290 int InsetQuotes::plaintext(odocstream & os, OutputParams const &) const
292 docstring const str = displayString();
298 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
300 if (times_ == DoubleQuotes) {
301 if (side_ == LeftQuote)
306 if (side_ == LeftQuote)
315 docstring InsetQuotes::xhtml(odocstream & os, OutputParams const & op) const
322 void InsetQuotes::tocString(odocstream & os) const
324 os << displayString();
328 void InsetQuotes::validate(LaTeXFeatures & features) const
330 bool const use_babel = features.useBabel();
331 char type = quote_char[quote_index[side_][language_]];
333 #ifdef DO_USE_DEFAULT_LANGUAGE
334 if (features.bufferParams().language->lang() == "default"
338 && lyxrc.fontenc != "T1") {
339 if (times_ == SingleQuotes)
341 case ',': features.require("quotesinglbase"); break;
342 case '<': features.require("guilsinglleft"); break;
343 case '>': features.require("guilsinglright"); break;
348 case ',': features.require("quotedblbase"); break;
349 case '<': features.require("guillemotleft"); break;
350 case '>': features.require("guillemotright"); break;