mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-12-14 17:39:58 +00:00
Fix wrong listings preamble encoding (bug #9382)
The listings package employs some dirty tricks internally, therefore it does not work with utf8 encoding, but requires fixed width encodings. This version of the fix uses ugly hardcoding instead of a layout file format change, so it could be backported if needed.
This commit is contained in:
parent
8a70d0ff9a
commit
15d1349847
@ -1435,7 +1435,8 @@ docstring const getFloatI18nPreamble(docstring const & type,
|
|||||||
|
|
||||||
|
|
||||||
docstring const i18npreamble(docstring const & templ, Language const * lang,
|
docstring const i18npreamble(docstring const & templ, Language const * lang,
|
||||||
Encoding const & enc, bool const polyglossia)
|
Encoding const & enc, bool const polyglossia,
|
||||||
|
bool const need_fixedwidth)
|
||||||
{
|
{
|
||||||
if (templ.empty())
|
if (templ.empty())
|
||||||
return templ;
|
return templ;
|
||||||
@ -1447,6 +1448,24 @@ docstring const i18npreamble(docstring const & templ, Language const * lang,
|
|||||||
string const langenc = lang->encoding()->iconvName();
|
string const langenc = lang->encoding()->iconvName();
|
||||||
string const texenc = lang->encoding()->latexName();
|
string const texenc = lang->encoding()->latexName();
|
||||||
string const bufenc = enc.iconvName();
|
string const bufenc = enc.iconvName();
|
||||||
|
Encoding const * testenc(&enc);
|
||||||
|
bool lang_fallback = false;
|
||||||
|
bool ascii_fallback = false;
|
||||||
|
if (need_fixedwidth && !enc.hasFixedWidth()) {
|
||||||
|
if (lang->encoding()->hasFixedWidth()) {
|
||||||
|
testenc = lang->encoding();
|
||||||
|
lang_fallback = true;
|
||||||
|
} else {
|
||||||
|
// We need a fixed width encoding, but both the buffer
|
||||||
|
// encoding and the language encoding are variable
|
||||||
|
// width. As a last fallback, try to convert to pure
|
||||||
|
// ASCII using the LaTeX commands defined in unicodesymbols.
|
||||||
|
testenc = encodings.fromLyXName("ascii");
|
||||||
|
if (!testenc)
|
||||||
|
return docstring();
|
||||||
|
ascii_fallback = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
// First and second character of plane 15 (Private Use Area)
|
// First and second character of plane 15 (Private Use Area)
|
||||||
string const s1 = "\xf3\xb0\x80\x80"; // U+F0000
|
string const s1 = "\xf3\xb0\x80\x80"; // U+F0000
|
||||||
string const s2 = "\xf3\xb0\x80\x81"; // U+F0001
|
string const s2 = "\xf3\xb0\x80\x81"; // U+F0001
|
||||||
@ -1460,16 +1479,18 @@ docstring const i18npreamble(docstring const & templ, Language const * lang,
|
|||||||
docstring const name = lang->translateLayout(key);
|
docstring const name = lang->translateLayout(key);
|
||||||
// Check whether name can be encoded in the buffer encoding
|
// Check whether name can be encoded in the buffer encoding
|
||||||
bool encodable = true;
|
bool encodable = true;
|
||||||
for (size_t i = 0; i < name.size(); ++i) {
|
for (size_t i = 0; i < name.size() && encodable; ++i)
|
||||||
if (!enc.encodable(name[i])) {
|
if (!testenc->encodable(name[i]))
|
||||||
encodable = false;
|
encodable = false;
|
||||||
break;
|
string translated;
|
||||||
}
|
if (encodable && !lang_fallback)
|
||||||
}
|
translated = to_utf8(name);
|
||||||
string const translated = encodable ? to_utf8(name)
|
else if (ascii_fallback)
|
||||||
: "\\inputencoding{" + texenc + "}"
|
translated = to_ascii(testenc->latexString(name).first);
|
||||||
+ s1 + langenc + s2 + to_utf8(name)
|
else
|
||||||
+ s1 + bufenc + s2;
|
translated = "\\inputencoding{" + texenc + "}"
|
||||||
|
+ s1 + langenc + s2 + to_utf8(name)
|
||||||
|
+ s1 + bufenc + s2;
|
||||||
preamble = subst(preamble, sub.str(), translated);
|
preamble = subst(preamble, sub.str(), translated);
|
||||||
}
|
}
|
||||||
return from_utf8(preamble);
|
return from_utf8(preamble);
|
||||||
@ -1494,20 +1515,20 @@ docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel, bool use_po
|
|||||||
snippets.insert(i18npreamble(tclass[*cit].langpreamble(),
|
snippets.insert(i18npreamble(tclass[*cit].langpreamble(),
|
||||||
buffer().language(),
|
buffer().language(),
|
||||||
buffer().params().encoding(),
|
buffer().params().encoding(),
|
||||||
use_polyglossia));
|
use_polyglossia, false));
|
||||||
// commands for language changing (for multilanguage documents)
|
// commands for language changing (for multilanguage documents)
|
||||||
if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
|
if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
|
||||||
snippets.insert(i18npreamble(
|
snippets.insert(i18npreamble(
|
||||||
tclass[*cit].babelpreamble(),
|
tclass[*cit].babelpreamble(),
|
||||||
buffer().language(),
|
buffer().language(),
|
||||||
buffer().params().encoding(),
|
buffer().params().encoding(),
|
||||||
use_polyglossia));
|
use_polyglossia, false));
|
||||||
for (lang_it lit = lbeg; lit != lend; ++lit)
|
for (lang_it lit = lbeg; lit != lend; ++lit)
|
||||||
snippets.insert(i18npreamble(
|
snippets.insert(i18npreamble(
|
||||||
tclass[*cit].babelpreamble(),
|
tclass[*cit].babelpreamble(),
|
||||||
*lit,
|
*lit,
|
||||||
buffer().params().encoding(),
|
buffer().params().encoding(),
|
||||||
use_polyglossia));
|
use_polyglossia, false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
|
if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
|
||||||
@ -1555,24 +1576,34 @@ docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel, bool use_po
|
|||||||
TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
|
TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
|
||||||
if (it == ils.end())
|
if (it == ils.end())
|
||||||
continue;
|
continue;
|
||||||
|
// The listings package does not work with variable width
|
||||||
|
// encodings, only with fixed width encodings. Therefore we
|
||||||
|
// need to force a fixed width encoding for
|
||||||
|
// \lstlistlistingname and \lstlistingname (bug 9382).
|
||||||
|
// This needs to be consistent with InsetListings::latex().
|
||||||
|
docstring const ilname = it->second.name();
|
||||||
|
bool const need_fixedwidth = !runparams_.isFullUnicode() &&
|
||||||
|
(ilname == "Listings" ||
|
||||||
|
ilname == "Include:Listings" ||
|
||||||
|
ilname == "TOC:Listings");
|
||||||
// language dependent commands (once per document)
|
// language dependent commands (once per document)
|
||||||
snippets.insert(i18npreamble(it->second.langpreamble(),
|
snippets.insert(i18npreamble(it->second.langpreamble(),
|
||||||
buffer().language(),
|
buffer().language(),
|
||||||
buffer().params().encoding(),
|
buffer().params().encoding(),
|
||||||
use_polyglossia));
|
use_polyglossia, need_fixedwidth));
|
||||||
// commands for language changing (for multilanguage documents)
|
// commands for language changing (for multilanguage documents)
|
||||||
if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
|
if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
|
||||||
snippets.insert(i18npreamble(
|
snippets.insert(i18npreamble(
|
||||||
it->second.babelpreamble(),
|
it->second.babelpreamble(),
|
||||||
buffer().language(),
|
buffer().language(),
|
||||||
buffer().params().encoding(),
|
buffer().params().encoding(),
|
||||||
use_polyglossia));
|
use_polyglossia, need_fixedwidth));
|
||||||
for (lang_it lit = lbeg; lit != lend; ++lit)
|
for (lang_it lit = lbeg; lit != lend; ++lit)
|
||||||
snippets.insert(i18npreamble(
|
snippets.insert(i18npreamble(
|
||||||
it->second.babelpreamble(),
|
it->second.babelpreamble(),
|
||||||
*lit,
|
*lit,
|
||||||
buffer().params().encoding(),
|
buffer().params().encoding(),
|
||||||
use_polyglossia));
|
use_polyglossia, need_fixedwidth));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,6 +145,8 @@ void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
|
|||||||
// listings package cannot deal with multi-byte-encoded
|
// listings package cannot deal with multi-byte-encoded
|
||||||
// glyphs (not needed with full-unicode aware backends
|
// glyphs (not needed with full-unicode aware backends
|
||||||
// such as XeTeX).
|
// such as XeTeX).
|
||||||
|
// This needs to be consistent with
|
||||||
|
// LaTeXFeatures::getTClassI18nPreamble().
|
||||||
Language const * const outer_language =
|
Language const * const outer_language =
|
||||||
(runparams.local_font != 0) ?
|
(runparams.local_font != 0) ?
|
||||||
runparams.local_font->language()
|
runparams.local_font->language()
|
||||||
|
@ -55,6 +55,8 @@ What's new
|
|||||||
|
|
||||||
- Do not output \protect unnecessarily before \caption (bug 9177).
|
- Do not output \protect unnecessarily before \caption (bug 9177).
|
||||||
|
|
||||||
|
- Fix wrong listings preamble encoding (bug 9382).
|
||||||
|
|
||||||
|
|
||||||
* LYX2LYX
|
* LYX2LYX
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user