]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
Fix #10583 - plain quote inset latex export in different font encodings.
[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  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetQuotes.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "Dimension.h"
21 #include "Encoding.h"
22 #include "Font.h"
23 #include "FuncStatus.h"
24 #include "FuncRequest.h"
25 #include "Language.h"
26 #include "LaTeXFeatures.h"
27 #include "Lexer.h"
28 #include "LyXRC.h"
29 #include "MetricsInfo.h"
30 #include "OutputParams.h"
31 #include "output_xhtml.h"
32 #include "Paragraph.h"
33 #include "ParIterator.h"
34 #include "texstream.h"
35
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
38
39 #include "support/debug.h"
40 #include "support/docstring.h"
41 #include "support/docstream.h"
42 #include "support/gettext.h"
43 #include "support/lstrings.h"
44 #include "support/textutils.h"
45
46 #include <string.h>
47
48 using namespace std;
49 using namespace lyx::support;
50
51 namespace lyx {
52
53 namespace {
54
55 /* codes used to read/write quotes to LyX files
56  * available styles:
57  * e    ``english''  (`inner quotation')
58  * s    ''swedish''  ('inner quotation')
59  * g    ,,german``   (,inner quotation`)
60  * p    ,,polish''   (,inner quotation')
61  * c    <<swiss>>    (<inner quotation>)
62  * a    >>danish<<   (>inner quotation<)
63  * q    "plain"      ('inner quotation')
64  * b    `british'    (``inner quotation'')
65  * w    >>swedishg>> ('inner quotation') ["g" = Guillemets]
66  * f    <<french>>   (``inner quotation'')
67  * i    <<frenchin>> (<<inner quotation>>) ["in" = Imprimerie Nationale]
68  * r    <<russian>>  (,,inner quotation``)
69  * j    [U+300C]cjk[U+300D]  ([U+300E]inner quotation[U+300F]) [CORNER BRACKETS]
70  * k    [U+300A]cjkangle[U+300B]  ([U+3008]inner quotation[U+3009]) [ANGLE BRACKETS]
71  * x    dynamic style (inherits document settings)
72  */
73
74 char const * const style_char = "esgpcaqbwfirjkx";
75 char const * const side_char = "lr" ;
76 char const * const level_char = "sd";
77
78 } // namespace anon
79
80
81 /////////////////////////////////////////////////////////////////////
82 //
83 // InsetQuotesParams
84 //
85 ///////////////////////////////////////////////////////////////////////
86
87 InsetQuotesParams quoteparams;
88
89
90 int InsetQuotesParams::stylescount() const
91 {
92         return strlen(style_char);
93 }
94
95
96 char InsetQuotesParams::getStyleChar(QuoteStyle const & style) const
97 {
98         return style_char[style];
99 }
100
101
102 InsetQuotesParams::QuoteStyle InsetQuotesParams::getQuoteStyle(string const & s,
103                             bool const allow_wildcards, QuoteStyle fb)
104 {
105         QuoteStyle res = fb;
106
107         string str = s;
108         if (str.length() != 3) {
109                 LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
110                         " bad string length.");
111                 str = "eld";
112         }
113
114         int i;
115
116         // '.' wildcard means: keep current style
117         if (!allow_wildcards || str[0] != '.') {
118                 for (i = 0; i < stylescount(); ++i) {
119                         if (str[0] == style_char[i]) {
120                                 res = QuoteStyle(i);
121                                 break;
122                         }
123                 }
124                 if (i >= stylescount()) {
125                         LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
126                                 " bad style specification.");
127                         res = EnglishQuotes;
128                 }
129         }
130
131         return res;
132 }
133
134
135 InsetQuotesParams::QuoteSide InsetQuotesParams::getQuoteSide(string const & s,
136                         bool const allow_wildcards, QuoteSide fb)
137 {
138         QuoteSide res = fb;
139
140         string str = s;
141         if (str.length() != 3) {
142                 LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
143                         " bad string length.");
144                 str = "eld";
145         }
146
147         int i;
148
149         // '.' wildcard means: keep current side
150         if (!allow_wildcards || str[1] != '.') {
151                 for (i = 0; i < 2; ++i) {
152                         if (str[1] == side_char[i]) {
153                                 res = InsetQuotesParams::QuoteSide(i);
154                                 break;
155                         }
156                 }
157                 if (i >= 2) {
158                         LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
159                                 " bad side specification.");
160                         res = OpeningQuote;
161                 }
162         }
163
164         return res;
165 }
166
167
168 InsetQuotesParams::QuoteLevel InsetQuotesParams::getQuoteLevel(string const & s,
169                         bool const allow_wildcards, QuoteLevel fb)
170 {
171         QuoteLevel res = fb;
172
173         string str = s;
174         if (str.length() != 3) {
175                 LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
176                         " bad string length.");
177                 str = "eld";
178         }
179
180         int i;
181
182         // '.' wildcard means: keep current level
183         if (!allow_wildcards || str[2] != '.') {
184                 for (i = 0; i < 2; ++i) {
185                         if (str[2] == level_char[i]) {
186                                 res = InsetQuotesParams::QuoteLevel(i);
187                                 break;
188                         }
189                 }
190                 if (i >= 2) {
191                         LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
192                                 " bad level specification.");
193                         res = InsetQuotesParams::PrimaryQuotes;
194                 }
195         }
196
197         return res;
198 }
199
200
201 char_type InsetQuotesParams::getQuoteChar(QuoteStyle const & style, QuoteLevel const & level,
202                                     QuoteSide const & side) const
203 {
204         // main opening quotation mark
205         char_type left_primary;
206         // main closing quotation mark
207         char_type right_primary;
208         // secondary (inner, 'single') opening quotation mark
209         char_type left_secondary;
210         // secondary (inner, 'single') closing quotation mark
211         char_type right_secondary;
212
213         switch (style) {
214         case EnglishQuotes: {
215                 left_primary = 0x201c; // ``
216                 right_primary = 0x201d; // ''
217                 left_secondary = 0x2018; // `
218                 right_secondary = 0x2019; // '
219                 break;
220         }
221         case SwedishQuotes: {
222                 left_primary = 0x201d; // ''
223                 right_primary = 0x201d; // ''
224                 left_secondary = 0x2019; // '
225                 right_secondary = 0x2019; // '
226                 break;
227         }
228         case GermanQuotes: {
229                 left_primary = 0x201e; // ,,
230                 right_primary = 0x201c; // ``
231                 left_secondary = 0x201a; // ,
232                 right_secondary = 0x2018; // `
233                 break;
234         }
235         case PolishQuotes: {
236                 left_primary =  0x201e; // ,,
237                 right_primary = 0x201d; // ''
238                 left_secondary = 0x201a; // ,
239                 right_secondary = 0x2019; // '
240                 break;
241         }
242         case SwissQuotes: {
243                 left_primary = 0x00ab; // <<
244                 right_primary = 0x00bb; // >>
245                 left_secondary = 0x2039; // <
246                 right_secondary = 0x203a; // >
247                 break;
248         }
249         case DanishQuotes: {
250                 left_primary = 0x00bb; // >>
251                 right_primary = 0x00ab; // <<
252                 left_secondary = 0x203a; // >
253                 right_secondary = 0x2039; // <
254                 break;
255         }
256         case PlainQuotes: {
257                 left_primary = 0x0022; // "
258                 right_primary = 0x0022; // "
259                 left_secondary = 0x0027; // '
260                 right_secondary = 0x0027; // '
261                 break;
262         }
263         case BritishQuotes: {
264                 left_primary = 0x2018; // `
265                 right_primary = 0x2019; // '
266                 left_secondary = 0x201c; // ``
267                 right_secondary = 0x201d; // ''
268                 break;
269         }
270         case SwedishGQuotes: {
271                 left_primary = 0x00bb; // >>
272                 right_primary = 0x00bb; // >>
273                 left_secondary = 0x2019; // '
274                 right_secondary = 0x2019; // '
275                 break;
276         }
277         case FrenchQuotes: {
278                 left_primary = 0x00ab; // <<
279                 right_primary = 0x00bb; // >>
280                 left_secondary = 0x201c; // ``
281                 right_secondary = 0x201d; // ''
282                 break;
283         }
284         case FrenchINQuotes:{
285                 left_primary = 0x00ab; // <<
286                 right_primary = 0x00bb; // >>
287                 left_secondary =  0x00ab; // <<
288                 right_secondary = 0x00bb; // >>
289                 break;
290         }
291         case RussianQuotes:{
292                 left_primary = 0x00ab; // <<
293                 right_primary = 0x00bb; // >>
294                 left_secondary =  0x201e; // ,,
295                 right_secondary = 0x201c; // ``
296                 break;
297         }
298         case CJKQuotes:{
299                 left_primary = 0x300c; // LEFT CORNER BRACKET
300                 right_primary = 0x300d; // RIGHT CORNER BRACKET
301                 left_secondary =  0x300e; // LEFT WHITE CORNER BRACKET
302                 right_secondary = 0x300f; // RIGHT WHITE CORNER BRACKET
303                 break;
304         }
305         case CJKAngleQuotes:{
306                 left_primary = 0x300a; // LEFT DOUBLE ANGLE BRACKET
307                 right_primary = 0x300b; // RIGHT DOUBLE ANGLE BRACKET
308                 left_secondary =  0x3008; // LEFT ANGLE BRACKET
309                 right_secondary = 0x3009; // RIGHT ANGLE BRACKET
310                 break;
311         }
312         case DynamicQuotes:
313         default:
314                 // should not happen
315                 left_primary = 0x003f; // ?
316                 right_primary = 0x003f; // ?
317                 left_secondary =  0x003f; // ?
318                 right_secondary = 0x003f; // ?
319                 break;
320         }
321
322         switch (level) {
323         case SecondaryQuotes:
324                 return (side == OpeningQuote) ? left_secondary : right_secondary;
325         case PrimaryQuotes:
326                 return (side == OpeningQuote) ? left_primary : right_primary;
327         default:
328                 break;
329         }
330
331         // should not happen
332         return 0x003f;
333 }
334
335
336 docstring InsetQuotesParams::getLaTeXQuote(char_type c, string const & op) const
337 {
338         string res;
339
340         switch (c){
341         case 0x201a: {// ,
342                 if (op == "babel")
343                         res = "\\glq";
344                 else
345                         res = "\\quotesinglbase";
346                 break;
347         }
348         case 0x2019: {// '
349                 if (op == "int")
350                         res = "\\textquoteleft";
351                 else
352                         res = "'";
353                 break;
354         }
355         case 0x2018: {// `
356                 if (op == "int")
357                         res = "\\textquoteright";
358                 else
359                         res = "`";
360                 break;
361         }
362         case 0x2039: {// <
363                 if (op == "babel")
364                         res = "\\flq";
365                 else
366                         res = "\\guilsinglleft";
367                 break;
368         }
369         case 0x203a: {// >
370                 if (op == "babel")
371                         res = "\\frq";
372                 else
373                         res = "\\guilsinglright";
374                 break;
375         }
376         case 0x0027: {// ' (plain)
377                 res = "\\textquotesingle";
378         }
379         case 0x201e: {// ,,
380                 if (op == "t1")
381                         res = ",,";
382                 else if (op == "babel")
383                         res = "\\glqq";
384                 else
385                         res = "\\quotedblbase";
386                 break;
387         }
388         case 0x201d: {// ''
389                 if (op == "int")
390                         res = "\\textquotedblleft";
391                 else
392                         res = "''";
393                 break;
394         }
395         case 0x201c: {// ``
396                 if (op == "int")
397                         res = "\\textquotedblright";
398                 else
399                         res = "``";
400                 break;
401         }
402         case 0x00ab: {// <<
403                 if (op == "t1")
404                         res = "<<";
405                 else if (op == "babel")
406                         res = "\\flqq";
407                 else
408                         res = "\\guillemotleft";
409                 break;
410         }
411         case 0x00bb: {// >>
412                 if (op == "t1")
413                         res = ">>";
414                 else if (op == "babel")
415                         res = "\\frqq";
416                 else
417                         res = "\\guillemotright";
418                 break;
419         }
420         case 0x0022: {// "
421                 res = "\\textquotedbl";
422         }
423         // The following are fakes
424         // This is just to get something symbolic
425         // in encodings where this chars would not be used ayway
426         case 0x300c: // LEFT CORNER BRACKET
427                 res = "\\ensuremath{\\lceil}";
428                 break;
429         case 0x300d: // RIGHT CORNER BRACKET
430                 res = "\\ensuremath{\\rfloor}";
431                 break;
432         case 0x300e: // LEFT WHITE CORNER BRACKET
433                 res = "\\ensuremath{\\llceil}";
434                 break;
435         case 0x300f: // RIGHT WHITE CORNER BRACKET
436                 res = "\\ensuremath{\\rrfloor}";
437                 break;
438         case 0x300a: // LEFT DOUBLE ANGLE BRACKET
439                 res = "\\ensuremath{\\langle\\kern-2.5pt\\langle}";
440                 break;
441         case 0x300b: // RIGHT DOUBLE ANGLE BRACKET
442                 res = "\\ensuremath{\\rangle\\kern-2.5pt\\rangle}";
443                 break;
444         case 0x3008: // LEFT ANGLE BRACKET
445                 res = "\\ensuremath{\\langle}";
446                 break;
447         case 0x3009: // RIGHT ANGLE BRACKET
448                 res = "\\ensuremath{\\rangle}";
449                 break;
450         default:
451                 break;
452         }
453         
454         return from_ascii(res);
455 }
456
457
458 docstring InsetQuotesParams::getHTMLQuote(char_type c) const
459 {
460         string res;
461
462         switch (c){
463         case 0x201a: // ,
464                 res = "&sbquo;";
465                 break;
466         case 0x2019: // '
467                 res = "&rsquo;";
468                 break;
469         case 0x2018: // `
470                 res = "&lsquo;";
471                 break;
472         case 0x2039: // <
473                 res = "&lsaquo;";
474                 break;
475         case 0x203a: // >
476                 res = "&rsaquo;";
477                 break;
478         case 0x0027: // ' (plain)
479                 res = "&#x27;";
480                 break;
481         case 0x201e: // ,,
482                 res = "&bdquo;";
483                 break;
484         case 0x201d: // ''
485                 res = "&rdquo;";
486                 break;
487         case 0x201c: // ``
488                 res = "&ldquo;";
489                 break;
490         case 0x00ab: // <<
491                 res = "&laquo;";
492                 break;
493         case 0x00bb: // >>
494                 res = "&raquo;";
495                 break;
496         case 0x0022: // "
497                 res = "&quot;";
498                 break;
499         case 0x300c: // LEFT CORNER BRACKET
500                 res = "&#x300c;";
501                 break;
502         case 0x300d: // RIGHT CORNER BRACKET
503                 res = "&#x300d;";
504                 break;
505         case 0x300e: // LEFT WHITE CORNER BRACKET
506                 res = "&#x300e;";
507                 break;
508         case 0x300f: // RIGHT WHITE CORNER BRACKET
509                 res = "&#x300f;";
510                 break;
511         case 0x300a: // LEFT DOUBLE ANGLE BRACKET
512                 res = "&#x300a;";
513                 break;
514         case 0x300b: // RIGHT DOUBLE ANGLE BRACKET
515                 res = "&#x300b;";
516                 break;
517         case 0x3008: // LEFT ANGLE BRACKET
518                 res = "&#x3008;";
519                 break;
520         case 0x3009: // RIGHT ANGLE BRACKET
521                 res = "&#x3009;";
522                 break;
523         default:
524                 break;
525         }
526         
527         return from_ascii(res);
528 }
529
530
531 map<string, docstring> InsetQuotesParams::getTypes() const
532 {
533         map<string, docstring> res;
534
535         int sty, sid, lev;
536         QuoteStyle style;
537         QuoteSide side;
538         QuoteLevel level;
539         string type;
540
541         // get all quote types
542         for (sty = 0; sty < stylescount(); ++sty) {
543                 style = QuoteStyle(sty);
544                 if (style == DynamicQuotes)
545                         continue;
546                 for (sid = 0; sid < 2; ++sid) {
547                         side = QuoteSide(sid);
548                         for (lev = 0; lev < 2; ++lev) {
549                                 type += style_char[style];
550                                 type += side_char[sid];
551                                 level = QuoteLevel(lev);
552                                 type += level_char[lev];
553                                 res[type] = docstring(1, getQuoteChar(style, level, side));
554                                 type.clear();
555                         }
556                 }
557         }
558         return res;
559 }
560
561
562 docstring const InsetQuotesParams::getGuiLabel(QuoteStyle const & qs, bool langdef)
563 {
564         docstring const styledesc =
565                 bformat(_("%1$souter%2$s and %3$sinner%4$s[[quotation marks]]"),
566                         docstring(1, getQuoteChar(qs, PrimaryQuotes, OpeningQuote)),
567                         docstring(1, getQuoteChar(qs, PrimaryQuotes, ClosingQuote)),
568                         docstring(1, getQuoteChar(qs, SecondaryQuotes, OpeningQuote)),
569                         docstring(1, getQuoteChar(qs, SecondaryQuotes, ClosingQuote))
570                         );
571
572         if (!langdef)
573                 return styledesc;
574
575         return bformat(_("%1$s[[quot. mark description]] (language default)"),
576                         styledesc);
577 }
578
579
580 docstring const InsetQuotesParams::getShortGuiLabel(docstring const string)
581 {
582         std::string const s = to_ascii(string);
583         QuoteStyle const style = getQuoteStyle(s);
584         QuoteSide const side = getQuoteSide(s);
585         QuoteLevel const level = getQuoteLevel(s);
586
587         return (side == OpeningQuote) ?
588                 bformat(_("%1$stext"),
589                        docstring(1, getQuoteChar(style, level, side))) :
590                 bformat(_("text%1$s"),
591                        docstring(1, getQuoteChar(style, level, side)));
592 }
593
594
595 /////////////////////////////////////////////////////////////////////
596 //
597 // InsetQuotes
598 //
599 ///////////////////////////////////////////////////////////////////////
600
601 InsetQuotes::InsetQuotes(Buffer * buf, string const & str)
602         : Inset(buf), 
603           style_(InsetQuotesParams::EnglishQuotes), side_(InsetQuotesParams::OpeningQuote),
604           pass_thru_(false)
605 {
606         if (buf) {
607                 global_style_ = buf->masterBuffer()->params().quotes_style;
608                 fontspec_ = buf->masterBuffer()->params().useNonTeXFonts;
609         }
610         else {
611                 global_style_ = InsetQuotesParams::EnglishQuotes;
612                 fontspec_ = false;
613         }
614
615         parseString(str);
616 }
617
618
619 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, InsetQuotesParams::QuoteLevel level,
620                          string const & side, string const & style)
621         : Inset(buf), level_(level), pass_thru_(false), fontspec_(false)
622 {
623         bool dynamic = false;
624         if (buf) {
625                 global_style_ = buf->masterBuffer()->params().quotes_style;
626                 fontenc_ = buf->masterBuffer()->params().main_font_encoding();
627                 dynamic = buf->masterBuffer()->params().dynamic_quotes;
628                 fontspec_ = buf->masterBuffer()->params().useNonTeXFonts;
629         } else {
630                 global_style_ = InsetQuotesParams::EnglishQuotes;
631                 fontenc_ = lyxrc.fontenc;
632                 fontspec_ = false;
633         }
634         if (style.empty())
635                 style_ = dynamic ? InsetQuotesParams::DynamicQuotes : global_style_;
636         else
637                 style_ = getStyle(style);
638
639         if (side == "left" || side == "opening")
640                 side_ = InsetQuotesParams::OpeningQuote;
641         else if (side == "right" || side == "closing")
642                 side_ = InsetQuotesParams::ClosingQuote;
643         else
644                 setSide(c);
645 }
646
647
648 docstring InsetQuotes::layoutName() const
649 {
650         return from_ascii("Quotes");
651 }
652
653
654 void InsetQuotes::setSide(char_type c)
655 {
656         // Decide whether opening or closing quote
657         if (lyx::isSpace(c) || isOpenPunctuation(c))
658                 side_ = InsetQuotesParams::OpeningQuote;// opening quote
659         else
660                 side_ = InsetQuotesParams::ClosingQuote;// closing quote
661 }
662
663
664 void InsetQuotes::parseString(string const & s, bool const allow_wildcards)
665 {
666         style_ = quoteparams.getQuoteStyle(s, allow_wildcards, style_);
667         side_ = quoteparams.getQuoteSide(s, allow_wildcards, side_);
668         level_ = quoteparams.getQuoteLevel(s, allow_wildcards, level_);
669 }
670
671
672 InsetQuotesParams::QuoteStyle InsetQuotes::getStyle(string const & s)
673 {
674         InsetQuotesParams::QuoteStyle qs = InsetQuotesParams::EnglishQuotes;
675         
676         if (s == "english")
677                 qs = InsetQuotesParams::EnglishQuotes;
678         else if (s == "swedish")
679                 qs = InsetQuotesParams::SwedishQuotes;
680         else if (s == "german")
681                 qs = InsetQuotesParams::GermanQuotes;
682         else if (s == "polish")
683                 qs = InsetQuotesParams::PolishQuotes;
684         else if (s == "swiss")
685                 qs = InsetQuotesParams::SwissQuotes;
686         else if (s == "danish")
687                 qs = InsetQuotesParams::DanishQuotes;
688         else if (s == "plain")
689                 qs = InsetQuotesParams::PlainQuotes;
690         else if (s == "british")
691                 qs = InsetQuotesParams::BritishQuotes;
692         else if (s == "swedishg")
693                 qs = InsetQuotesParams::SwedishGQuotes;
694         else if (s == "french")
695                 qs = InsetQuotesParams::FrenchQuotes;
696         else if (s == "frenchin")
697                 qs = InsetQuotesParams::FrenchINQuotes;
698         else if (s == "russian")
699                 qs = InsetQuotesParams::RussianQuotes;
700         else if (s == "cjk")
701                 qs = InsetQuotesParams::CJKQuotes;
702         else if (s == "cjkangle")
703                 qs = InsetQuotesParams::CJKAngleQuotes;
704         else if (s == "dynamic")
705                 qs = InsetQuotesParams::DynamicQuotes;
706
707         return qs;
708 }
709
710
711 docstring InsetQuotes::displayString() const
712 {
713         // In PassThru, we use straight quotes
714         if (pass_thru_)
715                 return (level_ == InsetQuotesParams::PrimaryQuotes) ?
716                                         from_ascii("\"") : from_ascii("'");
717
718         InsetQuotesParams::QuoteStyle style =
719                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
720
721         docstring retdisp = docstring(1, quoteparams.getQuoteChar(style, level_, side_));
722
723         // in French, thin spaces are added inside double guillemets
724         if (prefixIs(context_lang_, "fr")
725             && level_ == InsetQuotesParams::PrimaryQuotes
726             && (style == InsetQuotesParams::SwissQuotes
727                 || style == InsetQuotesParams::FrenchQuotes
728                 || style == InsetQuotesParams::FrenchINQuotes)) {
729                 // THIN SPACE (U+2009)
730                 char_type const thin_space = 0x2009;
731                 if (side_ == InsetQuotesParams::OpeningQuote)
732                         retdisp += thin_space;
733                 else
734                         retdisp = thin_space + retdisp;
735         }
736
737         return retdisp;
738 }
739
740
741 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
742 {
743         FontInfo & font = mi.base.font;
744         frontend::FontMetrics const & fm = theFontMetrics(font);
745         dim.asc = fm.maxAscent();
746         dim.des = fm.maxDescent();
747         dim.wid = fm.width(displayString());
748 }
749
750
751 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
752 {
753         FontInfo font = pi.base.font;
754         if (style_ == InsetQuotesParams::DynamicQuotes)
755                 font.setPaintColor(Color_special);
756         else
757                 font.setPaintColor(pi.textColor(font.realColor()));
758         pi.pain.text(x, y, displayString(), font);
759 }
760
761
762 string InsetQuotes::getType() const
763 {
764         string text;
765         text += style_char[style_];
766         text += side_char[side_];
767         text += level_char[level_];
768         return text;
769 }
770
771
772 void InsetQuotes::write(ostream & os) const
773 {
774         os << "Quotes " << getType();
775 }
776
777
778 void InsetQuotes::read(Lexer & lex)
779 {
780         lex.setContext("InsetQuotes::read");
781         lex.next();
782         parseString(lex.getString());
783         lex >> "\\end_inset";
784 }
785
786
787 void InsetQuotes::doDispatch(Cursor & cur, FuncRequest & cmd)
788 {
789         switch (cmd.action()) {
790         case LFUN_INSET_MODIFY: {
791                 string const first_arg = cmd.getArg(0);
792                 bool const change_type = first_arg == "changetype";
793                 if (!change_type) {
794                         // not for us
795                         // this will not be handled higher up
796                         cur.undispatched();
797                         return;
798                 }
799                 cur.recordUndoInset(this);
800                 parseString(cmd.getArg(1), true);
801                 cur.buffer()->updateBuffer();
802                 break;
803         }
804         default:
805                 Inset::doDispatch(cur, cmd);
806                 break;
807         }
808 }
809
810
811 bool InsetQuotes::getStatus(Cursor & cur, FuncRequest const & cmd,
812                 FuncStatus & flag) const
813 {
814         switch (cmd.action()) {
815
816         case LFUN_INSET_MODIFY: {
817                 string const first_arg = cmd.getArg(0);
818                 if (first_arg == "changetype") {
819                         string const type = cmd.getArg(1);
820                         flag.setOnOff(type == getType());
821                         flag.setEnabled(!pass_thru_);
822                         return true;
823                 }
824                 return Inset::getStatus(cur, cmd, flag);
825         }
826
827         default:
828                 return Inset::getStatus(cur, cmd, flag);
829         }
830 }
831
832
833 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
834 {
835         InsetQuotesParams::QuoteStyle style =
836                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
837         char_type quotechar = quoteparams.getQuoteChar(style, level_, side_);
838         docstring qstr;
839
840         // In pass-thru context, we output plain quotes
841         if (runparams.pass_thru)
842                 qstr = (level_ == InsetQuotesParams::PrimaryQuotes) ? from_ascii("\"") : from_ascii("'");
843         else if (style == InsetQuotesParams::PlainQuotes && fontspec_) {
844                 // For XeTeX and LuaTeX,we need to disable mapping to get straight
845                 // quotes. We define our own commands that do this
846                 qstr = (level_ == InsetQuotesParams::PrimaryQuotes) ?
847                         from_ascii("\\textquotedblplain") : from_ascii("\\textquotesingleplain");
848         }
849         else if (runparams.use_polyglossia) {
850                 // For polyglossia, we directly output the respective unicode chars 
851                 // (spacing and kerning is then handled respectively)
852                 qstr = docstring(1, quotechar);
853         }
854         else if (style == InsetQuotesParams::CJKQuotes || style  == InsetQuotesParams::CJKAngleQuotes) {
855                 if (runparams.encoding && runparams.encoding->encodable(quotechar))
856                         qstr = docstring(1, quotechar);
857                 else
858                         qstr = quoteparams.getLaTeXQuote(quotechar, "int");
859         }
860         else if ((style == InsetQuotesParams::SwissQuotes
861                  || style == InsetQuotesParams::FrenchQuotes
862                  || style == InsetQuotesParams::FrenchINQuotes)
863                  && level_ == InsetQuotesParams::PrimaryQuotes
864                  && prefixIs(runparams.local_font->language()->code(), "fr")) {
865                 // Specific guillemets of French babel
866                 // including correct French spacing
867                 if (side_ == InsetQuotesParams::OpeningQuote)
868                         qstr = from_ascii("\\og");
869                 else
870                         qstr = from_ascii("\\fg");
871         } else if (fontenc_ == "T1"
872                    && !runparams.local_font->language()->internalFontEncoding()) {
873                 // Quotation marks for T1 font encoding
874                 // (using ligatures)
875                 qstr = quoteparams.getLaTeXQuote(quotechar, "t1");
876         } else if (runparams.local_font->language()->internalFontEncoding()) {
877                 // Quotation marks for internal font encodings
878                 // (ligatures not featured)
879                 qstr = quoteparams.getLaTeXQuote(quotechar, "int");
880 #ifdef DO_USE_DEFAULT_LANGUAGE
881         } else if (doclang == "default") {
882 #else
883         } else if (!runparams.use_babel || runparams.isFullUnicode()) {
884 #endif
885                 // Standard quotation mark macros
886                 // These are also used by babel
887                 // without fontenc (XeTeX/LuaTeX)
888                 qstr = quoteparams.getLaTeXQuote(quotechar, "ot1");
889         } else {
890                 // Babel shorthand quotation marks (for T1/OT1)
891                 qstr = quoteparams.getLaTeXQuote(quotechar, "babel");
892         }
893
894         if (!runparams.pass_thru) {
895                 // Guard against unwanted ligatures with preceding text
896                 char_type const lastchar = os.lastChar();
897                 // !` ?` => !{}` ?{}`
898                 if (prefixIs(qstr, from_ascii("`"))
899                     && (lastchar == '!' || lastchar == '?'))
900                         os << "{}";
901                 // ``` ''' ,,, <<< >>>
902                 // => `{}`` '{}'' ,{},, <{}<< >{}>>
903                 if (contains(from_ascii(",'`<>"), lastchar)
904                     && prefixIs(qstr, lastchar))
905                         os << "{}";
906         }
907
908         os << qstr;
909
910         if (prefixIs(qstr, from_ascii("\\")) && !suffixIs(qstr, '}'))
911                 // properly terminate the command depending on the context
912                 os << termcmd;
913 }
914
915
916 int InsetQuotes::plaintext(odocstringstream & os, 
917         OutputParams const &, size_t) const
918 {
919         docstring const str = displayString();
920         os << str;
921         return str.size();
922 }
923
924
925 docstring InsetQuotes::getQuoteEntity() const {
926         InsetQuotesParams::QuoteStyle style =
927                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
928         docstring res = quoteparams.getHTMLQuote(quoteparams.getQuoteChar(style, level_, side_));
929         // in French, thin spaces are added inside double guillemets
930         if (prefixIs(context_lang_, "fr")
931             && level_ == InsetQuotesParams::PrimaryQuotes
932             && (style == InsetQuotesParams::FrenchQuotes
933                 || style == InsetQuotesParams::FrenchINQuotes
934                 || style == InsetQuotesParams::SwissQuotes)) {
935                 // THIN SPACE (U+2009)
936                 docstring const thin_space = from_ascii("&#x2009;");
937                 if (side_ == InsetQuotesParams::OpeningQuote)
938                         res += thin_space;
939                 else
940                         res = thin_space + res;
941         }
942         return res;
943 }
944
945
946 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
947 {
948         os << getQuoteEntity();
949         return 0;
950 }
951
952
953 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
954 {
955         xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
956         return docstring();
957 }
958
959
960 void InsetQuotes::toString(odocstream & os) const
961 {
962         os << displayString();
963 }
964
965
966 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
967 {
968         os += displayString();
969 }
970
971
972 void InsetQuotes::updateBuffer(ParIterator const & it, UpdateType /* utype*/)
973 {
974         BufferParams const & bp = buffer().masterBuffer()->params();
975         pass_thru_ = it.paragraph().isPassThru();
976         context_lang_ = it.paragraph().getFontSettings(bp, it.pos()).language()->code();
977         fontenc_ = bp.main_font_encoding();
978         global_style_ = bp.quotes_style;
979         fontspec_ = bp.useNonTeXFonts;
980 }
981
982
983 void InsetQuotes::validate(LaTeXFeatures & features) const
984 {
985         InsetQuotesParams::QuoteStyle style =
986                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
987         char_type type = quoteparams.getQuoteChar(style, level_, side_);
988
989         // Handle characters that are not natively supported by
990         // specific font encodings (we roll our own definitions)
991 #ifdef DO_USE_DEFAULT_LANGUAGE
992         if (features.bufferParams().language->lang() == "default"
993 #else
994         if (!features.useBabel()
995 #endif
996             && !features.runparams().isFullUnicode() && fontenc_ != "T1") {
997                 switch (type) {
998                 case 0x201a:
999                         features.require("quotesinglbase");
1000                         break;
1001                 case 0x2039:
1002                         features.require("guilsinglleft");
1003                         break;
1004                 case 0x203a:
1005                         features.require("guilsinglright");
1006                         break;
1007                 case 0x201e:
1008                         features.require("quotedblbase");
1009                         break;
1010                 case 0x00ab:
1011                         features.require("guillemotleft");
1012                         break;
1013                 case 0x00bb:
1014                         features.require("guillemotright");
1015                         break;
1016                 default:
1017                         break;
1018                 }
1019         }
1020         // Handle straight quotation marks. These need special care
1021         // in most output formats
1022         switch (type) {
1023         case 0x0027: {
1024                 if (features.runparams().isFullUnicode() && fontspec_)
1025                                 features.require("textquotesinglep");
1026                         else
1027                                 features.require("textcomp");
1028                         break;
1029         }
1030         case 0x0022: {
1031                 if (features.runparams().isFullUnicode() && fontspec_)
1032                         features.require("textquotedblp");
1033                 else if (fontenc_ != "T1")
1034                         features.require("textquotedbl");
1035                 break;
1036         }
1037         // we fake these from math
1038         case 0x300e: // LEFT WHITE CORNER BRACKET
1039         case 0x300f: // RIGHT WHITE CORNER BRACKET
1040                 if (!features.runparams().encoding
1041                     || !features.runparams().encoding->encodable(type))
1042                         features.require("stmaryrd");
1043                 break;
1044         default:
1045                 break;
1046         }
1047 }
1048
1049
1050 string InsetQuotes::contextMenuName() const
1051 {
1052         return "context-quote";
1053 }
1054
1055 } // namespace lyx