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"
32 // Quotes. Used for the various quotes. German, English, French,
33 // Danish, Polish, all either double or single.
37 // codes used to read/write quotes to LyX files
38 char const * const language_char = "esgpfa";
39 char const * const side_char = "lr" ;
40 char const * const times_char = "sd";
42 // List of known quote chars
43 char const * const quote_char = ",'`<>";
45 // Index of chars used for the quote. Index is [side, language]
46 int quote_index[2][6] = {
47 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
48 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
50 // Corresponding LaTeX code, for double and single quotes.
51 char const * const latex_quote_t1[2][5] =
52 { { "\\quotesinglbase{}", "'", "`",
53 "\\guilsinglleft{}", "\\guilsinglright{}" },
54 { ",,", "''", "``", "<<", ">>" } };
56 char const * const latex_quote_ot1[2][5] =
57 { { "\\quotesinglbase{}", "'", "`",
58 "\\guilsinglleft{}", "\\guilsinglright{}" },
59 { "\\quotedblbase{}", "''", "``",
60 "\\guillemotleft{}", "\\guillemotright{}" } };
62 char const * const latex_quote_babel[2][5] =
63 { { "\\glq{}", "'", "`", "\\flq{}", "\\frq{}" },
64 { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
69 InsetQuotes::InsetQuotes(string const & str)
75 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
76 InsetQuotes::quote_side s,
77 InsetQuotes::quote_times t)
78 : language(l), side(s), times(t)
82 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
83 : language(params.quotes_language), times(params.quotes_times)
85 // Decide whether left or right
87 case ' ': case '(': case '{': case '[': case '-': case ':':
88 case LyXParagraph::META_HFILL:
89 case LyXParagraph::META_NEWLINE:
90 side = InsetQuotes::LeftQ; // left quote
93 side = InsetQuotes::RightQ; // right quote
98 void InsetQuotes::ParseString(string const & s)
101 if (str.length() != 3) {
102 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
103 " bad string length." << endl;
109 for (i = 0; i < 6; ++i) {
110 if (str[0] == language_char[i]) {
111 language = InsetQuotes::quote_language(i);
116 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
117 " bad language specification." << endl;
118 language = InsetQuotes::EnglishQ;
121 for (i = 0; i < 2; ++i) {
122 if (str[1] == side_char[i]) {
123 side = InsetQuotes::quote_side(i);
128 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
129 " bad side specification." << endl;
130 side = InsetQuotes::LeftQ;
133 for (i = 0; i < 2; ++i) {
134 if (str[2] == times_char[i]) {
135 times = InsetQuotes::quote_times(i);
140 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
141 " bad times specification." << endl;
142 times = InsetQuotes::DoubleQ;
147 string const InsetQuotes::DispString() const
150 disp += quote_char[quote_index[side][language]];
151 if (times == InsetQuotes::DoubleQ)
154 if (lyxrc.font_norm_type == LyXRC::ISO_8859_1)
157 else if (disp == ">>")
164 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
166 return lyxfont::maxAscent(font);
170 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
172 return lyxfont::maxDescent(font);
176 int InsetQuotes::width(BufferView *, LyXFont const & font) const
178 string text = DispString();
181 for (string::size_type i = 0; i < text.length(); ++i) {
183 w += lyxfont::width('i', font);
184 else if (i == 0 || text[i] != text[i-1])
185 w += lyxfont::width(text[i], font);
187 w += lyxfont::width(',', font);
194 LyXFont const InsetQuotes::ConvertFont(LyXFont const & f) const
197 // quotes-insets cannot be latex of any kind
198 font.setLatex(LyXFont::OFF);
203 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
204 int baseline, float & x, bool) const
206 string text = DispString();
208 bv->painter().text(int(x), baseline, text, font);
209 x += width(bv, font);
213 void InsetQuotes::Write(Buffer const *, ostream & os) const
216 text += language_char[language];
217 text += side_char[side];
218 text += times_char[times];
219 os << "Quotes " << text;
223 void InsetQuotes::Read(Buffer const *, LyXLex & lex)
226 ParseString(lex.GetString());
228 string tmp(lex.GetString());
229 if (tmp != "\\end_inset")
230 lyxerr << "LyX Warning: Missing \\end_inset "
231 "in InsetQuotes::Read." << endl;
235 extern bool use_babel;
237 int InsetQuotes::Latex(Buffer const * buf, ostream & os,
238 bool /*fragile*/, bool) const
240 string const doclang = buf->GetLanguage()->lang();
241 int quoteind = quote_index[side][language];
244 if (lyxrc.fontenc == "T1") {
245 qstr = latex_quote_t1[times][quoteind];
246 #ifdef DO_USE_DEFAULT_LANGUAGE
247 } else if (doclang == "default") {
249 } else if (!use_babel) {
251 qstr = latex_quote_ot1[times][quoteind];
252 } else if (language == InsetQuotes::FrenchQ
253 && times == InsetQuotes::DoubleQ
254 && doclang == "frenchb") {
255 if (side == InsetQuotes::LeftQ)
260 qstr = latex_quote_babel[times][quoteind];
262 // Always guard against unfortunate ligatures (!` ?`)
263 if (prefixIs(qstr, "`"))
264 qstr.insert(0, "{}");
271 int InsetQuotes::Ascii(Buffer const *, ostream & os, int) const
278 int InsetQuotes::Linuxdoc(Buffer const *, ostream & os) const
285 int InsetQuotes::DocBook(Buffer const *, ostream & os) const
287 if (times == InsetQuotes::DoubleQ) {
288 if (side == InsetQuotes::LeftQ)
293 if (side == InsetQuotes::LeftQ)
302 void InsetQuotes::Validate(LaTeXFeatures & features) const
304 char type = quote_char[quote_index[side][language]];
306 if (features.bufferParams().language->lang() == "default"
307 && lyxrc.fontenc != "T1") {
308 if (times == InsetQuotes::SingleQ)
310 case ',': features.quotesinglbase = true; break;
311 case '<': features.guilsinglleft = true; break;
312 case '>': features.guilsinglright = true; break;
317 case ',': features.quotedblbase = true; break;
318 case '<': features.guillemotleft = true; break;
319 case '>': features.guillemotright = true; break;
326 Inset * InsetQuotes::Clone(Buffer const &) const
328 return new InsetQuotes(language, side, times);
332 Inset::Code InsetQuotes::LyxCode() const
334 return Inset::QUOTE_CODE;