1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
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"
35 // Quotes. Used for the various quotes. German, English, French,
36 // Danish, Polish, all either double or single.
40 // codes used to read/write quotes to LyX files
41 char const * const language_char = "esgpfa";
42 char const * const side_char = "lr" ;
43 char const * const times_char = "sd";
45 // List of known quote chars
46 char const * const quote_char = ",'`<>";
48 // Index of chars used for the quote. Index is [side, language]
49 int quote_index[2][6] = {
50 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
51 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
53 // Corresponding LaTeX code, for double and single quotes.
54 char const * const latex_quote_t1[2][5] =
55 { { "\\quotesinglbase{}", "'", "`",
56 "\\guilsinglleft{}", "\\guilsinglright{}" },
57 { ",,", "''", "``", "<<", ">>" } };
59 char const * const latex_quote_ot1[2][5] =
60 { { "\\quotesinglbase{}", "'", "`",
61 "\\guilsinglleft{}", "\\guilsinglright{}" },
62 { "\\quotedblbase{}", "''", "``",
63 "\\guillemotleft{}", "\\guillemotright{}" } };
65 char const * const latex_quote_babel[2][5] =
66 { { "\\glq{}", "'", "`", "\\flq{}", "\\frq{}" },
67 { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
72 InsetQuotes::InsetQuotes(string const & str)
78 InsetQuotes::InsetQuotes(quote_language l,
81 : language_(l), side_(s), times_(t)
85 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
86 : language_(params.quotes_language), times_(params.quotes_times)
88 // Decide whether left or right
91 case Paragraph::META_HFILL:
92 case Paragraph::META_NEWLINE:
93 side_ = LeftQ; // left quote
96 side_ = RightQ; // right quote
101 void InsetQuotes::parseString(string const & s)
104 if (str.length() != 3) {
105 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
106 " bad string length." << endl;
112 for (i = 0; i < 6; ++i) {
113 if (str[0] == language_char[i]) {
114 language_ = quote_language(i);
119 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
120 " bad language specification." << endl;
121 language_ = EnglishQ;
124 for (i = 0; i < 2; ++i) {
125 if (str[1] == side_char[i]) {
126 side_ = quote_side(i);
131 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
132 " bad side specification." << endl;
136 for (i = 0; i < 2; ++i) {
137 if (str[2] == times_char[i]) {
138 times_ = quote_times(i);
143 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
144 " bad times specification." << endl;
150 string const InsetQuotes::dispString(Language const * loclang) const
153 disp += quote_char[quote_index[side_][language_]];
154 if (times_ == DoubleQ)
157 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
158 || lyxrc.font_norm_type == LyXRC::ISO_8859_3
159 || lyxrc.font_norm_type == LyXRC::ISO_8859_4
160 || lyxrc.font_norm_type == LyXRC::ISO_8859_9) {
163 else if (disp == "''")
166 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
167 || lyxrc.font_norm_type == LyXRC::ISO_8859_9
168 || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
171 else if (disp == ">>")
175 // in french, spaces are added inside double quotes
176 if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
187 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
189 return font_metrics::maxAscent(font);
193 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
195 return font_metrics::maxDescent(font);
199 int InsetQuotes::width(BufferView *, LyXFont const & font) const
201 string const text = dispString(font.language());
204 for (string::size_type i = 0; i < text.length(); ++i) {
206 w += font_metrics::width('i', font);
207 else if (i == 0 || text[i] != text[i-1])
208 w += font_metrics::width(text[i], font);
210 w += font_metrics::width(',', font);
218 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
230 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
231 int baseline, float & x, bool) const
233 string const text = dispString(font.language());
235 if (text.length() == 2 && text[0] == text[1]) {
236 bv->painter().text(int(x), baseline, text[0], font);
237 int x2 = int(x + font_metrics::width(',', font));
238 bv->painter().text(x2, baseline, text[0], font);
240 bv->painter().text(int(x), baseline, text, font);
241 x += width(bv, font);
245 void InsetQuotes::write(Buffer const *, ostream & os) const
248 text += language_char[language_];
249 text += side_char[side_];
250 text += times_char[times_];
251 os << "Quotes " << text;
255 void InsetQuotes::read(Buffer const *, LyXLex & lex)
258 parseString(lex.getString());
260 if (lex.getString() != "\\end_inset") {
261 lex.printError("Missing \\end_inset at this point");
266 extern bool use_babel;
268 int InsetQuotes::latex(Buffer const * buf, ostream & os,
269 bool /*fragile*/, bool /* free_spc */) const
271 // How do we get the local language here??
272 lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
273 lyx::Assert(curr_pos != -1);
274 string const curr_lang =
275 parOwner()->getFont(buf->params,
276 curr_pos).language()->babel();
278 const int quoteind = quote_index[side_][language_];
281 if (language_ == FrenchQ && times_ == DoubleQ
282 && curr_lang == "frenchb") {
284 qstr = "\\og "; //the spaces are important here
286 qstr = " \\fg{}"; //and here
287 } else if (language_ == FrenchQ && times_ == DoubleQ
288 && curr_lang == "french") {
290 qstr = "<< "; //the spaces are important here
292 qstr = " >>"; //and here
293 } else if (lyxrc.fontenc == "T1") {
294 qstr = latex_quote_t1[times_][quoteind];
295 #ifdef DO_USE_DEFAULT_LANGUAGE
296 } else if (doclang == "default") {
298 } else if (!use_babel) {
300 qstr = latex_quote_ot1[times_][quoteind];
302 qstr = latex_quote_babel[times_][quoteind];
305 // Always guard against unfortunate ligatures (!` ?`)
306 if (prefixIs(qstr, "`"))
307 qstr.insert(0, "{}");
314 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
321 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
328 int InsetQuotes::docbook(Buffer const *, ostream & os, bool) const
330 if (times_ == DoubleQ) {
345 void InsetQuotes::validate(LaTeXFeatures & features) const
347 char type = quote_char[quote_index[side_][language_]];
349 #ifdef DO_USE_DEFAULT_LANGUAGE
350 if (features.bufferParams().language->lang() == "default"
354 && lyxrc.fontenc != "T1") {
355 if (times_ == SingleQ)
357 case ',': features.require("quotesinglbase"); break;
358 case '<': features.require("guilsinglleft"); break;
359 case '>': features.require("guilsinglright"); break;
364 case ',': features.require("quotedblbase"); break;
365 case '<': features.require("guillemotleft"); break;
366 case '>': features.require("guillemotright"); break;
373 Inset * InsetQuotes::clone(Buffer const &, bool) const
375 return new InsetQuotes(language_, side_, times_);
379 Inset::Code InsetQuotes::lyxCode() const
381 return Inset::QUOTE_CODE;