JKQTMathText: improved startup-speed by doing font searches only once per program start (via static variables)

This commit is contained in:
jkriege2 2019-06-30 17:59:04 +02:00
parent 01b6d7cee6
commit bbfcec74ab

View File

@ -3270,54 +3270,59 @@ JKQTMathText::JKQTMathText(QObject* parent):
blackboardSimulated=true; blackboardSimulated=true;
QString serifFont="serif"; static QString serifFont="serif";
QString sansFont="sans"; static QString sansFont="sans";
QString symbolFont="symbol"; static QString symbolFont="symbol";
QString scriptFont="script"; static QString scriptFont="script";
QString typewriterFont="typewriter"; static QString typewriterFont="typewriter";
QString decorativeFont="decorative"; static QString decorativeFont="decorative";
QString blackboardFont="blackboard"; static QString blackboardFont="blackboard";
QString fracturFont="fraktur"; static QString fracturFont="fraktur";
static bool firstStart=true;
QStringList fonts=fontdb.families(); if (firstStart) {
//qDebug()<<"fonts:\n"<<fonts; firstStart=false;
QStringList fonts=fontdb.families();
//qDebug()<<"fonts:\n"<<fonts;
for (const QString& f: fonts) { for (const QString& f: fonts) {
if (typewriterFont=="typewriter" && QFontInfo(QFont(f)).styleHint()==QFont::TypeWriter) { QFont fnt(f);
typewriterFont=f; QFontInfo fi(fnt);
} if (typewriterFont=="typewriter" && fi.styleHint()==QFont::TypeWriter) {
if (decorativeFont=="decorative" && QFontInfo(QFont(f)).styleHint()==QFont::Decorative) { typewriterFont=f;
decorativeFont=f; }
} if (decorativeFont=="decorative" && fi.styleHint()==QFont::Decorative) {
if (serifFont=="serif" && QFontInfo(QFont(f)).styleHint()==QFont::Serif) { decorativeFont=f;
serifFont=f; }
} if (serifFont=="serif" && fi.styleHint()==QFont::Serif) {
if (sansFont=="sans" && QFontInfo(QFont(f)).styleHint()==QFont::SansSerif) { serifFont=f;
sansFont=f; }
} if (sansFont=="sans" && fi.styleHint()==QFont::SansSerif) {
if (scriptFont=="script" && QFontInfo(QFont(f)).styleHint()==QFont::Cursive) { sansFont=f;
scriptFont=f; }
} if (scriptFont=="script" && fi.styleHint()==QFont::Cursive) {
} scriptFont=f;
auto checkForFonts=[&fonts](QString& targetfont, const QStringList& fontoptions) {
for (auto& f: fontoptions) {
if (fonts.contains(f)) {
targetfont=f;
break;
} }
} }
};
checkForFonts(serifFont, QStringList {"Times New Roman", "Times", "FreeSerif", "DejaVu Serif"}); auto checkForFonts=[&fonts](QString& targetfont, const QStringList& fontoptions) {
checkForFonts(sansFont, QStringList {"Arial Unicode MS", "Arial Unicode", "Lucida Sans Unicode", "Arial", "Helvetica", "FreeSans", "DejaVu Sans", "Lucida Sans"}); for (auto& f: fontoptions) {
checkForFonts(symbolFont, QStringList {"SymbolStandard", "Symbol"}); if (fonts.contains(f)) {
checkForFonts(typewriterFont, QStringList {"Courier New", "Courier", "Courier Std", "FreeMono", "CMU Typewriter Text", "UM Typewriter"}); targetfont=f;
checkForFonts(blackboardFont, QStringList {"Double Stroke", "CloisterOpenFace BT", "GoudyHandtooled BT", "Castellar", "MathJax_AMS", "Castellar Standard", "MathJax_AMS Standard", "Colonna MT"}); break;
checkForFonts(decorativeFont, QStringList {"Lucida Calligraphy", "Cookie", "Segoe Print", "Comic Sans", "Comic Sans MS", "Gabriola", "Gabriola Standard", "Lucida Handwriting Kursiv", "Lucida Handwriting", "Pristina", "Pristina Standard", "MathJax_Caligraphics"}); }
checkForFonts(scriptFont, QStringList {"Lucida Handwriting", "Dancing Script", "Amazone BT", "ScriptS", "ScriptC", "ScriptC Standard", "Script", "Brush Script MT", "Brush Script MT Kursiv", "MathJax_Script"}); }
checkForFonts(fracturFont, QStringList {"Old English Text MT", "Old English Text MT Standard", "UnifrakturMaguntia Standard", "UnifrakturMaguntia", "MathJax_Fraktur", "UnifrakturCook Fett"}); };
checkForFonts(serifFont, QStringList {"Times New Roman", "Times", "FreeSerif", "DejaVu Serif"});
checkForFonts(sansFont, QStringList {"Arial Unicode MS", "Arial Unicode", "Lucida Sans Unicode", "Arial", "Helvetica", "FreeSans", "DejaVu Sans", "Lucida Sans"});
checkForFonts(symbolFont, QStringList {"SymbolStandard", "Symbol"});
checkForFonts(typewriterFont, QStringList {"Courier New", "Courier", "Courier Std", "FreeMono", "CMU Typewriter Text", "UM Typewriter"});
checkForFonts(blackboardFont, QStringList {"Double Stroke", "CloisterOpenFace BT", "GoudyHandtooled BT", "Castellar", "MathJax_AMS", "Castellar Standard", "MathJax_AMS Standard", "Colonna MT"});
checkForFonts(decorativeFont, QStringList {"Lucida Calligraphy", "Cookie", "Segoe Print", "Comic Sans", "Comic Sans MS", "Gabriola", "Gabriola Standard", "Lucida Handwriting Kursiv", "Lucida Handwriting", "Pristina", "Pristina Standard", "MathJax_Caligraphics"});
checkForFonts(scriptFont, QStringList {"Lucida Handwriting", "Dancing Script", "Amazone BT", "ScriptS", "ScriptC", "ScriptC Standard", "Script", "Brush Script MT", "Brush Script MT Kursiv", "MathJax_Script"});
checkForFonts(fracturFont, QStringList {"Old English Text MT", "Old English Text MT Standard", "UnifrakturMaguntia Standard", "UnifrakturMaguntia", "MathJax_Fraktur", "UnifrakturCook Fett"});
}
if (serifFont!="serif") addReplacementFont("serif", serifFont); if (serifFont!="serif") addReplacementFont("serif", serifFont);
if (sansFont!="sans") addReplacementFont("sans", sansFont); if (sansFont!="sans") addReplacementFont("sans", sansFont);
@ -3406,44 +3411,46 @@ void JKQTMathText::saveSettings(QSettings& settings, const QString& group) const
} }
bool JKQTMathText::useSTIX(bool mathModeOnly) { bool JKQTMathText::useSTIX(bool mathModeOnly) {
QFontDatabase fdb;
static QStringList mathNames{"STIX Two Math", "STIX Math", "STIX Two Math Standard", "STIX Math Standard"}; static QStringList mathNames{"STIX Two Math", "STIX Math", "STIX Two Math Standard", "STIX Math Standard"};
static QStringList textNames{"STIX", "STIXGeneral", "STIX General"}; static QStringList textNames{"STIX", "STIXGeneral", "STIX General"};
QString textFamily; static QString textFamily;
QString mathFamily; static QString mathFamily;
for (const QString& name:mathNames) { if (textFamily.isEmpty() && mathFamily.isEmpty()) {
for (int i=0; i<fdb.families().size(); i++) { QFontDatabase fdb;
if (fdb.families().at(i).contains(name) ) { for (const QString& name:mathNames) {
mathFamily=fdb.families().at(i); for (int i=0; i<fdb.families().size(); i++) {
if (fdb.families().at(i).contains(name) ) {
mathFamily=fdb.families().at(i);
}
if (mathFamily.size()>0) {
break;
}
} }
if (mathFamily.size()>0) { if (mathFamily.size()>0) {
break; break;
} }
} }
if (mathFamily.size()>0) { for (const QString& name:textNames) {
break; for (int i=0; i<fdb.families().size(); i++) {
} if (fdb.families().at(i).contains(name) ) {
} textFamily=fdb.families().at(i);
for (const QString& name:textNames) { }
for (int i=0; i<fdb.families().size(); i++) { if (textFamily.size()>0) {
if (fdb.families().at(i).contains(name) ) { break;
textFamily=fdb.families().at(i); }
} }
if (textFamily.size()>0) { if (textFamily.size()>0) {
break; break;
} }
} }
if (textFamily.size()>0) { if (mathFamily.isEmpty() && !textFamily.isEmpty()) {
break; mathFamily=textFamily;
} else if (!mathFamily.isEmpty() && textFamily.isEmpty()) {
textFamily=mathFamily;
} }
} }
if (mathFamily.isEmpty() && !textFamily.isEmpty()) {
mathFamily=textFamily;
} else if (!mathFamily.isEmpty() && textFamily.isEmpty()) {
textFamily=mathFamily;
}
bool res=false; bool res=false;
@ -3473,23 +3480,25 @@ bool JKQTMathText::useXITS(bool mathModeOnly)
} }
QString textFamily; static QString textFamily;
QString mathFamily; static QString mathFamily;
for (int i=0; i<fdb.families().size(); i++) { if (textFamily.isEmpty() && mathFamily.isEmpty()) {
if (fdb.families().at(i).contains("XITS Math")) { for (int i=0; i<fdb.families().size(); i++) {
mathFamily=fdb.families().at(i); if (fdb.families().at(i).contains("XITS Math")) {
} else if (fdb.families().at(i).contains("XITS")) { mathFamily=fdb.families().at(i);
textFamily=fdb.families().at(i); } else if (fdb.families().at(i).contains("XITS")) {
textFamily=fdb.families().at(i);
}
if (mathFamily.size()>0 && textFamily.size()>0) {
break;
}
} }
if (mathFamily.size()>0 && textFamily.size()>0) { if (mathFamily.isEmpty() && !textFamily.isEmpty()) {
break; mathFamily=textFamily;
} else if (!mathFamily.isEmpty() && textFamily.isEmpty()) {
textFamily=mathFamily;
} }
} }
if (mathFamily.isEmpty() && !textFamily.isEmpty()) {
mathFamily=textFamily;
} else if (!mathFamily.isEmpty() && textFamily.isEmpty()) {
textFamily=mathFamily;
}
bool res=false; bool res=false;
@ -3510,29 +3519,30 @@ bool JKQTMathText::useXITS(bool mathModeOnly)
bool JKQTMathText::useASANA(bool mathModeOnly) bool JKQTMathText::useASANA(bool mathModeOnly)
{ {
QFontDatabase fdb; QFontDatabase fdb;
if (!fdb.families().contains("Asana") && !fdb.families().contains("Asana Math")) { if (!fdb.families().contains("Asana") && !fdb.families().contains("Asana Math")) {
if (QFile::exists(":/JKQTMathText/fonts/asana-math.otf")) { /*i=*/QFontDatabase::addApplicationFont(":/JKQTMathText/fonts/asana-math.otf"); } if (QFile::exists(":/JKQTMathText/fonts/asana-math.otf")) { /*i=*/QFontDatabase::addApplicationFont(":/JKQTMathText/fonts/asana-math.otf"); }
} }
QString textFamily; static QString textFamily;
QString mathFamily; static QString mathFamily;
for (int i=0; i<fdb.families().size(); i++) { if (textFamily.isEmpty() && mathFamily.isEmpty()) {
if (fdb.families().at(i).contains("Asana Math")) { for (int i=0; i<fdb.families().size(); i++) {
mathFamily=fdb.families().at(i); if (fdb.families().at(i).contains("Asana Math")) {
} else if (fdb.families().at(i).contains("Asana")) { mathFamily=fdb.families().at(i);
textFamily=fdb.families().at(i); } else if (fdb.families().at(i).contains("Asana")) {
textFamily=fdb.families().at(i);
}
if (mathFamily.size()>0 && textFamily.size()>0) {
break;
}
} }
if (mathFamily.size()>0 && textFamily.size()>0) { if (mathFamily.isEmpty() && !textFamily.isEmpty()) {
break; mathFamily=textFamily;
} else if (!mathFamily.isEmpty() && textFamily.isEmpty()) {
textFamily=mathFamily;
} }
} }
if (mathFamily.isEmpty() && !textFamily.isEmpty()) {
mathFamily=textFamily;
} else if (!mathFamily.isEmpty() && textFamily.isEmpty()) {
textFamily=mathFamily;
}
bool res=false; bool res=false;