2022-06-08 21:38:26 +08:00
|
|
|
/*
|
2024-02-06 21:50:49 +08:00
|
|
|
Copyright (c) 2008-2024 Jan W. Krieger (<jan@jkrieger.de>)
|
2022-06-08 21:38:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This software is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License (LGPL) as published by
|
|
|
|
the Free Software Foundation, either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License (LGPL) for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License (LGPL)
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "jkqtmathtext/jkqtmathtexttools.h"
|
|
|
|
#include "jkqtmathtext/jkqtmathtext.h"
|
|
|
|
#include "jkqtcommon/jkqtpstringtools.h"
|
2024-01-09 00:16:31 +08:00
|
|
|
#include "jkqtcommon/jkqtpcachingtools.h"
|
2022-06-08 21:38:26 +08:00
|
|
|
#include <cmath>
|
|
|
|
#include <QFontMetricsF>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QFontDatabase>
|
|
|
|
#include <QFontInfo>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QFont>
|
2024-01-09 00:16:31 +08:00
|
|
|
#include <QReadWriteLock>
|
2024-02-06 00:12:34 +08:00
|
|
|
#include <QHashFunctions>
|
2024-01-06 06:26:47 +08:00
|
|
|
#include <mutex>
|
2024-01-25 05:33:32 +08:00
|
|
|
#include <functional>
|
2022-06-08 21:38:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
void initJKQTMathTextResources()
|
|
|
|
{
|
2024-01-06 06:26:47 +08:00
|
|
|
static std::once_flag flag;
|
|
|
|
std::call_once(flag, []() {
|
|
|
|
#ifdef JKQTMATHTEXT_COMPILED_WITH_XITS
|
|
|
|
Q_INIT_RESOURCE(xits);
|
|
|
|
#endif
|
|
|
|
#ifdef JKQTMATHTEXT_COMPILED_WITH_FIRAMATH
|
|
|
|
Q_INIT_RESOURCE(firamath);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
);
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextFontSpecifier::JKQTMathTextFontSpecifier():
|
|
|
|
m_fontName(""),
|
2022-08-21 20:49:48 +08:00
|
|
|
m_mathFontName(""),
|
|
|
|
m_transformOnOutput(true)
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
2022-09-01 20:36:34 +08:00
|
|
|
initJKQTMathTextResources();
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextFontSpecifier::JKQTMathTextFontSpecifier(const QString &_fontName, const QString &_mathFontName):
|
|
|
|
m_fontName(_fontName),
|
2022-08-21 20:49:48 +08:00
|
|
|
m_mathFontName(_mathFontName),
|
|
|
|
m_transformOnOutput(true)
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
2022-09-01 20:36:34 +08:00
|
|
|
initJKQTMathTextResources();
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextFontSpecifier JKQTMathTextFontSpecifier::fromFontSpec(const QString &fontSpec)
|
|
|
|
{
|
|
|
|
JKQTMathTextFontSpecifier s;
|
|
|
|
s.setFontSpec(fontSpec);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTMathTextFontSpecifier::setFontSpec(const QString &fontSpec)
|
|
|
|
{
|
|
|
|
QStringList splitspec=fontSpec.split('+');
|
|
|
|
if (splitspec.size()==0) {
|
|
|
|
m_fontName=m_mathFontName="";
|
|
|
|
} else if (splitspec.size()==1) {
|
|
|
|
m_fontName=splitspec[0];
|
|
|
|
m_mathFontName="";
|
|
|
|
} else if (splitspec.size()==2) {
|
|
|
|
m_fontName=splitspec[0];
|
|
|
|
m_mathFontName=splitspec[1];
|
|
|
|
} else if (splitspec.size()>2) {
|
|
|
|
m_fontName=splitspec.mid(0, splitspec.size()-1).join('+');
|
|
|
|
m_mathFontName=splitspec.last();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString JKQTMathTextFontSpecifier::getFontSpec() const
|
|
|
|
{
|
|
|
|
QString res=m_fontName;
|
|
|
|
if (m_mathFontName.size()>0) res+="+"+m_mathFontName;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString JKQTMathTextFontSpecifier::fontName() const
|
|
|
|
{
|
2022-08-21 20:49:48 +08:00
|
|
|
if (m_transformOnOutput) return transformFontNameAndDecodeSpecialFonts(m_fontName, false);
|
|
|
|
else return m_fontName;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QString JKQTMathTextFontSpecifier::mathFontName() const
|
|
|
|
{
|
2022-08-21 20:49:48 +08:00
|
|
|
if (m_transformOnOutput) return transformFontNameAndDecodeSpecialFonts(m_mathFontName, true);
|
|
|
|
else return m_mathFontName;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2022-08-21 20:49:48 +08:00
|
|
|
QString JKQTMathTextFontSpecifier::fallbackSymbolsFontName() const
|
|
|
|
{
|
|
|
|
if (m_transformOnOutput) return transformFontNameAndDecodeSpecialFonts(m_fallbackSymbolFont, true);
|
|
|
|
else return m_fallbackSymbolFont;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTMathTextFontSpecifier::setFallbackSymbolsFontName(const QString &name)
|
|
|
|
{
|
|
|
|
m_fallbackSymbolFont=name;
|
|
|
|
}
|
|
|
|
|
2024-01-23 20:09:37 +08:00
|
|
|
QString JKQTMathTextFontSpecifier::transformFontName(const QString &fontName, bool /*mathmode*/)
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
|
|
|
const QString fnt=fontName.trimmed().toLower();
|
|
|
|
QFont testFnt;
|
2022-09-01 20:36:34 +08:00
|
|
|
if (fnt=="serif" || fnt=="times") {
|
2022-06-08 21:38:26 +08:00
|
|
|
testFnt.setStyleHint(QFont::StyleHint::Serif);
|
|
|
|
return testFnt.defaultFamily();
|
|
|
|
}
|
|
|
|
if (fnt=="sans-serif" || fnt=="sansserif" || fnt=="sans" || fnt=="sans serif") {
|
|
|
|
testFnt.setStyleHint(QFont::StyleHint::SansSerif);
|
|
|
|
return testFnt.defaultFamily();
|
|
|
|
}
|
|
|
|
if (fnt=="cursive") {
|
|
|
|
testFnt.setStyleHint(QFont::StyleHint::Cursive);
|
|
|
|
return testFnt.defaultFamily();
|
|
|
|
}
|
|
|
|
if (fnt=="typewriter") {
|
|
|
|
testFnt.setStyleHint(QFont::StyleHint::TypeWriter);
|
|
|
|
return testFnt.defaultFamily();
|
|
|
|
}
|
|
|
|
if (fnt=="monospace") {
|
|
|
|
testFnt.setStyleHint(QFont::StyleHint::Monospace);
|
|
|
|
return testFnt.defaultFamily();
|
|
|
|
}
|
|
|
|
if (fnt=="fantasy") {
|
|
|
|
testFnt.setStyleHint(QFont::StyleHint::Fantasy);
|
|
|
|
return testFnt.defaultFamily();
|
|
|
|
}
|
|
|
|
if (fnt=="system") {
|
|
|
|
testFnt.setStyleHint(QFont::StyleHint::System);
|
|
|
|
return testFnt.defaultFamily();
|
|
|
|
}
|
|
|
|
if (fnt=="decorative") {
|
|
|
|
testFnt.setStyleHint(QFont::StyleHint::Decorative);
|
|
|
|
return testFnt.defaultFamily();
|
|
|
|
}
|
|
|
|
if (fnt=="default" || fnt=="app" || fnt=="application") {
|
|
|
|
return QGuiApplication::font().family();
|
|
|
|
}
|
2022-09-01 20:36:34 +08:00
|
|
|
if (fnt=="appsf" || fnt=="application sf" || fnt=="application-sf") {
|
|
|
|
const QFont f=QGuiApplication::font().family();
|
|
|
|
if (f.styleHint()==QFont::SansSerif) {
|
|
|
|
testFnt.setStyleHint(QFont::StyleHint::Serif);
|
|
|
|
return testFnt.defaultFamily();
|
|
|
|
} else {
|
|
|
|
testFnt.setStyleHint(QFont::StyleHint::SansSerif);
|
|
|
|
return testFnt.defaultFamily();
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 21:38:26 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
|
|
|
|
if (fnt=="fixed") {
|
|
|
|
return QFontDatabase::systemFont(QFontDatabase::SystemFont::FixedFont).family();
|
|
|
|
}
|
|
|
|
if (fnt=="smallest_readable" || fnt=="smallestreadable" || fnt=="smallest readable" || fnt=="smallest") {
|
|
|
|
return QFontDatabase::systemFont(QFontDatabase::SystemFont::SmallestReadableFont).family();
|
|
|
|
}
|
|
|
|
if (fnt=="title") {
|
|
|
|
return QFontDatabase::systemFont(QFontDatabase::SystemFont::TitleFont).family();
|
|
|
|
}
|
|
|
|
if (fnt=="general") {
|
|
|
|
return QFontDatabase::systemFont(QFontDatabase::SystemFont::GeneralFont).family();
|
|
|
|
}
|
|
|
|
#elif QT_VERSION >= QT_VERSION_CHECK(5,2,0)
|
|
|
|
QFontDatabase fontDB;
|
|
|
|
if (fnt=="fixed") {
|
|
|
|
return fontDB.systemFont(QFontDatabase::SystemFont::FixedFont).family();
|
|
|
|
}
|
|
|
|
if (fnt=="smallest_readable" || fnt=="smallestreadable" || fnt=="smallest readable" || fnt=="smallest") {
|
|
|
|
return fontDB.systemFont(QFontDatabase::SystemFont::SmallestReadableFont).family();
|
|
|
|
}
|
|
|
|
if (fnt=="title") {
|
|
|
|
return fontDB.systemFont(QFontDatabase::SystemFont::TitleFont).family();
|
|
|
|
}
|
|
|
|
if (fnt=="general") {
|
|
|
|
return fontDB.systemFont(QFontDatabase::SystemFont::GeneralFont).family();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return fontName;
|
|
|
|
}
|
|
|
|
|
2022-08-21 20:49:48 +08:00
|
|
|
QString JKQTMathTextFontSpecifier::transformFontNameAndDecodeSpecialFonts(const QString &fontName, bool mathmode)
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
|
|
|
const QString fnt=fontName.toLower().trimmed();
|
2022-09-01 20:36:34 +08:00
|
|
|
if (fnt=="xits"||fnt=="xits_math") {
|
|
|
|
if (mathmode||fnt=="xits_math") return getXITSFamilies().mathFontName();
|
2022-08-21 20:49:48 +08:00
|
|
|
else return getXITSFamilies().fontName();
|
2022-09-01 20:36:34 +08:00
|
|
|
} else if (fnt=="asana"||fnt=="asana_math") {
|
|
|
|
if (mathmode||fnt=="asana_math") return getASANAFamilies().mathFontName();
|
2022-08-21 20:49:48 +08:00
|
|
|
else return getASANAFamilies().fontName();
|
2022-09-01 20:36:34 +08:00
|
|
|
} else if (fnt=="stix"||fnt=="stix_math") {
|
|
|
|
if (mathmode||fnt=="stix_math") return getSTIXFamilies().mathFontName();
|
2022-08-21 20:49:48 +08:00
|
|
|
else return getSTIXFamilies().fontName();
|
2022-09-01 20:36:34 +08:00
|
|
|
} else if (fnt=="fira"||fnt=="fira_math") {
|
|
|
|
if (mathmode||fnt=="firs_math") return getFIRAFamilies().mathFontName();
|
|
|
|
else return getFIRAFamilies().fontName();
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
2022-08-21 20:49:48 +08:00
|
|
|
return transformFontName(fontName, mathmode);
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JKQTMathTextFontSpecifier::hasFontName() const
|
|
|
|
{
|
|
|
|
return !m_fontName.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JKQTMathTextFontSpecifier::hasMathFontName() const
|
|
|
|
{
|
|
|
|
return !m_mathFontName.isEmpty();
|
|
|
|
}
|
|
|
|
|
2022-09-01 20:36:34 +08:00
|
|
|
bool JKQTMathTextFontSpecifier::hasFallbackSymbolFontName() const
|
2022-08-21 20:49:48 +08:00
|
|
|
{
|
|
|
|
return !m_fallbackSymbolFont.isEmpty();
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:38:26 +08:00
|
|
|
JKQTMathTextFontSpecifier JKQTMathTextFontSpecifier::getXITSFamilies()
|
|
|
|
{
|
2022-09-01 20:36:34 +08:00
|
|
|
initJKQTMathTextResources();
|
2024-01-06 06:26:47 +08:00
|
|
|
|
|
|
|
|
2022-06-08 21:38:26 +08:00
|
|
|
#if (QT_VERSION<QT_VERSION_CHECK(6, 0, 0))
|
|
|
|
QFontDatabase fdb;
|
|
|
|
const auto fontFamilies=fdb.families();
|
|
|
|
#else
|
|
|
|
const auto fontFamilies=QFontDatabase::families();
|
|
|
|
#endif
|
|
|
|
if (!fontFamilies.contains("XITS")) {
|
|
|
|
if (QFile::exists(":/JKQTMathText/fonts/xits-bold.otf")) { QFontDatabase::addApplicationFont(":/JKQTMathText/fonts/xits-bold.otf"); }
|
|
|
|
if (QFile::exists(":/JKQTMathText/fonts/xits-bolditalic.otf")) { QFontDatabase::addApplicationFont(":/JKQTMathText/fonts/xits-bolditalic.otf"); }
|
|
|
|
if (QFile::exists(":/JKQTMathText/fonts/xits-italic.otf")) { QFontDatabase::addApplicationFont(":/JKQTMathText/fonts/xits-italic.otf"); }
|
|
|
|
if (QFile::exists(":/JKQTMathText/fonts/xits-math.otf")) { QFontDatabase::addApplicationFont(":/JKQTMathText/fonts/xits-math.otf"); }
|
|
|
|
if (QFile::exists(":/JKQTMathText/fonts/xits-mathbold.otf")) { QFontDatabase::addApplicationFont(":/JKQTMathText/fonts/xits-mathbold.otf"); }
|
|
|
|
if (QFile::exists(":/JKQTMathText/fonts/xits-regular.otf")) { QFontDatabase::addApplicationFont(":/JKQTMathText/fonts/xits-regular.otf"); }
|
|
|
|
}
|
|
|
|
|
2024-01-06 06:26:47 +08:00
|
|
|
static JKQTMathTextFontSpecifier fontSpec;
|
2022-06-08 21:38:26 +08:00
|
|
|
if (fontSpec.m_fontName.isEmpty() && fontSpec.m_mathFontName.isEmpty()) {
|
2022-08-21 20:49:48 +08:00
|
|
|
fontSpec.m_transformOnOutput=false;
|
2022-06-08 21:38:26 +08:00
|
|
|
for (int i=0; i<fontFamilies.size(); i++) {
|
|
|
|
if (fontFamilies.at(i).contains("XITS Math")) {
|
|
|
|
fontSpec.m_mathFontName=fontFamilies.at(i);
|
|
|
|
} else if (fontFamilies.at(i).contains("XITS")) {
|
|
|
|
fontSpec.m_fontName=fontFamilies.at(i);
|
|
|
|
}
|
|
|
|
if (fontSpec.m_mathFontName.size()>0 && fontSpec.m_fontName.size()>0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fontSpec.m_mathFontName.isEmpty() && !fontSpec.m_fontName.isEmpty()) {
|
|
|
|
fontSpec.m_mathFontName=fontSpec.m_fontName;
|
|
|
|
} else if (!fontSpec.m_mathFontName.isEmpty() && fontSpec.m_fontName.isEmpty()) {
|
|
|
|
fontSpec.m_fontName=fontSpec.m_mathFontName;
|
|
|
|
}
|
2022-08-21 20:49:48 +08:00
|
|
|
fontSpec.m_fallbackSymbolFont=fontSpec.m_mathFontName;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return fontSpec;
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextFontSpecifier JKQTMathTextFontSpecifier::getASANAFamilies()
|
|
|
|
{
|
2024-01-06 06:26:47 +08:00
|
|
|
static JKQTMathTextFontSpecifier fontSpec=[]() -> JKQTMathTextFontSpecifier {
|
|
|
|
initJKQTMathTextResources();
|
|
|
|
#if (QT_VERSION<QT_VERSION_CHECK(6, 0, 0))
|
|
|
|
QFontDatabase fdb;
|
|
|
|
const auto fontFamilies=fdb.families();
|
|
|
|
#else
|
|
|
|
const auto fontFamilies=QFontDatabase::families();
|
|
|
|
#endif
|
|
|
|
if (!fontFamilies.contains("Asana") && !fontFamilies.contains("Asana Math")) {
|
|
|
|
if (QFile::exists(":/JKQTMathText/fonts/asana-math.otf")) { /*i=*/QFontDatabase::addApplicationFont(":/JKQTMathText/fonts/asana-math.otf"); }
|
|
|
|
}
|
2022-06-08 21:38:26 +08:00
|
|
|
|
|
|
|
|
2024-01-06 06:26:47 +08:00
|
|
|
JKQTMathTextFontSpecifier fontSpec;
|
2022-08-21 20:49:48 +08:00
|
|
|
fontSpec.m_transformOnOutput=false;
|
2022-06-08 21:38:26 +08:00
|
|
|
for (int i=0; i<fontFamilies.size(); i++) {
|
|
|
|
if (fontFamilies.at(i).contains("Asana Math")) {
|
|
|
|
fontSpec.m_mathFontName=fontFamilies.at(i);
|
|
|
|
} else if (fontFamilies.at(i).contains("Asana")) {
|
|
|
|
fontSpec.m_fontName=fontFamilies.at(i);
|
|
|
|
}
|
|
|
|
if (fontSpec.m_mathFontName.size()>0 && fontSpec.m_fontName.size()>0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fontSpec.m_mathFontName.isEmpty() && !fontSpec.m_fontName.isEmpty()) {
|
|
|
|
fontSpec.m_mathFontName=fontSpec.m_fontName;
|
|
|
|
} else if (!fontSpec.m_mathFontName.isEmpty() && fontSpec.m_fontName.isEmpty()) {
|
|
|
|
fontSpec.m_fontName=fontSpec.m_mathFontName;
|
|
|
|
}
|
2022-08-21 20:49:48 +08:00
|
|
|
fontSpec.m_fallbackSymbolFont=fontSpec.m_mathFontName;
|
2024-01-06 06:26:47 +08:00
|
|
|
return fontSpec;
|
|
|
|
}();
|
2022-06-08 21:38:26 +08:00
|
|
|
return fontSpec;
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextFontSpecifier JKQTMathTextFontSpecifier::getSTIXFamilies()
|
|
|
|
{
|
2024-01-06 06:26:47 +08:00
|
|
|
static JKQTMathTextFontSpecifier fontSpec=[]() -> JKQTMathTextFontSpecifier {
|
|
|
|
initJKQTMathTextResources();
|
|
|
|
static QStringList mathNames{"STIX Two Math", "STIX Math", "STIX Two Math Standard", "STIX Math Standard"};
|
|
|
|
static QStringList textNames{"STIX", "STIXGeneral", "STIX General"};
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2024-01-06 06:26:47 +08:00
|
|
|
JKQTMathTextFontSpecifier fontSpec;
|
2022-08-21 20:49:48 +08:00
|
|
|
fontSpec.m_transformOnOutput=false;
|
2022-06-08 21:38:26 +08:00
|
|
|
#if (QT_VERSION<QT_VERSION_CHECK(6, 0, 0))
|
|
|
|
QFontDatabase fdb;
|
|
|
|
const auto fontFamilies=fdb.families();
|
|
|
|
#else
|
|
|
|
const auto fontFamilies=QFontDatabase::families();
|
|
|
|
#endif
|
|
|
|
for (const QString& name:mathNames) {
|
|
|
|
for (int i=0; i<fontFamilies.size(); i++) {
|
|
|
|
if (fontFamilies.at(i).contains(name) ) {
|
|
|
|
fontSpec.m_mathFontName=fontFamilies.at(i);
|
|
|
|
}
|
|
|
|
if (fontSpec.m_mathFontName.size()>0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fontSpec.m_mathFontName.size()>0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const QString& name:textNames) {
|
|
|
|
for (int i=0; i<fontFamilies.size(); i++) {
|
|
|
|
if (fontFamilies.at(i).contains(name) ) {
|
|
|
|
fontSpec.m_fontName=fontFamilies.at(i);
|
|
|
|
}
|
|
|
|
if (fontSpec.m_fontName.size()>0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fontSpec.m_fontName.size()>0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fontSpec.m_mathFontName.isEmpty() && !fontSpec.m_fontName.isEmpty()) {
|
|
|
|
fontSpec.m_mathFontName=fontSpec.m_fontName;
|
|
|
|
} else if (!fontSpec.m_mathFontName.isEmpty() && fontSpec.m_fontName.isEmpty()) {
|
|
|
|
fontSpec.m_fontName=fontSpec.m_mathFontName;
|
|
|
|
}
|
2022-08-21 20:49:48 +08:00
|
|
|
fontSpec.m_fallbackSymbolFont=fontSpec.m_mathFontName;
|
2024-01-06 06:26:47 +08:00
|
|
|
return fontSpec;
|
|
|
|
}();
|
2022-06-08 21:38:26 +08:00
|
|
|
return fontSpec;
|
|
|
|
}
|
|
|
|
|
2022-09-01 20:36:34 +08:00
|
|
|
JKQTMathTextFontSpecifier JKQTMathTextFontSpecifier::getFIRAFamilies()
|
|
|
|
{
|
2024-01-06 06:26:47 +08:00
|
|
|
static JKQTMathTextFontSpecifier fontSpec=[]() -> JKQTMathTextFontSpecifier {
|
|
|
|
initJKQTMathTextResources();
|
|
|
|
#if (QT_VERSION<QT_VERSION_CHECK(6, 0, 0))
|
|
|
|
QFontDatabase fdb;
|
|
|
|
const auto fontFamilies=fdb.families();
|
|
|
|
#else
|
|
|
|
const auto fontFamilies=QFontDatabase::families();
|
|
|
|
#endif
|
|
|
|
if (!fontFamilies.contains("Fira Math")) {
|
|
|
|
if (QFile::exists(":/JKQTMathText/fonts/FiraMath-Regular.otf")) { QFontDatabase::addApplicationFont(":/JKQTMathText/fonts/FiraMath-Regular.otf"); }
|
|
|
|
}
|
2022-09-01 20:36:34 +08:00
|
|
|
|
2024-01-06 06:26:47 +08:00
|
|
|
JKQTMathTextFontSpecifier fontSpec;
|
2022-09-01 20:36:34 +08:00
|
|
|
fontSpec.m_transformOnOutput=false;
|
|
|
|
for (int i=0; i<fontFamilies.size(); i++) {
|
|
|
|
if (fontFamilies.at(i).contains("Fira Math")) {
|
|
|
|
fontSpec.m_mathFontName=fontFamilies.at(i);
|
|
|
|
fontSpec.m_fontName=fontFamilies.at(i);
|
|
|
|
}
|
|
|
|
if (fontFamilies.at(i).contains("Fira Sans")) {
|
|
|
|
fontSpec.m_fontName=fontFamilies.at(i);
|
|
|
|
}
|
|
|
|
if (fontSpec.m_mathFontName.size()>0 && fontSpec.m_fontName.size()>0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fontSpec.m_mathFontName.isEmpty() && !fontSpec.m_fontName.isEmpty()) {
|
|
|
|
fontSpec.m_mathFontName=fontSpec.m_fontName;
|
|
|
|
} else if (!fontSpec.m_mathFontName.isEmpty() && fontSpec.m_fontName.isEmpty()) {
|
|
|
|
fontSpec.m_fontName=fontSpec.m_mathFontName;
|
|
|
|
}
|
|
|
|
fontSpec.m_fallbackSymbolFont=fontSpec.m_mathFontName;
|
2024-01-06 06:26:47 +08:00
|
|
|
return fontSpec;
|
|
|
|
}();
|
2022-09-01 20:36:34 +08:00
|
|
|
|
|
|
|
return fontSpec;
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextFontSpecifier JKQTMathTextFontSpecifier::getAppFontFamilies()
|
|
|
|
{
|
2024-01-06 06:26:47 +08:00
|
|
|
static JKQTMathTextFontSpecifier fontSpec=[]() -> JKQTMathTextFontSpecifier {
|
|
|
|
JKQTMathTextFontSpecifier fontSpec;
|
2022-09-01 20:36:34 +08:00
|
|
|
#if (QT_VERSION<QT_VERSION_CHECK(6, 0, 0))
|
|
|
|
QFontDatabase fdb;
|
|
|
|
const auto fontFamilies=fdb.families();
|
|
|
|
#else
|
|
|
|
const auto fontFamilies=QFontDatabase::families();
|
|
|
|
#endif
|
|
|
|
const QFont f=QGuiApplication::font().family();
|
|
|
|
fontSpec.m_fontName=f.family();
|
|
|
|
fontSpec.m_mathFontName=f.family();
|
|
|
|
bool set=false;
|
|
|
|
if (f.family().toLower().startsWith("segoe ui")) {
|
|
|
|
if (fontFamilies.contains("Segoe UI Symbol")) {
|
|
|
|
fontSpec.m_fallbackSymbolFont=fontSpec.m_mathFontName="Segoe UI Symbol";
|
|
|
|
set=true;
|
|
|
|
}
|
|
|
|
} else if (f.family().toLower().startsWith("cambria")) {
|
|
|
|
if (fontFamilies.contains("Cambria Math")) {
|
|
|
|
fontSpec.m_fallbackSymbolFont=fontSpec.m_mathFontName="Cambria Math";
|
|
|
|
set=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!set) {
|
2024-01-11 18:31:12 +08:00
|
|
|
if (f.styleHint()==QFont::Serif) {
|
2022-09-01 20:36:34 +08:00
|
|
|
const JKQTMathTextFontSpecifier xits=getXITSFamilies();
|
|
|
|
if (xits.hasFallbackSymbolFontName()) fontSpec.m_fallbackSymbolFont=xits.fallbackSymbolsFontName();
|
|
|
|
if (xits.hasMathFontName()) fontSpec.m_mathFontName=xits.mathFontName();
|
2024-01-11 18:31:12 +08:00
|
|
|
} else {
|
|
|
|
const JKQTMathTextFontSpecifier fira=getFIRAFamilies();
|
|
|
|
if (fira.hasFallbackSymbolFontName()) fontSpec.m_fallbackSymbolFont=fira.fallbackSymbolsFontName();
|
|
|
|
if (fira.hasMathFontName()) fontSpec.m_mathFontName=fira.mathFontName();
|
2022-09-01 20:36:34 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-06 06:26:47 +08:00
|
|
|
return fontSpec;
|
|
|
|
}();
|
2022-09-01 20:36:34 +08:00
|
|
|
return fontSpec;
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextFontSpecifier JKQTMathTextFontSpecifier::getAppFontSFFamilies()
|
|
|
|
{
|
2024-01-06 06:26:47 +08:00
|
|
|
static JKQTMathTextFontSpecifier fontSpec=[]() -> JKQTMathTextFontSpecifier {
|
2024-01-11 18:31:12 +08:00
|
|
|
JKQTMathTextFontSpecifier fontSpec;
|
2022-09-01 20:36:34 +08:00
|
|
|
const QFont f=QGuiApplication::font().family();
|
|
|
|
QFont testFnt;
|
2024-01-11 18:31:12 +08:00
|
|
|
if (f.styleHint()==QFont::Serif) {
|
2022-09-01 20:36:34 +08:00
|
|
|
testFnt.setStyleHint(QFont::StyleHint::SansSerif);
|
2024-01-11 18:31:12 +08:00
|
|
|
fontSpec.m_fontName=fontSpec.m_mathFontName=testFnt.defaultFamily();;
|
|
|
|
} else {
|
|
|
|
testFnt.setStyleHint(QFont::StyleHint::Serif);
|
|
|
|
fontSpec.m_fontName=fontSpec.m_mathFontName=testFnt.defaultFamily();;
|
2022-09-01 20:36:34 +08:00
|
|
|
}
|
2024-01-06 06:26:47 +08:00
|
|
|
return fontSpec;
|
|
|
|
}();
|
2022-09-01 20:36:34 +08:00
|
|
|
return fontSpec;
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:38:26 +08:00
|
|
|
QString JKQTMathTextFontEncoding2String(JKQTMathTextFontEncoding e)
|
|
|
|
{
|
|
|
|
switch(e) {
|
2022-07-04 02:30:12 +08:00
|
|
|
case MTFEUnicode: return "MTFEUnicode";
|
|
|
|
case MTFEStandard: return "MTFELatin1";
|
|
|
|
case MTFEWinSymbol: return "MTFEWinSymbol";
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
|
2022-06-19 21:11:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
QString JKQTMathTextBraceType2String(JKQTMathTextBraceType type) {
|
|
|
|
switch(type) {
|
|
|
|
case MTBTAngleBracket:
|
|
|
|
return "angle_bracket";
|
|
|
|
case MTBTSquareBracket:
|
|
|
|
return "square_bracket";
|
|
|
|
case MTBTCeilBracket:
|
|
|
|
return "ceil_bracket";
|
|
|
|
case MTBTCurlyBracket:
|
|
|
|
return "curly_bracket";
|
|
|
|
case MTBTDoubleLine:
|
|
|
|
return "double_line";
|
|
|
|
case MTBTFloorBracket:
|
|
|
|
return "floor_bracket";
|
|
|
|
case MTBTParenthesis:
|
|
|
|
return "parenhesis";
|
|
|
|
case MTBTSingleLine:
|
|
|
|
return "single_line";
|
2022-06-27 03:17:42 +08:00
|
|
|
case MTBTTopCorner:
|
|
|
|
return "top_corner";
|
|
|
|
case MTBTBottomCorner:
|
|
|
|
return "bottom_corner";
|
2022-06-19 21:11:06 +08:00
|
|
|
case MTBTAny:
|
|
|
|
return "any";
|
|
|
|
case MTBTNone:
|
|
|
|
return "none";
|
2022-07-22 04:17:37 +08:00
|
|
|
case MTBTUnknown:
|
|
|
|
return "unknow";
|
2022-06-19 21:11:06 +08:00
|
|
|
}
|
2022-07-22 04:17:37 +08:00
|
|
|
return "???";
|
2022-06-19 21:11:06 +08:00
|
|
|
}
|
|
|
|
|
2022-08-03 15:55:45 +08:00
|
|
|
JKQTMathTextBraceType TokenName2JKQTMathTextBraceType(const QString &tokenName, bool* isOpening)
|
2022-06-19 21:11:06 +08:00
|
|
|
{
|
2022-08-03 15:55:45 +08:00
|
|
|
if (tokenName=="(") { if (isOpening) *isOpening=true; return MTBTParenthesis; }
|
|
|
|
if (tokenName==")") { if (isOpening) *isOpening=false; return MTBTParenthesis; }
|
|
|
|
if (tokenName=="[") { if (isOpening) *isOpening=true; return MTBTSquareBracket; }
|
|
|
|
if (tokenName=="]") { if (isOpening) *isOpening=false; return MTBTSquareBracket; }
|
|
|
|
if (tokenName=="{") { if (isOpening) *isOpening=true; return MTBTCurlyBracket; }
|
|
|
|
if (tokenName=="}") { if (isOpening) *isOpening=false; return MTBTCurlyBracket; }
|
|
|
|
if (tokenName=="|") { if (isOpening) *isOpening=true; return MTBTSingleLine; }
|
|
|
|
if (tokenName=="||" || tokenName=="#") { if (isOpening) *isOpening=true; return MTBTDoubleLine; }
|
|
|
|
|
|
|
|
if (tokenName=="<" || tokenName=="langle") { if (isOpening) *isOpening=true; return MTBTAngleBracket; }
|
|
|
|
if (tokenName==">" || tokenName=="rangle") { if (isOpening) *isOpening=false; return MTBTAngleBracket; }
|
|
|
|
if (tokenName=="_" || tokenName=="lfloor") { if (isOpening) *isOpening=true; return MTBTFloorBracket; }
|
|
|
|
if (tokenName=="_" || tokenName=="rfloor") { if (isOpening) *isOpening=false; return MTBTFloorBracket; }
|
|
|
|
if (tokenName=="~" || tokenName=="lceil") { if (isOpening) *isOpening=true; return MTBTCeilBracket; }
|
|
|
|
if (tokenName=="~" || tokenName=="rceil") { if (isOpening) *isOpening=false; return MTBTCeilBracket; }
|
|
|
|
if (tokenName=="ulcorner" || tokenName=="tlcorner") { if (isOpening) *isOpening=true; return MTBTTopCorner; }
|
|
|
|
if (tokenName=="urcorner" || tokenName=="trcorner") { if (isOpening) *isOpening=false; return MTBTTopCorner; }
|
|
|
|
if (tokenName=="blcorner" || tokenName=="llcorner") { if (isOpening) *isOpening=true; return MTBTBottomCorner; }
|
|
|
|
if (tokenName=="brcorner" || tokenName=="lrcorner") { if (isOpening) *isOpening=false; return MTBTBottomCorner; }
|
|
|
|
if (isOpening) *isOpening=true;
|
|
|
|
|
2022-06-19 21:11:06 +08:00
|
|
|
if (tokenName=="any") return MTBTAny;
|
|
|
|
if (tokenName=="." || tokenName=="" || tokenName=="none") return MTBTNone;
|
|
|
|
return MTBTUnknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextBraceType InstructionName2OpeningJKQTMathTextBraceType(const QString &tokenName)
|
|
|
|
{
|
|
|
|
if (tokenName=="{") return MTBTCurlyBracket;
|
|
|
|
if (tokenName=="|") return MTBTDoubleLine;
|
|
|
|
if (tokenName=="langle") return MTBTAngleBracket;
|
|
|
|
if (tokenName=="lfloor") return MTBTFloorBracket;
|
|
|
|
if (tokenName=="lceil") return MTBTCeilBracket;
|
2022-06-27 03:17:42 +08:00
|
|
|
if (tokenName=="tlcorner"||tokenName=="ulcorner") return MTBTTopCorner;
|
|
|
|
if (tokenName=="blcorner"||tokenName=="llcorner") return MTBTBottomCorner;
|
2022-06-19 21:11:06 +08:00
|
|
|
return MTBTUnknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextBraceType InstructionName2JKQTMathTextBraceType(const QString &tokenName)
|
|
|
|
{
|
|
|
|
if (tokenName=="{" || tokenName=="}") return MTBTCurlyBracket;
|
|
|
|
if (tokenName=="|") return MTBTDoubleLine;
|
|
|
|
if (tokenName=="langle" || tokenName=="rangle") return MTBTAngleBracket;
|
|
|
|
if (tokenName=="lfloor" || tokenName=="rfloor") return MTBTFloorBracket;
|
|
|
|
if (tokenName=="lceil" || tokenName=="rceil") return MTBTCeilBracket;
|
2022-06-27 03:17:42 +08:00
|
|
|
if (tokenName=="ulcorner" || tokenName=="urcorner"||tokenName=="tlcorner" || tokenName=="trcorner") return MTBTTopCorner;
|
|
|
|
if (tokenName=="blcorner" || tokenName=="brcorner"||tokenName=="llcorner" || tokenName=="lrcorner") return MTBTBottomCorner;
|
2022-06-19 21:11:06 +08:00
|
|
|
return MTBTUnknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TokenNameMatchesJKQTMathTextBraceType(const QString &token, JKQTMathTextBraceType type, bool acceptMTBTNone, bool* tokenEqualsNone)
|
|
|
|
{
|
|
|
|
const JKQTMathTextBraceType bt=TokenName2JKQTMathTextBraceType(token);
|
|
|
|
if (tokenEqualsNone) *tokenEqualsNone=(bt==MTBTNone);
|
|
|
|
if (type==MTBTAny) return true;
|
|
|
|
if (acceptMTBTNone && bt==MTBTNone) return true;
|
|
|
|
return (bt==type);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InstructionNameMatchesJKQTMathTextBraceType(const QString &token, JKQTMathTextBraceType type, bool acceptMTBTNone, bool* tokenEqualsNone)
|
|
|
|
{
|
|
|
|
const JKQTMathTextBraceType bt=InstructionName2JKQTMathTextBraceType(token);
|
|
|
|
if (tokenEqualsNone) *tokenEqualsNone=(bt==MTBTNone);
|
|
|
|
if (type==MTBTAny) return true;
|
|
|
|
if (acceptMTBTNone && bt==MTBTNone) return true;
|
|
|
|
return (bt==type);
|
|
|
|
}
|
|
|
|
|
2022-07-05 03:02:43 +08:00
|
|
|
QString JKQTMathTextEnvironment::FontSizeUnit2String(FontSizeUnit unit)
|
|
|
|
{
|
|
|
|
switch(unit) {
|
|
|
|
case PIXELS: return "pix";
|
|
|
|
default:
|
|
|
|
case POINTS: return "pt";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextEnvironment::FontSizeUnit JKQTMathTextEnvironment::String2FontSizeUnit(QString unit)
|
|
|
|
{
|
|
|
|
unit=unit.toLower().trimmed();
|
|
|
|
if (unit=="pt" || unit=="points" || unit=="point") return POINTS;
|
|
|
|
if (unit=="pix" || unit=="pixel" || unit=="pixels" || unit=="px") return PIXELS;
|
|
|
|
return POINTS;
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:38:26 +08:00
|
|
|
JKQTMathTextEnvironment::JKQTMathTextEnvironment() {
|
|
|
|
color=QColor("black");
|
|
|
|
font=MTEroman;
|
|
|
|
fontSize=10;
|
2022-07-05 03:02:43 +08:00
|
|
|
fontSizeUnit=POINTS;
|
2022-06-08 21:38:26 +08:00
|
|
|
bold=false;
|
|
|
|
italic=false;
|
2022-08-19 18:16:00 +08:00
|
|
|
capitalization=QFont::MixedCase;
|
2022-06-08 21:38:26 +08:00
|
|
|
underlined=false;
|
|
|
|
overline=false;
|
|
|
|
strike=false;
|
|
|
|
insideMath=false;
|
2022-08-02 17:38:40 +08:00
|
|
|
insideMathForceDigitsUpright=true;
|
2022-08-19 18:16:00 +08:00
|
|
|
insideMathUseTextStyle=false;
|
2022-08-02 17:38:40 +08:00
|
|
|
}
|
|
|
|
|
2022-08-19 18:16:00 +08:00
|
|
|
void JKQTMathTextEnvironment::beginMathMode(bool displaystyle)
|
2022-08-02 17:38:40 +08:00
|
|
|
{
|
|
|
|
insideMath=true;
|
|
|
|
insideMathForceDigitsUpright=true;
|
2022-08-19 18:16:00 +08:00
|
|
|
insideMathUseTextStyle=!displaystyle;
|
2022-08-02 17:38:40 +08:00
|
|
|
italic=true;
|
2022-08-19 18:16:00 +08:00
|
|
|
capitalization=QFont::MixedCase;
|
2022-08-02 17:38:40 +08:00
|
|
|
underlined=false;
|
|
|
|
overline=false;
|
|
|
|
strike=false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTMathTextEnvironment::endMathMode()
|
|
|
|
{
|
|
|
|
insideMath=false;
|
|
|
|
insideMathForceDigitsUpright=true;
|
|
|
|
italic=false;
|
2022-08-19 18:16:00 +08:00
|
|
|
capitalization=QFont::MixedCase;
|
2022-08-02 17:38:40 +08:00
|
|
|
underlined=false;
|
|
|
|
overline=false;
|
|
|
|
strike=false;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2022-08-19 18:16:00 +08:00
|
|
|
bool JKQTMathTextEnvironment::isMathDisplayStyle() const
|
|
|
|
{
|
|
|
|
if (insideMath) return !insideMathUseTextStyle;
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JKQTMathTextEnvironment::isMathTextStyle() const
|
|
|
|
{
|
|
|
|
return !isMathDisplayStyle();
|
|
|
|
}
|
|
|
|
|
2022-07-04 02:30:12 +08:00
|
|
|
JKQTMathTextFontEncoding JKQTMathTextEnvironment::getFontEncoding(JKQTMathText* parent) const {
|
|
|
|
switch (font) {
|
|
|
|
case MTEsans: if (insideMath) {
|
|
|
|
return parent->getFontEncodingMathSans();
|
|
|
|
} else {
|
|
|
|
return parent->getFontEncodingSans();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MTEmathSans: return parent->getFontEncodingMathSans(); break;
|
|
|
|
case MTEtypewriter: return parent->getFontEncodingTypewriter(); break;
|
|
|
|
case MTEscript: return parent->getFontEncodingScript(); break;
|
|
|
|
case MTEcaligraphic: return parent->getFontEncodingCaligraphic(); break;
|
|
|
|
case MTEblackboard: return parent->getFontEncodingBlackboard(); break;
|
|
|
|
case MTEfraktur: return parent->getFontEncodingFraktur(); break;
|
|
|
|
case MTEmathRoman: return parent->getFontEncodingMathRoman(); break;
|
|
|
|
case MTEroman: if (insideMath) {
|
|
|
|
return parent->getFontEncodingMathRoman();
|
|
|
|
} else {
|
|
|
|
return parent->getFontEncodingRoman();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return MTFEStandard;
|
|
|
|
}
|
|
|
|
return MTFEStandard;
|
|
|
|
}
|
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
QFont JKQTMathTextEnvironment::getFont(const JKQTMathText* parent) const {
|
2022-06-08 21:38:26 +08:00
|
|
|
QFont f;
|
|
|
|
switch (font) {
|
|
|
|
case MTEsans: if (insideMath) {
|
|
|
|
f.setFamily(parent->getFontMathSans());
|
|
|
|
} else {
|
|
|
|
f.setFamily(parent->getFontSans());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MTEmathSans: f.setFamily(parent->getFontMathSans()); break;
|
|
|
|
case MTEtypewriter: f.setFamily(parent->getFontTypewriter()); break;
|
|
|
|
case MTEscript: f.setFamily(parent->getFontScript()); break;
|
|
|
|
case MTEcaligraphic: f.setFamily(parent->getFontCaligraphic()); break;
|
|
|
|
case MTEblackboard: f.setFamily(parent->getFontBlackboard()); break;
|
|
|
|
case MTEfraktur: f.setFamily(parent->getFontFraktur()); break;
|
|
|
|
case MTEmathRoman: f.setFamily(parent->getFontMathRoman()); break;
|
2022-07-04 02:30:12 +08:00
|
|
|
case MTEFallbackSymbols: f.setFamily(parent->getFallbackFontSymbols()); break;
|
|
|
|
case MTECustomFont: f.setFamily(customFontName); break;
|
2022-06-08 21:38:26 +08:00
|
|
|
default:
|
|
|
|
case MTEroman: if (insideMath) {
|
|
|
|
f.setFamily(parent->getFontMathRoman());
|
|
|
|
} else {
|
|
|
|
f.setFamily(parent->getFontRoman());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
f.setBold(bold);
|
|
|
|
f.setItalic(italic);
|
|
|
|
f.setUnderline(underlined);
|
|
|
|
f.setOverline(overline);
|
|
|
|
f.setStrikeOut(strike);
|
2022-08-19 18:16:00 +08:00
|
|
|
f.setCapitalization(capitalization);
|
2022-07-05 03:02:43 +08:00
|
|
|
if (fontSizeUnit==POINTS) f.setPointSizeF(fontSize);
|
|
|
|
else if (fontSizeUnit==PIXELS) f.setPixelSize(static_cast<int>(fontSize));
|
2024-03-29 05:01:59 +08:00
|
|
|
const QFont::StyleStrategy strat=static_cast<QFont::StyleStrategy>(QFont::NoFontMerging|QFont::PreferAntialias);
|
|
|
|
f.setStyleStrategy(strat);
|
2022-06-08 21:38:26 +08:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2022-08-10 18:12:30 +08:00
|
|
|
JKQTMathTextEnvironment JKQTMathTextEnvironment::exchangedFontForRoman() const
|
|
|
|
{
|
|
|
|
if (insideMath) return exchangedFontFor(MTEmathRoman);
|
|
|
|
else return exchangedFontFor(MTEroman);
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextEnvironment JKQTMathTextEnvironment::exchangedFontFor(JKQTMathTextEnvironmentFont font) const
|
|
|
|
{
|
|
|
|
JKQTMathTextEnvironment newEnv=*this;
|
|
|
|
newEnv.font=font;
|
|
|
|
return newEnv;
|
|
|
|
}
|
|
|
|
|
2022-08-02 17:38:40 +08:00
|
|
|
QString JKQTMathTextEnvironment::toHtmlStart(JKQTMathTextEnvironment defaultEv, JKQTMathText* parentMathText) const {
|
2022-06-08 21:38:26 +08:00
|
|
|
QString s;
|
2022-07-05 03:02:43 +08:00
|
|
|
if (fontSizeUnit==POINTS) s=s+"font-size: "+QLocale::c().toString(fontSize)+"pt; ";
|
|
|
|
else if (fontSizeUnit==PIXELS) s=s+"font-size: "+QLocale::c().toString(fontSize)+"px; ";
|
|
|
|
|
2022-06-08 21:38:26 +08:00
|
|
|
if (insideMath) {
|
|
|
|
if (defaultEv.italic) {
|
|
|
|
if (!italic) s=s+"font-style: italic; ";
|
|
|
|
if (italic) s=s+"font-style: normal; ";
|
|
|
|
} else {
|
|
|
|
if (!italic) s=s+"font-style: italic; ";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!defaultEv.italic && italic) s=s+"font-style: italic; ";
|
|
|
|
}
|
|
|
|
if (bold && !defaultEv.bold) s=s+"font-weight: bold";
|
2022-08-02 17:38:40 +08:00
|
|
|
if (color!=defaultEv.color) s=s+"color: "+color.name();
|
|
|
|
if (font!=defaultEv.font) s=s+"font-family: "+getFont(parentMathText).family();
|
2022-06-08 21:38:26 +08:00
|
|
|
|
|
|
|
QStringList td;
|
|
|
|
if (underlined && !defaultEv.underlined) td<<"underline";
|
|
|
|
if (overline && !defaultEv.overline) td<<"overline";
|
|
|
|
if (strike && !defaultEv.strike) td<<"line-through";
|
|
|
|
if (td.size()>0) s=s+"text-decoration: "+td.join(", ");
|
|
|
|
return "<span style=\""+s+"\">";
|
|
|
|
}
|
|
|
|
|
2022-08-02 17:38:40 +08:00
|
|
|
QString JKQTMathTextEnvironment::toHtmlAfter(JKQTMathTextEnvironment /*defaultEv*/, JKQTMathText */*parentMathText*/) const {
|
2022-06-08 21:38:26 +08:00
|
|
|
return "</span>";
|
|
|
|
}
|
|
|
|
|
2024-01-11 21:22:46 +08:00
|
|
|
JKQTMathTextNodeSize::JKQTMathTextNodeSize(double width_, double baselineHeight_, double overallHeight_, double strikeoutPos_, double baselineXCorrection_, double topXCorrection_):
|
|
|
|
width(width_),
|
|
|
|
baselineHeight(baselineHeight_),
|
|
|
|
overallHeight(overallHeight_),
|
|
|
|
strikeoutPos(strikeoutPos_),
|
|
|
|
baselineXCorrection(baselineXCorrection_),
|
|
|
|
topXCorrection(topXCorrection_)
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JKQTMathTextFontDefinition::JKQTMathTextFontDefinition():
|
2022-07-04 02:30:12 +08:00
|
|
|
fontName("Times New Roman"), fontEncoding(MTFEStandard)
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QPainterPath JKQTMathTextMakeDArrow(double x, double y, double width, double arrowW, bool left, bool right) {
|
|
|
|
double x1=x;
|
|
|
|
double x2=x+width;
|
|
|
|
double dx=arrowW/4.0;
|
|
|
|
double y1=y-dx;
|
|
|
|
double y2=y+dx;
|
|
|
|
double x3=x2-arrowW/2.0;
|
|
|
|
double y3u=y-arrowW/2.0;
|
|
|
|
double y3d=y+arrowW/2.0;
|
|
|
|
double x3l=x+arrowW/2.0;
|
|
|
|
|
|
|
|
QPainterPath path;
|
|
|
|
path.moveTo(x1+dx, y1);
|
|
|
|
path.lineTo(x2-dx, y1);
|
|
|
|
path.moveTo(x1+dx, y2);
|
|
|
|
path.lineTo(x2-dx, y2);
|
|
|
|
if (right) {
|
|
|
|
path.moveTo(x3, y3u);
|
|
|
|
path.lineTo(x2, y);
|
|
|
|
path.lineTo(x3, y3d);
|
|
|
|
}
|
|
|
|
if (left) {
|
|
|
|
path.moveTo(x3l, y3u);
|
|
|
|
path.lineTo(x1, y);
|
|
|
|
path.lineTo(x3l, y3d);
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainterPath JKQTMathTextMakeArrow(double x, double y, double width, double arrowW, bool left, bool right) {
|
|
|
|
double x1=x;
|
|
|
|
double x2=x+width;
|
|
|
|
double x3=x2-arrowW/2.0;
|
|
|
|
double y3u=y-arrowW/2.0;
|
|
|
|
double y3d=y+arrowW/2.0;
|
|
|
|
double x3l=x+arrowW/2.0;
|
|
|
|
|
|
|
|
QPainterPath path;
|
|
|
|
path.moveTo(x1, y);
|
|
|
|
path.lineTo(x2, y);
|
|
|
|
if (right) {
|
|
|
|
path.moveTo(x3, y3u);
|
|
|
|
path.lineTo(x2, y);
|
|
|
|
path.lineTo(x3, y3d);
|
|
|
|
}
|
|
|
|
if (left) {
|
|
|
|
path.moveTo(x3l, y3u);
|
|
|
|
path.lineTo(x1, y);
|
|
|
|
path.lineTo(x3l, y3d);
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2022-06-27 05:42:06 +08:00
|
|
|
QPainterPath JKQTMathTextMakeHBracePath(double x, double ybrace, double width, double bw, double lineWidth, double cubicshrink, double cubiccontrolfac, double lineWidthShrinkFactor, double lineWidthGrowFactor) {
|
|
|
|
const double thinLW=lineWidthShrinkFactor*lineWidth;
|
|
|
|
const double thickLW=lineWidth*lineWidthGrowFactor;
|
|
|
|
const double xleft=x-width/2.0;
|
|
|
|
const double xleft_inner=xleft+thinLW;
|
|
|
|
const double xleftflat_leftbottom=xleft+cubicshrink*bw;
|
|
|
|
const double xleftflat_lefttop=xleft_inner+cubicshrink*bw;
|
|
|
|
const double xright=x+width/2.0;
|
|
|
|
const double xright_inner=xright-thinLW;
|
|
|
|
const double xrightflat_rightbottom=xright-cubicshrink*bw;
|
|
|
|
const double xrightflat_righttop=xright_inner-cubicshrink*bw;
|
|
|
|
const double xleftflat_righttop=x-bw*cubicshrink;
|
|
|
|
const double xlefttip=x-thinLW/2.0;
|
|
|
|
const double xleftflat_rightbottom=xlefttip-bw*cubicshrink;
|
|
|
|
const double xrightflat_lefttop=x+bw*cubicshrink;
|
|
|
|
const double xrighttip=x+thinLW/2.0;
|
|
|
|
const double xrightflat_leftbottom=xrighttip+bw*cubicshrink;
|
|
|
|
|
|
|
|
const double ytop=ybrace-bw*cubicshrink;
|
|
|
|
const double yctop=ybrace-thickLW/2.0;
|
|
|
|
const double ycbottom=ybrace+thickLW/2.0;
|
|
|
|
const double ybottom=ybrace+bw*cubicshrink;
|
|
|
|
const double ybottomtip=ybottom-thickLW;
|
|
|
|
|
|
|
|
const double dxyControl=bw*cubiccontrolfac;
|
2022-06-08 21:38:26 +08:00
|
|
|
|
|
|
|
QPainterPath path;
|
2022-06-27 05:42:06 +08:00
|
|
|
path.moveTo(xleft_inner, ytop);
|
|
|
|
path.lineTo(xleft, ytop); // top-left flat
|
|
|
|
path.cubicTo(xleft, ytop+dxyControl, xleftflat_leftbottom-dxyControl, ycbottom, xleftflat_leftbottom, ycbottom);
|
|
|
|
path.lineTo(xleftflat_rightbottom,ycbottom); // left arm, bottom
|
|
|
|
path.cubicTo(xleftflat_rightbottom+dxyControl, ycbottom, xlefttip, ybottom-dxyControl, xlefttip, ybottom);
|
|
|
|
path.lineTo(xrighttip, ybottom); // bottom flat
|
|
|
|
path.cubicTo(xrighttip, ybottom-dxyControl,xrightflat_leftbottom-dxyControl, ycbottom, xrightflat_leftbottom, ycbottom);
|
|
|
|
path.lineTo(xrightflat_rightbottom, ycbottom); // right arm, bottom
|
|
|
|
path.cubicTo(xrightflat_rightbottom+dxyControl, ycbottom, xright, ytop+dxyControl, xright, ytop);
|
|
|
|
|
|
|
|
path.lineTo(xright_inner,ytop); // top-right flat
|
|
|
|
path.cubicTo(xright_inner, ytop+dxyControl, xrightflat_righttop+dxyControl, yctop, xrightflat_righttop, yctop);
|
|
|
|
path.lineTo(xrightflat_lefttop, yctop); // right arm, top
|
|
|
|
path.cubicTo(xrightflat_lefttop-dxyControl, yctop, x, yctop, x, ybottomtip); // center-tip
|
|
|
|
path.cubicTo(x, yctop, xleftflat_righttop+dxyControl, yctop, xleftflat_righttop, yctop);
|
|
|
|
path.lineTo(xleftflat_lefttop, yctop); // left arm, top
|
|
|
|
path.cubicTo(xleftflat_lefttop-dxyControl, yctop, xleft_inner,ytop+dxyControl, xleft_inner, ytop);
|
|
|
|
path.closeSubpath();
|
2022-06-08 21:38:26 +08:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-09 00:16:31 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct JKQTMathTextCacheKeyBase {
|
2024-01-25 05:33:32 +08:00
|
|
|
JKQTMathTextCacheKeyBase():
|
|
|
|
f(), ldpiX(0), ldpiY(0), pdpiX(0), pdpiY(0)
|
|
|
|
{}
|
2024-01-09 00:16:31 +08:00
|
|
|
inline explicit JKQTMathTextCacheKeyBase(const QFont& f_, QPaintDevice *pd):
|
|
|
|
f(f_)
|
|
|
|
{
|
|
|
|
if (pd) {
|
|
|
|
ldpiX=pd->logicalDpiX();
|
|
|
|
ldpiY=pd->logicalDpiY();
|
|
|
|
pdpiX=pd->physicalDpiX();
|
|
|
|
pdpiY=pd->physicalDpiY();
|
|
|
|
} else {
|
|
|
|
ldpiX=0;
|
|
|
|
ldpiY=0;
|
|
|
|
pdpiX=0;
|
|
|
|
pdpiY=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QFont f;
|
|
|
|
int ldpiX, ldpiY, pdpiX, pdpiY;
|
|
|
|
|
|
|
|
inline bool operator==(const JKQTMathTextCacheKeyBase& other) const{
|
|
|
|
return ldpiX==other.ldpiX && ldpiY==other.ldpiY && pdpiX==other.pdpiX && pdpiY==other.pdpiY && f==other.f;
|
2024-01-25 05:33:32 +08:00
|
|
|
};
|
|
|
|
inline bool operator<(const JKQTMathTextCacheKeyBase& other) const{
|
|
|
|
return ldpiX<other.ldpiX && ldpiY<other.ldpiY && pdpiX<other.pdpiX && pdpiY<other.pdpiY && f<other.f;
|
|
|
|
};
|
|
|
|
inline bool operator!=(const JKQTMathTextCacheKeyBase& other) const{
|
|
|
|
return !operator==(other);
|
2024-01-09 00:16:31 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
|
2024-02-06 00:12:34 +08:00
|
|
|
inline size_t qHash(const JKQTMathTextCacheKeyBase& data, size_t /*seedin=0*/) {
|
2024-01-09 00:16:31 +08:00
|
|
|
#else
|
2024-02-06 00:12:34 +08:00
|
|
|
inline uint qHash(const JKQTMathTextCacheKeyBase& data, uint /*seedin=0*/) {
|
2024-01-09 00:16:31 +08:00
|
|
|
#endif
|
2024-02-06 00:12:34 +08:00
|
|
|
std::size_t seed=0;
|
|
|
|
jkqtp_combine_hash(seed, ::qHash(data.f,0));
|
|
|
|
jkqtp_combine_hash(seed, ::qHash(data.ldpiX,0));
|
|
|
|
jkqtp_combine_hash(seed, ::qHash(data.ldpiY,0));
|
|
|
|
jkqtp_combine_hash(seed, ::qHash(data.pdpiX,0));
|
|
|
|
jkqtp_combine_hash(seed, ::qHash(data.pdpiY,0));
|
|
|
|
return seed;
|
2024-01-09 00:16:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct JKQTMathTextCacheKeyBaseExt: public JKQTMathTextCacheKeyBase {
|
|
|
|
inline explicit JKQTMathTextCacheKeyBaseExt(const QFont& f_, QPaintDevice *pd_):
|
|
|
|
JKQTMathTextCacheKeyBase(f_,pd_), pd(pd_)
|
|
|
|
{}
|
|
|
|
|
|
|
|
QPaintDevice *pd;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class TText=QString>
|
|
|
|
struct JKQTMathTextTBRDataH: public JKQTMathTextCacheKeyBase {
|
2024-01-25 05:33:32 +08:00
|
|
|
JKQTMathTextTBRDataH():
|
|
|
|
JKQTMathTextCacheKeyBase(), text("")
|
|
|
|
{}
|
2024-01-09 00:16:31 +08:00
|
|
|
inline explicit JKQTMathTextTBRDataH(const QFont& f_, const TText& text_, QPaintDevice *pd):
|
|
|
|
JKQTMathTextCacheKeyBase(f_, pd), text(text_)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
TText text;
|
|
|
|
|
|
|
|
inline bool operator==(const JKQTMathTextTBRDataH& other) const{
|
|
|
|
return JKQTMathTextCacheKeyBase::operator==(other) && text==other.text;
|
2024-01-25 05:33:32 +08:00
|
|
|
};
|
|
|
|
inline bool operator!=(const JKQTMathTextTBRDataH& other) const{
|
|
|
|
return JKQTMathTextCacheKeyBase::operator!=(other) && text!=other.text;
|
|
|
|
};
|
|
|
|
inline bool operator<(const JKQTMathTextTBRDataH& other) const{
|
|
|
|
return JKQTMathTextCacheKeyBase::operator<(other) && text<other.text;
|
2024-01-09 00:16:31 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <class TText=QString>
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
|
|
|
|
inline size_t qHash(const JKQTMathTextTBRDataH<TText>& data, size_t /*seed=0*/) {
|
|
|
|
#else
|
2024-02-06 00:12:34 +08:00
|
|
|
inline uint qHash(const JKQTMathTextTBRDataH<TText>& data, uint /*seed=0*/) {
|
2024-01-09 00:16:31 +08:00
|
|
|
#endif
|
2024-02-06 00:12:34 +08:00
|
|
|
std::size_t seed=0;
|
|
|
|
jkqtp_combine_hash(seed, ::qHash(data.f,0));
|
|
|
|
jkqtp_combine_hash(seed, ::qHash(data.text,0));
|
|
|
|
jkqtp_combine_hash(seed, ::qHash(data.ldpiX,0));
|
|
|
|
jkqtp_combine_hash(seed, ::qHash(data.ldpiY,0));
|
|
|
|
jkqtp_combine_hash(seed, ::qHash(data.pdpiX,0));
|
|
|
|
jkqtp_combine_hash(seed, ::qHash(data.pdpiY,0));
|
|
|
|
return seed;
|
|
|
|
};
|
2024-01-09 00:16:31 +08:00
|
|
|
|
|
|
|
template <class TText=QString>
|
|
|
|
struct JKQTMathTextTBRDataHExt: public JKQTMathTextTBRDataH<TText> {
|
|
|
|
inline explicit JKQTMathTextTBRDataHExt(const QFont& f_, const TText& text_, QPaintDevice *pd_):
|
|
|
|
JKQTMathTextTBRDataH<TText>(f_,text_,pd_), pd(pd_)
|
|
|
|
{}
|
|
|
|
|
|
|
|
QPaintDevice *pd;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2024-03-18 18:32:09 +08:00
|
|
|
namespace std {
|
|
|
|
template<>
|
|
|
|
struct hash<JKQTMathTextCacheKeyBase>
|
2024-01-25 05:33:32 +08:00
|
|
|
{
|
2024-03-18 18:32:09 +08:00
|
|
|
size_t operator()(const JKQTMathTextCacheKeyBase& data) const noexcept
|
|
|
|
{
|
|
|
|
return qHash(data.f)+hash<int>()(data.ldpiX)+hash<int>()(data.ldpiY)+hash<int>()(data.pdpiX)+hash<int>()(data.pdpiY);
|
|
|
|
}
|
|
|
|
};
|
2024-01-25 05:33:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-03-18 18:32:09 +08:00
|
|
|
template<>
|
|
|
|
struct hash<JKQTMathTextTBRDataH<QString>>
|
2024-01-25 05:33:32 +08:00
|
|
|
{
|
2024-03-18 18:32:09 +08:00
|
|
|
size_t operator()(const JKQTMathTextTBRDataH<QString>& data) const noexcept
|
|
|
|
{
|
|
|
|
return qHash(data.f)+qHash(data.text)+hash<int>()(data.ldpiX)+hash<int>()(data.ldpiY)+hash<int>()(data.pdpiX)+hash<int>()(data.pdpiY);
|
|
|
|
}
|
|
|
|
};
|
2024-01-25 05:33:32 +08:00
|
|
|
|
2024-03-18 18:32:09 +08:00
|
|
|
template<>
|
|
|
|
struct hash<JKQTMathTextTBRDataH<QChar>>
|
2024-01-25 05:33:32 +08:00
|
|
|
{
|
2024-03-18 18:32:09 +08:00
|
|
|
size_t operator()(const JKQTMathTextTBRDataH<QChar>& data) const noexcept
|
|
|
|
{
|
|
|
|
return qHash(data.f)+qHash(data.text)+hash<int>()(data.ldpiX)+hash<int>()(data.ldpiY)+hash<int>()(data.pdpiX)+hash<int>()(data.pdpiY);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2024-01-09 00:16:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
QRectF JKQTMathTextGetTightBoundingRect(const QFont &f, const QString &text, QPaintDevice *pd)
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
2024-01-09 00:16:31 +08:00
|
|
|
//thread_local JKQTPDataCache<QRectF,JKQTMathTextTBRDataH,false,JKQTMathTextTBRDataHExt> cache(
|
2024-01-25 05:33:32 +08:00
|
|
|
static JKQTPDataCache<QRectF,JKQTMathTextTBRDataH<QString>,JKQTPDataCacheThreadSafe,JKQTMathTextTBRDataHExt<QString>> cache(
|
2024-01-09 00:16:31 +08:00
|
|
|
[](const JKQTMathTextTBRDataHExt<QString>& key) {
|
|
|
|
const QFontMetricsF fm(key.f, key.pd);
|
|
|
|
return fm.tightBoundingRect(key.text);
|
|
|
|
});
|
|
|
|
|
|
|
|
return cache.get_inline(f, text, pd);
|
|
|
|
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2024-01-09 00:16:31 +08:00
|
|
|
QRectF JKQTMathTextGetBoundingRect(const QFont &f, const QString &text, QPaintDevice *pd)
|
|
|
|
{
|
|
|
|
//thread_local JKQTPDataCache<QRectF,JKQTMathTextTBRDataH,false,JKQTMathTextTBRDataHExt> cache(
|
2024-01-25 05:33:32 +08:00
|
|
|
static JKQTPDataCache<QRectF,JKQTMathTextTBRDataH<QString>,JKQTPDataCacheThreadSafe,JKQTMathTextTBRDataHExt<QString>> cache(
|
2024-01-09 00:16:31 +08:00
|
|
|
[](const JKQTMathTextTBRDataHExt<QString>& key) {
|
|
|
|
const QFontMetricsF fm(key.f, key.pd);
|
|
|
|
return fm.boundingRect(key.text);
|
|
|
|
});
|
|
|
|
|
|
|
|
return cache.get_inline(f, text, pd);
|
|
|
|
}
|
2022-06-08 21:38:26 +08:00
|
|
|
|
|
|
|
|
2024-01-09 00:16:31 +08:00
|
|
|
qreal JKQTMathTextGetHorAdvance(const QFont &f, const QString &text, QPaintDevice *pd)
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
2024-01-09 00:16:31 +08:00
|
|
|
//thread_local JKQTPDataCache<double,JKQTMathTextTBRDataH,false,JKQTMathTextTBRDataHExt> cache(
|
2024-01-25 05:33:32 +08:00
|
|
|
static JKQTPDataCache<qreal,JKQTMathTextTBRDataH<QString>,JKQTPDataCacheThreadSafe,JKQTMathTextTBRDataHExt<QString>> cache(
|
2024-01-09 00:16:31 +08:00
|
|
|
[](const JKQTMathTextTBRDataHExt<QString>& key) {
|
|
|
|
const QFontMetricsF fm(key.f, key.pd);
|
|
|
|
#if (QT_VERSION>=QT_VERSION_CHECK(5, 15, 0))
|
|
|
|
return fm.horizontalAdvance(key.text);
|
|
|
|
#else
|
|
|
|
return fm.width(key.text);
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
|
|
|
|
return cache.get_inline(f, text, pd);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal JKQTMathTextGetRightBearing(const QFont &f, const QChar &text, QPaintDevice *pd)
|
|
|
|
{
|
|
|
|
//thread_local JKQTPDataCache<double,JKQTMathTextTBRDataH,false,JKQTMathTextTBRDataHExt> cache(
|
2024-01-25 05:33:32 +08:00
|
|
|
static JKQTPDataCache<qreal,JKQTMathTextTBRDataH<QChar>,JKQTPDataCacheThreadSafe,JKQTMathTextTBRDataHExt<QChar>> cache(
|
2024-01-09 00:16:31 +08:00
|
|
|
[](const JKQTMathTextTBRDataHExt<QChar>& key) {
|
|
|
|
const QFontMetricsF fm(key.f, key.pd);
|
|
|
|
return fm.rightBearing(key.text);
|
|
|
|
});
|
|
|
|
|
|
|
|
return cache.get_inline(f, text, pd);
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2024-01-09 00:16:31 +08:00
|
|
|
qreal JKQTMathTextGetLeftBearing(const QFont &f, const QChar &text, QPaintDevice *pd)
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
2024-01-09 00:16:31 +08:00
|
|
|
//thread_local JKQTPDataCache<double,JKQTMathTextTBRDataH,false,JKQTMathTextTBRDataHExt> cache(
|
2024-01-25 05:33:32 +08:00
|
|
|
static JKQTPDataCache<qreal,JKQTMathTextTBRDataH<QChar>,JKQTPDataCacheThreadSafe,JKQTMathTextTBRDataHExt<QChar>> cache(
|
2024-01-09 00:16:31 +08:00
|
|
|
[](const JKQTMathTextTBRDataHExt<QChar>& key) {
|
|
|
|
const QFontMetricsF fm(key.f, key.pd);
|
|
|
|
return fm.leftBearing(key.text);
|
|
|
|
});
|
|
|
|
|
|
|
|
return cache.get_inline(f, text, pd);
|
2022-06-08 21:38:26 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-09 00:16:31 +08:00
|
|
|
qreal JKQTMathTextGetFontAscent(const QFont &f, QPaintDevice *pd)
|
|
|
|
{
|
2024-01-25 05:33:32 +08:00
|
|
|
static JKQTPDataCache<qreal,JKQTMathTextCacheKeyBase,JKQTPDataCacheThreadSafe,JKQTMathTextCacheKeyBaseExt> cache(
|
2024-01-09 00:16:31 +08:00
|
|
|
[](const JKQTMathTextCacheKeyBaseExt& key) {
|
|
|
|
const QFontMetricsF fm(key.f, key.pd);
|
|
|
|
return fm.ascent();
|
|
|
|
});
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2024-01-09 00:16:31 +08:00
|
|
|
return cache.get_inline(f, pd);
|
|
|
|
}
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2024-01-09 00:16:31 +08:00
|
|
|
qreal JKQTMathTextGetFontDescent(const QFont &f, QPaintDevice *pd)
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
2024-01-25 05:33:32 +08:00
|
|
|
static JKQTPDataCache<qreal,JKQTMathTextCacheKeyBase,JKQTPDataCacheThreadSafe,JKQTMathTextCacheKeyBaseExt> cache(
|
2024-01-09 00:16:31 +08:00
|
|
|
[](const JKQTMathTextCacheKeyBaseExt& key) {
|
|
|
|
const QFontMetricsF fm(key.f, key.pd);
|
|
|
|
return fm.descent();
|
|
|
|
});
|
|
|
|
|
|
|
|
return cache.get_inline(f, pd);
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal JKQTMathTextGetFontHeight(const QFont &f, QPaintDevice *pd)
|
|
|
|
{
|
2024-01-25 05:33:32 +08:00
|
|
|
static JKQTPDataCache<qreal,JKQTMathTextCacheKeyBase,JKQTPDataCacheThreadSafe,JKQTMathTextCacheKeyBaseExt> cache(
|
2024-01-09 00:16:31 +08:00
|
|
|
[](const JKQTMathTextCacheKeyBaseExt& key) {
|
|
|
|
const QFontMetricsF fm(key.f, key.pd);
|
|
|
|
return fm.height();
|
|
|
|
});
|
|
|
|
|
|
|
|
return cache.get_inline(f, pd);
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal JKQTMathTextGetFontLineWidth(const QFont &f, QPaintDevice *pd)
|
|
|
|
{
|
2024-01-25 05:33:32 +08:00
|
|
|
static JKQTPDataCache<qreal,JKQTMathTextCacheKeyBase,JKQTPDataCacheThreadSafe,JKQTMathTextCacheKeyBaseExt> cache(
|
2024-01-09 00:16:31 +08:00
|
|
|
[](const JKQTMathTextCacheKeyBaseExt& key) {
|
|
|
|
const QFontMetricsF fm(key.f, key.pd);
|
|
|
|
return fm.lineWidth();
|
|
|
|
});
|
|
|
|
|
|
|
|
return cache.get_inline(f, pd);
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal JKQTMathTextGetFontLeading(const QFont &f, QPaintDevice *pd)
|
|
|
|
{
|
2024-01-25 05:33:32 +08:00
|
|
|
static JKQTPDataCache<qreal,JKQTMathTextCacheKeyBase,JKQTPDataCacheThreadSafe,JKQTMathTextCacheKeyBaseExt> cache(
|
2024-01-09 00:16:31 +08:00
|
|
|
[](const JKQTMathTextCacheKeyBaseExt& key) {
|
|
|
|
const QFontMetricsF fm(key.f, key.pd);
|
|
|
|
return fm.leading();
|
|
|
|
});
|
|
|
|
|
|
|
|
return cache.get_inline(f, pd);
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal JKQTMathTextGetFontLineSpacing(const QFont &f, QPaintDevice *pd)
|
|
|
|
{
|
2024-01-25 05:33:32 +08:00
|
|
|
static JKQTPDataCache<qreal,JKQTMathTextCacheKeyBase,JKQTPDataCacheThreadSafe,JKQTMathTextCacheKeyBaseExt> cache(
|
2024-01-09 00:16:31 +08:00
|
|
|
[](const JKQTMathTextCacheKeyBaseExt& key) {
|
|
|
|
const QFontMetricsF fm(key.f, key.pd);
|
|
|
|
return fm.lineSpacing();
|
|
|
|
});
|
|
|
|
|
|
|
|
return cache.get_inline(f, pd);
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal JKQTMathTextGetFontStrikoutPos(const QFont &f, QPaintDevice *pd)
|
|
|
|
{
|
2024-01-25 05:33:32 +08:00
|
|
|
static JKQTPDataCache<qreal,JKQTMathTextCacheKeyBase,JKQTPDataCacheThreadSafe,JKQTMathTextCacheKeyBaseExt> cache(
|
2024-01-09 00:16:31 +08:00
|
|
|
[](const JKQTMathTextCacheKeyBaseExt& key) {
|
|
|
|
const QFontMetricsF fm(key.f, key.pd);
|
|
|
|
return fm.strikeOutPos();
|
|
|
|
});
|
|
|
|
|
|
|
|
return cache.get_inline(f, pd);
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QFont JKQTMathTextGetNonItalic(const QFont &font)
|
|
|
|
{
|
|
|
|
QFont f=font;
|
|
|
|
f.setItalic(false);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2022-06-19 21:11:06 +08:00
|
|
|
|
|
|
|
bool isPrintableJKQTMathTextBraceType(JKQTMathTextBraceType type)
|
|
|
|
{
|
|
|
|
return (type!=MTBTAny) && (type!=MTBTUnknown);
|
|
|
|
}
|
2022-07-04 02:30:12 +08:00
|
|
|
|
|
|
|
JKQTMathTextFontEncoding estimateJKQTMathTextFontEncoding(QFont font)
|
|
|
|
{
|
|
|
|
font.setStyleStrategy(QFont::NoFontMerging);
|
|
|
|
const QString fontFamily=font.family().toLower();
|
|
|
|
if (fontFamily=="symbol") return JKQTMathTextFontEncoding::MTFEWinSymbol;
|
|
|
|
if (fontFamily.startsWith("xits") || fontFamily.startsWith("stix")||fontFamily.startsWith("asana")) return JKQTMathTextFontEncoding::MTFEUnicode;
|
|
|
|
const QFontMetricsF fm(font);
|
|
|
|
if (fm.inFont(QChar(0x3B1))) return JKQTMathTextFontEncoding::MTFEUnicode; // griechisch alpha
|
|
|
|
if (fm.inFont(QChar(0x2192))) return JKQTMathTextFontEncoding::MTFEUnicode; // pfeil nach rechts
|
|
|
|
if (fm.inFont(QChar(0x2202))) return JKQTMathTextFontEncoding::MTFEUnicode; // partial
|
|
|
|
if (fm.inFont(QChar(0x2208))) return JKQTMathTextFontEncoding::MTFEUnicode; // element
|
|
|
|
return JKQTMathTextFontEncoding::MTFELatin1;
|
|
|
|
}
|
2022-07-31 05:30:47 +08:00
|
|
|
|
|
|
|
QString JKQTMathTextHorizontalAlignment2String(JKQTMathTextHorizontalAlignment type)
|
|
|
|
{
|
|
|
|
switch(type) {
|
|
|
|
case MTHALeft: return "left";
|
|
|
|
case MTHARight: return "right";
|
|
|
|
default:
|
|
|
|
case MTHACentered: return "centered";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextHorizontalAlignment String2JKQTMathTextHorizontalAlignment(QString tokenName)
|
|
|
|
{
|
|
|
|
tokenName=tokenName.toLower().trimmed();
|
2022-07-31 13:56:13 +08:00
|
|
|
if (tokenName=="l" || tokenName=="left" || tokenName=="flushleft") return MTHALeft;
|
|
|
|
if (tokenName=="r" || tokenName=="right" || tokenName=="flushright") return MTHARight;
|
2022-07-31 05:30:47 +08:00
|
|
|
if (tokenName=="c" || tokenName=="center" || tokenName=="centered") return MTHACentered;
|
|
|
|
return MTHACentered;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString JKQTMathTextVerticalOrientation2String(JKQTMathTextVerticalOrientation mode)
|
|
|
|
{
|
|
|
|
switch(mode) {
|
|
|
|
case MTVOTop: return "top";
|
|
|
|
case MTVOCentered: return "centered";
|
|
|
|
case MTVOLastLine: return "last_line";
|
|
|
|
case MTVOBottom: return "bottom";
|
|
|
|
default:
|
|
|
|
case MTVOFirstLine: return "first_line";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextVerticalOrientation String2JKQTMathTextVerticalOrientation(QString tokenName)
|
|
|
|
{
|
|
|
|
tokenName=tokenName.toLower().trimmed();
|
|
|
|
if (tokenName=="p" || tokenName=="first_line" || tokenName=="first-line" || tokenName=="firstline" || tokenName=="line1") return MTVOFirstLine;
|
|
|
|
if (tokenName=="last_line" || tokenName=="last-line" || tokenName=="lastline" || tokenName=="linen") return MTVOLastLine;
|
|
|
|
if (tokenName=="t" || tokenName=="top") return MTVOTop;
|
|
|
|
if (tokenName=="b" || tokenName=="bottom") return MTVOBottom;
|
|
|
|
if (tokenName=="c" || tokenName=="center" || tokenName=="centered") return MTVOCentered;
|
|
|
|
return MTVOCentered;
|
|
|
|
}
|
2022-08-10 18:12:30 +08:00
|
|
|
|
|
|
|
QString JKQTMathTextBlackboradDrawingMode2String(JKQTMathTextBlackboradDrawingMode mode)
|
|
|
|
{
|
|
|
|
switch(mode) {
|
|
|
|
case MTBBDMfontDirectly: return "font_directly";
|
|
|
|
case MTBBDMsimulate: return "simulate";
|
|
|
|
case MTBBDMunicodeCharactersOrFontDirectly: return "unicode_or_font_directly";
|
|
|
|
case MTBBDMunicodeCharactersOrSimulate: return "unicode_or_simulate";
|
|
|
|
}
|
|
|
|
return "font_directly";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JKQTMathTextBlackboradDrawingMode String2JKQTMathTextBlackboradDrawingMode(QString mode)
|
|
|
|
{
|
|
|
|
mode=mode.toLower().simplified().trimmed();
|
|
|
|
if (mode=="font_directly" || mode=="font" || mode=="directly") return MTBBDMfontDirectly;
|
|
|
|
if (mode=="simulate") return MTBBDMsimulate;
|
|
|
|
if (mode=="unicode_or_font_directly" || mode=="unicode_or_font" || mode=="unicode_or_directly") return MTBBDMunicodeCharactersOrFontDirectly;
|
|
|
|
if (mode=="unicode_or_simulate") return MTBBDMunicodeCharactersOrSimulate;
|
|
|
|
if (mode=="default") return MTBBDMdefault;
|
|
|
|
return MTBBDMdefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTMathTextDrawStringSimBlackboard(QPainter &painter, const QFont &f, const QColor& color, double x, double y, const QString &txt)
|
|
|
|
{
|
2024-01-09 00:16:31 +08:00
|
|
|
const qreal lw=JKQTMathTextGetFontLineWidth(f, painter.device());
|
|
|
|
const QPen p(color, lw/4.0, Qt::SolidLine);
|
2022-08-10 18:12:30 +08:00
|
|
|
painter.setPen(p);
|
|
|
|
QPainterPath path;
|
|
|
|
path.addText(QPointF(x, y), f, txt);
|
2024-01-09 00:16:31 +08:00
|
|
|
path.addText(QPointF(x+lw/2.0, y), f, txt);
|
2022-08-10 18:12:30 +08:00
|
|
|
painter.drawPath(path);
|
|
|
|
}
|
2022-08-13 19:37:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
QString JKQTMathTextLineSpacingMode2String(JKQTMathTextLineSpacingMode mode)
|
|
|
|
{
|
|
|
|
switch(mode) {
|
|
|
|
case MTSMMinimalSpacing: return "minimal";
|
|
|
|
default:
|
|
|
|
case MTSMDefaultSpacing: return "default";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextLineSpacingMode String2JKQTMathTextLineSpacingMode(QString tokenName)
|
|
|
|
{
|
|
|
|
tokenName=tokenName.toLower().trimmed();
|
|
|
|
if (tokenName=="default") return MTSMDefaultSpacing;
|
|
|
|
if (tokenName=="minimal" || tokenName=="min" || tokenName=="minimum") return MTSMMinimalSpacing;
|
|
|
|
return MTSMDefaultSpacing;
|
|
|
|
}
|
2024-01-09 00:16:31 +08:00
|
|
|
|