From 6438636744339a433c0439077bea134b62a1793e Mon Sep 17 00:00:00 2001
From: Federico Terraneo <fede.tft@miosix.org>
Date: Sun, 13 Nov 2022 23:59:54 +0100
Subject: [PATCH] Only use defaultFont throughout the library to allow
 disabling unused fonts to save code size

---
 config/mxgui_settings.h              |  8 +++++++-
 display.cpp                          |  4 ++--
 drivers/display_bitsboard.cpp        |  1 -
 drivers/display_mp3v2.cpp            |  1 -
 drivers/display_oledboard2.cpp       |  1 -
 drivers/display_qt.cpp               |  1 -
 drivers/display_redbull_v2.cpp       |  1 -
 drivers/display_sony-newman.cpp      |  1 -
 drivers/display_st7735.cpp           |  1 -
 drivers/display_stm3210e-eval.cpp    |  1 -
 drivers/display_stm32f4discovery.cpp |  2 --
 drivers/display_strive.cpp           |  1 -
 drivers/display_win.cpp              |  1 -
 level2/application.cpp               | 12 +-----------
 level2/simple_plot.cpp               |  3 +--
 level2/simple_plot.h                 |  2 ++
 16 files changed, 13 insertions(+), 28 deletions(-)

diff --git a/config/mxgui_settings.h b/config/mxgui_settings.h
index 5c54fe6..1d2a77c 100644
--- a/config/mxgui_settings.h
+++ b/config/mxgui_settings.h
@@ -33,7 +33,7 @@
 #ifndef MXGUI_SETTINGS_H
 #define	MXGUI_SETTINGS_H
 
-#define MXGUI_SETTINGS_VERSION 101
+#define MXGUI_SETTINGS_VERSION 102
 
 // Before you can compile mxgui you have to configure it by editing this
 // file. After that, comment out this line to disable the reminder error.
@@ -100,6 +100,9 @@ static const unsigned int level2MaxNumApps=4;
 #define MXGUI_FONT_TAHOMA
 #define MXGUI_ENABLE_BOLD_FONTS
 
+//Default font
+#define defaultFont droid11
+
 #else //_MIOSIX
 
 // Enable or disable level 2.
@@ -151,6 +154,9 @@ static const unsigned int SIMULATOR_BGCOLOR=0;
 #define MXGUI_FONT_TAHOMA
 #define MXGUI_ENABLE_BOLD_FONTS
 
+//Default font
+#define defaultFont droid11
+
 #endif //_MIOSIX
 
 } //namespace mxgui
diff --git a/display.cpp b/display.cpp
index 936b3b4..dfda807 100644
--- a/display.cpp
+++ b/display.cpp
@@ -29,7 +29,7 @@
 #include "misc_inst.h"
 #include "pthread_lock.h"
 
-#if MXGUI_SETTINGS_VERSION != 101
+#if MXGUI_SETTINGS_VERSION != 102
 #error Wrong mxgui_settings.h version. You need to upgrade it.
 #endif
 
@@ -70,7 +70,7 @@ DisplayManager::DisplayManager()
 // class Display
 //
 
-Display::Display() : isDisplayOn(true), font(miscFixed)
+Display::Display() : isDisplayOn(true), font(defaultFont)
 {
     pthread_mutexattr_t temp;
     pthread_mutexattr_init(&temp);
diff --git a/drivers/display_bitsboard.cpp b/drivers/display_bitsboard.cpp
index cc4e197..bd1bd8f 100644
--- a/drivers/display_bitsboard.cpp
+++ b/drivers/display_bitsboard.cpp
@@ -269,7 +269,6 @@ DisplayImpl::~DisplayImpl()
 
 DisplayImpl::DisplayImpl(): buffer(0), last()
 {
-    setFont(miscFixed);
     setTextColor(make_pair(Color(black),Color(white)));
     {
         FastInterruptDisableLock dLock;
diff --git a/drivers/display_mp3v2.cpp b/drivers/display_mp3v2.cpp
index c5cd5b3..c11ae83 100644
--- a/drivers/display_mp3v2.cpp
+++ b/drivers/display_mp3v2.cpp
@@ -358,7 +358,6 @@ DisplayImpl::DisplayImpl(): buffer(0)
     //Power up sequence -- end
     //
     
-    setFont(droid11);
     setTextColor(make_pair(Color(0xffff),Color(0x0000)));
     clear(black);
 }
diff --git a/drivers/display_oledboard2.cpp b/drivers/display_oledboard2.cpp
index a0bd374..42d4cdd 100644
--- a/drivers/display_oledboard2.cpp
+++ b/drivers/display_oledboard2.cpp
@@ -485,7 +485,6 @@ DisplayImpl::DisplayImpl()
     sendCommand8(0x23,0x00);
     sendCommand8(0x26,0xa0);
     
-    setFont(droid21);
     setTextColor(make_pair(Color(0xffff),Color(0x0000)));
     clear(black);
     doTurnOn();
diff --git a/drivers/display_qt.cpp b/drivers/display_qt.cpp
index d27cf61..7e58fcb 100644
--- a/drivers/display_qt.cpp
+++ b/drivers/display_qt.cpp
@@ -245,7 +245,6 @@ DisplayImpl::~DisplayImpl()
 DisplayImpl::DisplayImpl(): buffer(0), last(), beginPixelCalled(false),
                             backend(QTBackend::instance())
 {
-    setFont(droid11);
     setTextColor(make_pair(Color(SIMULATOR_FGCOLOR),Color(SIMULATOR_BGCOLOR)));
 }
 
diff --git a/drivers/display_redbull_v2.cpp b/drivers/display_redbull_v2.cpp
index e1daaf9..9b3987f 100644
--- a/drivers/display_redbull_v2.cpp
+++ b/drivers/display_redbull_v2.cpp
@@ -291,7 +291,6 @@ DisplayImpl::DisplayImpl(): buffer(0)
     writeReg(0x004f,0);
     writeReg(0x004e,0);
     //Fill display
-    setFont(droid11);
     setTextColor(make_pair(white, black));
     clear(black);
 }
diff --git a/drivers/display_sony-newman.cpp b/drivers/display_sony-newman.cpp
index db89885..328b4ed 100644
--- a/drivers/display_sony-newman.cpp
+++ b/drivers/display_sony-newman.cpp
@@ -375,7 +375,6 @@ DisplayImpl::~DisplayImpl() {}
 DisplayImpl::DisplayImpl(): which(0)
 {
     doTurnOn();
-    setFont(droid11);
     setTextColor(make_pair(Color(0xffff),Color(0x0000)));
 }
 
diff --git a/drivers/display_st7735.cpp b/drivers/display_st7735.cpp
index 365f826..9819860 100644
--- a/drivers/display_st7735.cpp
+++ b/drivers/display_st7735.cpp
@@ -333,7 +333,6 @@ void DisplayGenericST7735::initialize() {
     sendCmds(initST7735b);
 
     doTurnOn();
-    setFont(droid11);
     setTextColor(make_pair(white, black));
 }
 
diff --git a/drivers/display_stm3210e-eval.cpp b/drivers/display_stm3210e-eval.cpp
index fea55f3..feb0ad5 100644
--- a/drivers/display_stm3210e-eval.cpp
+++ b/drivers/display_stm3210e-eval.cpp
@@ -290,7 +290,6 @@ DisplayImpl::DisplayImpl(): buffer(0), displayType(UNKNOWN)
             break;
     }
 
