]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
layout as string
[lyx.git] / src / insets / insetquotes.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "insetquotes.h"
18
19 #include "support/LAssert.h"
20 #include "support/lstrings.h"
21 #include "BufferView.h"
22 #include "LaTeXFeatures.h"
23 #include "Painter.h"
24 #include "buffer.h"
25 #include "debug.h"
26 #include "font.h"
27 #include "language.h"
28 #include "lyxfont.h"
29 #include "lyxrc.h"
30 #include "paragraph.h"
31
32 using std::ostream;
33 using std::endl;
34
35 // Quotes. Used for the various quotes. German, English, French,
36 // Danish, Polish, all either double or single.
37
38 namespace {
39
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";
44
45 // List of known quote chars
46 char const * const quote_char = ",'`<>";
47
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 } };  // "`'`'><"
52
53 // Corresponding LaTeX code, for double and single quotes.
54 char const * const latex_quote_t1[2][5] = 
55 { { "\\quotesinglbase{}",  "'", "`", 
56     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
57   { ",,", "''", "``", "<<", ">>" } };
58
59 char const * const latex_quote_ot1[2][5] = 
60 { { "\\quotesinglbase{}",  "'", "`", 
61     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
62   { "\\quotedblbase{}", "''", "``",
63     "\\guillemotleft{}", "\\guillemotright{}" } };
64
65 char const * const latex_quote_babel[2][5] = 
66 { { "\\glq{}",  "'", "`", "\\flq{}", "\\frq{}" },
67   { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
68
69 } // namespace anon
70
71
72 InsetQuotes::InsetQuotes(string const & str)
73 {
74         parseString(str);
75 }
76
77
78 InsetQuotes::InsetQuotes(quote_language l,
79                          quote_side s,
80                          quote_times t)
81         : language_(l), side_(s), times_(t)
82 {}
83
84
85 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
86         : language_(params.quotes_language), times_(params.quotes_times)
87 {
88         // Decide whether left or right 
89         switch (c) {
90         case ' ': case '(': 
91         case Paragraph::META_HFILL:
92         case Paragraph::META_NEWLINE: 
93                 side_ = LeftQ;   // left quote 
94                 break;
95         default:
96                 side_ = RightQ;  // right quote
97         }
98 }
99
100
101 void InsetQuotes::parseString(string const & s)
102 {
103         string str(s);
104         if (str.length() != 3) {
105                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
106                         " bad string length." << endl;
107                 str = "eld";
108         }
109
110         int i;
111         
112         for (i = 0; i < 6; ++i) {
113                 if (str[0] == language_char[i]) {
114                         language_ = quote_language(i);
115                         break;
116                 }
117         }
118         if (i >= 6) {
119                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
120                         " bad language specification." << endl;
121                 language_ = EnglishQ; 
122         }
123
124         for (i = 0; i < 2; ++i) {
125                 if (str[1] == side_char[i]) {
126                         side_ = quote_side(i);
127                         break;
128                 }
129         }
130         if (i >= 2) {
131                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
132                         " bad side specification." << endl;
133                 side_ = LeftQ; 
134         }
135
136         for (i = 0; i < 2; ++i) {
137                 if (str[2] == times_char[i]) {
138                         times_ = quote_times(i);
139                         break;
140                 }
141         }
142         if (i >= 2) {
143                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
144                         " bad times specification." << endl;
145                 times_ = DoubleQ; 
146         }
147 }
148
149
150 string const InsetQuotes::dispString(Language const * loclang) const
151 {
152         string disp;
153         disp += quote_char[quote_index[side_][language_]];
154         if (times_ == DoubleQ)
155                 disp += disp;
156
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) {
161                 if (disp == "'")
162                         disp = "´";
163                 else if (disp == "''")
164                         disp = "´´";
165         }
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) {
169                 if (disp == "<<")
170                         disp = '«';
171                 else if (disp == ">>")
172                         disp = '»';
173         }
174
175         // in french, spaces are added inside double quotes
176         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
177                 if (side_ == LeftQ)
178                         disp += " ";
179                 else
180                         disp = " " + disp;
181         }
182         
183         return disp;
184 }
185
186
187 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
188 {
189         return lyxfont::maxAscent(font);
190 }
191
192
193 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
194 {
195         return lyxfont::maxDescent(font);
196 }
197
198
199 int InsetQuotes::width(BufferView *, LyXFont const & font) const
200 {
201         string const text = dispString(font.language());
202         int w = 0;
203
204         for (string::size_type i = 0; i < text.length(); ++i) {
205                 if (text[i] == ' ')
206                         w += lyxfont::width('i', font);
207                 else if (i == 0 || text[i] != text[i-1])
208                         w += lyxfont::width(text[i], font);
209                 else
210                         w += lyxfont::width(',', font);
211         }
212
213         return w;
214 }
215
216
217 #if 0
218 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
219 {
220 #if 1
221         return f;
222 #else
223         LyXFont font(f);
224         return font;
225 #endif
226 }
227 #endif
228
229
230 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
231                        int baseline, float & x, bool) const
232 {
233         string const text = dispString(font.language());
234
235         bv->painter().text(int(x), baseline, text, font);
236         x += width(bv, font);
237 }
238
239
240 void InsetQuotes::write(Buffer const *, ostream & os) const
241 {
242         string text;
243         text += language_char[language_];
244         text += side_char[side_];
245         text += times_char[times_]; 
246         os << "Quotes " << text;
247 }
248
249
250 void InsetQuotes::read(Buffer const *, LyXLex & lex)
251 {
252         lex.nextToken();
253         parseString(lex.getString());
254         lex.next();
255         if (lex.getString() != "\\end_inset") {
256                 lex.printError("Missing \\end_inset at this point");
257         }
258 }
259
260
261 extern bool use_babel;
262
263 int InsetQuotes::latex(Buffer const * buf, ostream & os,
264                        bool /*fragile*/, bool /* free_spc */) const
265 {
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();
272
273         const int quoteind = quote_index[side_][language_];
274         string qstr;
275         
276         if (language_ == FrenchQ && times_ == DoubleQ
277             && curr_lang == "frenchb") {
278                 if (side_ == LeftQ) 
279                         qstr = "\\og "; //the spaces are important here
280                 else 
281                         qstr = " \\fg{}"; //and here
282         } else if (language_ == FrenchQ && times_ == DoubleQ
283                    && curr_lang == "french") {
284                 if (side_ == LeftQ) 
285                         qstr = "<< "; //the spaces are important here
286                 else 
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") {
292 #else
293         } else if (!use_babel) {
294 #endif
295                 qstr = latex_quote_ot1[times_][quoteind];
296         } else { 
297                 qstr = latex_quote_babel[times_][quoteind];
298         }
299
300         // Always guard against unfortunate ligatures (!` ?`)
301         if (prefixIs(qstr, "`"))
302                 qstr.insert(0, "{}");
303
304         os << qstr;
305         return 0;
306 }
307
308
309 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
310 {
311         os << "\"";
312         return 0;
313 }
314
315
316 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
317 {
318         os << "\"";
319         return 0;
320 }
321
322
323 int InsetQuotes::docbook(Buffer const *, ostream & os) const
324 {
325         if (times_ == DoubleQ) {
326                 if (side_ == LeftQ)
327                         os << "&ldquo;";
328                 else
329                         os << "&rdquo;";
330         } else {
331                 if (side_ == LeftQ)
332                         os << "&lsquo;";
333                 else
334                         os << "&rsquo;";
335         }
336         return 0;
337 }
338
339
340 void InsetQuotes::validate(LaTeXFeatures & features) const 
341 {
342         char type = quote_char[quote_index[side_][language_]];
343
344 #ifdef DO_USE_DEFAULT_LANGUAGE
345         if (features.bufferParams().language->lang() == "default" 
346 #else
347         if (!use_babel
348 #endif
349             && lyxrc.fontenc != "T1") {
350                 if (times_ == SingleQ) 
351                         switch (type) {
352                         case ',': features.require("quotesinglbase");  break;
353                         case '<': features.require("guilsinglleft");  break;
354                         case '>': features.require("guilsinglright"); break;
355                         default: break;
356                         }
357                 else 
358                         switch (type) {
359                         case ',': features.require("quotedblbase");   break;
360                         case '<': features.require("guillemotleft");  break;
361                         case '>': features.require("guillemotright"); break;
362                         default: break;
363                         }
364         }
365 }
366
367
368 Inset * InsetQuotes::clone(Buffer const &, bool) const
369 {
370   return new InsetQuotes(language_, side_, times_);
371 }
372
373
374 Inset::Code InsetQuotes::lyxCode() const
375 {
376   return Inset::QUOTE_CODE;
377 }