#include "Font.h"
#include "InsetList.h"
#include "Language.h"
+#include "LaTeXFeatures.h"
#include "Layout.h"
#include "LyXRC.h"
#include "OutputParams.h"
string const getPolyglossiaBegin(string const & lang_begin_command,
- string const & lang, string const & opts)
+ string const & lang, string const & opts,
+ bool const localswitch = false)
{
string result;
- if (!lang.empty())
- result = subst(lang_begin_command, "$$lang", lang);
+ if (!lang.empty()) {
+ // we need to revert the upcasing done in getPolyglossiaEnvName()
+ // in case we have a local polyglossia command (\textarabic).
+ string language = localswitch ? ascii_lowercase(lang) : lang;
+ result = subst(lang_begin_command, "$$lang", language);
+ }
string options = opts.empty() ?
string() : "[" + opts + "]";
result = subst(result, "$$opts", options);
<< "}\n";
} else
os << from_ascii(style.latexparam()) << '\n';
+ if (style.latextype == LATEX_BIB_ENVIRONMENT
+ || style.latextype == LATEX_ITEM_ENVIRONMENT
+ || style.latextype == LATEX_LIST_ENVIRONMENT) {
+ OutputParams rp = runparams;
+ rp.local_font = &pit->getFirstFontSettings(bparams);
+ latexArgInsets(paragraphs, pit, os, rp, style.listpreamble(),
+ "listpreamble:");
+ }
}
data.style = &style;
data.cjk_nested = false;
if (data.par_language->encoding()->package() == Encoding::CJK &&
state->open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
- if (prev_par_language->encoding()->package() == Encoding::CJK)
- os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
+ if (prev_par_language->encoding()->package() == Encoding::CJK) {
+ docstring const cjkenc = (bparams.encoding().name() == "utf8-cjk"
+ && LaTeXFeatures::isAvailable("CJKutf8")) ?
+ from_ascii("UTF8")
+ : from_ascii(data.par_language->encoding()->latexName());
+ os << "\\begin{CJK}{" << cjkenc
<< "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
+ }
state->open_encoding_ = CJK;
data.cjk_nested = true;
}
pit_type & pit, otexstream & os)
{
ParagraphList const & paragraphs = text.paragraphs();
- ParagraphList::const_iterator par = paragraphs.constIterator(pit);
+ ParagraphList::const_iterator ipar = paragraphs.constIterator(pit);
LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
- Layout const & current_layout = par->layout();
- depth_type const current_depth = par->params().depth();
- Length const & current_left_indent = par->params().leftIndent();
+ Layout const & current_layout = ipar->layout();
+ depth_type const current_depth = ipar->params().depth();
+ Length const & current_left_indent = ipar->params().leftIndent();
// This is for debugging purpose at the end.
pit_type const par_begin = pit;
// Default and preset args are always output, so if they require
// other arguments, consider this.
- Layout::LaTeXArgMap::const_iterator lit = latexargs.begin();
- Layout::LaTeXArgMap::const_iterator const lend = latexargs.end();
- for (; lit != lend; ++lit) {
- Layout::latexarg arg = (*lit).second;
+ for (auto const & larg : latexargs) {
+ Layout::latexarg const & arg = larg.second;
if ((!arg.presetarg.empty() || !arg.defaultarg.empty()) && !arg.requires.empty()) {
vector<string> req = getVectorFromString(arg.requires);
required.insert(required.end(), req.begin(), req.end());
latexargs.find(ins->name());
if (lait != latexargs.end()) {
Layout::latexarg arg = (*lait).second;
- docstring ldelim = arg.mandatory ?
+ docstring ldelim;
+ docstring rdelim;
+ if (!arg.nodelims) {
+ ldelim = arg.mandatory ?
from_ascii("{") : from_ascii("[");
- docstring rdelim = arg.mandatory ?
+ rdelim = arg.mandatory ?
from_ascii("}") : from_ascii("]");
+ }
if (!arg.ldelim.empty())
ldelim = arg.ldelim;
if (!arg.rdelim.empty())
rdelim = arg.rdelim;
ins->latexArgument(os, runparams, ldelim, rdelim, arg.presetarg);
+ if (prefix == "listpreamble:")
+ os << breakln;
inserted = true;
}
}
{
switch (style.latextype) {
case LATEX_COMMAND:
+ if (par.needsCProtection(runparams.moving_arg))
+ os << "\\cprotect";
os << '\\' << from_ascii(style.latexname());
// Command arguments
break;
case LATEX_ITEM_ENVIRONMENT:
case LATEX_LIST_ENVIRONMENT:
- os << "\\" + style.itemcommand();
- // Item arguments
- if (!style.itemargs().empty())
- latexArgInsets(par, os, runparams, style.itemargs(), "item:");
- os << " ";
+ if (runparams.for_search) {
+ os << "\\" + style.itemcommand() << "{" << style.latexname() << "}";
+ }
+ else {
+ os << "\\" + style.itemcommand();
+ // Item arguments
+ if (!style.itemargs().empty())
+ latexArgInsets(par, os, runparams, style.itemargs(), "item:");
+ os << " ";
+ }
break;
case LATEX_BIB_ENVIRONMENT:
// ignore this, the inset will write itself
otexstream & os,
OutputParams const & runparams_in,
string const & everypar,
- int start_pos, int end_pos)
+ int start_pos, int end_pos,
+ bool const force)
{
BufferParams const & bparams = runparams_in.is_child
? buf.masterParams() : buf.params();
Layout const & style = text.inset().forcePlainLayout() ?
bparams.documentClass().plainLayout() : par.layout();
- if (style.inpreamble)
+ if (style.inpreamble && !force)
return;
LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
os << '\n';
}
- par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
+ par.latex(bparams, outerfont, os, runparams, start_pos, end_pos, force);
return;
}
Paragraph const * nextpar = runparams.isLastPar
? 0 : ¶graphs.at(pit + 1);
+ bool const intitle_command = style.intitle && style.latextype == LATEX_COMMAND;
+
if (style.pass_thru) {
Font const outerfont = text.outerFont(pit);
parStartCommand(par, os, runparams, style);
+ if (intitle_command)
+ os << '{';
- par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
+ par.latex(bparams, outerfont, os, runparams, start_pos, end_pos, force);
// I did not create a parEndCommand for this minuscule
// task because in the other user of parStartCommand
// environment with nesting depth greater than (or equal to, but with
// a different layout) the current one. If there is no previous
// paragraph, the previous language is the outer language.
+ // Note further that we take the outer language also if the prior par
+ // is PassThru, since in that case it has latex_language, and all secondary
+ // languages have been closed (#10793).
bool const use_prev_env_language = state->prev_env_language_ != 0
&& priorpar
&& priorpar->layout().isEnvironment()
|| (priorpar->getDepth() == par.getDepth()
&& priorpar->layout() != par.layout()));
Language const * const prev_language =
- (pit != 0)
- ? (use_prev_env_language ? state->prev_env_language_
- : priorpar->getParLanguage(bparams))
- : outer_language;
-
+ runparams_in.for_search ?
+ languages.getLanguage("ignore")
+ :(priorpar && !priorpar->isPassThru())
+ ? (use_prev_env_language ? state->prev_env_language_
+ : priorpar->getParLanguage(bparams))
+ : outer_language;
bool const use_polyglossia = runparams.use_polyglossia;
string const par_lang = use_polyglossia ?
// For InTitle commands, we need to switch the language inside the command
// (see #10849); thus open the command here.
- bool const intitle_command = style.intitle && style.latextype == LATEX_COMMAND;
if (intitle_command) {
parStartCommand(par, os, runparams, style);
os << '{';
}
- // In some insets (such as Arguments), we cannot use \selectlanguage
- bool const localswitch = text.inset().forceLocalFontSwitch()
- || (using_begin_end && text.inset().forcePlainLayout());
+ // In some insets (such as Arguments), we cannot use \selectlanguage.
+ // Also, if an RTL language is set via environment in polyglossia,
+ // only a nested \\text<lang> command will reset the direction for LTR
+ // languages (see # 10111).
+ bool const in_polyglossia_rtl_env =
+ use_polyglossia
+ && runparams.local_font != 0
+ && outer_language->rightToLeft()
+ && !par_language->rightToLeft();
+ bool const localswitch = runparams_in.for_search
+ || text.inset().forceLocalFontSwitch()
+ || (using_begin_end && text.inset().forcePlainLayout())
+ || in_polyglossia_rtl_env;
if (localswitch) {
lang_begin_command = use_polyglossia ?
"\\text$$lang$$opts{" : lyxrc.language_command_local;
lang_command_termination.clear();
}
- if (par_lang != prev_lang
- // check if we already put language command in TeXEnvironment()
- && !(style.isEnvironment()
- && (pit == 0 || (priorpar->layout() != par.layout()
- && priorpar->getDepth() <= par.getDepth())
- || priorpar->getDepth() < par.getDepth())))
- {
- if ((!using_begin_end || langOpenedAtThisLevel(state)) &&
- !lang_end_command.empty() &&
- prev_lang != outer_lang &&
- !prev_lang.empty() &&
- (!using_begin_end || !style.isEnvironment()))
- {
+ bool const localswitch_needed = localswitch && par_lang != outer_lang;
+
+ // localswitches need to be closed and reopened at each par
+ if (runparams_in.for_search || ((par_lang != prev_lang || localswitch_needed)
+ // check if we already put language command in TeXEnvironment()
+ && !(style.isEnvironment()
+ && (pit == 0 || (priorpar->layout() != par.layout()
+ && priorpar->getDepth() <= par.getDepth())
+ || priorpar->getDepth() < par.getDepth())))) {
+ if (!localswitch
+ && (!using_begin_end || langOpenedAtThisLevel(state))
+ && !lang_end_command.empty()
+ && prev_lang != outer_lang
+ && !prev_lang.empty()
+ && (!using_begin_end || !style.isEnvironment())) {
os << from_ascii(subst(lang_end_command,
- "$$lang",
- prev_lang))
+ "$$lang",
+ prev_lang))
<< lang_command_termination;
if (using_begin_end)
popLanguageName();
// This behavior is not correct for ArabTeX, though.
if (!using_begin_end
// not for ArabTeX
- && par_language->lang() != "arabic_arabtex"
- && outer_language->lang() != "arabic_arabtex"
+ && par_language->lang() != "arabic_arabtex"
+ && outer_language->lang() != "arabic_arabtex"
// are we in an inset?
&& runparams.local_font != 0
// is the inset within an \L or \R?
os << "\\L{";
}
// With CJK, the CJK tag has to be closed first (see below)
- if (runparams.encoding->package() != Encoding::CJK
+ if ((runparams.encoding->package() != Encoding::CJK || runparams.for_search)
&& (par_lang != openLanguageName(state) || localswitch)
&& !par_lang.empty()) {
string bc = use_polyglossia ?
- getPolyglossiaBegin(lang_begin_command, par_lang, par_language->polyglossiaOpts())
+ getPolyglossiaBegin(lang_begin_command, par_lang,
+ par_language->polyglossiaOpts(),
+ localswitch)
: subst(lang_begin_command, "$$lang", par_lang);
os << bc;
os << lang_command_termination;
// context (nesting issue).
if (par_language->encoding()->package() == Encoding::CJK
&& state->open_encoding_ != CJK && state->cjk_inherited_ == 0) {
- os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
+ docstring const cjkenc = (bparams.encoding().name() == "utf8-cjk"
+ && LaTeXFeatures::isAvailable("CJKutf8")) ?
+ from_ascii("UTF8")
+ : from_ascii(par_language->encoding()->latexName());
+ os << "\\begin{CJK}{" << cjkenc
<< "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
state->open_encoding_ = CJK;
}
if (runparams.encoding->package() == Encoding::CJK
&& par_lang != openLanguageName(state)
&& !par_lang.empty()) {
- os << from_ascii(subst(
- lang_begin_command,
- "$$lang",
- par_lang))
- << lang_command_termination;
+ string bc = use_polyglossia ?
+ getPolyglossiaBegin(lang_begin_command, par_lang,
+ par_language->polyglossiaOpts(),
+ localswitch)
+ : subst(lang_begin_command, "$$lang", par_lang);
+ os << bc
+ << lang_command_termination;
if (using_begin_end)
pushLanguageName(par_lang, localswitch);
}
// FIXME UNICODE
os << from_utf8(everypar);
- par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
+ par.latex(bparams, outerfont, os, runparams, start_pos, end_pos, force);
Font const font = par.empty()
? par.getLayoutFont(bparams, outerfont)
&&((nextpar && par_lang != nextpar_lang)
|| (runparams.isLastPar && par_lang != outer_lang));
- if ((intitle_command && using_begin_end)
+ if (localswitch_needed
+ || (intitle_command && using_begin_end)
|| closing_rtl_ltr_environment
|| ((runparams.isLastPar || close_lang_switch)
&& (par_lang != outer_lang || (using_begin_end
// we need to reset the language at the end of footnote or
// float.
- if (pending_newline || close_lang_switch)
+ if (!localswitch && (pending_newline || close_lang_switch))
os << '\n';
// when the paragraph uses CJK, the language has to be closed earlier
- if (font.language()->encoding()->package() != Encoding::CJK) {
+ if ((font.language()->encoding()->package() != Encoding::CJK) || runparams_in.for_search) {
if (lang_end_command.empty()) {
// If this is a child, we should restore the
// master language after the last paragraph.
&& current_lang != openLanguageName(state)) {
string bc = use_polyglossia ?
getPolyglossiaBegin(lang_begin_command, current_lang,
- current_language->polyglossiaOpts())
+ current_language->polyglossiaOpts(),
+ localswitch)
: subst(lang_begin_command, "$$lang", current_lang);
os << bc;
pending_newline = !localswitch;
&& style != nextpar->layout())))
|| (atSameLastLangSwitchDepth(state)
&& state->lang_switch_depth_.size()
- && cur_lang != par_lang))
+ && cur_lang != par_lang)
+ || in_polyglossia_rtl_env)
{
if (using_begin_end && !localswitch)
os << breakln;
// if this is a CJK-paragraph and the next isn't, close CJK
// also if the next paragraph is a multilingual environment (because of nesting)
if (nextpar
- && state->open_encoding_ == CJK
+ && (state->open_encoding_ == CJK && bparams.encoding().iconvName() != "UTF-8")
&& (nextpar_language->encoding()->package() != Encoding::CJK
|| (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
// inbetween environments, CJK has to be closed later (nesting!)
}
// If this is the last paragraph, close the CJK environment
- // if necessary. If it's an environment, we'll have to \end that first.
- if (runparams.isLastPar && !style.isEnvironment()) {
+ // if necessary. If it's an environment or nested in an environment,
+ // we'll have to \end that first.
+ if (runparams.isLastPar && !style.isEnvironment()
+ && par.params().depth() < 1) {
switch (state->open_encoding_) {
case CJK: {
// do nothing at the end of child documents
if (maintext && buf.masterBuffer() != &buf)
break;
- // end of main text
+ // end of main text: also insert a \clearpage (see #5386)
if (maintext) {
- os << "\n\\end{CJK}\n";
+ os << "\n\\clearpage\n\\end{CJK}\n";
// end of an inset
} else
os << "\\end{CJK}";
}
// Open a CJK environment at the beginning of the main buffer
- // if the document's language is a CJK language
- // (but not in child documents)
+ // (but not in child documents or documents using system fonts)
+ // if the document's language is a CJK language (with some exceptions)
+ // or the document encoding is utf8-cjk:
OutputState * state = getOutputState();
- if (maintext && !is_child
- && bparams.encoding().package() == Encoding::CJK) {
- os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
- << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
+ if (maintext && !is_child && !bparams.useNonTeXFonts
+ && ((bparams.language->encoding()->package() == Encoding::CJK
+ && (bparams.encoding().iconvName() != "UTF-8"
+ || bparams.encoding().name() == "utf8-cjk"
+ || bparams.encoding().name() == "utf8" ))
+ || (bparams.encoding().name() == "utf8-cjk"
+ && LaTeXFeatures::isAvailable("CJKutf8")))) {
+ docstring const cjkenc = bparams.encoding().iconvName() == "UTF-8"
+ ? from_ascii("UTF8") : from_ascii(bparams.encoding().latexName());
+ os << "\\begin{CJK}{" << cjkenc
+ << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
state->open_encoding_ = CJK;
}
// if "auto begin" is switched off, explicitly switch the
// If the last paragraph is an environment, we'll have to close
// CJK at the very end to do proper nesting.
if (maintext && !is_child && state->open_encoding_ == CJK) {
- os << "\\end{CJK}\n";
+ os << "\\clearpage\n\\end{CJK}\n";
state->open_encoding_ = none;
}
// Likewise for polyglossia or when using begin/end commands
pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
OutputParams const & runparams, Encoding const & newEnc,
- bool force)
+ bool force, bool noswitchmacro)
{
// XeTeX/LuaTeX use only one encoding per document:
// * with useNonTeXFonts: "utf8plain",
// * with XeTeX and TeX fonts: "ascii" (inputenc fails),
// * with LuaTeX and TeX fonts: only one encoding accepted by luainputenc.
- if (runparams.isFullUnicode())
+ if (runparams.isFullUnicode() || newEnc.name() == "inherit")
return make_pair(false, 0);
Encoding const & oldEnc = *runparams.encoding;
bool moving_arg = runparams.moving_arg;
- // If we switch from/to CJK, we need to switch anyway, despite custom inputenc
+ // If we switch from/to CJK, we need to switch anyway, despite custom inputenc,
+ // except if we use CJKutf8
bool const from_to_cjk =
- (oldEnc.package() == Encoding::CJK && newEnc.package() != Encoding::CJK)
- || (oldEnc.package() != Encoding::CJK && newEnc.package() == Encoding::CJK);
+ ((oldEnc.package() == Encoding::CJK && newEnc.package() != Encoding::CJK)
+ || (oldEnc.package() != Encoding::CJK && newEnc.package() == Encoding::CJK))
+ && ((bparams.encoding().name() != "utf8-cjk" && bparams.encoding().name() != "utf8")
+ || !LaTeXFeatures::isAvailable("CJKutf8"));
if (!force && !from_to_cjk
&& ((bparams.inputenc != "auto" && bparams.inputenc != "default") || moving_arg))
return make_pair(false, 0);
// This does of course only work in special cases (e.g. switch from
// tis620-0 to latin1, but the text in latin1 contains ASCII only),
// but it is the best we can do
- if (oldEnc.package() == Encoding::none
- || newEnc.package() == Encoding::none)
+ //
+ // 2019-01-08 Possibly no longer required since tis620-0 is supported
+ // by inputenc (but check special encodings "utf8-plain" and "default").
+ if (oldEnc.package() == Encoding::none || newEnc.package() == Encoding::none)
return make_pair(false, 0);
- LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
- << oldEnc.name() << " to " << newEnc.name());
- os << setEncoding(newEnc.iconvName());
- if (bparams.inputenc == "default")
- return make_pair(true, 0);
+ // change encoding (not required with UTF8)
+ if (bparams.encoding().iconvName() != "UTF-8") {
+ LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
+ << oldEnc.name() << " to " << newEnc.name());
+ os << setEncoding(newEnc.iconvName());
+ if (bparams.inputenc == "default")
+ return make_pair(true, 0);
+ }
docstring const inputenc_arg(from_ascii(newEnc.latexName()));
OutputState * state = getOutputState();
state->open_encoding_ = inputenc;
}
// with the japanese option, inputenc is omitted.
- if (runparams.use_japanese)
+ if (runparams.use_japanese || noswitchmacro)
return make_pair(true, count);
os << "\\inputencoding{" << inputenc_arg << '}';
return make_pair(true, count + 16);
os << "\\egroup";
count += 7;
}
- os << "\\begin{CJK}{" << inputenc_arg << "}{"
+ docstring const cjkenc = (bparams.encoding().iconvName() == "UTF-8"
+ && LaTeXFeatures::isAvailable("CJKutf8"))
+ ? from_ascii("UTF8") : from_ascii(newEnc.latexName());
+ os << "\\begin{CJK}{" << cjkenc << "}{"
<< from_ascii(bparams.fonts_cjk) << "}";
state->open_encoding_ = CJK;
return make_pair(true, count + 15);
}
// Dead code to avoid a warning:
return make_pair(true, 0);
-
}
} // namespace lyx