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