]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
Follow some of the performance advice from cppcheck
[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         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, bool const rtl) 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                 if (rtl)
325                         return (side == ClosingQuote) ? left_secondary : right_secondary;
326                 return (side == OpeningQuote) ? left_secondary : right_secondary;
327         case PrimaryQuotes:
328                 if (rtl)
329                         return (side == ClosingQuote) ? left_primary : right_primary;
330                 return (side == OpeningQuote) ? left_primary : right_primary;
331         default:
332                 break;
333         }
334
335         // should not happen
336         return 0x003f;
337 }
338
339
340 docstring InsetQuotesParams::getLaTeXQuote(char_type c, string const & op,
341                                            bool const rtl) const
342 {
343         string res;
344
345         switch (c){
346         case 0x201a: {// ,
347                 if (op == "babel")
348                         res = "\\glq";
349                 else
350                         res = "\\quotesinglbase";
351                 break;
352         }
353         case 0x2019: {// '
354                 if (op == "int")
355                         // This macro is redefined in rtl mode
356                         res = rtl ? "\\textquoteright" : "\\textquoteleft";
357                 else
358                         res = "'";
359                 break;
360         }
361         case 0x2018: {// `
362                 if (op == "int")
363                         // This macro is redefined in rtl mode
364                         res = rtl ? "\\textquoteleft" : "\\textquoteright";
365                 else
366                         res = "`";
367                 break;
368         }
369         case 0x2039: {// <
370                 if (op == "babel")
371                         res = "\\flq";
372                 else
373                         res = "\\guilsinglleft";
374                 break;
375         }
376         case 0x203a: {// >
377                 if (op == "babel")
378                         res = "\\frq";
379                 else
380                         res = "\\guilsinglright";
381                 break;
382         }
383         case 0x0027: {// ' (plain)
384                 res = "\\textquotesingle";
385                 break;
386         }
387         case 0x201e: {// ,,
388                 if (op == "t1")
389                         res = ",,";
390                 else if (op == "babel")
391                         res = "\\glqq";
392                 else
393                         res = "\\quotedblbase";
394                 break;
395         }
396         case 0x201d: {// ''
397                 if (op == "int")
398                         // This macro is redefined in rtl mode
399                         res = rtl ? "\\textquotedblright" : "\\textquotedblleft";
400                 else
401                         res = "''";
402                 break;
403         }
404         case 0x201c: {// ``
405                 if (op == "int")
406                         // This macro is redefined in rtl mode
407                         res = rtl ? "\\textquotedblleft" : "\\textquotedblright";
408                 else
409                         res = "``";
410                 break;
411         }
412         case 0x00ab: {// <<
413                 if (op == "t1")
414                         res = "<<";
415                 else if (op == "babel")
416                         res = "\\flqq";
417                 else
418                         res = "\\guillemotleft";
419                 break;
420         }
421         case 0x00bb: {// >>
422                 if (op == "t1")
423                         res = ">>";
424                 else if (op == "babel")
425                         res = "\\frqq";
426                 else
427                         res = "\\guillemotright";
428                 break;
429         }
430         case 0x0022: {// "
431                 res = "\\textquotedbl";
432                 break;
433         }
434         // The following are fakes
435         // This is just to get something symbolic
436         // in encodings where this chars would not be used ayway
437         case 0x300c: // LEFT CORNER BRACKET
438                 res = "\\ensuremath{\\lceil}";
439                 break;
440         case 0x300d: // RIGHT CORNER BRACKET
441                 res = "\\ensuremath{\\rfloor}";
442                 break;
443         case 0x300e: // LEFT WHITE CORNER BRACKET
444                 res = "\\ensuremath{\\llceil}";
445                 break;
446         case 0x300f: // RIGHT WHITE CORNER BRACKET
447                 res = "\\ensuremath{\\rrfloor}";
448                 break;
449         case 0x300a: // LEFT DOUBLE ANGLE BRACKET
450                 res = "\\ensuremath{\\langle\\kern-2.5pt\\langle}";
451                 break;
452         case 0x300b: // RIGHT DOUBLE ANGLE BRACKET
453                 res = "\\ensuremath{\\rangle\\kern-2.5pt\\rangle}";
454                 break;
455         case 0x3008: // LEFT ANGLE BRACKET
456                 res = "\\ensuremath{\\langle}";
457                 break;
458         case 0x3009: // RIGHT ANGLE BRACKET
459                 res = "\\ensuremath{\\rangle}";
460                 break;
461         default:
462                 break;
463         }
464
465         return from_ascii(res);
466 }
467
468
469 docstring InsetQuotesParams::getHTMLQuote(char_type c) const
470 {
471         string res;
472
473         switch (c){
474         case 0x201a: // ,
475                 res = "&sbquo;";
476                 break;
477         case 0x2019: // '
478                 res = "&rsquo;";
479                 break;
480         case 0x2018: // `
481                 res = "&lsquo;";
482                 break;
483         case 0x2039: // <
484                 res = "&lsaquo;";
485                 break;
486         case 0x203a: // >
487                 res = "&rsaquo;";
488                 break;
489         case 0x0027: // ' (plain)
490                 res = "&#x27;";
491                 break;
492         case 0x201e: // ,,
493                 res = "&bdquo;";
494                 break;
495         case 0x201d: // ''
496                 res = "&rdquo;";
497                 break;
498         case 0x201c: // ``
499                 res = "&ldquo;";
500                 break;
501         case 0x00ab: // <<
502                 res = "&laquo;";
503                 break;
504         case 0x00bb: // >>
505                 res = "&raquo;";
506                 break;
507         case 0x0022: // "
508                 res = "&quot;";
509                 break;
510         case 0x300c: // LEFT CORNER BRACKET
511                 res = "&#x300c;";
512                 break;
513         case 0x300d: // RIGHT CORNER BRACKET
514                 res = "&#x300d;";
515                 break;
516         case 0x300e: // LEFT WHITE CORNER BRACKET
517                 res = "&#x300e;";
518                 break;
519         case 0x300f: // RIGHT WHITE CORNER BRACKET
520                 res = "&#x300f;";
521                 break;
522         case 0x300a: // LEFT DOUBLE ANGLE BRACKET
523                 res = "&#x300a;";
524                 break;
525         case 0x300b: // RIGHT DOUBLE ANGLE BRACKET
526                 res = "&#x300b;";
527                 break;
528         case 0x3008: // LEFT ANGLE BRACKET
529                 res = "&#x3008;";
530                 break;
531         case 0x3009: // RIGHT ANGLE BRACKET
532                 res = "&#x3009;";
533                 break;
534         default:
535                 break;
536         }
537
538         return from_ascii(res);
539 }
540
541
542 map<string, docstring> InsetQuotesParams::getTypes() const
543 {
544         map<string, docstring> res;
545
546         int sty, sid, lev;
547         QuoteStyle style;
548         QuoteSide side;
549         QuoteLevel level;
550         string type;
551
552         // get all quote types
553         for (sty = 0; sty < stylescount(); ++sty) {
554                 style = QuoteStyle(sty);
555                 if (style == DynamicQuotes)
556                         continue;
557                 for (sid = 0; sid < 2; ++sid) {
558                         side = QuoteSide(sid);
559                         for (lev = 0; lev < 2; ++lev) {
560                                 type += style_char[style];
561                                 type += side_char[sid];
562                                 level = QuoteLevel(lev);
563                                 type += level_char[lev];
564                                 res[type] = docstring(1, getQuoteChar(style, level, side));
565                                 type.clear();
566                         }
567                 }
568         }
569         return res;
570 }
571
572
573 docstring const InsetQuotesParams::getGuiLabel(QuoteStyle const & qs, bool langdef)
574 {
575         docstring const styledesc =
576                 bformat(_("%1$souter%2$s and %3$sinner%4$s[[quotation marks]]"),
577                         docstring(1, getQuoteChar(qs, PrimaryQuotes, OpeningQuote)),
578                         docstring(1, getQuoteChar(qs, PrimaryQuotes, ClosingQuote)),
579                         docstring(1, getQuoteChar(qs, SecondaryQuotes, OpeningQuote)),
580                         docstring(1, getQuoteChar(qs, SecondaryQuotes, ClosingQuote))
581                         );
582
583         if (!langdef)
584                 return styledesc;
585
586         return bformat(_("%1$s[[quot. mark description]] (language default)"),
587                         styledesc);
588 }
589
590
591 docstring const InsetQuotesParams::getShortGuiLabel(docstring const & str)
592 {
593         string const s = to_ascii(str);
594         QuoteStyle const style = getQuoteStyle(s);
595         QuoteSide const side = getQuoteSide(s);
596         QuoteLevel const level = getQuoteLevel(s);
597
598         return (side == OpeningQuote) ?
599                 bformat(_("%1$stext"),
600                        docstring(1, getQuoteChar(style, level, side))) :
601                 bformat(_("text%1$s"),
602                        docstring(1, getQuoteChar(style, level, side)));
603 }
604
605
606 /////////////////////////////////////////////////////////////////////
607 //
608 // InsetQuotes
609 //
610 ///////////////////////////////////////////////////////////////////////
611
612 InsetQuotes::InsetQuotes(Buffer * buf, string const & str)
613         : Inset(buf),
614           style_(InsetQuotesParams::EnglishQuotes), side_(InsetQuotesParams::OpeningQuote),
615           pass_thru_(false), internal_fontenc_(false), rtl_(false)
616 {
617         if (buf) {
618                 global_style_ = buf->masterBuffer()->params().quotes_style;
619                 fontspec_ = buf->masterBuffer()->params().useNonTeXFonts;
620         }
621         else {
622                 global_style_ = InsetQuotesParams::EnglishQuotes;
623                 fontspec_ = false;
624         }
625
626         parseString(str);
627 }
628
629
630 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, InsetQuotesParams::QuoteLevel level,
631                          string const & side, string const & style)
632         : Inset(buf), level_(level), pass_thru_(false), fontspec_(false),
633           internal_fontenc_(false), rtl_(false)
634 {
635         bool dynamic = false;
636         if (buf) {
637                 global_style_ = buf->masterBuffer()->params().quotes_style;
638                 fontenc_ = buf->masterBuffer()->params().main_font_encoding();
639                 dynamic = buf->masterBuffer()->params().dynamic_quotes;
640                 fontspec_ = buf->masterBuffer()->params().useNonTeXFonts;
641         } else {
642                 global_style_ = InsetQuotesParams::EnglishQuotes;
643                 fontenc_ = "OT1";
644                 fontspec_ = false;
645         }
646         if (style.empty())
647                 style_ = dynamic ? InsetQuotesParams::DynamicQuotes : global_style_;
648         else
649                 style_ = getStyle(style);
650
651         if (side == "left" || side == "opening")
652                 side_ = InsetQuotesParams::OpeningQuote;
653         else if (side == "right" || side == "closing")
654                 side_ = InsetQuotesParams::ClosingQuote;
655         else
656                 setSide(c);
657 }
658
659
660 docstring InsetQuotes::layoutName() const
661 {
662         return from_ascii("Quotes");
663 }
664
665
666 void InsetQuotes::setSide(char_type c)
667 {
668         // Decide whether opening or closing quote
669         if (lyx::isSpace(c) || isOpenPunctuation(c))
670                 side_ = InsetQuotesParams::OpeningQuote;// opening quote
671         else
672                 side_ = InsetQuotesParams::ClosingQuote;// closing quote
673 }
674
675
676 void InsetQuotes::parseString(string const & s, bool const allow_wildcards)
677 {
678         style_ = quoteparams.getQuoteStyle(s, allow_wildcards, style_);
679         side_ = quoteparams.getQuoteSide(s, allow_wildcards, side_);
680         level_ = quoteparams.getQuoteLevel(s, allow_wildcards, level_);
681 }
682
683
684 InsetQuotesParams::QuoteStyle InsetQuotes::getStyle(string const & s)
685 {
686         InsetQuotesParams::QuoteStyle qs = InsetQuotesParams::EnglishQuotes;
687
688         if (s == "english")
689                 qs = InsetQuotesParams::EnglishQuotes;
690         else if (s == "swedish")
691                 qs = InsetQuotesParams::SwedishQuotes;
692         else if (s == "german")
693                 qs = InsetQuotesParams::GermanQuotes;
694         else if (s == "polish")
695                 qs = InsetQuotesParams::PolishQuotes;
696         else if (s == "swiss")
697                 qs = InsetQuotesParams::SwissQuotes;
698         else if (s == "danish")
699                 qs = InsetQuotesParams::DanishQuotes;
700         else if (s == "plain")
701                 qs = InsetQuotesParams::PlainQuotes;
702         else if (s == "british")
703                 qs = InsetQuotesParams::BritishQuotes;
704         else if (s == "swedishg")
705                 qs = InsetQuotesParams::SwedishGQuotes;
706         else if (s == "french")
707                 qs = InsetQuotesParams::FrenchQuotes;
708         else if (s == "frenchin")
709                 qs = InsetQuotesParams::FrenchINQuotes;
710         else if (s == "russian")
711                 qs = InsetQuotesParams::RussianQuotes;
712         else if (s == "cjk")
713                 qs = InsetQuotesParams::CJKQuotes;
714         else if (s == "cjkangle")
715                 qs = InsetQuotesParams::CJKAngleQuotes;
716         else if (s == "dynamic")
717                 qs = InsetQuotesParams::DynamicQuotes;
718
719         return qs;
720 }
721
722
723 docstring InsetQuotes::displayString() const
724 {
725         // In PassThru, we use straight quotes
726         if (pass_thru_)
727                 return (level_ == InsetQuotesParams::PrimaryQuotes) ?
728                                         from_ascii("\"") : from_ascii("'");
729
730         InsetQuotesParams::QuoteStyle style =
731                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
732
733         docstring retdisp = docstring(1, quoteparams.getQuoteChar(style, level_, side_, rtl_));
734
735         // in French, thin spaces are added inside double guillemets
736         if (prefixIs(context_lang_, "fr")
737             && level_ == InsetQuotesParams::PrimaryQuotes
738             && (style == InsetQuotesParams::SwissQuotes
739                 || style == InsetQuotesParams::FrenchQuotes
740                 || style == InsetQuotesParams::FrenchINQuotes)) {
741                 // THIN SPACE (U+2009)
742                 char_type const thin_space = 0x2009;
743                 if (side_ == InsetQuotesParams::OpeningQuote)
744                         retdisp += thin_space;
745                 else
746                         retdisp = thin_space + retdisp;
747         }
748
749         return retdisp;
750 }
751
752
753 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
754 {
755         FontInfo & font = mi.base.font;
756         frontend::FontMetrics const & fm = theFontMetrics(font);
757         dim.asc = fm.maxAscent();
758         dim.des = fm.maxDescent();
759         dim.wid = fm.width(displayString());
760 }
761
762
763 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
764 {
765         FontInfo font = pi.base.font;
766         if (style_ == InsetQuotesParams::DynamicQuotes)
767                 font.setPaintColor(Color_special);
768         else
769                 font.setPaintColor(pi.textColor(font.realColor()));
770         pi.pain.text(x, y, displayString(), font);
771 }
772
773
774 string InsetQuotes::getType() const
775 {
776         string text;
777         text += style_char[style_];
778         text += side_char[side_];
779         text += level_char[level_];
780         return text;
781 }
782
783
784 void InsetQuotes::write(ostream & os) const
785 {
786         os << "Quotes " << getType();
787 }
788
789
790 void InsetQuotes::read(Lexer & lex)
791 {
792         lex.setContext("InsetQuotes::read");
793         lex.next();
794         parseString(lex.getString());
795         lex >> "\\end_inset";
796 }
797
798
799 void InsetQuotes::doDispatch(Cursor & cur, FuncRequest & cmd)
800 {
801         switch (cmd.action()) {
802         case LFUN_INSET_MODIFY: {
803                 string const first_arg = cmd.getArg(0);
804                 bool const change_type = first_arg == "changetype";
805                 if (!change_type) {
806                         // not for us
807                         // this will not be handled higher up
808                         cur.undispatched();
809                         return;
810                 }
811                 cur.recordUndoInset(this);
812                 parseString(cmd.getArg(1), true);
813                 cur.forceBufferUpdate();
814                 break;
815         }
816         default:
817                 Inset::doDispatch(cur, cmd);
818                 break;
819         }
820 }
821
822
823 bool InsetQuotes::getStatus(Cursor & cur, FuncRequest const & cmd,
824                 FuncStatus & flag) const
825 {
826         switch (cmd.action()) {
827
828         case LFUN_INSET_MODIFY: {
829                 string const first_arg = cmd.getArg(0);
830                 if (first_arg == "changetype") {
831                         string const type = cmd.getArg(1);
832                         flag.setOnOff(type == getType());
833                         flag.setEnabled(!pass_thru_);
834                         return true;
835                 }
836                 return Inset::getStatus(cur, cmd, flag);
837         }
838
839         default:
840                 return Inset::getStatus(cur, cmd, flag);
841         }
842 }
843
844
845 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
846 {
847         InsetQuotesParams::QuoteStyle style =
848                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
849         char_type quotechar = quoteparams.getQuoteChar(style, level_, side_, rtl_);
850         docstring qstr;
851
852         // In pass-thru context, we output plain quotes
853         if (runparams.pass_thru)
854                 qstr = (level_ == InsetQuotesParams::PrimaryQuotes) ? from_ascii("\"") : from_ascii("'");
855         else if (style == InsetQuotesParams::PlainQuotes && fontspec_) {
856                 // For XeTeX and LuaTeX,we need to disable mapping to get straight
857                 // quotes. We define our own commands that do this
858                 qstr = (level_ == InsetQuotesParams::PrimaryQuotes) ?
859                         from_ascii("\\textquotedblplain") : from_ascii("\\textquotesingleplain");
860         }
861         else if (runparams.use_polyglossia) {
862                 // For polyglossia, we directly output the respective unicode chars
863                 // (spacing and kerning is then handled respectively)
864                 qstr = docstring(1, quotechar);
865         }
866         // The CJK marks are not yet covered by utf8 inputenc (we don't have the entry in
867         // unicodesymbols, since we don't want to add fake synbols there).
868         else if (style == InsetQuotesParams::CJKQuotes || style  == InsetQuotesParams::CJKAngleQuotes) {
869                 if (runparams.encoding && runparams.encoding->name() != "utf8"
870                     && runparams.encoding->encodable(quotechar))
871                         qstr = docstring(1, quotechar);
872                 else
873                         qstr = quoteparams.getLaTeXQuote(quotechar, "int");
874         }
875         else if ((style == InsetQuotesParams::SwissQuotes
876                  || style == InsetQuotesParams::FrenchQuotes
877                  || style == InsetQuotesParams::FrenchINQuotes)
878                  && level_ == InsetQuotesParams::PrimaryQuotes
879                  && prefixIs(runparams.local_font->language()->code(), "fr")) {
880                 // Specific guillemets of French babel
881                 // including correct French spacing
882                 if (side_ == InsetQuotesParams::OpeningQuote)
883                         qstr = from_ascii("\\og");
884                 else
885                         qstr = from_ascii("\\fg");
886         } else if (fontenc_ == "T1"
887                    && !runparams.local_font->language()->internalFontEncoding()) {
888                 // Quotation marks for T1 font encoding
889                 // (using ligatures)
890                 qstr = quoteparams.getLaTeXQuote(quotechar, "t1");
891         } else if (runparams.local_font->language()->internalFontEncoding()) {
892                 // Quotation marks for internal font encodings
893                 // (ligatures not featured)
894                 qstr = quoteparams.getLaTeXQuote(quotechar, "int", rtl_);
895 #ifdef DO_USE_DEFAULT_LANGUAGE
896         } else if ((doclang == "default"
897 #else
898         } else if ((!runparams.use_babel
899 #endif
900                    || (fontenc_ != "T1" && fontenc_ != "OT1"))
901                    || runparams.isFullUnicode()) {
902                 // Standard quotation mark macros
903                 // These are also used by babel
904                 // without fontenc (XeTeX/LuaTeX)
905                 qstr = quoteparams.getLaTeXQuote(quotechar, "ot1");
906         } else {
907                 // Babel shorthand quotation marks (for T1/OT1)
908                 qstr = quoteparams.getLaTeXQuote(quotechar, "babel");
909         }
910
911         if (!runparams.pass_thru) {
912                 // Guard against unwanted ligatures with preceding text
913                 char_type const lastchar = os.lastChar();
914                 // LuaTeX does not respect {} as ligature breaker by design,
915                 // see https://tex.stackexchange.com/q/349725/19291
916                 docstring const nolig =
917                                 (runparams.flavor == OutputParams::LUATEX
918                                  || runparams.flavor == OutputParams::DVILUATEX) ?
919                                         from_ascii("\\/") : from_ascii("{}");
920                 // !` ?` => !{}` ?{}`
921                 if (prefixIs(qstr, from_ascii("`"))
922                     && (lastchar == '!' || lastchar == '?'))
923                         os << nolig;
924                 // ``` ''' ,,, <<< >>>
925                 // => `{}`` '{}'' ,{},, <{}<< >{}>>
926                 if (contains(from_ascii(",'`<>"), lastchar)
927                     && prefixIs(qstr, lastchar))
928                         os << nolig;
929         }
930
931         os << qstr;
932
933         if (prefixIs(qstr, from_ascii("\\")) && !suffixIs(qstr, '}'))
934                 // properly terminate the command depending on the context
935                 os << termcmd;
936 }
937
938
939 int InsetQuotes::plaintext(odocstringstream & os,
940         OutputParams const &, size_t) const
941 {
942         docstring const str = displayString();
943         os << str;
944         return str.size();
945 }
946
947
948 docstring InsetQuotes::getQuoteEntity() const {
949         InsetQuotesParams::QuoteStyle style =
950                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
951         docstring res = quoteparams.getHTMLQuote(quoteparams.getQuoteChar(style, level_, side_));
952         // in French, thin spaces are added inside double guillemets
953         if (prefixIs(context_lang_, "fr")
954             && level_ == InsetQuotesParams::PrimaryQuotes
955             && (style == InsetQuotesParams::FrenchQuotes
956                 || style == InsetQuotesParams::FrenchINQuotes
957                 || style == InsetQuotesParams::SwissQuotes)) {
958                 // THIN SPACE (U+2009)
959                 docstring const thin_space = from_ascii("&#x2009;");
960                 if (side_ == InsetQuotesParams::OpeningQuote)
961                         res += thin_space;
962                 else
963                         res = thin_space + res;
964         }
965         return res;
966 }
967
968
969 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
970 {
971         os << getQuoteEntity();
972         return 0;
973 }
974
975
976 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
977 {
978         xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
979         return docstring();
980 }
981
982
983 void InsetQuotes::toString(odocstream & os) const
984 {
985         os << displayString();
986 }
987
988
989 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
990 {
991         os += displayString();
992 }
993
994
995 void InsetQuotes::updateBuffer(ParIterator const & it, UpdateType /* utype*/)
996 {
997         BufferParams const & bp = buffer().masterBuffer()->params();
998         Font const & font = it.paragraph().getFontSettings(bp, it.pos());
999         pass_thru_ = it.paragraph().isPassThru();
1000         context_lang_ = font.language()->code();
1001         internal_fontenc_ = font.language()->internalFontEncoding();
1002         fontenc_ = bp.main_font_encoding();
1003         global_style_ = bp.quotes_style;
1004         fontspec_ = bp.useNonTeXFonts;
1005         rtl_ = font.isRightToLeft();
1006 }
1007
1008
1009 void InsetQuotes::validate(LaTeXFeatures & features) const
1010 {
1011         InsetQuotesParams::QuoteStyle style =
1012                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
1013         char_type type = quoteparams.getQuoteChar(style, level_, side_);
1014
1015         // Handle characters that are not natively supported by
1016         // specific font encodings (we roll our own definitions)
1017 #ifdef DO_USE_DEFAULT_LANGUAGE
1018         if (features.bufferParams().language->lang() == "default"
1019 #else
1020         if (!features.useBabel()
1021 #endif
1022             && !features.runparams().isFullUnicode() && fontenc_ != "T1") {
1023                 switch (type) {
1024                 case 0x201a:
1025                         features.require("quotesinglbase");
1026                         break;
1027                 case 0x2039:
1028                         features.require("guilsinglleft");
1029                         break;
1030                 case 0x203a:
1031                         features.require("guilsinglright");
1032                         break;
1033                 case 0x201e:
1034                         features.require("quotedblbase");
1035                         break;
1036                 case 0x00ab:
1037                         features.require("guillemotleft");
1038                         break;
1039                 case 0x00bb:
1040                         features.require("guillemotright");
1041                         break;
1042                 default:
1043                         break;
1044                 }
1045         }
1046         // Handle straight quotation marks. These need special care
1047         // in most output formats
1048         switch (type) {
1049         case 0x0027: {
1050                 if (features.runparams().isFullUnicode() && fontspec_)
1051                                 features.require("textquotesinglep");
1052                         else
1053                                 features.require("textcomp");
1054                         break;
1055         }
1056         case 0x0022: {
1057                 if (features.runparams().isFullUnicode() && fontspec_)
1058                         features.require("textquotedblp");
1059                 else if (fontenc_ != "T1" || internal_fontenc_)
1060                         features.require("textquotedbl");
1061                 break;
1062         }
1063         // we fake these from math (also for utf8 inputenc
1064         // currently; see above)
1065         case 0x300e: // LEFT WHITE CORNER BRACKET
1066         case 0x300f: // RIGHT WHITE CORNER BRACKET
1067                 if (!features.runparams().encoding
1068                     || features.runparams().encoding->name() == "utf8"
1069                     || !features.runparams().encoding->encodable(type))
1070                         features.require("stmaryrd");
1071                 break;
1072         default:
1073                 break;
1074         }
1075 }
1076
1077
1078 string InsetQuotes::contextMenuName() const
1079 {
1080         return "context-quote";
1081 }
1082
1083 } // namespace lyx