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