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
8 * Full author contact details are available in file CREDITS.
13 #include "InsetQuotes.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
19 #include "Dimension.h"
21 #include "FuncStatus.h"
22 #include "FuncRequest.h"
24 #include "LaTeXFeatures.h"
27 #include "MetricsInfo.h"
28 #include "OutputParams.h"
29 #include "output_xhtml.h"
30 #include "Paragraph.h"
31 #include "ParIterator.h"
32 #include "texstream.h"
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
37 #include "support/debug.h"
38 #include "support/docstring.h"
39 #include "support/docstream.h"
40 #include "support/lstrings.h"
43 using namespace lyx::support;
49 /* codes used to read/write quotes to LyX files
58 char const * const language_char = "esgpfa";
59 char const * const side_char = "lr" ;
60 char const * const times_char = "sd";
62 // List of known quote chars
63 char const * const quote_char = ",'`<>";
65 // Unicode characters needed by each quote type
66 char_type const display_quote_char[2][5] = {
67 { 0x201a, 0x2019, 0x2018, 0x2039, 0x203a},
68 { 0x201e, 0x201d, 0x201c, 0x00ab, 0x00bb}
71 // Index of chars used for the quote. Index is [side, language]
72 int quote_index[2][6] = {
73 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
74 { 1, 1, 2, 1, 4, 3 } // "`'`'><"
77 // Corresponding LaTeX code, for double and single quotes.
78 char const * const latex_quote_t1[2][5] = {
79 { "\\quotesinglbase", "'", "`",
80 "\\guilsinglleft", "\\guilsinglright" },
81 { ",,", "''", "``", "<<", ">>" }
84 char const * const latex_quote_ot1[2][5] = {
85 { "\\quotesinglbase", "'", "`",
86 "\\guilsinglleft", "\\guilsinglright" },
87 { "\\quotedblbase", "''", "``",
88 "\\guillemotleft", "\\guillemotright" }
91 char const * const latex_quote_noligatures[2][5] = {
92 { "\\quotesinglbase", "\\textquoteleft", "\\textquoteright",
93 "\\guilsinglleft", "\\guilsinglright" },
94 { "\\quotedblbase", "\\textquotedblleft", "\\textquotedblright",
95 "\\guillemotleft", "\\guillemotright" }
98 char const * const latex_quote_babel[2][5] = {
99 { "\\glq", "'", "`", "\\flq", "\\frq" },
100 { "\\glqq", "''", "``", "\\flqq", "\\frqq" }
103 char const * const html_quote[2][5] = {
104 { "‚", "’", "‘",
105 "‹", "›" },
106 { "„", "”", "“", "«", "»" }
112 InsetQuotes::InsetQuotes(Buffer * buf, string const & str) : Inset(buf)
118 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, QuoteTimes t,
119 string const & s, string const & l)
120 : Inset(buf), times_(t), pass_thru_(false)
123 language_ = l.empty() ? buf->params().quotes_language : getLanguage(l);
124 fontenc_ = (buf->params().fontenc == "global")
125 ? lyxrc.fontenc : buf->params().fontenc;
127 language_ = l.empty() ? EnglishQuotes : getLanguage(l);
128 fontenc_ = lyxrc.fontenc;
133 else if (s == "right")
140 docstring InsetQuotes::layoutName() const
142 return from_ascii("Quotes");
146 void InsetQuotes::setSide(char_type c)
148 // Decide whether left or right
153 side_ = LeftQuote; // left quote
156 side_ = RightQuote; // right quote
161 void InsetQuotes::parseString(string const & s)
164 if (str.length() != 3) {
165 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
166 " bad string length." << endl;
172 for (i = 0; i < 6; ++i) {
173 if (str[0] == language_char[i]) {
174 language_ = QuoteLanguage(i);
179 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
180 " bad language specification." << endl;
181 language_ = EnglishQuotes;
184 for (i = 0; i < 2; ++i) {
185 if (str[1] == side_char[i]) {
186 side_ = QuoteSide(i);
191 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
192 " bad side specification." << endl;
196 for (i = 0; i < 2; ++i) {
197 if (str[2] == times_char[i]) {
198 times_ = QuoteTimes(i);
203 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
204 " bad times specification." << endl;
205 times_ = DoubleQuotes;
210 InsetQuotes::QuoteLanguage InsetQuotes::getLanguage(string const & s)
212 QuoteLanguage ql = EnglishQuotes;
216 else if (s == "swedish")
218 else if (s == "german")
220 else if (s == "polish")
222 else if (s == "french")
224 else if (s == "danish")
231 map<string, docstring> InsetQuotes::getTypes() const
233 map<string, docstring> res;
241 // get all quote types
242 for (l = 0; l < 6; ++l) {
243 lang = QuoteLanguage(l);
244 for (s = 0; s < 2; ++s) {
246 for (t = 0; t < 2; ++t) {
247 type += language_char[lang];
248 type += side_char[s];
249 times = QuoteTimes(t);
250 type += times_char[t];
251 res[type] = docstring(1, display_quote_char[times][quote_index[side][lang]]);
260 docstring InsetQuotes::displayString() const
262 // In PassThru, we use straight quotes
264 return (times_ == DoubleQuotes) ? from_ascii("\"") : from_ascii("'");
266 int const index = quote_index[side_][language_];
267 docstring retdisp = docstring(1, display_quote_char[times_][index]);
269 // in French, thin spaces are added inside double guillemets
270 // FIXME: this should be done by a separate quote type.
271 if (prefixIs(context_lang_, "fr")
272 && times_ == DoubleQuotes && language_ == FrenchQuotes) {
273 // THIN SPACE (U+2009)
274 char_type const thin_space = 0x2009;
275 if (side_ == LeftQuote)
276 retdisp += thin_space;
278 retdisp = thin_space + retdisp;
285 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
287 FontInfo & font = mi.base.font;
288 frontend::FontMetrics const & fm = theFontMetrics(font);
289 dim.asc = fm.maxAscent();
290 dim.des = fm.maxDescent();
291 dim.wid = fm.width(displayString());
295 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
297 FontInfo font = pi.base.font;
298 font.setPaintColor(pi.textColor(font.realColor()));
299 pi.pain.text(x, y, displayString(), font);
303 string InsetQuotes::getType() const
306 text += language_char[language_];
307 text += side_char[side_];
308 text += times_char[times_];
313 void InsetQuotes::write(ostream & os) const
315 os << "Quotes " << getType();
319 void InsetQuotes::read(Lexer & lex)
321 lex.setContext("InsetQuotes::read");
323 parseString(lex.getString());
324 lex >> "\\end_inset";
328 void InsetQuotes::doDispatch(Cursor & cur, FuncRequest & cmd)
330 switch (cmd.action()) {
331 case LFUN_INSET_MODIFY: {
332 string const first_arg = cmd.getArg(0);
333 bool const change_type = first_arg == "changetype";
336 // this will not be handled higher up
340 cur.recordUndoInset(this);
341 parseString(cmd.getArg(1));
342 cur.buffer()->updateBuffer();
346 Inset::doDispatch(cur, cmd);
352 bool InsetQuotes::getStatus(Cursor & cur, FuncRequest const & cmd,
353 FuncStatus & flag) const
355 switch (cmd.action()) {
357 case LFUN_INSET_MODIFY: {
358 string const first_arg = cmd.getArg(0);
359 if (first_arg == "changetype") {
360 string const type = cmd.getArg(1);
361 flag.setOnOff(type == getType());
362 flag.setEnabled(!pass_thru_);
365 return Inset::getStatus(cur, cmd, flag);
369 return Inset::getStatus(cur, cmd, flag);
374 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
376 const int quoteind = quote_index[side_][language_];
379 // In pass-thru context, we output plain quotes
380 if (runparams.pass_thru)
381 qstr = (times_ == DoubleQuotes) ? from_ascii("\"") : from_ascii("'");
382 else if (runparams.use_polyglossia) {
383 // For polyglossia, we directly output the respective unicode chars
384 // (spacing and kerning is then handled respectively)
385 qstr = docstring(1, display_quote_char[times_][quoteind]);
387 else if (language_ == FrenchQuotes && times_ == DoubleQuotes
388 && prefixIs(runparams.local_font->language()->code(), "fr")) {
389 // Specific guillemets of French babel
390 // including correct French spacing
391 if (side_ == LeftQuote)
392 qstr = from_ascii("\\og");
394 qstr = from_ascii("\\fg");
395 } else if (fontenc_ == "T1"
396 && !runparams.local_font->language()->internalFontEncoding()) {
397 // Quotation marks for T1 font encoding
399 qstr = from_ascii(latex_quote_t1[times_][quoteind]);
400 } else if (runparams.local_font->language()->internalFontEncoding()) {
401 // Quotation marks for internal font encodings
402 // (ligatures not featured)
403 qstr = from_ascii(latex_quote_noligatures[times_][quoteind]);
404 #ifdef DO_USE_DEFAULT_LANGUAGE
405 } else if (doclang == "default") {
407 } else if (!runparams.use_babel || runparams.isFullUnicode()) {
409 // Standard quotation mark macros
410 // These are also used by babel
411 // without fontenc (XeTeX/LuaTeX)
412 qstr = from_ascii(latex_quote_ot1[times_][quoteind]);
414 // Babel shorthand quotation marks (for T1/OT1)
415 qstr = from_ascii(latex_quote_babel[times_][quoteind]);
418 if (!runparams.pass_thru) {
419 // Guard against unwanted ligatures with preceding text
420 char_type const lastchar = os.lastChar();
421 // !` ?` => !{}` ?{}`
422 if (prefixIs(qstr, from_ascii("`"))
423 && (lastchar == '!' || lastchar == '?'))
425 // ``` ''' ,,, <<< >>>
426 // => `{}`` '{}'' ,{},, <{}<< >{}>>
427 if (contains(from_ascii(",'`<>"), lastchar)
428 && prefixIs(qstr, lastchar))
434 if (prefixIs(qstr, from_ascii("\\")))
435 // properly terminate the command depending on the context
440 int InsetQuotes::plaintext(odocstringstream & os,
441 OutputParams const &, size_t) const
443 docstring const str = displayString();
449 docstring InsetQuotes::getQuoteEntity() const {
450 const int quoteind = quote_index[side_][language_];
451 docstring res = from_ascii(html_quote[times_][quoteind]);
452 // in French, thin spaces are added inside double guillemets
453 // FIXME: this should be done by a separate quote type.
454 if (prefixIs(context_lang_, "fr")
455 && times_ == DoubleQuotes && language_ == FrenchQuotes) {
456 // THIN SPACE (U+2009)
457 docstring const thin_space = from_ascii(" ");
458 if (side_ == LeftQuote)
461 res = thin_space + res;
467 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
469 os << getQuoteEntity();
474 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
476 xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
481 void InsetQuotes::toString(odocstream & os) const
483 os << displayString();
487 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
489 os += displayString();
493 void InsetQuotes::updateBuffer(ParIterator const & it, UpdateType /* utype*/)
495 BufferParams const & bp = buffer().masterBuffer()->params();
496 pass_thru_ = it.paragraph().isPassThru();
497 context_lang_ = it.paragraph().getFontSettings(bp, it.pos()).language()->code();
501 void InsetQuotes::validate(LaTeXFeatures & features) const
503 char type = quote_char[quote_index[side_][language_]];
505 #ifdef DO_USE_DEFAULT_LANGUAGE
506 if (features.bufferParams().language->lang() == "default"
508 if (!features.useBabel()
510 && !features.usePolyglossia() && fontenc_ != "T1") {
511 if (times_ == SingleQuotes)
513 case ',': features.require("quotesinglbase"); break;
514 case '<': features.require("guilsinglleft"); break;
515 case '>': features.require("guilsinglright"); break;
520 case ',': features.require("quotedblbase"); break;
521 case '<': features.require("guillemotleft"); break;
522 case '>': features.require("guillemotright"); break;
529 string InsetQuotes::contextMenuName() const
531 return "context-quote";