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"
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 lyxfont::maxAscent(font);
193 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
195 return lyxfont::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 += lyxfont::width('i', font);
207 else if (i == 0 || text[i] != text[i-1])
208 w += lyxfont::width(text[i], font);
210 w += lyxfont::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 bv->painter().text(int(x), baseline, text, font);
236 x += width(bv, font);
240 void InsetQuotes::write(Buffer const *, ostream & os) const
243 text += language_char[language_];
244 text += side_char[side_];
245 text += times_char[times_];
246 os << "Quotes " << text;
250 void InsetQuotes::read(Buffer const *, LyXLex & lex)
253 parseString(lex.getString());
255 if (lex.getString() != "\\end_inset") {
256 lex.printError("Missing \\end_inset at this point");
261 extern bool use_babel;
263 int InsetQuotes::latex(Buffer const * buf, ostream & os,
264 bool /*fragile*/, bool /* free_spc */) const
266 // How do we get the local language here??
267 lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
268 lyx::Assert(curr_pos != -1);
269 string const curr_lang =
270 parOwner()->getFont(buf->params,
271 curr_pos).language()->babel();
273 const int quoteind = quote_index[side_][language_];
276 if (language_ == FrenchQ && times_ == DoubleQ
277 && curr_lang == "frenchb") {
279 qstr = "\\og "; //the spaces are important here
281 qstr = " \\fg{}"; //and here
282 } else if (language_ == FrenchQ && times_ == DoubleQ
283 && curr_lang == "french") {
285 qstr = "<< "; //the spaces are important here
287 qstr = " >>"; //and here
288 } else if (lyxrc.fontenc == "T1") {
289 qstr = latex_quote_t1[times_][quoteind];
290 #ifdef DO_USE_DEFAULT_LANGUAGE
291 } else if (doclang == "default") {
293 } else if (!use_babel) {
295 qstr = latex_quote_ot1[times_][quoteind];
297 qstr = latex_quote_babel[times_][quoteind];
300 // Always guard against unfortunate ligatures (!` ?`)
301 if (prefixIs(qstr, "`"))
302 qstr.insert(0, "{}");
309 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
316 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
323 int InsetQuotes::docbook(Buffer const *, ostream & os) const
325 if (times_ == DoubleQ) {
340 void InsetQuotes::validate(LaTeXFeatures & features) const
342 char type = quote_char[quote_index[side_][language_]];
344 #ifdef DO_USE_DEFAULT_LANGUAGE
345 if (features.bufferParams().language->lang() == "default"
349 && lyxrc.fontenc != "T1") {
350 if (times_ == SingleQ)
352 case ',': features.require("quotesinglbase"); break;
353 case '<': features.require("guilsinglleft"); break;
354 case '>': features.require("guilsinglright"); break;
359 case ',': features.require("quotedblbase"); break;
360 case '<': features.require("guillemotleft"); break;
361 case '>': features.require("guillemotright"); break;
368 Inset * InsetQuotes::clone(Buffer const &, bool) const
370 return new InsetQuotes(language_, side_, times_);
374 Inset::Code InsetQuotes::lyxCode() const
376 return Inset::QUOTE_CODE;