]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
This commit is a big rework of the FontLoader/FontMetrics interaction. Only Qt4 for...
[lyx.git] / src / insets / insetquotes.C
1 /**
2  * \file insetquotes.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetquotes.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "debug.h"
18 #include "language.h"
19 #include "LaTeXFeatures.h"
20 #include "lyxlex.h"
21 #include "lyxrc.h"
22 #include "metricsinfo.h"
23 #include "outputparams.h"
24 #include "paragraph.h"
25 #include "paragraph_funcs.h"
26
27 #include "frontends/Application.h"
28 #include "frontends/FontLoader.h"
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.h"
31
32 #include "support/lstrings.h"
33
34
35 using lyx::docstring;
36 using lyx::support::prefixIs;
37
38 using std::endl;
39 using std::string;
40 using std::auto_ptr;
41 using std::ostream;
42
43
44 namespace {
45
46 /* codes used to read/write quotes to LyX files
47  * e    ``english''
48  * s    ''spanish''
49  * g    ,,german``
50  * p    ,,polish''
51  * f    <<french>>
52  * a    >>danish<<
53  */
54
55 char const * const language_char = "esgpfa";
56 char const * const side_char = "lr" ;
57 char const * const times_char = "sd";
58
59 // List of known quote chars
60 char const * const quote_char = ",'`<>";
61
62 // Index of chars used for the quote. Index is [side, language]
63 int quote_index[2][6] = {
64         { 2, 1, 0, 0, 3, 4 },    // "'',,<>"
65         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
66
67 // Corresponding LaTeX code, for double and single quotes.
68 char const * const latex_quote_t1[2][5] =
69 { { "\\quotesinglbase ",  "'", "`",
70     "\\guilsinglleft{}", "\\guilsinglright{}" },
71   { ",,", "''", "``", "<<", ">>" } };
72
73 char const * const latex_quote_ot1[2][5] =
74 { { "\\quotesinglbase ",  "'", "`",
75     "\\guilsinglleft{}", "\\guilsinglright{}" },
76   { "\\quotedblbase ", "''", "``",
77     "\\guillemotleft{}", "\\guillemotright{}" } };
78
79 char const * const latex_quote_babel[2][5] =
80 { { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
81   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
82
83 } // namespace anon
84
85
86 InsetQuotes::InsetQuotes(string const & str)
87 {
88         parseString(str);
89 }
90
91
92 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
93         : language_(l), side_(s), times_(t)
94 {}
95
96
97 InsetQuotes::InsetQuotes(lyx::char_type c, BufferParams const & params)
98         : language_(params.quotes_language), times_(params.quotes_times)
99 {
100         getPosition(c);
101 }
102
103
104 InsetQuotes::InsetQuotes(lyx::char_type c, quote_language l, quote_times t)
105         : language_(l), times_(t)
106 {
107         getPosition(c);
108 }
109
110
111 void InsetQuotes::getPosition(lyx::char_type c)
112 {
113         // Decide whether left or right
114         switch (c) {
115         case ' ': case '(': case '[':
116                 side_ = LeftQ;   // left quote
117                 break;
118         default:
119                 side_ = RightQ;  // right quote
120         }
121 }
122
123
124 void InsetQuotes::parseString(string const & s)
125 {
126         string str(s);
127         if (str.length() != 3) {
128                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
129                         " bad string length." << endl;
130                 str = "eld";
131         }
132
133         int i;
134
135         for (i = 0; i < 6; ++i) {
136                 if (str[0] == language_char[i]) {
137                         language_ = quote_language(i);
138                         break;
139                 }
140         }
141         if (i >= 6) {
142                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
143                         " bad language specification." << endl;
144                 language_ = EnglishQ;
145         }
146
147         for (i = 0; i < 2; ++i) {
148                 if (str[1] == side_char[i]) {
149                         side_ = quote_side(i);
150                         break;
151                 }
152         }
153         if (i >= 2) {
154                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
155                         " bad side specification." << endl;
156                 side_ = LeftQ;
157         }
158
159         for (i = 0; i < 2; ++i) {
160                 if (str[2] == times_char[i]) {
161                         times_ = quote_times(i);
162                         break;
163                 }
164         }
165         if (i >= 2) {
166                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
167                         " bad times specification." << endl;
168                 times_ = DoubleQ;
169         }
170 }
171
172
173 string const InsetQuotes::dispString(Language const * loclang) const
174 {
175         string disp;
176         disp += quote_char[quote_index[side_][language_]];
177         if (times_ == DoubleQ)
178                 disp += disp;
179
180         if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
181             || lyxrc.font_norm_type == LyXRC::ISO_8859_9
182             || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
183                 if (disp == "<<")
184                         disp = '«';
185                 else if (disp == ">>")
186                         disp = '»';
187         }
188
189         // in french, spaces are added inside double quotes
190         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
191                 if (side_ == LeftQ)
192                         disp += ' ';
193                 else
194                         disp.insert(string::size_type(0), 1, ' ');
195         }
196
197         return disp;
198 }
199
200
201 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
202 {
203         LyXFont & font = mi.base.font;
204         lyx::frontend::FontMetrics const & fm =
205                 theApp->fontLoader().metrics(font);
206         dim.asc = fm.maxAscent();
207         dim.des = fm.maxDescent();
208         dim.wid = 0;
209
210         string const text = dispString(font.language());
211         for (string::size_type i = 0; i < text.length(); ++i) {
212                 if (text[i] == ' ')
213                         dim.wid += fm.width('i');
214                 else if (i == 0 || text[i] != text[i - 1])
215                         dim.wid += fm.width(text[i]);
216                 else
217                         dim.wid += fm.width(',');
218         }
219         dim_ = dim;
220 }
221
222
223 #if 0
224 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
225 {
226 #if 1
227         return f;
228 #else
229         LyXFont font(f);
230         return font;
231 #endif
232 }
233 #endif
234
235
236 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
237 {
238         string const text = dispString(pi.base.font.language());
239
240         if (text.length() == 2 && text[0] == text[1]) {
241                 pi.pain.text(x, y, text[0], pi.base.font);
242                 int const t = theApp->fontLoader().metrics(pi.base.font)
243                         .width(',');
244                 pi.pain.text(x + t, y, text[0], pi.base.font);
245         } else {
246                 docstring dtext(text.begin(), text.end());
247                 pi.pain.text(x, y, dtext, pi.base.font);
248         }
249 }
250
251
252 void InsetQuotes::write(Buffer const &, ostream & os) const
253 {
254         string text;
255         text += language_char[language_];
256         text += side_char[side_];
257         text += times_char[times_];
258         os << "Quotes " << text;
259 }
260
261
262 void InsetQuotes::read(Buffer const &, LyXLex & lex)
263 {
264         lex.next();
265         parseString(lex.getString());
266         lex.next();
267         if (lex.getString() != "\\end_inset") {
268                 lex.printError("Missing \\end_inset at this point");
269         }
270 }
271
272
273 int InsetQuotes::latex(Buffer const &, ostream & os,
274                        OutputParams const & runparams) const
275 {
276         const int quoteind = quote_index[side_][language_];
277         string qstr;
278
279         if (language_ == FrenchQ && times_ == DoubleQ
280             && prefixIs(runparams.local_font->language()->code(), "fr")) {
281                 if (side_ == LeftQ)
282                         qstr = "\\og "; //the spaces are important here
283                 else
284                         qstr = " \\fg{}"; //and here
285         } else if (lyxrc.fontenc == "T1") {
286                 qstr = latex_quote_t1[times_][quoteind];
287 #ifdef DO_USE_DEFAULT_LANGUAGE
288         } else if (doclang == "default") {
289 #else
290         } else if (!runparams.use_babel) {
291 #endif
292                 qstr = latex_quote_ot1[times_][quoteind];
293         } else {
294                 qstr = latex_quote_babel[times_][quoteind];
295         }
296
297         // Always guard against unfortunate ligatures (!` ?`)
298         if (prefixIs(qstr, "`"))
299                 qstr.insert(0, "{}");
300
301         os << qstr;
302         return 0;
303 }
304
305
306 int InsetQuotes::plaintext(Buffer const &, ostream & os,
307                        OutputParams const &) const
308 {
309         os << '"';
310         return 0;
311 }
312
313
314 int InsetQuotes::docbook(Buffer const &, ostream & os,
315                          OutputParams const &) const
316 {
317         if (times_ == DoubleQ) {
318                 if (side_ == LeftQ)
319                         os << "&ldquo;";
320                 else
321                         os << "&rdquo;";
322         } else {
323                 if (side_ == LeftQ)
324                         os << "&lsquo;";
325                 else
326                         os << "&rsquo;";
327         }
328         return 0;
329 }
330
331
332 int InsetQuotes::textString(Buffer const & buf, ostream & os,
333                        OutputParams const & op) const
334 {
335         return plaintext(buf, os, op);
336 }
337
338
339 void InsetQuotes::validate(LaTeXFeatures & features) const
340 {
341         bool const use_babel = features.useBabel();
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 auto_ptr<InsetBase> InsetQuotes::doClone() const
369 {
370         return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
371 }
372
373
374 InsetBase::Code InsetQuotes::lyxCode() const
375 {
376   return InsetBase::QUOTE_CODE;
377 }