From 122371e74399194941cb8fe6b1d5651cc39d4d80 Mon Sep 17 00:00:00 2001 From: jkriege2 Date: Wed, 16 Aug 2023 13:49:16 +0200 Subject: [PATCH] NEW: Added JKQTMathText::setFontOptions(), which allows to make fonts initially e.g. bold, italic, ... and extended JKQTMathText::setFontSpecial() accordingly --- doc/dox/whatsnew.dox | 4 +- lib/jkqtmathtext/jkqtmathtext.cpp | 113 ++++++++++++++++++++++++------ lib/jkqtmathtext/jkqtmathtext.h | 37 ++++++++++ 3 files changed, 130 insertions(+), 24 deletions(-) diff --git a/doc/dox/whatsnew.dox b/doc/dox/whatsnew.dox index df5adcdb18..67436d32a9 100644 --- a/doc/dox/whatsnew.dox +++ b/doc/dox/whatsnew.dox @@ -90,7 +90,8 @@ Changes, compared to \ref page_whatsnew_V4_0_0 "v4.0.0" include:
  • NEW: add color palettes from Green's HELIX method, see https://arxiv.org/abs/1108.5083, also see https://jiffyclub.github.io/palettable/cubehelix/, see JKQTPCreateGreensCubeHelixLUT() and e.g. JKQTPMathImageCubeHelixClassic, JKQTPMathImageCubeHelix1, ...
  • NEW: Using Q_SIGNALS/Q_SLOTS instead of signals/slots MOC-keywords ... this allows for interoperability with other signals/slots frameworks, thanks to user:nickmontini for the proposal
  • NEW: you can use any (preferably stepped/categorial) JKQTPMathImageColorPalette as default graph colors list in a style.ini file, by setting auto_styles/use_color_from_palette=PALETTE_NAME
  • -
  • NEW added entry for plotLabelColor to JKQTBasePlotterStyle
  • +
  • NEW: added entry for JKQTBasePlotterStyle::plotLabelColor to set the plot label color
  • +
  • NEW: Due to addition of JKQTMathText::setFontOptions() and the matchign extension of JKQTMathText::setFontSpecial() (see below) you can now add modifiers like +BOLD+ITALIC to any font-name provided to JKQTPlotter and in style INI-files
  • JKQTMathText:
  • diff --git a/lib/jkqtmathtext/jkqtmathtext.cpp b/lib/jkqtmathtext/jkqtmathtext.cpp index eb9f5c6fc3..98fcd27767 100644 --- a/lib/jkqtmathtext/jkqtmathtext.cpp +++ b/lib/jkqtmathtext/jkqtmathtext.cpp @@ -81,6 +81,7 @@ JKQTMathText::JKQTMathText(QObject* parent, bool useFontsForGUI): fontSize=10; fontSizeUnits=JKQTMathTextEnvironment::POINTS; + fontOptions=BaseFontDefault; fontColor=QColor("black"); brace_factor=1.04; brace_y_shift_factor=0.7;//-1; @@ -207,6 +208,7 @@ JKQTMathText::~JKQTMathText() { void JKQTMathText::loadSettings(const QSettings& settings, const QString& group){ fontSize=settings.value(group+"font_size", fontSize).toDouble(); fontSizeUnits=JKQTMathTextEnvironment::String2FontSizeUnit(settings.value(group+"font_size_units", JKQTMathTextEnvironment::FontSizeUnit2String(fontSizeUnits)).toString()); + fontOptions=String2BaseFontOptions(settings.value(group+"font_options", BaseFontOptions2String(fontOptions)).toString()); fontColor=jkqtp_String2QColor(settings.value(group+"font_color", jkqtp_QColor2String(fontColor)).toString()); brace_factor=settings.value(group+"brace_factor", brace_factor).toDouble(); subsuper_size_factor=settings.value(group+"subsuper_size_factor", subsuper_size_factor).toDouble(); @@ -254,6 +256,7 @@ void JKQTMathText::loadSettings(const QSettings& settings, const QString& group) void JKQTMathText::saveSettings(QSettings& settings, const QString& group) const{ settings.setValue(group+"font_size", fontSize); settings.setValue(group+"font_size_units", JKQTMathTextEnvironment::FontSizeUnit2String(fontSizeUnits)); + settings.setValue(group+"font_options", BaseFontOptions2String(fontOptions)); settings.setValue(group+"font_color", jkqtp_QColor2String(fontColor)); settings.setValue(group+ "brace_factor", brace_factor); settings.setValue(group+ "subsuper_size_factor", subsuper_size_factor); @@ -423,13 +426,16 @@ QString JKQTMathText::toHtml(bool *ok, double fontPointSize) { bool okk=false; if (getNodeTree()!=nullptr) { JKQTMathTextEnvironment ev; - ev.color=fontColor; + modifyEnvironmentFromFontSettings(ev); ev.fontSize=fontPointSize; - ev.fontSizeUnit=JKQTMathTextEnvironment::POINTS; + ev.fontSizeUnit=JKQTMathTextEnvironment::PIXELS; JKQTMathTextEnvironment defaultev; - defaultev.fontSize=fontPointSize; - defaultev.fontSizeUnit=JKQTMathTextEnvironment::POINTS; + const auto defColor=defaultev.color; + modifyEnvironmentFromFontSettings(defaultev); + defaultev.color=defColor; + ev.fontSize=fontPointSize; + ev.fontSizeUnit=JKQTMathTextEnvironment::PIXELS; okk=getNodeTree()->toHtml(s, ev, defaultev); } @@ -437,6 +443,30 @@ QString JKQTMathText::toHtml(bool *ok, double fontPointSize) { return s; } +QString JKQTMathText::BaseFontOptions2String(JKQTMathText::BaseFontOptions opt) +{ + QStringList res; + if (opt.testFlag(BaseFontBold)) res<<"BOLD"; + if (opt.testFlag(BaseFontItalic)) res<<"ITALIC"; + if (opt.testFlag(BaseFontUnderlined)) res<<"UNDERLINED"; + if (opt.testFlag(BaseFontSmallCaps)) res<<"SMALLCAPS"; + + return res.join('+'); +} + +JKQTMathText::BaseFontOptions JKQTMathText::String2BaseFontOptions(const QString &s) +{ + BaseFontOptions res=BaseFontDefault; + const auto opts=s.toUpper().trimmed().simplified().split('+'); + for (const auto& o: opts) { + if (o=="BOLD" || o=="BF" || o=="B") res.setFlag(BaseFontBold); + else if (o=="ITALIC" || o=="IT" || o=="I") res.setFlag(BaseFontItalic); + else if (o=="UNDERLINED" || o=="UL" || o=="U") res.setFlag(BaseFontUnderlined); + else if (o=="SMALLCAPS" || o=="SC" || o=="S") res.setFlag(BaseFontSmallCaps); + } + return res; +} + void JKQTMathText::setFontColor(const QColor &__value) { @@ -482,6 +512,21 @@ double JKQTMathText::getFontSizePixels() const else return -1; } +void JKQTMathText::setFontOptions(JKQTMathText::BaseFontOptions opts) +{ + fontOptions=opts; +} + +void JKQTMathText::setFontOption(JKQTMathText::BaseFontOption opt, bool enabled) +{ + fontOptions.setFlag(opt, enabled); +} + +JKQTMathText::BaseFontOptions JKQTMathText::getFontOptions() const +{ + return fontOptions; +} + void JKQTMathText::addReplacementFont(const QString &nonUseFont, const QString &useFont, JKQTMathTextFontEncoding useFontEncoding) { fontReplacements.insert(nonUseFont, useFont); fontEncodingReplacements.insert(nonUseFont, useFontEncoding); @@ -537,12 +582,16 @@ void JKQTMathText::setFontSpecial(const QString &fontSpec) { if (fontSpec.trimmed().size()==0) return; QString beforePlus=fontSpec; - QString afterPlus=""; - const int Iplus=fontSpec.lastIndexOf('+'); - if (Iplus>=0 && Iplus1) { + beforePlus=afterPlus.first(); + afterPlus.removeFirst(); + } else { + afterPlus.clear(); } + + if (beforePlus.toUpper()=="GUI") useGuiFonts(); else { const QStringList splitSlash=beforePlus.split('/'); @@ -565,10 +614,22 @@ void JKQTMathText::setFontSpecial(const QString &fontSpec) qDebug()<<"JKQTMathText::setFontSpecial(): undecodable fontSpec '"<parsedNode; } @@ -1174,15 +1236,24 @@ void JKQTMathText::getSizeDetail(QPainter& painter, double& width, double& ascen strikeoutPos=s.strikeoutPos; } + +void JKQTMathText::modifyEnvironmentFromFontSettings(JKQTMathTextEnvironment &ev) const +{ + ev.color=fontColor; + ev.fontSize=fontSize; + ev.fontSizeUnit=fontSizeUnits; + ev.bold=fontOptions.testFlag(BaseFontBold); + ev.italic=fontOptions.testFlag(BaseFontItalic); + ev.underlined=fontOptions.testFlag(BaseFontUnderlined); + if (fontOptions.testFlag(BaseFontSmallCaps)) ev.capitalization=QFont::SmallCaps; +} + JKQTMathTextNodeSize JKQTMathText::getSizeDetail(QPainter &painter) { JKQTMathTextNodeSize s; if (getNodeTree()!=nullptr) { JKQTMathTextEnvironment ev; - ev.color=fontColor; - ev.fontSize=fontSize; - ev.fontSizeUnit=fontSizeUnits; - + modifyEnvironmentFromFontSettings(ev); s=getNodeTree()->getSize(painter, ev); } return s; @@ -1199,9 +1270,7 @@ double JKQTMathText::draw(QPainter& painter, double x, double y, bool drawBoxes) painter.setPen(fontColor); painter.setBrush(Qt::NoBrush); JKQTMathTextEnvironment ev; - ev.color=fontColor; - ev.fontSize=fontSize; - ev.fontSizeUnit=fontSizeUnits; + modifyEnvironmentFromFontSettings(ev); getNodeTree()->setDrawBoxes(drawBoxes); const double xend=getNodeTree()->draw(painter, x, y, ev); return xend; @@ -1216,9 +1285,7 @@ void JKQTMathText::draw(QPainter& painter, unsigned int flags, QRectF rect, bool painter.setBrush(Qt::NoBrush); JKQTMathTextEnvironment ev; - ev.color=fontColor; - ev.fontSize=fontSize; - ev.fontSizeUnit=fontSizeUnits; + modifyEnvironmentFromFontSettings(ev); getNodeTree()->setDrawBoxes(drawBoxes); const JKQTMathTextNodeSize size= getSizeDetail(painter); diff --git a/lib/jkqtmathtext/jkqtmathtext.h b/lib/jkqtmathtext/jkqtmathtext.h index 23677300ad..81f3973494 100644 --- a/lib/jkqtmathtext/jkqtmathtext.h +++ b/lib/jkqtmathtext/jkqtmathtext.h @@ -333,6 +333,20 @@ class JKQTMATHTEXT_LIB_EXPORT JKQTMathText : public QObject { /** @name Font Handling */ /**@{*/ + /** \brief options for the base font */ + enum BaseFontOption { + BaseFontBold = 0x01, /*!< \brief output is bold (equivalent to placinf text in \textbf{...}) */ + BaseFontItalic = 0x02, /*!< \brief output is italic (equivalent to placinf text in \textit{...}) */ + BaseFontUnderlined = 0x04, /*!< \brief output is underlined (equivalent to placinf text in \underlined{...}) */ + BaseFontSmallCaps = 0x08, /*!< \brief output is underlined (equivalent to placinf text in \underlined{...}) */ + BaseFontDefault=0x00, /*!< \brief base font is used as is, without modifications */ + }; + Q_DECLARE_FLAGS(BaseFontOptions, BaseFontOption) + Q_FLAG(BaseFontOptions) + /** \brief convert BaseFontOptions to a string (e.g. BOLD+ITALIC */ + static QString BaseFontOptions2String(BaseFontOptions opt); + /** \brief convert a string (generated by BaseFontOptions2String() to BaseFontOptions */ + static BaseFontOptions String2BaseFontOptions(const QString& s); /** \copydoc fontColor */ void setFontColor(const QColor & __value); /** \copydoc fontColor */ @@ -353,6 +367,21 @@ class JKQTMATHTEXT_LIB_EXPORT JKQTMathText : public QObject { /** \brief returns the currently set default font size in pixels, if it was defined in points using setFontSizePixels(), or -1 if it was set in points with setFontSize() * \see setFontSizePixels(), fontSize, fontSizeUnits */ double getFontSizePixels() const; + /** \brief set (overwrite) the options applied to the base font + * + * \see BaseFontOptions + */ + void setFontOptions(BaseFontOptions opts); + /** \brief en/disable the given base font option + * + * \see BaseFontOptions + */ + void setFontOption(BaseFontOption opt,bool enabled=true); + /** \brief returns the options applied to the base font + * + * \see BaseFontOptions + */ + BaseFontOptions getFontOptions() const; /** \brief add a font pair to the table with font replacements @@ -412,6 +441,10 @@ class JKQTMATHTEXT_LIB_EXPORT JKQTMathText : public QObject { * - \c +ASANA use ASANA for math/symbols, calls useASANA() * - \c +STIX use STIX for math/symbols, calls useSTIX() * - \c +FIRA use Fira Math for math/symbols, calls useFiraMath() + * - \c +BOLD make font initially bold + * - \c +ITALIC make font initially italic + * - \c +UNDERLINED make font initially underlines + * - \c +SMALLCAPS make font initially smallcaps * . * - \c GUI use GUI-fonts, calls useGuiFonts() * . @@ -849,6 +882,10 @@ class JKQTMATHTEXT_LIB_EXPORT JKQTMathText : public QObject { double fontSize; /** \brief unit of fontSize */ JKQTMathTextEnvironment::FontSizeUnit fontSizeUnits; + /** \brief additional options for the base fonts */ + BaseFontOptions fontOptions; + /** \brief sets all properties of JKQTMathTextEnvironment \a ev from the fon settings stored in this class (e.g. fontSize, fontOptions, ...) */ + void modifyEnvironmentFromFontSettings(JKQTMathTextEnvironment& ev) const; /** \brief stores information about the different fonts used by LaTeX markup */