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