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 "BufferView.h"
18 #include "dimension.h"
20 #include "LaTeXFeatures.h"
21 #include "latexrunparams.h"
25 #include "paragraph.h"
26 #include "metricsinfo.h"
27 #include "frontends/font_metrics.h"
28 #include "frontends/Painter.h"
29 #include "support/LAssert.h"
30 #include "support/lstrings.h"
32 using namespace lyx::support;
39 // codes used to read/write quotes to LyX files
40 char const * const language_char = "esgpfa";
41 char const * const side_char = "lr" ;
42 char const * const times_char = "sd";
44 // List of known quote chars
45 char const * const quote_char = ",'`<>";
47 // Index of chars used for the quote. Index is [side, language]
48 int quote_index[2][6] = {
49 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
50 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
52 // Corresponding LaTeX code, for double and single quotes.
53 char const * const latex_quote_t1[2][5] =
54 { { "\\quotesinglbase ", "'", "`",
55 "\\guilsinglleft{}", "\\guilsinglright{}" },
56 { ",,", "''", "``", "<<", ">>" } };
58 char const * const latex_quote_ot1[2][5] =
59 { { "\\quotesinglbase ", "'", "`",
60 "\\guilsinglleft{}", "\\guilsinglright{}" },
61 { "\\quotedblbase ", "''", "``",
62 "\\guillemotleft{}", "\\guillemotright{}" } };
64 char const * const latex_quote_babel[2][5] =
65 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
66 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
71 InsetQuotes::InsetQuotes(string const & str)
77 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
78 : language_(l), side_(s), times_(t)
82 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
83 : language_(params.quotes_language), times_(params.quotes_times)
85 // Decide whether left or right
88 #warning eh ? I am lost here ...
89 //case Paragraph::META_HFILL:
90 // case Paragraph::META_NEWLINE:
91 side_ = LeftQ; // left quote
94 side_ = RightQ; // right quote
99 void InsetQuotes::parseString(string const & s)
102 if (str.length() != 3) {
103 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
104 " bad string length." << endl;
110 for (i = 0; i < 6; ++i) {
111 if (str[0] == language_char[i]) {
112 language_ = quote_language(i);
117 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
118 " bad language specification." << endl;
119 language_ = EnglishQ;
122 for (i = 0; i < 2; ++i) {
123 if (str[1] == side_char[i]) {
124 side_ = quote_side(i);
129 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
130 " bad side specification." << endl;
134 for (i = 0; i < 2; ++i) {
135 if (str[2] == times_char[i]) {
136 times_ = quote_times(i);
141 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
142 " bad times specification." << endl;
148 string const InsetQuotes::dispString(Language const * loclang) const
151 disp += quote_char[quote_index[side_][language_]];
152 if (times_ == DoubleQ)
155 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
156 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
157 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
160 else if (disp == ">>")
164 // in french, spaces are added inside double quotes
165 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
169 disp.insert(string::size_type(0), 1, ' ');
176 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
178 LyXFont & font = mi.base.font;
179 dim.asc = font_metrics::maxAscent(font);
180 dim.des = font_metrics::maxDescent(font);
183 string const text = dispString(font.language());
184 for (string::size_type i = 0; i < text.length(); ++i) {
186 dim.wid += font_metrics::width('i', font);
187 else if (i == 0 || text[i] != text[i - 1])
188 dim.wid += font_metrics::width(text[i], font);
190 dim.wid += font_metrics::width(',', font);
197 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
209 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
211 string const text = dispString(pi.base.font.language());
213 if (text.length() == 2 && text[0] == text[1]) {
214 pi.pain.text(x, y, text[0], pi.base.font);
215 int const t = font_metrics::width(',', pi.base.font);
216 pi.pain.text(x + t, y, text[0], pi.base.font);
218 pi.pain.text(x, y, text, pi.base.font);
223 void InsetQuotes::write(Buffer const *, ostream & os) const
226 text += language_char[language_];
227 text += side_char[side_];
228 text += times_char[times_];
229 os << "Quotes " << text;
233 void InsetQuotes::read(Buffer const *, LyXLex & lex)
236 parseString(lex.getString());
238 if (lex.getString() != "\\end_inset") {
239 lex.printError("Missing \\end_inset at this point");
244 int InsetQuotes::latex(Buffer const * buf, ostream & os,
245 LatexRunParams const & runparams) const
247 // How do we get the local language here??
248 lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
249 Assert(curr_pos != -1);
251 #warning FIXME. We _must_ find another way to get the language. (Lgb)
253 // This cannot be used. (Lgb)
254 string const curr_lang =
255 parOwner()->getFont(buf->params,
256 curr_pos).language()->babel();
258 // And this is not the way... (Lgb)
259 string const curr_lang = buf->params.language->lang();
261 const int quoteind = quote_index[side_][language_];
264 if (language_ == FrenchQ && times_ == DoubleQ
265 && curr_lang == "frenchb") {
267 qstr = "\\og "; //the spaces are important here
269 qstr = " \\fg{}"; //and here
270 } else if (language_ == FrenchQ && times_ == DoubleQ
271 && curr_lang == "french") {
273 qstr = "<< "; //the spaces are important here
275 qstr = " >>"; //and here
276 } else if (lyxrc.fontenc == "T1") {
277 qstr = latex_quote_t1[times_][quoteind];
278 #ifdef DO_USE_DEFAULT_LANGUAGE
279 } else if (doclang == "default") {
281 } else if (!runparams.use_babel) {
283 qstr = latex_quote_ot1[times_][quoteind];
285 qstr = latex_quote_babel[times_][quoteind];
288 // Always guard against unfortunate ligatures (!` ?`)
289 if (prefixIs(qstr, "`"))
290 qstr.insert(0, "{}");
297 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
304 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
311 int InsetQuotes::docbook(Buffer const *, ostream & os, bool) const
313 if (times_ == DoubleQ) {
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_ == SingleQ)
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;
357 InsetBase * InsetQuotes::clone() const
359 return new InsetQuotes(language_, side_, times_);
363 Inset::Code InsetQuotes::lyxCode() const
365 return Inset::QUOTE_CODE;