1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 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"
31 // Quotes. Used for the various quotes. German, English, French,
32 // Danish, Polish, all either double or single.
34 // codes used to read/write quotes to LyX files
35 static char const * const language_char = "esgpfa";
36 static char const * const side_char = "lr" ;
37 static char const * const times_char = "sd";
39 // List of known quote chars
40 static char const * const quote_char = ",'`<>";
42 // Index of chars used for the quote. Index is [side, language]
43 int quote_index[2][6] = {
44 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
45 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
47 // Corresponding LaTeX code, for double and single quotes.
48 static char const * const latex_quote_t1[2][5] =
49 { { "\\quotesinglbase{}", "'", "`",
50 "\\guilsinglleft{}", "\\guilsinglright{}" },
51 { ",,", "''", "``", "<<", ">>" } };
53 static char const * const latex_quote_ot1[2][5] =
54 { { "\\quotesinglbase{}", "'", "`",
55 "\\guilsinglleft{}", "\\guilsinglright{}" },
56 { "\\quotedblbase{}", "''", "``",
57 "\\guillemotleft{}", "\\guillemotright{}" } };
59 static char const * const latex_quote_babel[2][5] =
60 { { "\\glq{}", "'", "`", "\\flq{}", "\\frq{}" },
61 { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
64 InsetQuotes::InsetQuotes(string const & str)
70 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
71 InsetQuotes::quote_side s,
72 InsetQuotes::quote_times t)
73 : language(l), side(s), times(t)
77 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
78 : language(params.quotes_language), times(params.quotes_times)
80 // Decide whether left or right
82 case ' ': case '(': case '{': case '[': case '-': case ':':
83 case LyXParagraph::META_HFILL:
84 case LyXParagraph::META_NEWLINE:
85 side = InsetQuotes::LeftQ; // left quote
88 side = InsetQuotes::RightQ; // right quote
93 void InsetQuotes::ParseString(string const & s)
96 if (str.length() != 3) {
97 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
98 " bad string length." << endl;
104 for (i = 0; i < 6; ++i) {
105 if (str[0] == language_char[i]) {
106 language = InsetQuotes::quote_language(i);
111 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
112 " bad language specification." << endl;
113 language = InsetQuotes::EnglishQ;
116 for (i = 0; i < 2; ++i) {
117 if (str[1] == side_char[i]) {
118 side = InsetQuotes::quote_side(i);
123 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
124 " bad side specification." << endl;
125 side = InsetQuotes::LeftQ;
128 for (i = 0; i < 2; ++i) {
129 if (str[2] == times_char[i]) {
130 times = InsetQuotes::quote_times(i);
135 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
136 " bad times specification." << endl;
137 times = InsetQuotes::DoubleQ;
142 string const InsetQuotes::DispString() const
145 disp += quote_char[quote_index[side][language]];
146 if (times == InsetQuotes::DoubleQ)
149 if (lyxrc.font_norm == "iso8859-1")
152 else if (disp == ">>")
159 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
161 return lyxfont::maxAscent(font);
165 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
167 return lyxfont::maxDescent(font);
171 int InsetQuotes::width(BufferView *, LyXFont const & font) const
173 string text = DispString();
176 for (string::size_type i = 0; i < text.length(); ++i) {
178 w += lyxfont::width('i', font);
179 else if (i == 0 || text[i] != text[i-1])
180 w += lyxfont::width(text[i], font);
182 w += lyxfont::width(',', font);
189 LyXFont const InsetQuotes::ConvertFont(LyXFont const & f) const
192 // quotes-insets cannot be latex of any kind
193 font.setLatex(LyXFont::OFF);
198 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
199 int baseline, float & x, bool) const
201 string text = DispString();
203 bv->painter().text(int(x), baseline, text, font);
204 x += width(bv, font);
208 void InsetQuotes::Write(Buffer const *, ostream & os) const
211 text += language_char[language];
212 text += side_char[side];
213 text += times_char[times];
214 os << "Quotes " << text;
218 void InsetQuotes::Read(Buffer const *, LyXLex & lex)
221 ParseString(lex.GetString());
223 string tmp(lex.GetString());
224 if (tmp != "\\end_inset")
225 lyxerr << "LyX Warning: Missing \\end_inset "
226 "in InsetQuotes::Read." << endl;
230 int InsetQuotes::Latex(Buffer const * buf, ostream & os,
231 bool /*fragile*/, bool) const
233 string doclang = buf->GetLanguage()->lang();
234 int quoteind = quote_index[side][language];
237 if (lyxrc.fontenc == "T1") {
238 qstr = latex_quote_t1[times][quoteind];
239 } else if (doclang == "default") {
240 qstr = latex_quote_ot1[times][quoteind];
241 } else if (language == InsetQuotes::FrenchQ
242 && times == InsetQuotes::DoubleQ
243 && doclang == "frenchb") {
244 if (side == InsetQuotes::LeftQ)
249 qstr = latex_quote_babel[times][quoteind];
251 // Always guard against unfortunate ligatures (!` ?`)
252 qstr.insert(0, "{}");
259 int InsetQuotes::Ascii(Buffer const *, ostream & os, int) const
266 int InsetQuotes::Linuxdoc(Buffer const *, ostream & os) const
273 int InsetQuotes::DocBook(Buffer const *, ostream & os) const
275 if(times == InsetQuotes::DoubleQ) {
276 if (side == InsetQuotes::LeftQ)
281 if (side == InsetQuotes::LeftQ)
290 void InsetQuotes::Validate(LaTeXFeatures & features) const
292 char type = quote_char[quote_index[side][language]];
294 if (features.bufferParams().language->lang() == "default"
295 && lyxrc.fontenc != "T1") {
296 if (times == InsetQuotes::SingleQ)
298 case ',': features.quotesinglbase = true; break;
299 case '<': features.guilsinglleft = true; break;
300 case '>': features.guilsinglright = true; break;
305 case ',': features.quotedblbase = true; break;
306 case '<': features.guillemotleft = true; break;
307 case '>': features.guillemotright = true; break;
314 Inset * InsetQuotes::Clone(Buffer const &) const
316 return new InsetQuotes(language, side, times);
320 Inset::Code InsetQuotes::LyxCode() const
322 return Inset::QUOTE_CODE;