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