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;
40 /* codes used to read/write quotes to LyX files
49 char const * const language_char = "esgpfa";
50 char const * const side_char = "lr" ;
51 char const * const times_char = "sd";
53 // List of known quote chars
54 char const * const quote_char = ",'`<>";
56 // Index of chars used for the quote. Index is [side, language]
57 int quote_index[2][6] = {
58 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
59 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
61 // Corresponding LaTeX code, for double and single quotes.
62 char const * const latex_quote_t1[2][5] =
63 { { "\\quotesinglbase ", "'", "`",
64 "\\guilsinglleft{}", "\\guilsinglright{}" },
65 { ",,", "''", "``", "<<", ">>" } };
67 char const * const latex_quote_ot1[2][5] =
68 { { "\\quotesinglbase ", "'", "`",
69 "\\guilsinglleft{}", "\\guilsinglright{}" },
70 { "\\quotedblbase ", "''", "``",
71 "\\guillemotleft{}", "\\guillemotright{}" } };
73 char const * const latex_quote_babel[2][5] =
74 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
75 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
80 InsetQuotes::InsetQuotes(string const & str)
86 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
87 : language_(l), side_(s), times_(t)
91 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
92 : language_(params.quotes_language), times_(params.quotes_times)
94 // Decide whether left or right
97 #warning eh ? I am lost here ...
98 //case Paragraph::META_HFILL:
99 // case Paragraph::META_NEWLINE:
100 side_ = LeftQ; // left quote
103 side_ = RightQ; // right quote
108 void InsetQuotes::parseString(string const & s)
111 if (str.length() != 3) {
112 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
113 " bad string length." << endl;
119 for (i = 0; i < 6; ++i) {
120 if (str[0] == language_char[i]) {
121 language_ = quote_language(i);
126 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
127 " bad language specification." << endl;
128 language_ = EnglishQ;
131 for (i = 0; i < 2; ++i) {
132 if (str[1] == side_char[i]) {
133 side_ = quote_side(i);
138 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
139 " bad side specification." << endl;
143 for (i = 0; i < 2; ++i) {
144 if (str[2] == times_char[i]) {
145 times_ = quote_times(i);
150 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
151 " bad times specification." << endl;
157 string const InsetQuotes::dispString(Language const * loclang) const
160 disp += quote_char[quote_index[side_][language_]];
161 if (times_ == DoubleQ)
164 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
165 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
166 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
169 else if (disp == ">>")
173 // in french, spaces are added inside double quotes
174 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
178 disp.insert(string::size_type(0), 1, ' ');
185 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
187 LyXFont & font = mi.base.font;
188 dim.asc = font_metrics::maxAscent(font);
189 dim.des = font_metrics::maxDescent(font);
192 string const text = dispString(font.language());
193 for (string::size_type i = 0; i < text.length(); ++i) {
195 dim.wid += font_metrics::width('i', font);
196 else if (i == 0 || text[i] != text[i - 1])
197 dim.wid += font_metrics::width(text[i], font);
199 dim.wid += font_metrics::width(',', font);
206 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
218 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
220 string const text = dispString(pi.base.font.language());
222 if (text.length() == 2 && text[0] == text[1]) {
223 pi.pain.text(x, y, text[0], pi.base.font);
224 int const t = font_metrics::width(',', pi.base.font);
225 pi.pain.text(x + t, y, text[0], pi.base.font);
227 pi.pain.text(x, y, text, pi.base.font);
232 void InsetQuotes::write(Buffer const *, ostream & os) const
235 text += language_char[language_];
236 text += side_char[side_];
237 text += times_char[times_];
238 os << "Quotes " << text;
242 void InsetQuotes::read(Buffer const *, LyXLex & lex)
245 parseString(lex.getString());
247 if (lex.getString() != "\\end_inset") {
248 lex.printError("Missing \\end_inset at this point");
253 int InsetQuotes::latex(Buffer const * buf, ostream & os,
254 LatexRunParams const & runparams) const
256 // How do we get the local language here??
257 lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
258 Assert(curr_pos != -1);
260 #warning FIXME. We _must_ find another way to get the language. (Lgb)
262 // This cannot be used. (Lgb)
263 string const curr_lang =
264 parOwner()->getFont(buf->params,
265 curr_pos).language()->babel();
267 // And this is not the way... (Lgb)
268 string const curr_lang = buf->params.language->lang();
270 const int quoteind = quote_index[side_][language_];
273 if (language_ == FrenchQ && times_ == DoubleQ
274 && curr_lang == "frenchb") {
276 qstr = "\\og "; //the spaces are important here
278 qstr = " \\fg{}"; //and here
279 } else if (language_ == FrenchQ && times_ == DoubleQ
280 && curr_lang == "french") {
282 qstr = "<< "; //the spaces are important here
284 qstr = " >>"; //and here
285 } else if (lyxrc.fontenc == "T1") {
286 qstr = latex_quote_t1[times_][quoteind];
287 #ifdef DO_USE_DEFAULT_LANGUAGE
288 } else if (doclang == "default") {
290 } else if (!runparams.use_babel) {
292 qstr = latex_quote_ot1[times_][quoteind];
294 qstr = latex_quote_babel[times_][quoteind];
297 // Always guard against unfortunate ligatures (!` ?`)
298 if (prefixIs(qstr, "`"))
299 qstr.insert(0, "{}");
306 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
313 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
320 int InsetQuotes::docbook(Buffer const *, ostream & os, bool) const
322 if (times_ == DoubleQ) {
337 void InsetQuotes::validate(LaTeXFeatures & features) const
339 bool const use_babel = features.useBabel();
340 char type = quote_char[quote_index[side_][language_]];
342 #ifdef DO_USE_DEFAULT_LANGUAGE
343 if (features.bufferParams().language->lang() == "default"
347 && lyxrc.fontenc != "T1") {
348 if (times_ == SingleQ)
350 case ',': features.require("quotesinglbase"); break;
351 case '<': features.require("guilsinglleft"); break;
352 case '>': features.require("guilsinglright"); break;
357 case ',': features.require("quotedblbase"); break;
358 case '<': features.require("guillemotleft"); break;
359 case '>': features.require("guillemotright"); break;
366 auto_ptr<InsetBase> InsetQuotes::clone() const
368 return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
372 InsetOld::Code InsetQuotes::lyxCode() const
374 return InsetOld::QUOTE_CODE;