From 9962e606a6169d349137d45892fcbc16d531c4ef Mon Sep 17 00:00:00 2001 From: vsonnier Date: Wed, 22 Jun 2016 19:04:59 +0200 Subject: [PATCH] FIX out-of-bounds dB display --- src/panel/SpectrumPanel.cpp | 5 ++- src/ui/GLPanel.cpp | 38 +++++++++++++---- src/ui/GLPanel.h | 2 + src/util/GLFont.cpp | 84 ++++++++++++++++++++++++------------- src/util/GLFont.h | 5 ++- 5 files changed, 94 insertions(+), 40 deletions(-) diff --git a/src/panel/SpectrumPanel.cpp b/src/panel/SpectrumPanel.cpp index fa42cfd..4f21a9f 100644 --- a/src/panel/SpectrumPanel.cpp +++ b/src/panel/SpectrumPanel.cpp @@ -265,8 +265,8 @@ void SpectrumPanel::drawPanelContents() { glLineWidth(1.0); if (showDb) { - float dbPanelWidth = (1.0/viewWidth)*75.0; - float dbPanelHeight = (1.0/viewHeight)*14.0; + float dbPanelWidth = (1.0 / viewWidth)*75.0 * GLFont::getScaleFactor(); + float dbPanelHeight = (1.0/viewHeight)*14.0 * GLFont::getScaleFactor(); std::stringstream ssLabel(""); @@ -276,6 +276,7 @@ void SpectrumPanel::drawPanelContents() { dbPanelCeil.setText(ssLabel.str(), GLFont::GLFONT_ALIGN_RIGHT); dbPanelCeil.setSize(dbPanelWidth, dbPanelHeight); dbPanelCeil.setPosition(-1.0 + dbPanelWidth, 1.0 - dbPanelHeight); + ssLabel.str(""); if (getCeilValue() != getFloorValue() && fftSize) { diff --git a/src/ui/GLPanel.cpp b/src/ui/GLPanel.cpp index c37e448..3a5a105 100644 --- a/src/ui/GLPanel.cpp +++ b/src/ui/GLPanel.cpp @@ -391,24 +391,48 @@ void GLTextPanel::drawPanelContents() { glColor4f(1, 1, 1, 1.0); GLFont::GLFontSize sz; - - if (pdim.y <= 16) { + //beware here: the really applied font may have been scaled up compared to the "user" requested one, so that + //we must negate it here to compute "user" font selection. + float pdimy = pdim.y / GLFont::getScaleFactor(); + + if (pdimy <= 16) { sz = GLFont::GLFONT_SIZE12; - } else if (pdim.y <= 18) { + } else if (pdimy <= 18) { sz = GLFont::GLFONT_SIZE16; - } else if(pdim.y <= 24) { + } else if(pdimy <= 24) { sz = GLFont::GLFONT_SIZE18; - } else if(pdim.y <= 32) { + }else if (pdimy <= 27) { sz = GLFont::GLFONT_SIZE24; + } + else if (pdimy <= 32) { + sz = GLFont::GLFONT_SIZE27; - } else if(pdim.y <= 48) { + } + else if (pdimy <= 36) { sz = GLFont::GLFONT_SIZE32; - } else { + } + else if (pdimy <= 48) { + sz = GLFont::GLFONT_SIZE36; + + } + else if (pdimy <= 64) { + sz = GLFont::GLFONT_SIZE48; + + } + else if (pdimy <= 72) { + sz = GLFont::GLFONT_SIZE64; + + } + else if (pdimy <= 96) { + sz = GLFont::GLFONT_SIZE72; + + } + else { sz = GLFont::GLFONT_SIZE48; } diff --git a/src/ui/GLPanel.h b/src/ui/GLPanel.h index ed329fe..b1e3a32 100644 --- a/src/ui/GLPanel.h +++ b/src/ui/GLPanel.h @@ -62,6 +62,8 @@ public: GLPanel(); void setPosition(float x, float y); + + void setSize(float w, float h); float getWidth(); float getHeight(); diff --git a/src/util/GLFont.cpp b/src/util/GLFont.cpp index 220f9ca..8ed1c9f 100644 --- a/src/util/GLFont.cpp +++ b/src/util/GLFont.cpp @@ -17,7 +17,7 @@ static std::wstring getExePath(void) #define RES_FOLDER "" #endif -#define GC_PERIOD 50 +#define GC_DRAW_COUNT_PERIOD 50 #define GC_DRAW_COUNT_LIMIT 10 GLFontStringCache::GLFontStringCache() { @@ -57,7 +57,7 @@ GLFont::GLFontSize GLFont::userFontZoomMapping[GLFont::GLFontSize::GLFONT_SIZE_M GLFont::GLFontSize::GLFONT_SIZE96 }; -GLFont::GLFontScale GLFont::currentScaleFactor = GLFont::GLFontScale::GLFONT_SCALE_NORMAL; +std::atomic GLFont::currentScale{ GLFont::GLFontScale::GLFONT_SCALE_NORMAL }; std::mutex GLFont::g_userFontZoomMappingMutex; @@ -520,7 +520,7 @@ void GLFont::drawString(const std::wstring& str, float xpos, float ypos, Align h std::lock_guard lock(cache_busy); - if (gcCounter > GC_PERIOD) { + if (gcCounter > GC_DRAW_COUNT_PERIOD) { doCacheGC(); gcCounter = 0; @@ -837,34 +837,33 @@ GLFont &GLFont::getFont(GLFontSize esize) { void GLFont::setScale(GLFontScale scale) { - //By default, populate with normal font (1:1 matching) then overrides - //0) Normal: + //safety vs. inputs + if (scale < GLFONT_SCALE_NORMAL || scale > GLFONT_SCALE_LARGE) { + + scale = GLFontScale::GLFONT_SCALE_NORMAL; + } + std::lock_guard lock(g_userFontZoomMappingMutex); - - userFontZoomMapping[GLFont::GLFONT_SIZE12] = GLFont::GLFONT_SIZE12; - userFontZoomMapping[GLFont::GLFONT_SIZE16] = GLFont::GLFONT_SIZE16; - userFontZoomMapping[GLFont::GLFONT_SIZE18] = GLFont::GLFONT_SIZE18; - userFontZoomMapping[GLFont::GLFONT_SIZE24] = GLFont::GLFONT_SIZE24; - userFontZoomMapping[GLFont::GLFONT_SIZE27] = GLFont::GLFONT_SIZE27; - userFontZoomMapping[GLFont::GLFONT_SIZE32] = GLFont::GLFONT_SIZE32; - userFontZoomMapping[GLFont::GLFONT_SIZE36] = GLFont::GLFONT_SIZE36; - userFontZoomMapping[GLFont::GLFONT_SIZE48] = GLFont::GLFONT_SIZE48; - userFontZoomMapping[GLFont::GLFONT_SIZE64] = GLFont::GLFONT_SIZE64; - userFontZoomMapping[GLFont::GLFONT_SIZE72] = GLFont::GLFONT_SIZE72; - userFontZoomMapping[GLFont::GLFONT_SIZE96] = GLFont::GLFONT_SIZE96; + //Normal font (1:1 matching) + if (scale == GLFontScale::GLFONT_SCALE_NORMAL) { - currentScaleFactor = scale; + userFontZoomMapping[GLFont::GLFONT_SIZE12] = GLFont::GLFONT_SIZE12; + userFontZoomMapping[GLFont::GLFONT_SIZE16] = GLFont::GLFONT_SIZE16; + userFontZoomMapping[GLFont::GLFONT_SIZE18] = GLFont::GLFONT_SIZE18; + userFontZoomMapping[GLFont::GLFONT_SIZE24] = GLFont::GLFONT_SIZE24; + userFontZoomMapping[GLFont::GLFONT_SIZE27] = GLFont::GLFONT_SIZE27; + userFontZoomMapping[GLFont::GLFONT_SIZE32] = GLFont::GLFONT_SIZE32; + userFontZoomMapping[GLFont::GLFONT_SIZE36] = GLFont::GLFONT_SIZE36; + userFontZoomMapping[GLFont::GLFONT_SIZE48] = GLFont::GLFONT_SIZE48; + userFontZoomMapping[GLFont::GLFONT_SIZE64] = GLFont::GLFONT_SIZE64; + userFontZoomMapping[GLFont::GLFONT_SIZE72] = GLFont::GLFONT_SIZE72; + userFontZoomMapping[GLFont::GLFONT_SIZE96] = GLFont::GLFONT_SIZE96; - //safety vs. inputs - if (currentScaleFactor < GLFONT_SCALE_NORMAL || currentScaleFactor > GLFONT_SCALE_LARGE) { - - currentScaleFactor = GLFontScale::GLFONT_SCALE_NORMAL; + //override depending of zoom level: + //Medium : more or less 1.5 x } - - //override depending of zoom level: - //Medium : more or less 1.5 x - if (currentScaleFactor == GLFontScale::GLFONT_SCALE_MEDIUM) { + else if (scale == GLFontScale::GLFONT_SCALE_MEDIUM) { userFontZoomMapping[GLFont::GLFONT_SIZE12] = GLFont::GLFONT_SIZE18; userFontZoomMapping[GLFont::GLFONT_SIZE16] = GLFont::GLFONT_SIZE24; @@ -875,9 +874,13 @@ void GLFont::setScale(GLFontScale scale) { userFontZoomMapping[GLFont::GLFONT_SIZE36] = GLFont::GLFONT_SIZE48; userFontZoomMapping[GLFont::GLFONT_SIZE48] = GLFont::GLFONT_SIZE72; userFontZoomMapping[GLFont::GLFONT_SIZE64] = GLFont::GLFONT_SIZE96; + + //too big: not-scaled properly and saturating: + userFontZoomMapping[GLFont::GLFONT_SIZE72] = GLFont::GLFONT_SIZE96; + userFontZoomMapping[GLFont::GLFONT_SIZE96] = GLFont::GLFONT_SIZE96; } //Large : 2x normal, more or less - else if (currentScaleFactor == GLFontScale::GLFONT_SCALE_LARGE) { + else if (scale == GLFontScale::GLFONT_SCALE_LARGE) { userFontZoomMapping[GLFont::GLFONT_SIZE12] = GLFont::GLFONT_SIZE24; userFontZoomMapping[GLFont::GLFONT_SIZE16] = GLFont::GLFONT_SIZE32; @@ -887,7 +890,14 @@ void GLFont::setScale(GLFontScale scale) { userFontZoomMapping[GLFont::GLFONT_SIZE32] = GLFont::GLFONT_SIZE64; userFontZoomMapping[GLFont::GLFONT_SIZE36] = GLFont::GLFONT_SIZE72; userFontZoomMapping[GLFont::GLFONT_SIZE48] = GLFont::GLFONT_SIZE96; + + //too big: not-scaled properly or saturating: + userFontZoomMapping[GLFont::GLFONT_SIZE64] = GLFont::GLFONT_SIZE96; + userFontZoomMapping[GLFont::GLFONT_SIZE72] = GLFont::GLFONT_SIZE96; + userFontZoomMapping[GLFont::GLFONT_SIZE96] = GLFont::GLFONT_SIZE96; } + + currentScale.store(scale); //Flush all the GC stuff clearAllCaches(); @@ -895,8 +905,22 @@ void GLFont::setScale(GLFontScale scale) { GLFont::GLFontScale GLFont::getScale() { - std::lock_guard lock(g_userFontZoomMappingMutex); - - return currentScaleFactor; + return currentScale.load(); +} + +double GLFont::getScaleFactor() { + + GLFontScale scale = currentScale.load(); + + if (scale == GLFONT_SCALE_MEDIUM) { + + return 1.5; + } + else if (scale == GLFONT_SCALE_LARGE) { + + return 2.0; + } + + return 1.0; } diff --git a/src/util/GLFont.h b/src/util/GLFont.h index 28f2363..7d5573f 100644 --- a/src/util/GLFont.h +++ b/src/util/GLFont.h @@ -107,6 +107,9 @@ public: static GLFontScale getScale(); + //Mean current scale factor: 1.0 in normal, 1.5 medium, 2.0 for large + static double getScaleFactor(); + //Public drawing font, 16 bit char version. void drawString(const std::wstring& str, float xpos, float ypos, Align hAlign = GLFONT_ALIGN_LEFT, Align vAlign = GLFONT_ALIGN_TOP, int vpx=0, int vpy=0, bool cacheable = false); @@ -128,7 +131,7 @@ private: //which map a user-requested font to a final one depending of the zoom level. static GLFontSize userFontZoomMapping[GLFontSize::GLFONT_SIZE_MAX]; - static GLFontScale currentScaleFactor; + static std::atomic currentScale; //load a given font file, (lazy loading) void loadFontOnce();