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
14 #pragma implementation
17 #include "insetquotes.h"
19 #include "support/LAssert.h"
20 #include "support/lstrings.h"
21 #include "BufferView.h"
22 #include "LaTeXFeatures.h"
23 #include "frontends/Painter.h"
26 #include "frontends/font_metrics.h"
30 #include "paragraph.h"
36 // Quotes. Used for the various quotes. German, English, French,
37 // Danish, Polish, all either double or single.
41 // codes used to read/write quotes to LyX files
42 char const * const language_char = "esgpfa";
43 char const * const side_char = "lr" ;
44 char const * const times_char = "sd";
46 // List of known quote chars
47 char const * const quote_char = ",'`<>";
49 // Index of chars used for the quote. Index is [side, language]
50 int quote_index[2][6] = {
51 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
52 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
54 // Corresponding LaTeX code, for double and single quotes.
55 char const * const latex_quote_t1[2][5] =
56 { { "\\quotesinglbase ", "'", "`",
57 "\\guilsinglleft{}", "\\guilsinglright{}" },
58 { ",,", "''", "``", "<<", ">>" } };
60 char const * const latex_quote_ot1[2][5] =
61 { { "\\quotesinglbase ", "'", "`",
62 "\\guilsinglleft{}", "\\guilsinglright{}" },
63 { "\\quotedblbase ", "''", "``",
64 "\\guillemotleft{}", "\\guillemotright{}" } };
66 char const * const latex_quote_babel[2][5] =
67 { { "\\glq ", "'", "`", "\\flq{}", "\\frq{}" },
68 { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
73 InsetQuotes::InsetQuotes(string const & str)
79 InsetQuotes::InsetQuotes(quote_language l,
82 : language_(l), side_(s), times_(t)
86 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
87 : language_(params.quotes_language), times_(params.quotes_times)
89 // Decide whether left or right
92 case Paragraph::META_HFILL:
93 case Paragraph::META_NEWLINE:
94 side_ = LeftQ; // left quote
97 side_ = RightQ; // right quote
102 void InsetQuotes::parseString(string const & s)
105 if (str.length() != 3) {
106 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
107 " bad string length." << endl;
113 for (i = 0; i < 6; ++i) {
114 if (str[0] == language_char[i]) {
115 language_ = quote_language(i);
120 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
121 " bad language specification." << endl;
122 language_ = EnglishQ;
125 for (i = 0; i < 2; ++i) {
126 if (str[1] == side_char[i]) {
127 side_ = quote_side(i);
132 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
133 " bad side specification." << endl;
137 for (i = 0; i < 2; ++i) {
138 if (str[2] == times_char[i]) {
139 times_ = quote_times(i);
144 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
145 " bad times specification." << endl;
151 string const InsetQuotes::dispString(Language const * loclang) const
154 disp += quote_char[quote_index[side_][language_]];
155 if (times_ == DoubleQ)
158 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
159 || lyxrc.font_norm_type == LyXRC::ISO_8859_3
160 || lyxrc.font_norm_type == LyXRC::ISO_8859_4
161 || lyxrc.font_norm_type == LyXRC::ISO_8859_9) {
164 else if (disp == "''")
167 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
168 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
169 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
172 else if (disp == ">>")
176 // in french, spaces are added inside double quotes
177 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
181 disp.insert(string::size_type(0), 1, ' ');
188 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
190 return font_metrics::maxAscent(font);
194 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
196 return font_metrics::maxDescent(font);
200 int InsetQuotes::width(BufferView *, LyXFont const & font) const
202 string const text = dispString(font.language());
205 for (string::size_type i = 0; i < text.length(); ++i) {
207 w += font_metrics::width('i', font);
208 else if (i == 0 || text[i] != text[i - 1])
209 w += font_metrics::width(text[i], font);
211 w += font_metrics::width(',', font);
219 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
231 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
232 int baseline, float & x, bool) const
234 string const text = dispString(font.language());
236 if (text.length() == 2 && text[0] == text[1]) {
237 bv->painter().text(int(x), baseline, text[0], font);
238 int x2 = int(x + font_metrics::width(',', font));
239 bv->painter().text(x2, baseline, text[0], font);
241 bv->painter().text(int(x), baseline, text, font);
242 x += width(bv, font);
246 void InsetQuotes::write(Buffer const *, ostream & os) const
249 text += language_char[language_];
250 text += side_char[side_];
251 text += times_char[times_];
252 os << "Quotes " << text;
256 void InsetQuotes::read(Buffer const *, LyXLex & lex)
259 parseString(lex.getString());
261 if (lex.getString() != "\\end_inset") {
262 lex.printError("Missing \\end_inset at this point");
267 extern bool use_babel;
269 int InsetQuotes::latex(Buffer const * buf, ostream & os,
270 bool /*fragile*/, bool /* free_spc */) const
272 // How do we get the local language here??
273 lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
274 lyx::Assert(curr_pos != -1);
275 string const curr_lang =
276 parOwner()->getFont(buf->params,
277 curr_pos).language()->babel();
279 const int quoteind = quote_index[side_][language_];
282 if (language_ == FrenchQ && times_ == DoubleQ
283 && curr_lang == "frenchb") {
285 qstr = "\\og "; //the spaces are important here
287 qstr = " \\fg{}"; //and here
288 } else if (language_ == FrenchQ && times_ == DoubleQ
289 && curr_lang == "french") {
291 qstr = "<< "; //the spaces are important here
293 qstr = " >>"; //and here
294 } else if (lyxrc.fontenc == "T1") {
295 qstr = latex_quote_t1[times_][quoteind];
296 #ifdef DO_USE_DEFAULT_LANGUAGE
297 } else if (doclang == "default") {
299 } else if (!use_babel) {
301 qstr = latex_quote_ot1[times_][quoteind];
303 qstr = latex_quote_babel[times_][quoteind];
306 // Always guard against unfortunate ligatures (!` ?`)
307 if (prefixIs(qstr, "`"))
308 qstr.insert(0, "{}");
315 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
322 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
329 int InsetQuotes::docbook(Buffer const *, ostream & os, bool) const
331 if (times_ == DoubleQ) {
346 void InsetQuotes::validate(LaTeXFeatures & features) const
348 char type = quote_char[quote_index[side_][language_]];
350 #ifdef DO_USE_DEFAULT_LANGUAGE
351 if (features.bufferParams().language->lang() == "default"
355 && lyxrc.fontenc != "T1") {
356 if (times_ == SingleQ)
358 case ',': features.require("quotesinglbase"); break;
359 case '<': features.require("guilsinglleft"); break;
360 case '>': features.require("guilsinglright"); break;
365 case ',': features.require("quotedblbase"); break;
366 case '<': features.require("guillemotleft"); break;
367 case '>': features.require("guillemotright"); break;
374 Inset * InsetQuotes::clone(Buffer const &, bool) const
376 return new InsetQuotes(language_, side_, times_);
380 Inset::Code InsetQuotes::lyxCode() const
382 return Inset::QUOTE_CODE;