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"
19 #include "LaTeXFeatures.h"
22 #include "MetricsInfo.h"
23 #include "OutputParams.h"
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
28 #include "support/lstrings.h"
33 using support::prefixIs;
43 /* codes used to read/write quotes to LyX files
52 char const * const language_char = "esgpfa";
53 char const * const side_char = "lr" ;
54 char const * const times_char = "sd";
56 // List of known quote chars
57 char const * const quote_char = ",'`<>";
59 // Index of chars used for the quote. Index is [side, language]
60 int quote_index[2][6] = {
61 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
62 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
64 // Corresponding LaTeX code, for double and single quotes.
65 char const * const latex_quote_t1[2][5] =
66 { { "\\quotesinglbase ", "'", "`",
67 "\\guilsinglleft{}", "\\guilsinglright{}" },
68 { ",,", "''", "``", "<<", ">>" } };
70 char const * const latex_quote_ot1[2][5] =
71 { { "\\quotesinglbase ", "'", "`",
72 "\\guilsinglleft{}", "\\guilsinglright{}" },
73 { "\\quotedblbase ", "''", "``",
74 "\\guillemotleft{}", "\\guillemotright{}" } };
76 char const * const latex_quote_babel[2][5] =
77 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
78 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
83 InsetQuotes::InsetQuotes(string const & str)
89 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
90 : language_(l), side_(s), times_(t)
95 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
96 : language_(params.quotes_language), times_(params.quotes_times)
102 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
103 : language_(l), times_(t)
109 void InsetQuotes::getPosition(char_type c)
111 // Decide whether left or right
113 case ' ': case '(': case '[':
114 side_ = LeftQ; // left quote
117 side_ = RightQ; // right quote
122 void InsetQuotes::parseString(string const & s)
125 if (str.length() != 3) {
126 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
127 " bad string length." << endl;
133 for (i = 0; i < 6; ++i) {
134 if (str[0] == language_char[i]) {
135 language_ = quote_language(i);
140 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
141 " bad language specification." << endl;
142 language_ = EnglishQ;
145 for (i = 0; i < 2; ++i) {
146 if (str[1] == side_char[i]) {
147 side_ = quote_side(i);
152 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
153 " bad side specification." << endl;
157 for (i = 0; i < 2; ++i) {
158 if (str[2] == times_char[i]) {
159 times_ = quote_times(i);
164 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
165 " bad times specification." << endl;
171 docstring const InsetQuotes::dispString(Language const * loclang) const
174 disp += quote_char[quote_index[side_][language_]];
175 if (times_ == DoubleQ)
181 retdisp = docstring(1, 0x00ab); //'«';
182 else if (disp == ">>")
183 retdisp = docstring(1, 0x00bb); //'»';
185 // The below are supposed to work, but something fails.
186 else if (disp == ",,")
187 retdisp = docstring(1, 0x201e);
188 else if (disp == "''")
189 retdisp == docstring(1, 0x201d);
190 else if (disp == "``")
191 retdisp == docstring(1, 0x201c);
192 else if (disp == "<")
193 retdisp = docstring(1, 0x2039);
194 else if (disp == ">")
195 retdisp = docstring(1, 0x203a);
196 else if (disp == ",")
197 retdisp = docstring(1, 0x201a);
198 else if (disp == "'")
199 retdisp = docstring(1, 0x2019);
200 else if (disp == "`")
201 retdisp = docstring(1, 0x2018);
204 retdisp = lyx::from_ascii(disp);
206 // in french, spaces are added inside double quotes
207 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
211 retdisp.insert(docstring::size_type(0), 1, ' ');
218 bool InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
220 Font & font = mi.base.font;
221 frontend::FontMetrics const & fm =
222 theFontMetrics(font);
223 dim.asc = fm.maxAscent();
224 dim.des = fm.maxDescent();
227 docstring const text = dispString(font.language());
228 for (string::size_type i = 0; i < text.length(); ++i) {
230 dim.wid += fm.width('i');
231 else if (i == 0 || text[i] != text[i - 1])
232 dim.wid += fm.width(text[i]);
234 dim.wid += fm.width(',');
236 bool const changed = dim_ != dim;
243 Font const InsetQuotes::convertFont(Font const & f) const
255 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
257 docstring const text = dispString(pi.base.font.language());
259 if (text.length() == 2 && text[0] == text[1]) {
260 pi.pain.text(x, y, text[0], pi.base.font);
261 int const t = theFontMetrics(pi.base.font)
263 pi.pain.text(x + t, y, text[0], pi.base.font);
265 pi.pain.text(x, y, text, pi.base.font);
267 setPosCache(pi, x, y);
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 auto_ptr<Inset> InsetQuotes::doClone() const
389 return auto_ptr<Inset>(new InsetQuotes(language_, side_, times_));
393 Inset::Code InsetQuotes::lyxCode() const
395 return Inset::QUOTE_CODE;