tex2lyx: cleanup font handling

This commit is contained in:
Juergen Spitzmueller 2019-07-16 10:32:24 +02:00
parent ebf0fe6756
commit 7e2a424621

View File

@ -475,12 +475,11 @@ void Preamble::add_package(string const & name, vector<string> & options)
namespace { namespace {
// Given is a string like "scaled=0.9" or "Scale=0.9", return 0.9 * 100 // Given is a string like "scaled=0.9" or "scale=0.9", return 0.9 * 100
bool scale_as_percentage(string const & scale, string & percentage) bool scale_as_percentage(string const & scale, string & percentage)
{ {
string::size_type pos = scale.find('='); if (contains(scale, '=')) {
if (pos != string::npos) { string const value = support::split(scale, '=');
string value = scale.substr(pos + 1);
if (isStrDbl(value)) { if (isStrDbl(value)) {
percentage = convert<string>( percentage = convert<string>(
static_cast<int>(100 * convert<double>(value))); static_cast<int>(100 * convert<double>(value)));
@ -745,13 +744,21 @@ void Preamble::handle_package(Parser &p, string const & name,
p.setEncoding("UTF-8"); p.setEncoding("UTF-8");
} }
// vector of all options for easier pasing and
// skipping
vector<string> allopts = getVectorFromString(opts);
// this stored the potential extra options
string xopts;
//
// roman fonts // roman fonts
//
// By default, we use the package name as LyX font name,
// so this only needs to be reset if these names differ
if (is_known(name, known_roman_font_packages)) if (is_known(name, known_roman_font_packages))
h_font_roman[0] = name; h_font_roman[0] = name;
vector<string> allopts = getVectorFromString(opts);
string xopts;
if (name == "ccfonts") { if (name == "ccfonts") {
for (auto const & opt : allopts) { for (auto const & opt : allopts) {
if (!xopts.empty()) if (!xopts.empty())
@ -776,7 +783,6 @@ void Preamble::handle_package(Parser &p, string const & name,
if (name == "fourier") { if (name == "fourier") {
h_font_roman[0] = "utopia"; h_font_roman[0] = "utopia";
// when font uses real small capitals
for (auto const & opt : allopts) { for (auto const & opt : allopts) {
if (opt == "osf") { if (opt == "osf") {
h_font_roman_osf = "true"; h_font_roman_osf = "true";
@ -796,7 +802,6 @@ void Preamble::handle_package(Parser &p, string const & name,
} }
if (name == "garamondx") { if (name == "garamondx") {
h_font_roman[0] = "garamondx";
for (auto const & opt : allopts) { for (auto const & opt : allopts) {
if (opt == "osfI") { if (opt == "osfI") {
h_font_roman_osf = "true"; h_font_roman_osf = "true";
@ -812,7 +817,6 @@ void Preamble::handle_package(Parser &p, string const & name,
} }
if (name == "libertine") { if (name == "libertine") {
h_font_roman[0] = "libertine";
// this automatically invokes biolinum // this automatically invokes biolinum
h_font_sans[0] = "biolinum"; h_font_sans[0] = "biolinum";
// as well as libertineMono // as well as libertineMono
@ -869,15 +873,24 @@ void Preamble::handle_package(Parser &p, string const & name,
} }
if (name == "mathdesign") { if (name == "mathdesign") {
if (opts.find("charter") != string::npos) for (auto const & opt : allopts) {
h_font_roman[0] = "md-charter"; if (opt == "charter") {
if (opts.find("garamond") != string::npos) h_font_roman[0] = "md-charter";
h_font_roman[0] = "md-garamond"; continue;
if (opts.find("utopia") != string::npos) }
h_font_roman[0] = "md-utopia"; if (opt == "garamond") {
if (opts.find("expert") != string::npos) { h_font_roman[0] = "md-garamond";
h_font_sc = "true"; continue;
h_font_roman_osf = "true"; }
if (opt == "utopia") {
h_font_roman[0] = "md-utopia";
continue;
}
if (opt == "expert") {
h_font_sc = "true";
h_font_roman_osf = "true";
continue;
}
} }
} }
@ -917,14 +930,12 @@ void Preamble::handle_package(Parser &p, string const & name,
h_font_roman[0] = "cochineal"; h_font_roman[0] = "cochineal";
if (name == "cochineal") { if (name == "cochineal") {
h_font_roman[0] = "cochineal";
// cochineal can have several options, e.g. [proportional,osf]
for (auto const & opt : allopts) { for (auto const & opt : allopts) {
if (opt == "osf") { if (opt == "osf" || opt == "oldstyle") {
h_font_roman_osf = "true"; h_font_roman_osf = "true";
continue; continue;
} }
if (opt == "proportional") if (opt == "proportional" || opt == "p")
continue; continue;
if (!xopts.empty()) if (!xopts.empty())
xopts += ", "; xopts += ", ";
@ -935,30 +946,28 @@ void Preamble::handle_package(Parser &p, string const & name,
options.clear(); options.clear();
} }
if (name == "eco")
// font uses old-style figure
h_font_roman_osf = "true";
if (name == "noto") { if (name == "noto") {
// noto can have several options h_font_roman[0] = "NotoSerif-TLF";
if (opts.empty())
h_font_roman[0] = "NotoSerif-TLF";
string::size_type pos = opts.find("rm");
if (pos != string::npos)
h_font_roman[0] = "NotoSerif-TLF";
pos = opts.find("sf");
if (pos != string::npos)
h_font_sans[0] = "NotoSans-TLF";
pos = opts.find("nott");
if (pos != string::npos) {
h_font_roman[0] = "NotoSerif-TLF";
h_font_sans[0] = "NotoSans-TLF";
}
// noto as typewriter is handled in handling of \ttdefault // noto as typewriter is handled in handling of \ttdefault
// special cases are handled in handling of \rmdefault and \sfdefault // special cases are handled in handling of \rmdefault and \sfdefault
for (auto const & opt : allopts) { for (auto const & opt : allopts) {
if (opt == "rm") if (opt == "rm") {
h_font_roman[0] = "NotoSerif-TLF";
continue; continue;
if (opt == "sf") }
if (opt == "sf") {
h_font_sans[0] = "NotoSans-TLF";
continue; continue;
if (opt == "nott") }
if (opt == "nott") {
h_font_roman[0] = "NotoSerif-TLF";
h_font_sans[0] = "NotoSans-TLF";
continue; continue;
}
if (opt == "osf") { if (opt == "osf") {
h_font_roman_osf = "true"; h_font_roman_osf = "true";
continue; continue;
@ -996,25 +1005,24 @@ void Preamble::handle_package(Parser &p, string const & name,
} }
if (name == "plex-serif") { if (name == "plex-serif") {
if (opts.empty()) h_font_roman[0] = "IBMPlexSerif";
h_font_roman[0] = "IBMPlexSerif";
else if (opts.find("thin") != string::npos)
h_font_roman[0] = "IBMPlexSerifThin";
else if (opts.find("extralight") != string::npos)
h_font_roman[0] = "IBMPlexSerifExtraLight";
else if (opts.find("light") != string::npos)
h_font_roman[0] = "IBMPlexSerifLight";
else if (opts.find("semibold") != string::npos)
h_font_roman[0] = "IBMPlexSerifSemibold";
for (auto const & opt : allopts) { for (auto const & opt : allopts) {
if (opt == "thin") if (opt == "thin") {
h_font_roman[0] = "IBMPlexSerifThin";
continue; continue;
if (opt == "extralight") }
if (opt == "extralight") {
h_font_roman[0] = "IBMPlexSerifExtraLight";
continue; continue;
if (opt == "light") }
if (opt == "light") {
h_font_roman[0] = "IBMPlexSerifLight";
continue; continue;
if (opt == "semibold") }
if (opt == "semibold") {
h_font_roman[0] = "IBMPlexSerifSemibold";
continue; continue;
}
if (!xopts.empty()) if (!xopts.empty())
xopts += ", "; xopts += ", ";
xopts += opt; xopts += opt;
@ -1023,29 +1031,29 @@ void Preamble::handle_package(Parser &p, string const & name,
h_font_roman_opts = xopts; h_font_roman_opts = xopts;
options.clear(); options.clear();
} }
if (name == "noto-serif") { if (name == "noto-serif") {
h_font_roman[0] = "NotoSerifRegular"; h_font_roman[0] = "NotoSerifRegular";
if (!opts.empty()) {
if (opts.find("thin") != string::npos)
h_font_roman[0] = "NotoSerifThin";
else if (opts.find("medium") != string::npos)
h_font_roman[0] = "NotoSerifMedium";
else if (opts.find("extralight") != string::npos)
h_font_roman[0] = "NotoSerifExtralight";
else if (opts.find("light") != string::npos)
h_font_roman[0] = "NotoSerifLight";
}
for (auto const & opt : allopts) { for (auto const & opt : allopts) {
if (opt == "regular") if (opt == "regular")
// skip
continue; continue;
if (opt == "thin") if (opt == "thin") {
h_font_roman[0] = "NotoSerifThin";
continue; continue;
if (opt == "extralight") }
if (opt == "extralight") {
h_font_roman[0] = "NotoSerifExtralight";
continue; continue;
if (opt == "light") }
if (opt == "light") {
h_font_roman[0] = "NotoSerifLight";
continue; continue;
if (opt == "semibold") }
if (opt == "medium") {
h_font_roman[0] = "NotoSerifMedium";
continue; continue;
}
if (!xopts.empty()) if (!xopts.empty())
xopts += ", "; xopts += ", ";
xopts += opt; xopts += opt;
@ -1071,21 +1079,43 @@ void Preamble::handle_package(Parser &p, string const & name,
options.clear(); options.clear();
} }
//
// sansserif fonts // sansserif fonts
//
// By default, we use the package name as LyX font name,
// so this only needs to be reset if these names differ.
// Also, we handle the scaling option here generally.
if (is_known(name, known_sans_font_packages)) { if (is_known(name, known_sans_font_packages)) {
h_font_sans[0] = name; h_font_sans[0] = name;
if (options.size() >= 1) { if (contains(opts, "scale")) {
if (scale_as_percentage(opts, h_font_sf_scale[0])) vector<string>::const_iterator it = allopts.begin();
options.clear(); for (; it != allopts.end() ; ++it) {
string const opt = *it;
if (prefixIs(opt, "scaled=") || prefixIs(opt, "scale=")) {
if (scale_as_percentage(opt, h_font_sf_scale[0])) {
allopts.erase(it);
break;
}
}
}
} }
} }
if (name == "biolinum" || name == "biolinum-type1") { if (name == "biolinum" || name == "biolinum-type1") {
h_font_sans[0] = "biolinum"; h_font_sans[0] = "biolinum";
// biolinum can have several options, e.g. [osf,scaled=0.97] for (auto const & opt : allopts) {
string::size_type pos = opts.find("osf"); if (prefixIs(opt, "osf")) {
if (pos != string::npos) h_font_sans_osf = "true";
h_font_sans_osf = "true"; continue;
}
if (!xopts.empty())
xopts += ", ";
xopts += opt;
}
if (!xopts.empty())
h_font_sans_opts = xopts;
options.clear();
} }
if (name == "cantarell") { if (name == "cantarell") {
@ -1096,10 +1126,6 @@ void Preamble::handle_package(Parser &p, string const & name,
h_font_sans_osf = "true"; h_font_sans_osf = "true";
continue; continue;
} }
if (prefixIs(opt, "scale=")) {
scale_as_percentage(opt, h_font_sf_scale[0]);
continue;
}
if (!xopts.empty()) if (!xopts.empty())
xopts += ", "; xopts += ", ";
xopts += opt; xopts += opt;
@ -1111,14 +1137,9 @@ void Preamble::handle_package(Parser &p, string const & name,
if (name == "PTSans") { if (name == "PTSans") {
h_font_sans[0] = "PTSans-TLF"; h_font_sans[0] = "PTSans-TLF";
if (options.size() >= 1) {
if (scale_as_percentage(opts, h_font_sf_scale[0]))
options.clear();
}
} }
if (name == "FiraSans") { if (name == "FiraSans") {
h_font_sans[0] = "FiraSans";
h_font_sans_osf = "true"; h_font_sans_osf = "true";
for (auto const & opt : allopts) { for (auto const & opt : allopts) {
if (opt == "book") { if (opt == "book") {
@ -1148,10 +1169,6 @@ void Preamble::handle_package(Parser &p, string const & name,
h_font_sans_osf = "false"; h_font_sans_osf = "false";
continue; continue;
} }
if (prefixIs(opt, "scale=") || prefixIs(opt, "scaled=")) {
scale_as_percentage(opt, h_font_sf_scale[0]);
continue;
}
if (!xopts.empty()) if (!xopts.empty())
xopts += ", "; xopts += ", ";
xopts += opt; xopts += opt;
@ -1162,29 +1179,26 @@ void Preamble::handle_package(Parser &p, string const & name,
} }
if (name == "plex-sans") { if (name == "plex-sans") {
if (opts.find("condensed") != string::npos) h_font_sans[0] = "IBMPlexSans";
h_font_sans[0] = "IBMPlexSansCondensed";
else if (opts.find("thin") != string::npos)
h_font_sans[0] = "IBMPlexSansThin";
else if (opts.find("extralight") != string::npos)
h_font_sans[0] = "IBMPlexSansExtraLight";
else if (opts.find("light") != string::npos)
h_font_sans[0] = "IBMPlexSansLight";
else if (opts.find("semibold") != string::npos)
h_font_sans[0] = "IBMPlexSansSemibold";
else
h_font_sans[0] = "IBMPlexSans";
for (auto const & opt : allopts) { for (auto const & opt : allopts) {
if (opt == "thin") if (opt == "condensed") {
h_font_sans[0] = "IBMPlexSansCondensed";
continue; continue;
if (opt == "extralight") }
if (opt == "thin") {
h_font_sans[0] = "IBMPlexSansThin";
continue; continue;
if (opt == "light") }
if (opt == "extralight") {
h_font_sans[0] = "IBMPlexSansExtraLight";
continue; continue;
if (opt == "semibold") }
if (opt == "light") {
h_font_sans[0] = "IBMPlexSansLight";
continue; continue;
if (prefixIs(opt, "scale=")) { }
scale_as_percentage(opt, h_font_sf_scale[0]); if (opt == "semibold") {
h_font_sans[0] = "IBMPlexSansSemibold";
continue; continue;
} }
if (!xopts.empty()) if (!xopts.empty())
@ -1198,27 +1212,25 @@ void Preamble::handle_package(Parser &p, string const & name,
if (name == "noto-sans") { if (name == "noto-sans") {
h_font_sans[0] = "NotoSansRegular"; h_font_sans[0] = "NotoSansRegular";
if (!opts.empty()) {
if (opts.find("medium") != string::npos)
h_font_sans[0] = "NotoSansMedium";
else if (opts.find("thin") != string::npos)
h_font_sans[0] = "NotoSansThin";
else if (opts.find("extralight") != string::npos)
h_font_sans[0] = "NotoSansExtralight";
else if (opts.find("light") != string::npos)
h_font_sans[0] = "NotoSansLight";
}
for (auto const & opt : allopts) { for (auto const & opt : allopts) {
if (opt == "regular") if (opt == "regular")
continue; continue;
if (opt == "thin") if (opt == "medium") {
h_font_sans[0] = "NotoSansMedium";
continue; continue;
if (opt == "extralight") }
if (opt == "thin") {
h_font_sans[0] = "NotoSansThin";
continue; continue;
if (opt == "light") }
if (opt == "extralight") {
h_font_sans[0] = "NotoSansExtralight";
continue; continue;
if (opt == "semibold") }
if (opt == "light") {
h_font_sans[0] = "NotoSansLight";
continue; continue;
}
if (opt == "osf") { if (opt == "osf") {
h_font_sans_osf = "true"; h_font_sans_osf = "true";
continue; continue;
@ -1235,10 +1247,6 @@ void Preamble::handle_package(Parser &p, string const & name,
if (name == "sourcesanspro") { if (name == "sourcesanspro") {
h_font_sans[0] = "ADOBESourceSansPro"; h_font_sans[0] = "ADOBESourceSansPro";
for (auto const & opt : allopts) { for (auto const & opt : allopts) {
if (prefixIs(opt, "scaled=")) {
scale_as_percentage(opt, h_font_sf_scale[0]);
continue;
}
if (opt == "osf") { if (opt == "osf") {
h_font_sans_osf = "true"; h_font_sans_osf = "true";
continue; continue;
@ -1252,15 +1260,27 @@ void Preamble::handle_package(Parser &p, string const & name,
options.clear(); options.clear();
} }
//
// typewriter fonts // typewriter fonts
if (is_known(name, known_typewriter_font_packages)) { //
// fourier can be set as roman font _only_
// fourier as typewriter is handled in handling of \ttdefault // By default, we use the package name as LyX font name,
if (name != "fourier") { // so this only needs to be reset if these names differ.
h_font_typewriter[0] = name; // Also, we handle the scaling option here generally.
if (options.size() >= 1) { // Note: fourier can be set as roman font _only_
if (scale_as_percentage(opts, h_font_tt_scale[0])) // fourier as typewriter is handled in handling of \ttdefault
options.clear(); if (is_known(name, known_typewriter_font_packages) && name != "fourier") {
h_font_typewriter[0] = name;
if (contains(opts, "scale")) {
vector<string>::const_iterator it = allopts.begin();
for (; it != allopts.end() ; ++it) {
string const opt = *it;
if (prefixIs(opt, "scaled=") || prefixIs(opt, "scale=")) {
if (scale_as_percentage(opt, h_font_tt_scale[0])) {
allopts.erase(it);
break;
}
}
} }
} }
} }
@ -1275,10 +1295,6 @@ void Preamble::handle_package(Parser &p, string const & name,
h_font_typewriter_osf = "false"; h_font_typewriter_osf = "false";
continue; continue;
} }
if (prefixIs(opt, "scale=") || prefixIs(opt, "scaled=")) {
scale_as_percentage(opt, h_font_tt_scale[0]);
continue;
}
if (!xopts.empty()) if (!xopts.empty())
xopts += ", "; xopts += ", ";
xopts += opt; xopts += opt;
@ -1297,27 +1313,22 @@ void Preamble::handle_package(Parser &p, string const & name,
} }
if (name == "plex-mono") { if (name == "plex-mono") {
if (opts.find("thin") != string::npos) h_font_typewriter[0] = "IBMPlexMono";
h_font_typewriter[0] = "IBMPlexMonoThin";
else if (opts.find("extralight") != string::npos)
h_font_typewriter[0] = "IBMPlexMonoExtraLight";
else if (opts.find("light") != string::npos)
h_font_typewriter[0] = "IBMPlexMonoLight";
else if (opts.find("semibold") != string::npos)
h_font_typewriter[0] = "IBMPlexMonoSemibold";
else
h_font_typewriter[0] = "IBMPlexMono";
for (auto const & opt : allopts) { for (auto const & opt : allopts) {
if (opt == "thin") if (opt == "thin") {
h_font_typewriter[0] = "IBMPlexMonoThin";
continue; continue;
if (opt == "extralight") }
if (opt == "extralight") {
h_font_typewriter[0] = "IBMPlexMonoExtraLight";
continue; continue;
if (opt == "light") }
if (opt == "light") {
h_font_typewriter[0] = "IBMPlexMonoLight";
continue; continue;
if (opt == "semibold") }
continue; if (opt == "semibold"){
if (prefixIs(opt, "scale=")) { h_font_typewriter[0] = "IBMPlexMonoSemibold";
scale_as_percentage(opt, h_font_tt_scale[0]);
continue; continue;
} }
if (!xopts.empty()) if (!xopts.empty())
@ -1346,10 +1357,6 @@ void Preamble::handle_package(Parser &p, string const & name,
if (name == "sourcecodepro") { if (name == "sourcecodepro") {
h_font_typewriter[0] = "ADOBESourceCodePro"; h_font_typewriter[0] = "ADOBESourceCodePro";
for (auto const & opt : allopts) { for (auto const & opt : allopts) {
if (prefixIs(opt, "scaled=")) {
scale_as_percentage(opt, h_font_tt_scale[0]);
continue;
}
if (opt == "osf") { if (opt == "osf") {
h_font_typewriter_osf = "true"; h_font_typewriter_osf = "true";
continue; continue;
@ -1363,11 +1370,12 @@ void Preamble::handle_package(Parser &p, string const & name,
options.clear(); options.clear();
} }
// font uses old-style figure //
if (name == "eco")
h_font_roman_osf = "true";
// math fonts // math fonts
//
// By default, we use the package name as LyX font name,
// so this only needs to be reset if these names differ.
if (is_known(name, known_math_font_packages)) if (is_known(name, known_math_font_packages))
h_font_math[0] = name; h_font_math[0] = name;