changeset 28767:b075aa252ca7

improved detection of standard fixed witdh font (bug #59128) * resource-manager.cc (get_default_font_family): use QFontDatabase for the systems standard fixed width font (requires Qt 5.2) or use the hard coded default Monaco on macOS * gui-preferences-global.h: use Monaco as default monospace font on macOS; use Q_OS_xxx macros instead of Q_WS_xxx macros
author Torsten Lilge <ttl-octave@mailbox.org>
date Tue, 22 Sep 2020 16:13:25 +0200
parents ec9efcc717cc
children bffd48bb4b8c
files libgui/src/gui-preferences-global.h libgui/src/resource-manager.cc
diffstat 2 files changed, 35 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/gui-preferences-global.h	Mon Sep 21 15:20:33 2020 +0200
+++ b/libgui/src/gui-preferences-global.h	Tue Sep 22 16:13:25 2020 +0200
@@ -31,12 +31,12 @@
 // Global preferences
 
 // Get the default monospaced font
-#if defined (Q_WS_X11)
+#if defined (Q_OS_WIN)
+const QString global_font_family = "Courier";
+#elif defined (Q_OS_MAC)
+const QString global_font_family = "Monaco";
+#else
 const QString global_font_family = "Monospace";
-#elif defined (Q_WS_WIN) || defined (Q_WS_MAC)
-const QString global_font_family = "Courier";
-#else
-const QString global_font_family = "Courier";
 #endif
 
 const gui_pref
--- a/libgui/src/resource-manager.cc	Mon Sep 21 15:20:33 2020 +0200
+++ b/libgui/src/resource-manager.cc	Tue Sep 22 16:13:25 2020 +0200
@@ -33,6 +33,8 @@
 
 #include <QDir>
 #include <QFile>
+#include <QFontComboBox>
+#include <QFontDatabase>
 #include <QLibraryInfo>
 #include <QMessageBox>
 #include <QNetworkProxy>
@@ -217,11 +219,35 @@
 
   QString resource_manager::get_default_font_family (void)
   {
-    // Get the default monospaced font
-    QFont fixed_font;
-    fixed_font.setStyleHint (QFont::Monospace);
-    QString default_family = fixed_font.defaultFamily ();
+    QString default_family;
+
+#if defined (Q_OS_MAC)
+  // Use hard coded default on macOS, since selection of fixed width
+  // default font is unreliable (see bug #59128).
+
+  // Get all available fixed width fonts via a font combobox
+  QFontComboBox font_combo_box;
+  font_combo_box.setFontFilters (QFontComboBox::MonospacedFonts);
+  QStringList fonts;
+
+  for (int index = 0; index < font_combo_box.count(); index++)
+    fonts << font_combo_box.itemText(index);
 
+  // Test for macOS default fixed width font
+  if (fonts.contains (global_mono_font.def.toString ()))
+    default_family = global_mono_font.def.toString ();
+#endif
+
+  // If default font is still empty (on all other platforms or
+  // if macOS default font is not available): use QFontDatabase
+  if (default_family.isEmpty ())
+    {
+      // Get the system's default monospaced font
+      QFont fixed_font = QFontDatabase::systemFont (QFontDatabase::FixedFont);
+      default_family = fixed_font.defaultFamily ();
+    }
+
+    // Test env variable which has preference
     std::string env_default_family = sys::env::getenv ("OCTAVE_DEFAULT_FONT");
     if (! env_default_family.empty ())
       default_family = QString::fromStdString (env_default_family);