From e4c014c1a8fb232b38e6828963071a7b6eaf8d08 Mon Sep 17 00:00:00 2001 From: Filippo Scognamiglio Date: Mon, 22 Oct 2018 00:04:31 +0200 Subject: [PATCH] Initial support for system monospace fonts. --- app/app.pro | 6 ++- app/main.cpp | 5 +++ app/monospacefontmanager.cpp | 25 ++++++++++++ app/monospacefontmanager.h | 15 ++++++++ app/qml/ApplicationSettings.qml | 18 +++++++-- app/qml/Fonts.qml | 65 +++++++++++++++++++++++++++++++- app/qml/PreprocessedTerminal.qml | 11 ++---- 7 files changed, 131 insertions(+), 14 deletions(-) create mode 100644 app/monospacefontmanager.cpp create mode 100644 app/monospacefontmanager.h diff --git a/app/app.pro b/app/app.pro index 45caebe..39bd4ec 100644 --- a/app/app.pro +++ b/app/app.pro @@ -4,10 +4,12 @@ TARGET = cool-retro-term DESTDIR = $$OUT_PWD/../ HEADERS += \ - fileio.h + fileio.h \ + monospacefontmanager.h SOURCES = main.cpp \ - fileio.cpp + fileio.cpp \ + monospacefontmanager.cpp macx:ICON = icons/crt.icns diff --git a/app/main.cpp b/app/main.cpp index 28c71e1..993e7fa 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -10,7 +10,10 @@ #include #include +#include + #include +#include QString getNamedArgument(QStringList args, QString name, QString defaultName) { @@ -42,6 +45,7 @@ int main(int argc, char *argv[]) QQmlApplicationEngine engine; FileIO fileIO; + MonospaceFontManager monospaceFontManager; #if !defined(Q_OS_MAC) app.setWindowIcon(QIcon::fromTheme("cool-retro-term", QIcon(":../icons/32x32/cool-retro-term.png"))); @@ -83,6 +87,7 @@ int main(int argc, char *argv[]) engine.rootContext()->setContextProperty("workdir", getNamedArgument(args, "--workdir", "$HOME")); engine.rootContext()->setContextProperty("fileIO", &fileIO); + engine.rootContext()->setContextProperty("monospaceSystemFonts", monospaceFontManager.retrieveMonospaceFonts()); engine.rootContext()->setContextProperty("devicePixelRatio", app.devicePixelRatio()); diff --git a/app/monospacefontmanager.cpp b/app/monospacefontmanager.cpp new file mode 100644 index 0000000..28e910f --- /dev/null +++ b/app/monospacefontmanager.cpp @@ -0,0 +1,25 @@ +#include "monospacefontmanager.h" + +#include + +MonospaceFontManager::MonospaceFontManager(QObject *parent) : QObject(parent) +{ + +} + +QStringList MonospaceFontManager::retrieveMonospaceFonts() { + QStringList result; + + QFontDatabase fontDatabase; + QStringList fontFamilies = fontDatabase.families(); + + for (int i = 0; i < fontFamilies.size(); i++) { + QString fontFamily = fontFamilies[i]; + QFont font(fontFamily); + if (fontDatabase.isFixedPitch(font.family())) { + result.append(fontFamily); + } + } + + return result; +} diff --git a/app/monospacefontmanager.h b/app/monospacefontmanager.h new file mode 100644 index 0000000..53064a8 --- /dev/null +++ b/app/monospacefontmanager.h @@ -0,0 +1,15 @@ +#ifndef MONOSPACEFONTMANAGER_H +#define MONOSPACEFONTMANAGER_H + +#include +#include + +class MonospaceFontManager : public QObject +{ + Q_OBJECT +public: + explicit MonospaceFontManager(QObject *parent = nullptr); + Q_INVOKABLE QStringList retrieveMonospaceFonts(); +}; + +#endif // MONOSPACEFONTMANAGER_H diff --git a/app/qml/ApplicationSettings.qml b/app/qml/ApplicationSettings.qml index 96297b4..37f330d 100644 --- a/app/qml/ApplicationSettings.qml +++ b/app/qml/ApplicationSettings.qml @@ -102,7 +102,7 @@ QtObject{ property var fontNames: ["TERMINUS_SCALED", "COMMODORE_PET", "COMMODORE_PET"] property var fontlist: fontManager.item.fontlist - signal terminalFontChanged(string fontSource, int pixelSize, int lineSpacing, real screenScaling, real fontWidth) + signal terminalFontChanged(string fontFamily, int pixelSize, int lineSpacing, real screenScaling, real fontWidth) signal initializedSettings() @@ -119,12 +119,15 @@ QtObject{ onLoaded: handleFontChanged() } + property FontLoader fontLoader: FontLoader { } + onFontScalingChanged: handleFontChanged(); onFontWidthChanged: handleFontChanged(); function getIndexByName(name) { for (var i = 0; i < fontlist.count; i++) { - if (name === fontlist.get(i).name) + var requestedName = fontlist.get(i).name; + if (name === requestedName) return i; } return 0; // If the font is not available default to 0. @@ -154,10 +157,17 @@ QtObject{ var lineSpacing = fontManager.item.lineSpacing; var screenScaling = fontManager.item.screenScaling; var fontWidth = fontManager.item.defaultFontWidth * appSettings.fontWidth; + var fontFamily = fontManager.item.family; + var isSystemFont = fontManager.item.isSystemFont; lowResolutionFont = fontManager.item.lowResolutionFont; - terminalFontChanged(fontSource, pixelSize, lineSpacing, screenScaling, fontWidth); + if (!isSystemFont) { + fontLoader.source = fontSource; + fontFamily = fontLoader.name; + } + + terminalFontChanged(fontFamily, pixelSize, lineSpacing, screenScaling, fontWidth); } // FRAMES ///////////////////////////////////////////////////////////////// @@ -496,7 +506,7 @@ QtObject{ Component.onDestruction: { storeSettings(); storeCustomProfiles(); - //storage.dropSettings(); //DROPS THE SETTINGS!.. REMEMBER TO DISABLE ONCE ENABLED!! +// storage.dropSettings(); //DROPS THE SETTINGS!.. REMEMBER TO DISABLE ONCE ENABLED!! } // VARS /////////////////////////////////////////////////////////////////// diff --git a/app/qml/Fonts.qml b/app/qml/Fonts.qml index 7189723..16d4815 100644 --- a/app/qml/Fonts.qml +++ b/app/qml/Fonts.qml @@ -41,6 +41,10 @@ QtObject{ property real defaultFontWidth: fontlist.get(selectedFontIndex).fontWidth + property string family: fontlist.get(selectedFontIndex).family + + property bool isSystemFont: fontlist.get(selectedFontIndex).isSystemFont + // There are two kind of fonts: low resolution and high resolution. // Low resolution font sets the lowResolutionFont property to true. // They are rendered at a fixed pixel size and the texture is upscaled @@ -49,7 +53,7 @@ QtObject{ // size of the screen, and the scaling directly controls their pixels size. // Those are slower to render but are not pixelated. - property ListModel fontlist: ListModel{ + property ListModel fontlist: ListModel { ListElement{ name: "TERMINUS_SCALED" text: "Terminus (Modern)" @@ -59,6 +63,8 @@ QtObject{ baseScaling: 3.0 fontWidth: 1.0 lowResolutionFont: true + isSystemFont: false + family: "" } ListElement{ name: "PRO_FONT_SCALED" @@ -69,6 +75,8 @@ QtObject{ baseScaling: 3.0 fontWidth: 1.0 lowResolutionFont: true + isSystemFont: false + family: "" } ListElement{ name: "EXCELSIOR_SCALED" @@ -79,6 +87,8 @@ QtObject{ baseScaling: 2.4 fontWidth: 1.0 lowResolutionFont: true + isSystemFont: false + family: "" } ListElement{ name: "COMMODORE_PET_SCALED" @@ -89,6 +99,8 @@ QtObject{ baseScaling: 3.5 fontWidth: 0.7 lowResolutionFont: true + isSystemFont: false + family: "" } ListElement{ name: "PROGGY_TINY_SCALED" @@ -99,6 +111,8 @@ QtObject{ baseScaling: 3.0 fontWidth: 0.9 lowResolutionFont: true + isSystemFont: false + family: "" } ListElement{ name: "APPLE_II_SCALED" @@ -109,6 +123,8 @@ QtObject{ baseScaling: 3.5 fontWidth: 0.8 lowResolutionFont: true + isSystemFont: false + family: "" } ListElement{ name: "ATARI_400_SCALED" @@ -119,6 +135,8 @@ QtObject{ baseScaling: 3.5 fontWidth: 0.7 lowResolutionFont: true + isSystemFont: false + family: "" } ListElement{ name: "COMMODORE_64_SCALED" @@ -129,6 +147,8 @@ QtObject{ baseScaling: 3.5 fontWidth: 0.7 lowResolutionFont: true + isSystemFont: false + family: "" } ListElement{ name: "ATARI_ST_SCALED" @@ -139,6 +159,8 @@ QtObject{ baseScaling: 2.0 fontWidth: 1.0 lowResolutionFont: true + isSystemFont: false + family: "" } ListElement{ name: "IBM_DOS" @@ -149,6 +171,8 @@ QtObject{ baseScaling: 2.0 fontWidth: 1.0 lowResolutionFont: true + isSystemFont: false + family: "" } ListElement{ name: "HERMIT" @@ -158,6 +182,8 @@ QtObject{ pixelSize: 28 fontWidth: 1.0 lowResolutionFont: false + isSystemFont: false + family: "" } ListElement{ name: "TERMINUS" @@ -167,6 +193,8 @@ QtObject{ pixelSize: 35 fontWidth: 1.0 lowResolutionFont: false + isSystemFont: false + family: "" } ListElement{ name: "PRO_FONT" @@ -176,6 +204,8 @@ QtObject{ pixelSize: 35 fontWidth: 1.0 lowResolutionFont: false + isSystemFont: false + family: "" } ListElement{ name: "MONACO" @@ -185,6 +215,8 @@ QtObject{ pixelSize: 30 fontWidth: 1.0 lowResolutionFont: false + isSystemFont: false + family: "" } ListElement{ name: "INCONSOLATA" @@ -194,6 +226,8 @@ QtObject{ pixelSize: 35 fontWidth: 1.0 lowResolutionFont: false + isSystemFont: false + family: "" } ListElement{ name: "IBM_3278" @@ -203,6 +237,8 @@ QtObject{ pixelSize: 32 fontWidth: 1.0 lowResolutionFont: false + isSystemFont: false + family: "" } ListElement{ name: "Knight_TV" @@ -213,6 +249,33 @@ QtObject{ fontWidth: 1.0 baseScaling: 3.0 lowResolutionFont: true + isSystemFont: false + family: "" + } + } + + Component.onCompleted: addSystemFonts() + + function addSystemFonts() { + var families = monospaceSystemFonts; + for (var i = 0; i < families.length; i++) { + console.log("Adding system font: ", families[i]) + fontlist.append(convertToListElement(families[i])) + } + } + + function convertToListElement(family) { + return { + name: "System: " + family, + text: qsTr("System: ") + family, + source: "", + lineSpacing: 0.1, + pixelSize: 30, + fontWidth: 1.0, + baseScaling: 1.0, + lowResolutionFont: false, + isSystemFont: true, + family: family } } } diff --git a/app/qml/PreprocessedTerminal.qml b/app/qml/PreprocessedTerminal.qml index 76803f4..e168689 100644 --- a/app/qml/PreprocessedTerminal.qml +++ b/app/qml/PreprocessedTerminal.qml @@ -114,14 +114,10 @@ Item{ } } - FontLoader{ id: fontLoader } - - function handleFontChange(fontSource, pixelSize, lineSpacing, screenScaling, fontWidth){ - fontLoader.source = fontSource; - + function handleFontChanged(fontFamily, pixelSize, lineSpacing, screenScaling, fontWidth) { kterminal.antialiasText = !appSettings.lowResolutionFont; font.pixelSize = pixelSize; - font.family = fontLoader.name; + font.family = fontFamily; terminalContainer.fontWidth = fontWidth; terminalContainer.screenScaling = screenScaling; @@ -129,6 +125,7 @@ Item{ kterminal.lineSpacing = lineSpacing; } + function startSession() { appSettings.initializedSettings.disconnect(startSession); @@ -152,7 +149,7 @@ Item{ forceActiveFocus(); } Component.onCompleted: { - appSettings.terminalFontChanged.connect(handleFontChange); + appSettings.terminalFontChanged.connect(handleFontChanged); appSettings.initializedSettings.connect(startSession); } }