]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
Use proper font encoding in InsetQuotes
[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) : Inset(buf)
604 {
605         if (buf) {
606                 global_style_ = buf->masterBuffer()->params().quotes_style;
607                 fontspec_ = buf->masterBuffer()->params().useNonTeXFonts;
608         }
609         else {
610                 global_style_ = InsetQuotesParams::EnglishQuotes;
611                 fontspec_ = false;
612         }
613
614         parseString(str);
615 }
616
617
618 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, InsetQuotesParams::QuoteLevel level,
619                          string const & side, string const & style)
620         : Inset(buf), level_(level), pass_thru_(false), fontspec_(false)
621 {
622         bool dynamic = false;
623         if (buf) {
624                 global_style_ = buf->masterBuffer()->params().quotes_style;
625                 fontenc_ = buf->masterBuffer()->params().font_encoding();
626                 dynamic = buf->masterBuffer()->params().dynamic_quotes;
627                 fontspec_ = buf->masterBuffer()->params().useNonTeXFonts;
628         } else {
629                 global_style_ = InsetQuotesParams::EnglishQuotes;
630                 fontenc_ = lyxrc.fontenc;
631                 fontspec_ = false;
632         }
633         if (style.empty())
634                 style_ = dynamic ? InsetQuotesParams::DynamicQuotes : global_style_;
635         else
636                 style_ = getStyle(style);
637
638         if (side == "left" || side == "opening")
639                 side_ = InsetQuotesParams::OpeningQuote;
640         else if (side == "right" || side == "closing")
641                 side_ = InsetQuotesParams::ClosingQuote;
642         else
643                 setSide(c);
644 }
645
646
647 docstring InsetQuotes::layoutName() const
648 {
649         return from_ascii("Quotes");
650 }
651
652
653 void InsetQuotes::setSide(char_type c)
654 {
655         // Decide whether opening or closing quote
656         if (lyx::isSpace(c) || isOpenPunctuation(c))
657                 side_ = InsetQuotesParams::OpeningQuote;// opening quote
658         else
659                 side_ = InsetQuotesParams::ClosingQuote;// closing quote
660 }
661
662
663 void InsetQuotes::parseString(string const & s, bool const allow_wildcards)
664 {
665         style_ = quoteparams.getQuoteStyle(s, allow_wildcards, style_);
666         side_ = quoteparams.getQuoteSide(s, allow_wildcards, side_);
667         level_ = quoteparams.getQuoteLevel(s, allow_wildcards, level_);
668 }
669
670
671 InsetQuotesParams::QuoteStyle InsetQuotes::getStyle(string const & s)
672 {
673         InsetQuotesParams::QuoteStyle qs = InsetQuotesParams::EnglishQuotes;
674         
675         if (s == "english")
676                 qs = InsetQuotesParams::EnglishQuotes;
677         else if (s == "swedish")
678                 qs = InsetQuotesParams::SwedishQuotes;
679         else if (s == "german")
680                 qs = InsetQuotesParams::GermanQuotes;
681         else if (s == "polish")
682                 qs = InsetQuotesParams::PolishQuotes;
683         else if (s == "swiss")
684                 qs = InsetQuotesParams::SwissQuotes;
685         else if (s == "danish")
686                 qs = InsetQuotesParams::DanishQuotes;
687         else if (s == "plain")
688                 qs = InsetQuotesParams::PlainQuotes;
689         else if (s == "british")
690                 qs = InsetQuotesParams::BritishQuotes;
691         else if (s == "swedishg")
692                 qs = InsetQuotesParams::SwedishGQuotes;
693         else if (s == "french")
694                 qs = InsetQuotesParams::FrenchQuotes;
695         else if (s == "frenchin")
696                 qs = InsetQuotesParams::FrenchINQuotes;
697         else if (s == "russian")
698                 qs = InsetQuotesParams::RussianQuotes;
699         else if (s == "cjk")
700                 qs = InsetQuotesParams::CJKQuotes;
701         else if (s == "cjkangle")
702                 qs = InsetQuotesParams::CJKAngleQuotes;
703         else if (s == "dynamic")
704                 qs = InsetQuotesParams::DynamicQuotes;
705
706         return qs;
707 }
708
709
710 docstring InsetQuotes::displayString() const
711 {
712         // In PassThru, we use straight quotes
713         if (pass_thru_)
714                 return (level_ == InsetQuotesParams::PrimaryQuotes) ?
715                                         from_ascii("\"") : from_ascii("'");
716
717         InsetQuotesParams::QuoteStyle style =
718                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
719
720         docstring retdisp = docstring(1, quoteparams.getQuoteChar(style, level_, side_));
721
722         // in French, thin spaces are added inside double guillemets
723         if (prefixIs(context_lang_, "fr")
724             && level_ == InsetQuotesParams::PrimaryQuotes
725             && (style == InsetQuotesParams::SwissQuotes
726                 || style == InsetQuotesParams::FrenchQuotes
727                 || style == InsetQuotesParams::FrenchINQuotes)) {
728                 // THIN SPACE (U+2009)
729                 char_type const thin_space = 0x2009;
730                 if (side_ == InsetQuotesParams::OpeningQuote)
731                         retdisp += thin_space;
732                 else
733                         retdisp = thin_space + retdisp;
734         }
735
736         return retdisp;
737 }
738
739
740 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
741 {
742         FontInfo & font = mi.base.font;
743         frontend::FontMetrics const & fm = theFontMetrics(font);
744         dim.asc = fm.maxAscent();
745         dim.des = fm.maxDescent();
746         dim.wid = fm.width(displayString());
747 }
748
749
750 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
751 {
752         FontInfo font = pi.base.font;
753         if (style_ == InsetQuotesParams::DynamicQuotes)
754                 font.setPaintColor(Color_special);
755         else
756                 font.setPaintColor(pi.textColor(font.realColor()));
757         pi.pain.text(x, y, displayString(), font);
758 }
759
760
761 string InsetQuotes::getType() const
762 {
763         string text;
764         text += style_char[style_];
765         text += side_char[side_];
766         text += level_char[level_];
767         return text;
768 }
769
770
771 void InsetQuotes::write(ostream & os) const
772 {
773         os << "Quotes " << getType();
774 }
775
776
777 void InsetQuotes::read(Lexer & lex)
778 {
779         lex.setContext("InsetQuotes::read");
780         lex.next();
781         parseString(lex.getString());
782         lex >> "\\end_inset";
783 }
784
785
786 void InsetQuotes::doDispatch(Cursor & cur, FuncRequest & cmd)
787 {
788         switch (cmd.action()) {
789         case LFUN_INSET_MODIFY: {
790                 string const first_arg = cmd.getArg(0);
791                 bool const change_type = first_arg == "changetype";
792                 if (!change_type) {
793                         // not for us
794                         // this will not be handled higher up
795                         cur.undispatched();
796                         return;
797                 }
798                 cur.recordUndoInset(this);
799                 parseString(cmd.getArg(1), true);
800                 cur.buffer()->updateBuffer();
801                 break;
802         }
803         default:
804                 Inset::doDispatch(cur, cmd);
805                 break;
806         }
807 }
808
809
810 bool InsetQuotes::getStatus(Cursor & cur, FuncRequest const & cmd,
811                 FuncStatus & flag) const
812 {
813         switch (cmd.action()) {
814
815         case LFUN_INSET_MODIFY: {
816                 string const first_arg = cmd.getArg(0);
817                 if (first_arg == "changetype") {
818                         string const type = cmd.getArg(1);
819                         flag.setOnOff(type == getType());
820                         flag.setEnabled(!pass_thru_);
821                         return true;
822                 }
823                 return Inset::getStatus(cur, cmd, flag);
824         }
825
826         default:
827                 return Inset::getStatus(cur, cmd, flag);
828         }
829 }
830
831
832 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
833 {
834         InsetQuotesParams::QuoteStyle style =
835                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
836         char_type quotechar = quoteparams.getQuoteChar(style, level_, side_);
837         docstring qstr;
838
839         // In pass-thru context, we output plain quotes
840         if (runparams.pass_thru)
841                 qstr = (level_ == InsetQuotesParams::PrimaryQuotes) ? from_ascii("\"") : from_ascii("'");
842         else if (style == InsetQuotesParams::PlainQuotes && fontspec_) {
843                 // For XeTeX and LuaTeX,we need to disable mapping to get straight
844                 // quotes. We define our own commands that do this
845                 qstr = (level_ == InsetQuotesParams::PrimaryQuotes) ?
846                         from_ascii("\\textquotedblplain") : from_ascii("\\textquotesingleplain");
847         }
848         else if (runparams.use_polyglossia) {
849                 // For polyglossia, we directly output the respective unicode chars 
850                 // (spacing and kerning is then handled respectively)
851                 qstr = docstring(1, quotechar);
852         }
853         else if (style == InsetQuotesParams::CJKQuotes || style  == InsetQuotesParams::CJKAngleQuotes) {
854                 if (runparams.encoding && runparams.encoding->encodable(quotechar))
855                         qstr = docstring(1, quotechar);
856                 else
857                         qstr = quoteparams.getLaTeXQuote(quotechar, "int");
858         }
859         else if ((style == InsetQuotesParams::SwissQuotes
860                  || style == InsetQuotesParams::FrenchQuotes
861                  || style == InsetQuotesParams::FrenchINQuotes)
862                  && level_ == InsetQuotesParams::PrimaryQuotes
863                  && prefixIs(runparams.local_font->language()->code(), "fr")) {
864                 // Specific guillemets of French babel
865                 // including correct French spacing
866                 if (side_ == InsetQuotesParams::OpeningQuote)
867                         qstr = from_ascii("\\og");
868                 else
869                         qstr = from_ascii("\\fg");
870         } else if (fontenc_ == "T1"
871                    && !runparams.local_font->language()->internalFontEncoding()) {
872                 // Quotation marks for T1 font encoding
873                 // (using ligatures)
874                 qstr = quoteparams.getLaTeXQuote(quotechar, "t1");
875         } else if (runparams.local_font->language()->internalFontEncoding()) {
876                 // Quotation marks for internal font encodings
877                 // (ligatures not featured)
878                 qstr = quoteparams.getLaTeXQuote(quotechar, "int");
879 #ifdef DO_USE_DEFAULT_LANGUAGE
880         } else if (doclang == "default") {
881 #else
882         } else if (!runparams.use_babel || runparams.isFullUnicode()) {
883 #endif
884                 // Standard quotation mark macros
885                 // These are also used by babel
886                 // without fontenc (XeTeX/LuaTeX)
887                 qstr = quoteparams.getLaTeXQuote(quotechar, "ot1");
888         } else {
889                 // Babel shorthand quotation marks (for T1/OT1)
890                 qstr = quoteparams.getLaTeXQuote(quotechar, "babel");
891         }
892
893         if (!runparams.pass_thru) {
894                 // Guard against unwanted ligatures with preceding text
895                 char_type const lastchar = os.lastChar();
896                 // !` ?` => !{}` ?{}`
897                 if (prefixIs(qstr, from_ascii("`"))
898                     && (lastchar == '!' || lastchar == '?'))
899                         os << "{}";
900                 // ``` ''' ,,, <<< >>>
901                 // => `{}`` '{}'' ,{},, <{}<< >{}>>
902                 if (contains(from_ascii(",'`<>"), lastchar)
903                     && prefixIs(qstr, lastchar))
904                         os << "{}";
905         }
906
907         os << qstr;
908
909         if (prefixIs(qstr, from_ascii("\\")) && !suffixIs(qstr, '}'))
910                 // properly terminate the command depending on the context
911                 os << termcmd;
912 }
913
914
915 int InsetQuotes::plaintext(odocstringstream & os, 
916         OutputParams const &, size_t) const
917 {
918         docstring const str = displayString();
919         os << str;
920         return str.size();
921 }
922
923
924 docstring InsetQuotes::getQuoteEntity() const {
925         InsetQuotesParams::QuoteStyle style =
926                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
927         docstring res = quoteparams.getHTMLQuote(quoteparams.getQuoteChar(style, level_, side_));
928         // in French, thin spaces are added inside double guillemets
929         if (prefixIs(context_lang_, "fr")
930             && level_ == InsetQuotesParams::PrimaryQuotes
931             && (style == InsetQuotesParams::FrenchQuotes
932                 || style == InsetQuotesParams::FrenchINQuotes
933                 || style == InsetQuotesParams::SwissQuotes)) {
934                 // THIN SPACE (U+2009)
935                 docstring const thin_space = from_ascii("&#x2009;");
936                 if (side_ == InsetQuotesParams::OpeningQuote)
937                         res += thin_space;
938                 else
939                         res = thin_space + res;
940         }
941         return res;
942 }
943
944
945 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
946 {
947         os << getQuoteEntity();
948         return 0;
949 }
950
951
952 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
953 {
954         xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
955         return docstring();
956 }
957
958
959 void InsetQuotes::toString(odocstream & os) const
960 {
961         os << displayString();
962 }
963
964
965 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
966 {
967         os += displayString();
968 }
969
970
971 void InsetQuotes::updateBuffer(ParIterator const & it, UpdateType /* utype*/)
972 {
973         BufferParams const & bp = buffer().masterBuffer()->params();
974         pass_thru_ = it.paragraph().isPassThru();
975         context_lang_ = it.paragraph().getFontSettings(bp, it.pos()).language()->code();
976         fontenc_ = bp.font_encoding();
977         global_style_ = bp.quotes_style;
978         fontspec_ = bp.useNonTeXFonts;
979 }
980
981
982 void InsetQuotes::validate(LaTeXFeatures & features) const
983 {
984         InsetQuotesParams::QuoteStyle style =
985                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
986         char_type type = quoteparams.getQuoteChar(style, level_, side_);
987
988         // Handle characters that are not natively supported by
989         // specific font encodings (we roll our own definitions)
990 #ifdef DO_USE_DEFAULT_LANGUAGE
991         if (features.bufferParams().language->lang() == "default"
992 #else
993         if (!features.useBabel()
994 #endif
995             && !features.runparams().isFullUnicode() && fontenc_ != "T1") {
996                 switch (type) {
997                 case 0x201a:
998                         features.require("quotesinglbase");
999                         break;
1000                 case 0x2039:
1001                         features.require("guilsinglleft");
1002                         break;
1003                 case 0x203a:
1004                         features.require("guilsinglright");
1005                         break;
1006                 case 0x201e:
1007                         features.require("quotedblbase");
1008                         break;
1009                 case 0x00ab:
1010                         features.require("guillemotleft");
1011                         break;
1012                 case 0x00bb:
1013                         features.require("guillemotright");
1014                         break;
1015                 default:
1016                         break;
1017                 }
1018         }
1019         // Handle straight quotation marks. These need special care
1020         // in most output formats
1021         switch (type) {
1022         case 0x0027: {
1023                 if (features.runparams().isFullUnicode() && fontspec_)
1024                                 features.require("textquotesinglep");
1025                         else
1026                                 features.require("textcomp");
1027                         break;
1028         }
1029         case 0x0022: {
1030                 if (features.runparams().isFullUnicode() && fontspec_)
1031                         features.require("textquotedblp");
1032                 else if (fontenc_ != "T1")
1033                         features.require("textquotedbl");
1034                 break;
1035         }
1036         // we fake these from math
1037         case 0x300e: // LEFT WHITE CORNER BRACKET
1038         case 0x300f: // RIGHT WHITE CORNER BRACKET
1039                 if (!features.runparams().encoding
1040                     || !features.runparams().encoding->encodable(type))
1041                         features.require("stmaryrd");
1042                 break;
1043         default:
1044                 break;
1045         }
1046 }
1047
1048
1049 string InsetQuotes::contextMenuName() const
1050 {
1051         return "context-quote";
1052 }
1053
1054 } // namespace lyx