2 * \file InsetQuotes.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jean-Marc Lasgouttes
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "InsetQuotes.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
20 #include "Dimension.h"
23 #include "FuncStatus.h"
24 #include "FuncRequest.h"
26 #include "LaTeXFeatures.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"
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
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"
48 using namespace lyx::support;
54 /* codes used to read/write quotes to LyX files
56 * e ``english'' (`inner quotation')
57 * s ''swedish'' ('inner quotation')
58 * g ,,german`` (,inner quotation`)
59 * p ,,polish'' (,inner quotation')
60 * c <<swiss>> (<inner quotation>)
61 * a >>danish<< (>inner quotation<)
62 * q "plain" ('inner quotation')
63 * b `british' (``inner quotation'')
64 * w >>swedishg>> ('inner quotation') ["g" = Guillemets]
65 * f <<french>> (``inner quotation'')
66 * i <<frenchin>> (<<inner quotation>>) ["in" = Imprimerie Nationale]
67 * r <<russian>> (,,inner quotation``)
68 * j [U+300C]cjk[U+300D] ([U+300E]inner quotation[U+300F]) [CORNER BRACKETS]
69 * k [U+300A]cjkangle[U+300B] ([U+3008]inner quotation[U+3009]) [ANGLE BRACKETS]
70 * x dynamic style (inherits document settings)
73 char const * const style_char = "esgpcaqbwfirjkx";
74 char const * const side_char = "lr" ;
75 char const * const level_char = "sd";
80 /////////////////////////////////////////////////////////////////////
84 ///////////////////////////////////////////////////////////////////////
86 InsetQuotesParams quoteparams;
89 int InsetQuotesParams::stylescount() const
91 return strlen(style_char);
95 char_type InsetQuotesParams::getQuoteChar(QuoteStyle const & style, QuoteLevel const & level,
96 QuoteSide const & side) const
98 // main opening quotation mark
99 char_type left_primary;
100 // main closing quotation mark
101 char_type right_primary;
102 // secondary (inner, 'single') opening quotation mark
103 char_type left_secondary;
104 // secondary (inner, 'single') closing quotation mark
105 char_type right_secondary;
108 case EnglishQuotes: {
109 left_primary = 0x201c; // ``
110 right_primary = 0x201d; // ''
111 left_secondary = 0x2018; // `
112 right_secondary = 0x2019; // '
115 case SwedishQuotes: {
116 left_primary = 0x201d; // ''
117 right_primary = 0x201d; // ''
118 left_secondary = 0x2019; // '
119 right_secondary = 0x2019; // '
123 left_primary = 0x201e; // ,,
124 right_primary = 0x201c; // ``
125 left_secondary = 0x201a; // ,
126 right_secondary = 0x2018; // `
130 left_primary = 0x201e; // ,,
131 right_primary = 0x201d; // ''
132 left_secondary = 0x201a; // ,
133 right_secondary = 0x2019; // '
137 left_primary = 0x00ab; // <<
138 right_primary = 0x00bb; // >>
139 left_secondary = 0x2039; // <
140 right_secondary = 0x203a; // >
144 left_primary = 0x00bb; // >>
145 right_primary = 0x00ab; // <<
146 left_secondary = 0x203a; // >
147 right_secondary = 0x2039; // <
151 left_primary = 0x0022; // "
152 right_primary = 0x0022; // "
153 left_secondary = 0x0027; // '
154 right_secondary = 0x0027; // '
157 case BritishQuotes: {
158 left_primary = 0x2018; // `
159 right_primary = 0x2019; // '
160 left_secondary = 0x201c; // ``
161 right_secondary = 0x201d; // ''
164 case SwedishGQuotes: {
165 left_primary = 0x00bb; // >>
166 right_primary = 0x00bb; // >>
167 left_secondary = 0x2019; // '
168 right_secondary = 0x2019; // '
172 left_primary = 0x00ab; // <<
173 right_primary = 0x00bb; // >>
174 left_secondary = 0x201c; // ``
175 right_secondary = 0x201d; // ''
178 case FrenchINQuotes:{
179 left_primary = 0x00ab; // <<
180 right_primary = 0x00bb; // >>
181 left_secondary = 0x00ab; // <<
182 right_secondary = 0x00bb; // >>
186 left_primary = 0x00ab; // <<
187 right_primary = 0x00bb; // >>
188 left_secondary = 0x201e; // ,,
189 right_secondary = 0x201c; // ``
193 left_primary = 0x300c; // LEFT CORNER BRACKET
194 right_primary = 0x300d; // RIGHT CORNER BRACKET
195 left_secondary = 0x300e; // LEFT WHITE CORNER BRACKET
196 right_secondary = 0x300f; // RIGHT WHITE CORNER BRACKET
199 case CJKAngleQuotes:{
200 left_primary = 0x300a; // LEFT DOUBLE ANGLE BRACKET
201 right_primary = 0x300b; // RIGHT DOUBLE ANGLE BRACKET
202 left_secondary = 0x3008; // LEFT ANGLE BRACKET
203 right_secondary = 0x3009; // RIGHT ANGLE BRACKET
209 left_primary = 0x003f; // ?
210 right_primary = 0x003f; // ?
211 left_secondary = 0x003f; // ?
212 right_secondary = 0x003f; // ?
217 case SecondaryQuotes:
218 return (side == OpeningQuote) ? left_secondary : right_secondary;
220 return (side == OpeningQuote) ? left_primary : right_primary;
230 docstring InsetQuotesParams::getLaTeXQuote(char_type c, string const & op) const
239 res = "\\quotesinglbase";
244 res = "\\textquoteleft";
251 res = "\\textquoteright";
260 res = "\\guilsinglleft";
267 res = "\\guilsinglright";
270 case 0x0027: {// ' (plain)
271 res = "\\textquotesingle";
277 else if (op == "babel")
280 res = "\\quotedblbase";
285 res = "\\textquotedblleft";
292 res = "\\textquotedblright";
300 else if (op == "babel")
303 res = "\\guillemotleft";
309 else if (op == "babel")
312 res = "\\guillemotright";
316 res = "\\textquotedbl";
319 // The following are fakes
320 // This is just to get something symbolic
321 // in encodings where this chars would not be used ayway
322 case 0x300c: // LEFT CORNER BRACKET
323 res = "\\ensuremath{\\lceil}";
325 case 0x300d: // RIGHT CORNER BRACKET
326 res = "\\ensuremath{\\rfloor}";
328 case 0x300e: // LEFT WHITE CORNER BRACKET
329 res = "\\ensuremath{\\llceil}";
331 case 0x300f: // RIGHT WHITE CORNER BRACKET
332 res = "\\ensuremath{\\rrfloor}";
334 case 0x300a: // LEFT DOUBLE ANGLE BRACKET
335 res = "\\ensuremath{\\langle\\kern-2.5pt\\langle}";
337 case 0x300b: // RIGHT DOUBLE ANGLE BRACKET
338 res = "\\ensuremath{\\rangle\\kern-2.5pt\\rangle}";
340 case 0x3008: // LEFT ANGLE BRACKET
341 res = "\\ensuremath{\\langle}";
343 case 0x3009: // RIGHT ANGLE BRACKET
344 res = "\\ensuremath{\\rangle}";
350 return from_ascii(res);
354 docstring InsetQuotesParams::getHTMLQuote(char_type c) const
374 case 0x0027: // ' (plain)
395 case 0x300c: // LEFT CORNER BRACKET
398 case 0x300d: // RIGHT CORNER BRACKET
401 case 0x300e: // LEFT WHITE CORNER BRACKET
404 case 0x300f: // RIGHT WHITE CORNER BRACKET
407 case 0x300a: // LEFT DOUBLE ANGLE BRACKET
410 case 0x300b: // RIGHT DOUBLE ANGLE BRACKET
413 case 0x3008: // LEFT ANGLE BRACKET
416 case 0x3009: // RIGHT ANGLE BRACKET
423 return from_ascii(res);
427 map<string, docstring> InsetQuotesParams::getTypes() const
429 map<string, docstring> res;
437 // get all quote types
438 for (sty = 0; sty < stylescount(); ++sty) {
439 style = QuoteStyle(sty);
440 if (style == DynamicQuotes)
442 for (sid = 0; sid < 2; ++sid) {
443 side = QuoteSide(sid);
444 for (lev = 0; lev < 2; ++lev) {
445 type += style_char[style];
446 type += side_char[sid];
447 level = QuoteLevel(lev);
448 type += level_char[lev];
449 res[type] = docstring(1, getQuoteChar(style, level, side));
458 docstring const InsetQuotesParams::getGuiLabel(QuoteStyle const & qs)
460 return bformat(_("%1$souter%2$s and %3$sinner%4$s[[quotation marks]]"),
461 docstring(1, quoteparams.getQuoteChar(qs, PrimaryQuotes, OpeningQuote)),
462 docstring(1, quoteparams.getQuoteChar(qs, PrimaryQuotes, ClosingQuote)),
463 docstring(1, quoteparams.getQuoteChar(qs, SecondaryQuotes, OpeningQuote)),
464 docstring(1, quoteparams.getQuoteChar(qs, SecondaryQuotes, ClosingQuote))
469 /////////////////////////////////////////////////////////////////////
473 ///////////////////////////////////////////////////////////////////////
475 InsetQuotes::InsetQuotes(Buffer * buf, string const & str) : Inset(buf)
478 global_style_ = buf->masterBuffer()->params().quotes_style;
480 global_style_ = InsetQuotesParams::EnglishQuotes;
486 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, InsetQuotesParams::QuoteLevel level,
487 string const & side, string const & style)
488 : Inset(buf), level_(level), pass_thru_(false)
490 bool dynamic = false;
492 global_style_ = buf->masterBuffer()->params().quotes_style;
493 fontenc_ = (buf->masterBuffer()->params().fontenc == "global")
494 ? lyxrc.fontenc : buf->params().fontenc;
495 dynamic = buf->masterBuffer()->params().dynamic_quotes;
497 global_style_ = InsetQuotesParams::EnglishQuotes;
498 fontenc_ = lyxrc.fontenc;
501 style_ = dynamic ? InsetQuotesParams::DynamicQuotes : global_style_;
503 style_ = getStyle(style);
505 if (side == "left" || side == "opening")
506 side_ = InsetQuotesParams::OpeningQuote;
507 else if (side == "right" || side == "closing")
508 side_ = InsetQuotesParams::ClosingQuote;
514 docstring InsetQuotes::layoutName() const
516 return from_ascii("Quotes");
520 void InsetQuotes::setSide(char_type c)
522 // Decide whether opening or closing quote
527 side_ = InsetQuotesParams::OpeningQuote;// opening quote
530 side_ = InsetQuotesParams::ClosingQuote;// closing quote
535 void InsetQuotes::parseString(string const & s, bool const allow_wildcards)
538 if (str.length() != 3) {
539 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
540 " bad string length." << endl;
546 // '.' wildcard means: keep current stylee
547 if (!allow_wildcards || str[0] != '.') {
548 for (i = 0; i < quoteparams.stylescount(); ++i) {
549 if (str[0] == style_char[i]) {
550 style_ = InsetQuotesParams::QuoteStyle(i);
554 if (i >= quoteparams.stylescount()) {
555 LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
556 " bad style specification.");
557 style_ = InsetQuotesParams::EnglishQuotes;
561 // '.' wildcard means: keep current side
562 if (!allow_wildcards || str[1] != '.') {
563 for (i = 0; i < 2; ++i) {
564 if (str[1] == side_char[i]) {
565 side_ = InsetQuotesParams::QuoteSide(i);
570 LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
571 " bad side specification.");
572 side_ = InsetQuotesParams::OpeningQuote;
576 // '.' wildcard means: keep current level
577 if (!allow_wildcards || str[2] != '.') {
578 for (i = 0; i < 2; ++i) {
579 if (str[2] == level_char[i]) {
580 level_ = InsetQuotesParams::QuoteLevel(i);
585 LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
586 " bad level specification.");
587 level_ = InsetQuotesParams::PrimaryQuotes;
593 InsetQuotesParams::QuoteStyle InsetQuotes::getStyle(string const & s)
595 InsetQuotesParams::QuoteStyle qs = InsetQuotesParams::EnglishQuotes;
598 qs = InsetQuotesParams::EnglishQuotes;
599 else if (s == "swedish")
600 qs = InsetQuotesParams::SwedishQuotes;
601 else if (s == "german")
602 qs = InsetQuotesParams::GermanQuotes;
603 else if (s == "polish")
604 qs = InsetQuotesParams::PolishQuotes;
605 else if (s == "swiss")
606 qs = InsetQuotesParams::SwissQuotes;
607 else if (s == "danish")
608 qs = InsetQuotesParams::DanishQuotes;
609 else if (s == "plain")
610 qs = InsetQuotesParams::PlainQuotes;
611 else if (s == "british")
612 qs = InsetQuotesParams::BritishQuotes;
613 else if (s == "swedishg")
614 qs = InsetQuotesParams::SwedishGQuotes;
615 else if (s == "french")
616 qs = InsetQuotesParams::FrenchQuotes;
617 else if (s == "frenchin")
618 qs = InsetQuotesParams::FrenchINQuotes;
619 else if (s == "russian")
620 qs = InsetQuotesParams::RussianQuotes;
622 qs = InsetQuotesParams::CJKQuotes;
623 else if (s == "cjkangle")
624 qs = InsetQuotesParams::CJKAngleQuotes;
625 else if (s == "dynamic")
626 qs = InsetQuotesParams::DynamicQuotes;
632 docstring InsetQuotes::displayString() const
634 // In PassThru, we use straight quotes
636 return (level_ == InsetQuotesParams::PrimaryQuotes) ?
637 from_ascii("\"") : from_ascii("'");
639 InsetQuotesParams::QuoteStyle style =
640 (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
642 docstring retdisp = docstring(1, quoteparams.getQuoteChar(style, level_, side_));
644 // in French, thin spaces are added inside double guillemets
645 if (prefixIs(context_lang_, "fr")
646 && level_ == InsetQuotesParams::PrimaryQuotes
647 && (style == InsetQuotesParams::SwissQuotes
648 || style == InsetQuotesParams::FrenchQuotes
649 || style == InsetQuotesParams::FrenchINQuotes)) {
650 // THIN SPACE (U+2009)
651 char_type const thin_space = 0x2009;
652 if (side_ == InsetQuotesParams::OpeningQuote)
653 retdisp += thin_space;
655 retdisp = thin_space + retdisp;
662 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
664 FontInfo & font = mi.base.font;
665 frontend::FontMetrics const & fm = theFontMetrics(font);
666 dim.asc = fm.maxAscent();
667 dim.des = fm.maxDescent();
668 dim.wid = fm.width(displayString());
672 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
674 FontInfo font = pi.base.font;
675 if (style_ == InsetQuotesParams::DynamicQuotes)
676 font.setPaintColor(Color_special);
678 font.setPaintColor(pi.textColor(font.realColor()));
679 pi.pain.text(x, y, displayString(), font);
683 string InsetQuotes::getType() const
686 text += style_char[style_];
687 text += side_char[side_];
688 text += level_char[level_];
693 void InsetQuotes::write(ostream & os) const
695 os << "Quotes " << getType();
699 void InsetQuotes::read(Lexer & lex)
701 lex.setContext("InsetQuotes::read");
703 parseString(lex.getString());
704 lex >> "\\end_inset";
708 void InsetQuotes::doDispatch(Cursor & cur, FuncRequest & cmd)
710 switch (cmd.action()) {
711 case LFUN_INSET_MODIFY: {
712 string const first_arg = cmd.getArg(0);
713 bool const change_type = first_arg == "changetype";
716 // this will not be handled higher up
720 cur.recordUndoInset(this);
721 parseString(cmd.getArg(1), true);
722 cur.buffer()->updateBuffer();
726 Inset::doDispatch(cur, cmd);
732 bool InsetQuotes::getStatus(Cursor & cur, FuncRequest const & cmd,
733 FuncStatus & flag) const
735 switch (cmd.action()) {
737 case LFUN_INSET_MODIFY: {
738 string const first_arg = cmd.getArg(0);
739 if (first_arg == "changetype") {
740 string const type = cmd.getArg(1);
741 flag.setOnOff(type == getType());
742 flag.setEnabled(!pass_thru_);
745 return Inset::getStatus(cur, cmd, flag);
749 return Inset::getStatus(cur, cmd, flag);
754 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
756 InsetQuotesParams::QuoteStyle style =
757 (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
758 char_type quotechar = quoteparams.getQuoteChar(style, level_, side_);
761 // In pass-thru context, we output plain quotes
762 if (runparams.pass_thru)
763 qstr = (level_ == InsetQuotesParams::PrimaryQuotes) ? from_ascii("\"") : from_ascii("'");
764 else if (style == InsetQuotesParams::PlainQuotes && runparams.isFullUnicode()) {
765 // For XeTeX and LuaTeX,we need to disable mapping to get straight
766 // quotes. We define our own commands that do this
767 qstr = (level_ == InsetQuotesParams::PrimaryQuotes) ?
768 from_ascii("\\textquotedblplain") : from_ascii("\\textquotesingleplain");
770 else if (runparams.use_polyglossia) {
771 // For polyglossia, we directly output the respective unicode chars
772 // (spacing and kerning is then handled respectively)
773 qstr = docstring(1, quotechar);
775 else if (style == InsetQuotesParams::CJKQuotes || style == InsetQuotesParams::CJKAngleQuotes) {
776 if (runparams.encoding && runparams.encoding->encodable(quotechar))
777 qstr = docstring(1, quotechar);
779 qstr = quoteparams.getLaTeXQuote(quotechar, "int");
781 else if ((style == InsetQuotesParams::SwissQuotes
782 || style == InsetQuotesParams::FrenchQuotes
783 || style == InsetQuotesParams::FrenchINQuotes)
784 && level_ == InsetQuotesParams::PrimaryQuotes
785 && prefixIs(runparams.local_font->language()->code(), "fr")) {
786 // Specific guillemets of French babel
787 // including correct French spacing
788 if (side_ == InsetQuotesParams::OpeningQuote)
789 qstr = from_ascii("\\og");
791 qstr = from_ascii("\\fg");
792 } else if (fontenc_ == "T1"
793 && !runparams.local_font->language()->internalFontEncoding()) {
794 // Quotation marks for T1 font encoding
796 qstr = quoteparams.getLaTeXQuote(quotechar, "t1");
797 } else if (runparams.local_font->language()->internalFontEncoding()) {
798 // Quotation marks for internal font encodings
799 // (ligatures not featured)
800 qstr = quoteparams.getLaTeXQuote(quotechar, "int");
801 #ifdef DO_USE_DEFAULT_LANGUAGE
802 } else if (doclang == "default") {
804 } else if (!runparams.use_babel || runparams.isFullUnicode()) {
806 // Standard quotation mark macros
807 // These are also used by babel
808 // without fontenc (XeTeX/LuaTeX)
809 qstr = quoteparams.getLaTeXQuote(quotechar, "ot1");
811 // Babel shorthand quotation marks (for T1/OT1)
812 qstr = quoteparams.getLaTeXQuote(quotechar, "babel");
815 if (!runparams.pass_thru) {
816 // Guard against unwanted ligatures with preceding text
817 char_type const lastchar = os.lastChar();
818 // !` ?` => !{}` ?{}`
819 if (prefixIs(qstr, from_ascii("`"))
820 && (lastchar == '!' || lastchar == '?'))
822 // ``` ''' ,,, <<< >>>
823 // => `{}`` '{}'' ,{},, <{}<< >{}>>
824 if (contains(from_ascii(",'`<>"), lastchar)
825 && prefixIs(qstr, lastchar))
831 if (prefixIs(qstr, from_ascii("\\")) && !suffixIs(qstr, '}'))
832 // properly terminate the command depending on the context
837 int InsetQuotes::plaintext(odocstringstream & os,
838 OutputParams const &, size_t) const
840 docstring const str = displayString();
846 docstring InsetQuotes::getQuoteEntity() const {
847 InsetQuotesParams::QuoteStyle style =
848 (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
849 docstring res = quoteparams.getHTMLQuote(quoteparams.getQuoteChar(style, level_, side_));
850 // in French, thin spaces are added inside double guillemets
851 if (prefixIs(context_lang_, "fr")
852 && level_ == InsetQuotesParams::PrimaryQuotes
853 && (style == InsetQuotesParams::FrenchQuotes
854 || style == InsetQuotesParams::FrenchINQuotes
855 || style == InsetQuotesParams::SwissQuotes)) {
856 // THIN SPACE (U+2009)
857 docstring const thin_space = from_ascii(" ");
858 if (side_ == InsetQuotesParams::OpeningQuote)
861 res = thin_space + res;
867 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
869 os << getQuoteEntity();
874 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
876 xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
881 void InsetQuotes::toString(odocstream & os) const
883 os << displayString();
887 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
889 os += displayString();
893 void InsetQuotes::updateBuffer(ParIterator const & it, UpdateType /* utype*/)
895 BufferParams const & bp = buffer().masterBuffer()->params();
896 pass_thru_ = it.paragraph().isPassThru();
897 context_lang_ = it.paragraph().getFontSettings(bp, it.pos()).language()->code();
898 fontenc_ = (bp.fontenc == "global") ? lyxrc.fontenc : bp.fontenc;
899 global_style_ = bp.quotes_style;
903 void InsetQuotes::validate(LaTeXFeatures & features) const
905 InsetQuotesParams::QuoteStyle style =
906 (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
907 char_type type = quoteparams.getQuoteChar(style, level_, side_);
909 // Handle characters that are not natively supported by
910 // specific font encodings (we roll our own definitions)
911 #ifdef DO_USE_DEFAULT_LANGUAGE
912 if (features.bufferParams().language->lang() == "default"
914 if (!features.useBabel()
916 && !features.runparams().isFullUnicode() && fontenc_ != "T1") {
919 features.require("quotesinglbase");
922 features.require("guilsinglleft");
925 features.require("guilsinglright");
928 features.require("quotedblbase");
931 features.require("guillemotleft");
934 features.require("guillemotright");
940 // Handle straight quotation marks. These need special care
941 // in most output formats
944 if (features.runparams().isFullUnicode())
945 features.require("textquotesinglep");
947 features.require("textcomp");
951 if (features.runparams().isFullUnicode())
952 features.require("textquotedblp");
953 else if (fontenc_ != "T1")
954 features.require("textquotedbl");
957 // we fake these from math
958 case 0x300e: // LEFT WHITE CORNER BRACKET
959 case 0x300f: // RIGHT WHITE CORNER BRACKET
960 if (!features.runparams().encoding
961 || !features.runparams().encoding->encodable(type))
962 features.require("stmaryrd");
970 string InsetQuotes::contextMenuName() const
972 return "context-quote";