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"
24 #include "paragraph.h"
25 #include "paragraph_funcs.h"
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
30 #include "support/lstrings.h"
35 using support::prefixIs;
45 /* codes used to read/write quotes to LyX files
54 char const * const language_char = "esgpfa";
55 char const * const side_char = "lr" ;
56 char const * const times_char = "sd";
58 // List of known quote chars
59 char const * const quote_char = ",'`<>";
61 // Index of chars used for the quote. Index is [side, language]
62 int quote_index[2][6] = {
63 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
64 { 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 { ",,", "''", "``", "<<", ">>" } };
72 char const * const latex_quote_ot1[2][5] =
73 { { "\\quotesinglbase ", "'", "`",
74 "\\guilsinglleft{}", "\\guilsinglright{}" },
75 { "\\quotedblbase ", "''", "``",
76 "\\guillemotleft{}", "\\guillemotright{}" } };
78 char const * const latex_quote_babel[2][5] =
79 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
80 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
85 InsetQuotes::InsetQuotes(string const & str)
88 setInsetName(from_utf8("InsetQuotes"));
92 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
93 : language_(l), side_(s), times_(t)
95 setInsetName(from_utf8("InsetQuotes"));
99 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
100 : language_(params.quotes_language), times_(params.quotes_times)
103 setInsetName(from_utf8("InsetQuotes"));
107 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
108 : language_(l), times_(t)
111 setInsetName(from_utf8("InsetQuotes"));
115 void InsetQuotes::getPosition(char_type c)
117 // Decide whether left or right
119 case ' ': case '(': case '[':
120 side_ = LeftQ; // left quote
123 side_ = RightQ; // right quote
128 void InsetQuotes::parseString(string const & s)
131 if (str.length() != 3) {
132 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
133 " bad string length." << endl;
139 for (i = 0; i < 6; ++i) {
140 if (str[0] == language_char[i]) {
141 language_ = quote_language(i);
146 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
147 " bad language specification." << endl;
148 language_ = EnglishQ;
151 for (i = 0; i < 2; ++i) {
152 if (str[1] == side_char[i]) {
153 side_ = quote_side(i);
158 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
159 " bad side specification." << endl;
163 for (i = 0; i < 2; ++i) {
164 if (str[2] == times_char[i]) {
165 times_ = quote_times(i);
170 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
171 " bad times specification." << endl;
177 docstring const InsetQuotes::dispString(Language const * loclang) const
180 disp += quote_char[quote_index[side_][language_]];
181 if (times_ == DoubleQ)
187 retdisp = docstring(1, 0x00ab); //'«';
188 else if (disp == ">>")
189 retdisp = docstring(1, 0x00bb); //'»';
191 // The below are supposed to work, but something fails.
192 else if (disp == ",,")
193 retdisp = docstring(1, 0x201e);
194 else if (disp == "''")
195 retdisp == docstring(1, 0x201d);
196 else if (disp == "``")
197 retdisp == docstring(1, 0x201c);
198 else if (disp == "<")
199 retdisp = docstring(1, 0x2039);
200 else if (disp == ">")
201 retdisp = docstring(1, 0x203a);
202 else if (disp == ",")
203 retdisp = docstring(1, 0x201a);
204 else if (disp == "'")
205 retdisp = docstring(1, 0x2019);
206 else if (disp == "`")
207 retdisp = docstring(1, 0x2018);
210 retdisp = lyx::from_ascii(disp);
212 // in french, spaces are added inside double quotes
213 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
217 retdisp.insert(docstring::size_type(0), 1, ' ');
224 bool InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
226 LyXFont & font = mi.base.font;
227 frontend::FontMetrics const & fm =
228 theFontMetrics(font);
229 dim.asc = fm.maxAscent();
230 dim.des = fm.maxDescent();
233 docstring const text = dispString(font.language());
234 for (string::size_type i = 0; i < text.length(); ++i) {
236 dim.wid += fm.width('i');
237 else if (i == 0 || text[i] != text[i - 1])
238 dim.wid += fm.width(text[i]);
240 dim.wid += fm.width(',');
242 bool const changed = dim_ != dim;
249 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
261 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
263 docstring const text = dispString(pi.base.font.language());
265 if (text.length() == 2 && text[0] == text[1]) {
266 pi.pain.text(x, y, text[0], pi.base.font);
267 int const t = theFontMetrics(pi.base.font)
269 pi.pain.text(x + t, y, text[0], pi.base.font);
271 pi.pain.text(x, y, text, pi.base.font);
273 setPosCache(pi, x, y);
277 void InsetQuotes::write(Buffer const &, ostream & os) const
280 text += language_char[language_];
281 text += side_char[side_];
282 text += times_char[times_];
283 os << "Quotes " << text;
287 void InsetQuotes::read(Buffer const &, LyXLex & lex)
290 parseString(lex.getString());
292 if (lex.getString() != "\\end_inset") {
293 lex.printError("Missing \\end_inset at this point");
298 int InsetQuotes::latex(Buffer const &, odocstream & os,
299 OutputParams const & runparams) const
301 const int quoteind = quote_index[side_][language_];
304 if (language_ == FrenchQ && times_ == DoubleQ
305 && prefixIs(runparams.local_font->language()->code(), "fr")) {
307 qstr = "\\og "; //the spaces are important here
309 qstr = " \\fg{}"; //and here
310 } else if (lyxrc.fontenc == "T1") {
311 qstr = latex_quote_t1[times_][quoteind];
312 #ifdef DO_USE_DEFAULT_LANGUAGE
313 } else if (doclang == "default") {
315 } else if (!runparams.use_babel) {
317 qstr = latex_quote_ot1[times_][quoteind];
319 qstr = latex_quote_babel[times_][quoteind];
322 // Always guard against unfortunate ligatures (!` ?`)
323 if (prefixIs(qstr, "`"))
324 qstr.insert(0, "{}");
326 os << from_ascii(qstr);
331 int InsetQuotes::plaintext(Buffer const &, odocstream & os,
332 OutputParams const &) const
339 int InsetQuotes::docbook(Buffer const &, odocstream & os,
340 OutputParams const &) const
342 if (times_ == DoubleQ) {
357 int InsetQuotes::textString(Buffer const & buf, odocstream & os,
358 OutputParams const & op) const
360 return plaintext(buf, os, op);
364 void InsetQuotes::validate(LaTeXFeatures & features) const
366 bool const use_babel = features.useBabel();
367 char type = quote_char[quote_index[side_][language_]];
369 #ifdef DO_USE_DEFAULT_LANGUAGE
370 if (features.bufferParams().language->lang() == "default"
374 && lyxrc.fontenc != "T1") {
375 if (times_ == SingleQ)
377 case ',': features.require("quotesinglbase"); break;
378 case '<': features.require("guilsinglleft"); break;
379 case '>': features.require("guilsinglright"); break;
384 case ',': features.require("quotedblbase"); break;
385 case '<': features.require("guillemotleft"); break;
386 case '>': features.require("guillemotright"); break;
393 auto_ptr<InsetBase> InsetQuotes::doClone() const
395 return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
399 InsetBase::Code InsetQuotes::lyxCode() const
401 return InsetBase::QUOTE_CODE;