-    setFont(droid11);
     setTextColor(make_pair(Color(0xffff),Color(0x0000)));
     clear(black);
 }
diff --git a/drivers/display_stm32f4discovery.cpp b/drivers/display_stm32f4discovery.cpp
index f0799a1..4448c11 100644
--- a/drivers/display_stm32f4discovery.cpp
+++ b/drivers/display_stm32f4discovery.cpp
@@ -469,7 +469,6 @@ DisplayImpl::DisplayImpl()
             | 0                //no dithering
             | LTDC_GCR_LTDCEN; //Display enabled
     
-    setFont(droid11);
     setTextColor(make_pair(Color(0xffff),Color(0x0000)));
     clear(black);
 }
@@ -1317,7 +1316,6 @@ DisplayImpl::DisplayImpl()
     // Update the display
     DSI->WCR |= DSI_WCR_LTDCEN;
 
-    setFont(droid11);
     setTextColor(make_pair(Color(0xffff), Color(0x0000)));
     clear(black);
 }
diff --git a/drivers/display_strive.cpp b/drivers/display_strive.cpp
index 7e95ba3..a196ca3 100644
--- a/drivers/display_strive.cpp
+++ b/drivers/display_strive.cpp
@@ -479,7 +479,6 @@ DisplayImpl::DisplayImpl(): buffer(0)
     writeReg(0x07, D0 | D1 |DTE | GON | BASEE);
 
     //Fill display
-    setFont(droid11);
     setTextColor(make_pair(white, black));
     clear(black);
 }
diff --git a/drivers/display_win.cpp b/drivers/display_win.cpp
index 483aa47..d6d8635 100644
--- a/drivers/display_win.cpp
+++ b/drivers/display_win.cpp
@@ -250,7 +250,6 @@ DisplayImpl::DisplayImpl():
         beginPixelCalled(false),
         backend(WinBackend::instance())
 {
-    setFont(droid11);
     setTextColor(make_pair(Color(0xffff), Color(0x0000)));
 }
 
diff --git a/level2/application.cpp b/level2/application.cpp
index 8ab1ee6..67e1700 100644
--- a/level2/application.cpp
+++ b/level2/application.cpp
@@ -67,17 +67,7 @@ Drawable::~Drawable()
 // class Window
 //
 
-Window::Window() : prefs(white,black,
-#ifdef MXGUI_FONT_DROID11
-    droid11),
-#elif defined(MXGUI_FONT_TAHOMA)
-    tahoma),
-#elif defined(MXGUI_FONT_MISCFIXED)
-    miscFixed),
-#else
-#error "Need a font"
-#endif
-    redrawNeeded(false)
+Window::Window() : prefs(white,black,defaultFont), redrawNeeded(false)
 {
     pthread_mutex_init(&mutex,NULL);
     pthread_cond_init(&cond,NULL);
diff --git a/level2/simple_plot.cpp b/level2/simple_plot.cpp
index 502bd1c..989934b 100644
--- a/level2/simple_plot.cpp
+++ b/level2/simple_plot.cpp
@@ -13,8 +13,7 @@ namespace mxgui {
 // class SimplePlot
 //
 
-SimplePlot::SimplePlot(Point upperLeft, Point lowerRight) : upperLeft(upperLeft), lowerRight(lowerRight),
-    font(droid11)
+SimplePlot::SimplePlot(Point upperLeft, Point lowerRight) : upperLeft(upperLeft), lowerRight(lowerRight), font(defaultFont)
 {
     foreground=white;
     background=black;
diff --git a/level2/simple_plot.h b/level2/simple_plot.h
index 01318e3..559e05a 100644
--- a/level2/simple_plot.h
+++ b/level2/simple_plot.h
@@ -30,6 +30,8 @@ public:
     
     void draw(DrawingContext& dc, const std::vector<Dataset>& dataset,
               bool fullRedraw=false);
+
+    void setFont(const Font& font) { this->font=font; }
     
     Point upperLeft;
     Point lowerRight;
-- 
GitLab