]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
* src/insets/InsetTabular.h:
[lyx.git] / src / insets / InsetQuotes.cpp
1 /**
2  * \file InsetQuotes.cpp
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 "BufferView.h"
18 #include "Dimension.h"
19 #include "Language.h"
20 #include "LaTeXFeatures.h"
21 #include "Lexer.h"
22 #include "LyXRC.h"
23 #include "MetricsInfo.h"
24 #include "OutputParams.h"
25
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
28
29 #include "support/debug.h"
30 #include "support/docstring.h"
31 #include "support/docstream.h"
32 #include "support/lstrings.h"
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38
39 namespace {
40
41 /* codes used to read/write quotes to LyX files
42  * e    ``english''
43  * s    ''spanish''
44  * g    ,,german``
45  * p    ,,polish''
46  * f    <<french>>
47  * a    >>danish<<
48  */
49
50 char const * const language_char = "esgpfa";
51 char const * const side_char = "lr" ;
52 char const * const times_char = "sd";
53
54 // List of known quote chars
55 char const * const quote_char = ",'`<>";
56
57 // Unicode characters needed by each quote type
58 char_type const display_quote_char[2][5] = {
59         { 0x201a, 0x2019, 0x2018, 0x2039, 0x203a},
60         { 0x201e, 0x201d, 0x201c, 0x00ab, 0x00bb}
61 };
62
63 // Index of chars used for the quote. Index is [side, language]
64 int quote_index[2][6] = {
65         { 2, 1, 0, 0, 3, 4 },    // "'',,<>"
66         { 1, 1, 2, 1, 4, 3 }     // "`'`'><"
67 };
68
69 // Corresponding LaTeX code, for double and single quotes.
70 char const * const latex_quote_t1[2][5] = {
71         { "\\quotesinglbase ",  "'", "`",
72     "\\guilsinglleft{}", "\\guilsinglright{}" },
73   { ",,", "''", "``", "<<", ">>" }
74 };
75
76 char const * const latex_quote_ot1[2][5] = {
77         { "\\quotesinglbase ",  "'", "`",
78     "\\guilsinglleft{}", "\\guilsinglright{}" },
79   { "\\quotedblbase ", "''", "``",
80     "\\guillemotleft{}", "\\guillemotright{}" }
81 };
82
83 char const * const latex_quote_babel[2][5] = {
84         { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
85   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" }
86 };
87
88 } // namespace anon
89
90
91 InsetQuotes::InsetQuotes(string const & str)
92 {
93         parseString(str);
94 }
95
96
97 InsetQuotes::InsetQuotes(QuoteLanguage l, QuoteSide s, QuoteTimes t)
98         : language_(l), side_(s), times_(t)
99 {
100 }
101
102
103 InsetQuotes::InsetQuotes(Buffer const & buf, char_type c)
104         : language_(buf.params().quotes_language), times_(buf.params().quotes_times)
105 {
106         setSide(c);
107 }
108
109
110 InsetQuotes::InsetQuotes(char_type c, QuoteLanguage l, QuoteTimes t)
111         : language_(l), times_(t)
112 {
113         setSide(c);
114 }
115
116
117 docstring InsetQuotes::name() const
118 {
119         return from_ascii("Quotes");
120 }
121
122
123 void InsetQuotes::setSide(char_type c)
124 {
125         // Decide whether left or right
126         switch (c) {
127         case ' ':
128         case '(':
129         case '[':
130                 side_ = LeftQuote;   // left quote
131                 break;
132         default:
133                 side_ = RightQuote;  // right quote
134         }
135 }
136
137
138 void InsetQuotes::parseString(string const & s)
139 {
140         string str = s;
141         if (str.length() != 3) {
142                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
143                         " bad string length." << endl;
144                 str = "eld";
145         }
146
147         int i;
148
149         for (i = 0; i < 6; ++i) {
150                 if (str[0] == language_char[i]) {
151                         language_ = QuoteLanguage(i);
152                         break;
153                 }
154         }
155         if (i >= 6) {
156                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
157                         " bad language specification." << endl;
158                 language_ = EnglishQuotes;
159         }
160
161         for (i = 0; i < 2; ++i) {
162                 if (str[1] == side_char[i]) {
163                         side_ = QuoteSide(i);
164                         break;
165                 }
166         }
167         if (i >= 2) {
168                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
169                         " bad side specification." << endl;
170                 side_ = LeftQuote;
171         }
172
173         for (i = 0; i < 2; ++i) {
174                 if (str[2] == times_char[i]) {
175                         times_ = QuoteTimes(i);
176                         break;
177                 }
178         }
179         if (i >= 2) {
180                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
181                         " bad times specification." << endl;
182                 times_ = DoubleQuotes;
183         }
184 }
185
186
187 docstring InsetQuotes::displayString() const
188 {
189         Language const * loclang = buffer().params().language;
190         int const index = quote_index[side_][language_];
191         docstring retdisp = docstring(1, display_quote_char[times_][index]);
192
193         // in french, spaces are added inside double quotes
194         if (times_ == DoubleQuotes && prefixIs(loclang->code(), "fr")) {
195                 if (side_ == LeftQuote)
196                         retdisp += ' ';
197                 else
198                         retdisp.insert(size_t(0), 1, ' ');
199         }
200
201         return retdisp;
202 }
203
204
205 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
206 {
207         FontInfo & font = mi.base.font;
208         frontend::FontMetrics const & fm =
209                 theFontMetrics(font);
210         dim.asc = fm.maxAscent();
211         dim.des = fm.maxDescent();
212         dim.wid = 0;
213
214         // FIXME: should we add a language or a font parameter member?
215         docstring const text = displayString();
216         for (string::size_type i = 0; i < text.length(); ++i) {
217                 if (text[i] == ' ')
218                         dim.wid += fm.width('i');
219                 else if (i == 0 || text[i] != text[i - 1])
220                         dim.wid += fm.width(text[i]);
221                 else
222                         dim.wid += fm.width(',');
223         }
224 }
225
226
227 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
228 {
229         // FIXME: should we add a language or a font parameter member?
230         docstring const text = displayString();
231
232         if (text.length() == 2 && text[0] == text[1]) {
233                 pi.pain.text(x, y, text[0], pi.base.font);
234                 int const t = theFontMetrics(pi.base.font)
235                         .width(',');
236                 pi.pain.text(x + t, y, text[0], pi.base.font);
237         } else {
238                 pi.pain.text(x, y, text, pi.base.font);
239         }
240 }
241
242
243 void InsetQuotes::write(ostream & os) const
244 {
245         string text;
246         text += language_char[language_];
247         text += side_char[side_];
248         text += times_char[times_];
249         os << "Quotes " << text;
250 }
251
252
253 void InsetQuotes::read(Lexer & lex)
254 {
255         lex.setContext("InsetQuotes::read");
256         lex.next();
257         parseString(lex.getString());
258         lex >> "\\end_inset";
259 }
260
261
262 int InsetQuotes::latex(odocstream & os, OutputParams const & runparams) const
263 {
264         const int quoteind = quote_index[side_][language_];
265         string qstr;
266
267         if (language_ == FrenchQuotes && times_ == DoubleQuotes
268             && prefixIs(runparams.local_font->language()->code(), "fr")) {
269                 if (side_ == LeftQuote)
270                         qstr = "\\og "; //the spaces are important here
271                 else
272                         qstr = " \\fg{}"; //and here
273         } else if (lyxrc.fontenc == "T1") {
274                 qstr = latex_quote_t1[times_][quoteind];
275 #ifdef DO_USE_DEFAULT_LANGUAGE
276         } else if (doclang == "default") {
277 #else
278         } else if (!runparams.use_babel) {
279 #endif
280                 qstr = latex_quote_ot1[times_][quoteind];
281         } else {
282                 qstr = latex_quote_babel[times_][quoteind];
283         }
284
285         // Always guard against unfortunate ligatures (!` ?`)
286         if (prefixIs(qstr, "`"))
287                 qstr.insert(0, "{}");
288
289         os << from_ascii(qstr);
290         return 0;
291 }
292
293
294 int InsetQuotes::plaintext(odocstream & os, OutputParams const &) const
295 {
296         docstring const str = displayString();
297         os << str;
298         return str.size();
299 }
300
301
302 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
303 {
304         if (times_ == DoubleQuotes) {
305                 if (side_ == LeftQuote)
306                         os << "&ldquo;";
307                 else
308                         os << "&rdquo;";
309         } else {
310                 if (side_ == LeftQuote)
311                         os << "&lsquo;";
312                 else
313                         os << "&rsquo;";
314         }
315         return 0;
316 }
317
318
319 void InsetQuotes::textString(odocstream & os) const
320 {
321         os << displayString();
322 }
323
324
325 void InsetQuotes::validate(LaTeXFeatures & features) const
326 {
327         bool const use_babel = features.useBabel();
328         char type = quote_char[quote_index[side_][language_]];
329
330 #ifdef DO_USE_DEFAULT_LANGUAGE
331         if (features.bufferParams().language->lang() == "default"
332 #else
333         if (!use_babel
334 #endif
335             && lyxrc.fontenc != "T1") {
336                 if (times_ == SingleQuotes)
337                         switch (type) {
338                         case ',': features.require("quotesinglbase"); break;
339                         case '<': features.require("guilsinglleft");  break;
340                         case '>': features.require("guilsinglright"); break;
341                         default: break;
342                         }
343                 else
344                         switch (type) {
345                         case ',': features.require("quotedblbase");   break;
346                         case '<': features.require("guillemotleft");  break;
347                         case '>': features.require("guillemotright"); break;
348                         default: break;
349                         }
350         }
351 }
352
353 } // namespace lyx