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"
18 #include "support/lyxlib.h"
23 #include "LaTeXFeatures.h"
24 #include "support/lstrings.h"
28 #include "BufferView.h"
33 // Quotes. Used for the various quotes. German, English, French,
34 // Danish, Polish, all either double or single.
38 // codes used to read/write quotes to LyX files
39 char const * const language_char = "esgpfa";
40 char const * const side_char = "lr" ;
41 char const * const times_char = "sd";
43 // List of known quote chars
44 char const * const quote_char = ",'`<>";
46 // Index of chars used for the quote. Index is [side, language]
47 int quote_index[2][6] = {
48 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
49 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
51 // Corresponding LaTeX code, for double and single quotes.
52 char const * const latex_quote_t1[2][5] =
53 { { "\\quotesinglbase{}", "'", "`",
54 "\\guilsinglleft{}", "\\guilsinglright{}" },
55 { ",,", "''", "``", "<<", ">>" } };
57 char const * const latex_quote_ot1[2][5] =
58 { { "\\quotesinglbase{}", "'", "`",
59 "\\guilsinglleft{}", "\\guilsinglright{}" },
60 { "\\quotedblbase{}", "''", "``",
61 "\\guillemotleft{}", "\\guillemotright{}" } };
63 char const * const latex_quote_babel[2][5] =
64 { { "\\glq{}", "'", "`", "\\flq{}", "\\frq{}" },
65 { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
70 InsetQuotes::InsetQuotes(string const & str)
76 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
77 InsetQuotes::quote_side s,
78 InsetQuotes::quote_times t)
79 : language(l), side(s), times(t)
83 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
84 : language(params.quotes_language), times(params.quotes_times)
86 // Decide whether left or right
88 case ' ': case '(': case '{': case '[': case '-': case ':':
89 case Paragraph::META_HFILL:
90 case Paragraph::META_NEWLINE:
91 side = InsetQuotes::LeftQ; // left quote
94 side = InsetQuotes::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 = InsetQuotes::quote_language(i);
117 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
118 " bad language specification." << endl;
119 language = InsetQuotes::EnglishQ;
122 for (i = 0; i < 2; ++i) {
123 if (str[1] == side_char[i]) {
124 side = InsetQuotes::quote_side(i);
129 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
130 " bad side specification." << endl;
131 side = InsetQuotes::LeftQ;
134 for (i = 0; i < 2; ++i) {
135 if (str[2] == times_char[i]) {
136 times = InsetQuotes::quote_times(i);
141 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
142 " bad times specification." << endl;
143 times = InsetQuotes::DoubleQ;
148 string const InsetQuotes::dispString() const
151 disp += quote_char[quote_index[side][language]];
152 if (times == InsetQuotes::DoubleQ)
155 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1)
158 else if (disp == ">>")
165 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
167 return lyxfont::maxAscent(font);
171 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
173 return lyxfont::maxDescent(font);
177 int InsetQuotes::width(BufferView *, LyXFont const & font) const
179 string text = dispString();
182 for (string::size_type i = 0; i < text.length(); ++i) {
184 w += lyxfont::width('i', font);
185 else if (i == 0 || text[i] != text[i-1])
186 w += lyxfont::width(text[i], font);
188 w += lyxfont::width(',', font);
195 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
199 // quotes-insets cannot be latex of any kind
200 font.setLatex(LyXFont::OFF);
206 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
207 int baseline, float & x, bool) const
209 string text = dispString();
211 bv->painter().text(int(x), baseline, text, font);
212 x += width(bv, font);
216 void InsetQuotes::write(Buffer const *, ostream & os) const
219 text += language_char[language];
220 text += side_char[side];
221 text += times_char[times];
222 os << "Quotes " << text;
226 void InsetQuotes::read(Buffer const *, LyXLex & lex)
229 parseString(lex.GetString());
231 string tmp(lex.GetString());
232 if (tmp != "\\end_inset")
233 lyxerr << "LyX Warning: Missing \\end_inset "
234 "in InsetQuotes::Read." << endl;
238 extern bool use_babel;
240 int InsetQuotes::latex(Buffer const * buf, ostream & os,
241 bool /*fragile*/, bool) const
243 string const doclang = buf->getLanguage()->lang();
244 int quoteind = quote_index[side][language];
247 if (lyxrc.fontenc == "T1") {
248 qstr = latex_quote_t1[times][quoteind];
249 #ifdef DO_USE_DEFAULT_LANGUAGE
250 } else if (doclang == "default") {
252 } else if (!use_babel) {
254 qstr = latex_quote_ot1[times][quoteind];
255 } else if (language == InsetQuotes::FrenchQ
256 && times == InsetQuotes::DoubleQ
257 && doclang == "frenchb") {
258 if (side == InsetQuotes::LeftQ)
263 qstr = latex_quote_babel[times][quoteind];
265 // Always guard against unfortunate ligatures (!` ?`)
266 if (prefixIs(qstr, "`"))
267 qstr.insert(0, "{}");
274 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
281 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
288 int InsetQuotes::docBook(Buffer const *, ostream & os) const
290 if (times == InsetQuotes::DoubleQ) {
291 if (side == InsetQuotes::LeftQ)
296 if (side == InsetQuotes::LeftQ)
305 void InsetQuotes::validate(LaTeXFeatures & features) const
307 char type = quote_char[quote_index[side][language]];
309 if (features.bufferParams().language->lang() == "default"
310 && lyxrc.fontenc != "T1") {
311 if (times == InsetQuotes::SingleQ)
313 case ',': features.quotesinglbase = true; break;
314 case '<': features.guilsinglleft = true; break;
315 case '>': features.guilsinglright = true; break;
320 case ',': features.quotedblbase = true; break;
321 case '<': features.guillemotleft = true; break;
322 case '>': features.guillemotright = true; break;
329 Inset * InsetQuotes::clone(Buffer const &, bool) const
331 return new InsetQuotes(language, side, times);
335 Inset::Code InsetQuotes::lyxCode() const
337 return Inset::QUOTE_CODE;