mirror of
https://github.com/cjcliffe/CubicSDR.git
synced 2026-06-02 22:14:47 -04:00
DONE. Now GLFont.getFont() retturns a proxy that automatically selects the best font for the required size
This commit is contained in:
+55
-100
@@ -26,6 +26,8 @@ GLFontStringCache::GLFontStringCache() {
|
||||
|
||||
//Static initialization of all available fonts,
|
||||
//using aggregate syntax (Cx11+)
|
||||
|
||||
//Fonts must be listed in increasing size for Drawer to work !
|
||||
GLFont GLFont::fonts[GLFont::GLFontSize::GLFONT_SIZE_MAX] = {
|
||||
|
||||
{ GLFont::GLFontSize::GLFONT_SIZE12, L"fonts/vera_sans_mono12.fnt" },
|
||||
@@ -42,25 +44,9 @@ GLFont GLFont::fonts[GLFont::GLFontSize::GLFONT_SIZE_MAX] = {
|
||||
|
||||
};
|
||||
|
||||
//default mapping: one-to-one (normal scale factor)
|
||||
GLFont::GLFontSize GLFont::userFontZoomMapping[GLFont::GLFontSize::GLFONT_SIZE_MAX] = {
|
||||
GLFont::GLFontSize::GLFONT_SIZE12,
|
||||
GLFont::GLFontSize::GLFONT_SIZE16,
|
||||
GLFont::GLFontSize::GLFONT_SIZE18,
|
||||
GLFont::GLFontSize::GLFONT_SIZE24,
|
||||
GLFont::GLFontSize::GLFONT_SIZE27,
|
||||
GLFont::GLFontSize::GLFONT_SIZE32,
|
||||
GLFont::GLFontSize::GLFONT_SIZE36,
|
||||
GLFont::GLFontSize::GLFONT_SIZE48,
|
||||
GLFont::GLFontSize::GLFONT_SIZE64,
|
||||
GLFont::GLFontSize::GLFONT_SIZE72,
|
||||
GLFont::GLFontSize::GLFONT_SIZE96
|
||||
};
|
||||
|
||||
std::atomic<GLFont::GLFontScale> GLFont::currentScale{ GLFont::GLFontScale::GLFONT_SCALE_NORMAL };
|
||||
|
||||
std::mutex GLFont::g_userFontZoomMappingMutex;
|
||||
|
||||
|
||||
GLFontChar::GLFontChar() :
|
||||
id(0), x(0), y(0), width(0), height(0), xoffset(0), yoffset(0), xadvance(0), aspect(1), index(0) {
|
||||
@@ -460,13 +446,13 @@ void GLFont::loadFontOnce() {
|
||||
}
|
||||
|
||||
std::cout << "Loaded font '" << fontName << "' from '" << fontFileSource << "', parsed " << characters.size() << " characters." << std::endl;
|
||||
|
||||
loaded = true;
|
||||
} else {
|
||||
std::cout << "Error loading font file " << fontFileSource << std::endl;
|
||||
}
|
||||
|
||||
input.close();
|
||||
|
||||
loaded = true;
|
||||
}
|
||||
|
||||
@@ -501,10 +487,7 @@ float GLFont::getStringWidth(const std::wstring& str, float size, float viewAspe
|
||||
}
|
||||
|
||||
// Draw string, immediate
|
||||
void GLFont::drawString(const std::wstring& str, float xpos, float ypos, Align hAlign, Align vAlign, int vpx, int vpy, bool cacheable) {
|
||||
|
||||
//load pxHeight from the font itself, but because it is an "internal font", the scaling has already been applied.
|
||||
int pxHeight = pixHeight;
|
||||
void GLFont::drawString(const std::wstring& str, int pxHeight, float xpos, float ypos, Align hAlign, Align vAlign, int vpx, int vpy, bool cacheable) {
|
||||
|
||||
pxHeight *= 2;
|
||||
|
||||
@@ -630,7 +613,7 @@ void GLFont::drawString(const std::wstring& str, float xpos, float ypos, Align h
|
||||
}
|
||||
|
||||
// Draw string, immediate, 8 bit version
|
||||
void GLFont::drawString(const std::string& str, float xpos, float ypos, Align hAlign, Align vAlign, int vpx, int vpy, bool cacheable) {
|
||||
void GLFont::drawString(const std::string& str, int pxHeight, float xpos, float ypos, Align hAlign, Align vAlign, int vpx, int vpy, bool cacheable) {
|
||||
|
||||
//Displayed string is wstring, so use wxString to do the heavy lifting of converting str...
|
||||
#ifdef WIN32
|
||||
@@ -642,7 +625,7 @@ void GLFont::drawString(const std::string& str, float xpos, float ypos, Align hA
|
||||
|
||||
wsTmp.assign(str);
|
||||
|
||||
drawString(wsTmp.ToStdWstring(), xpos, ypos, hAlign, vAlign, vpx, vpy, cacheable);
|
||||
drawString(wsTmp.ToStdWstring(), pxHeight, xpos, ypos, hAlign, vAlign, vpx, vpy, cacheable);
|
||||
}
|
||||
|
||||
// Draw cached GLFontCacheString
|
||||
@@ -818,32 +801,11 @@ void GLFont::clearAllCaches() {
|
||||
}
|
||||
|
||||
|
||||
GLFont &GLFont::getFont(GLFontSize esize) {
|
||||
GLFont::Drawer GLFont::getFont(int requestedSize, double scaleFactor) {
|
||||
|
||||
//really load the internal font instead!
|
||||
|
||||
GLFontSize internalFontSize = GLFONT_SIZE12;
|
||||
{ //guard block
|
||||
std::lock_guard<std::mutex> lock(g_userFontZoomMappingMutex);
|
||||
internalFontSize = userFontZoomMapping[esize];
|
||||
}
|
||||
|
||||
//load lazily...
|
||||
fonts[internalFontSize].loadFontOnce();
|
||||
|
||||
return fonts[internalFontSize];
|
||||
return GLFont::Drawer(requestedSize, scaleFactor);
|
||||
}
|
||||
|
||||
GLFont &GLFont::getRawFont(GLFontSize esize) {
|
||||
|
||||
//Do not apply the scaling, really returns the requested font.
|
||||
|
||||
|
||||
//load lazily...
|
||||
fonts[esize].loadFontOnce();
|
||||
|
||||
return fonts[esize];
|
||||
}
|
||||
|
||||
|
||||
void GLFont::setScale(GLFontScale scale) {
|
||||
@@ -853,60 +815,6 @@ void GLFont::setScale(GLFontScale scale) {
|
||||
|
||||
scale = GLFontScale::GLFONT_SCALE_NORMAL;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(g_userFontZoomMappingMutex);
|
||||
|
||||
//Normal font (1:1 matching)
|
||||
if (scale == GLFontScale::GLFONT_SCALE_NORMAL) {
|
||||
|
||||
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;
|
||||
|
||||
//override depending of zoom level:
|
||||
//Medium : more or less 1.5 x
|
||||
}
|
||||
else if (scale == GLFontScale::GLFONT_SCALE_MEDIUM) {
|
||||
|
||||
userFontZoomMapping[GLFont::GLFONT_SIZE12] = GLFont::GLFONT_SIZE18;
|
||||
userFontZoomMapping[GLFont::GLFONT_SIZE16] = GLFont::GLFONT_SIZE24;
|
||||
userFontZoomMapping[GLFont::GLFONT_SIZE18] = GLFont::GLFONT_SIZE27;
|
||||
userFontZoomMapping[GLFont::GLFONT_SIZE24] = GLFont::GLFONT_SIZE36;
|
||||
userFontZoomMapping[GLFont::GLFONT_SIZE27] = GLFont::GLFONT_SIZE36;
|
||||
userFontZoomMapping[GLFont::GLFONT_SIZE32] = GLFont::GLFONT_SIZE48;
|
||||
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 (scale == GLFontScale::GLFONT_SCALE_LARGE) {
|
||||
|
||||
userFontZoomMapping[GLFont::GLFONT_SIZE12] = GLFont::GLFONT_SIZE24;
|
||||
userFontZoomMapping[GLFont::GLFONT_SIZE16] = GLFont::GLFONT_SIZE32;
|
||||
userFontZoomMapping[GLFont::GLFONT_SIZE18] = GLFont::GLFONT_SIZE36;
|
||||
userFontZoomMapping[GLFont::GLFONT_SIZE24] = GLFont::GLFONT_SIZE48;
|
||||
userFontZoomMapping[GLFont::GLFONT_SIZE27] = GLFont::GLFONT_SIZE48;
|
||||
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);
|
||||
|
||||
@@ -935,3 +843,50 @@ double GLFont::getScaleFactor() {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
GLFont::Drawer::Drawer(int basicFontSize, double scaleFactor) {
|
||||
|
||||
//Selection of the final font: scan GLFont::fonts to find the biggest font such as
|
||||
// its pixHeight <= basicFontSize * scaleFactor.
|
||||
//then compute finalScaleFactor the zooming factor of renderingFont to reach a
|
||||
//final font size of basicFontSize* scaleFactor:
|
||||
renderingFontIndex = 0;
|
||||
|
||||
double targetSize = basicFontSize * scaleFactor;
|
||||
|
||||
fonts[0].loadFontOnce();
|
||||
|
||||
for (int i = 0; i < GLFONT_SIZE_MAX - 1; i++) {
|
||||
|
||||
fonts[i + 1].loadFontOnce();
|
||||
|
||||
if (fonts[i + 1].pixHeight <= targetSize) {
|
||||
|
||||
renderingFontIndex = i + 1;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
} //end for
|
||||
|
||||
//
|
||||
double rawSize = fonts[renderingFontIndex].pixHeight;
|
||||
|
||||
//targetSize may not be reached yet, so the effective rendering font: fonts[renderingFontIndex] must be scaled up a bit.
|
||||
renderingFontScaleFactor = targetSize / rawSize;
|
||||
}
|
||||
|
||||
void GLFont::Drawer::drawString(const std::wstring& str, float xpos, float ypos, Align hAlign, Align vAlign, int vpx, int vpy, bool cacheable) {
|
||||
|
||||
GLFont& appliedFont = fonts[renderingFontIndex];
|
||||
|
||||
appliedFont.drawString(str, round(appliedFont.pixHeight * renderingFontScaleFactor), xpos, ypos, hAlign, vAlign, vpx, vpy, cacheable);
|
||||
}
|
||||
|
||||
//Public drawing font, 8 bit char version.
|
||||
void GLFont::Drawer::drawString(const std::string& str, float xpos, float ypos, Align hAlign, Align vAlign, int vpx, int vpy, bool cacheable) {
|
||||
|
||||
GLFont& appliedFont = fonts[renderingFontIndex];
|
||||
|
||||
appliedFont.drawString(str, round(appliedFont.pixHeight * renderingFontScaleFactor), xpos, ypos, hAlign, vAlign, vpx, vpy, cacheable);
|
||||
}
|
||||
|
||||
|
||||
+49
-26
@@ -68,20 +68,26 @@ private:
|
||||
int index;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class GLFont {
|
||||
public:
|
||||
|
||||
|
||||
|
||||
|
||||
enum Align {
|
||||
GLFONT_ALIGN_LEFT, GLFONT_ALIGN_RIGHT, GLFONT_ALIGN_CENTER, GLFONT_ALIGN_TOP, GLFONT_ALIGN_BOTTOM
|
||||
};
|
||||
enum GLFontSize {
|
||||
GLFONT_SIZE12,
|
||||
GLFONT_SIZE16,
|
||||
GLFONT_SIZE12,
|
||||
GLFONT_SIZE16,
|
||||
GLFONT_SIZE18,
|
||||
GLFONT_SIZE24,
|
||||
GLFONT_SIZE24,
|
||||
GLFONT_SIZE27, //new
|
||||
GLFONT_SIZE32,
|
||||
GLFONT_SIZE32,
|
||||
GLFONT_SIZE36, //new
|
||||
GLFONT_SIZE48,
|
||||
GLFONT_SIZE48,
|
||||
GLFONT_SIZE64, //new
|
||||
GLFONT_SIZE72, //new
|
||||
GLFONT_SIZE96, //new
|
||||
@@ -98,13 +104,7 @@ public:
|
||||
GLFont(GLFontSize size, std::wstring fontFileName);
|
||||
~GLFont();
|
||||
|
||||
//The User request a font to display, but internally
|
||||
//it will be translated to another font depending of the scale level
|
||||
static GLFont& getFont(GLFontSize esize);
|
||||
|
||||
//Return the requested raw font, without applying scaling.
|
||||
static GLFont &GLFont::getRawFont(GLFontSize esize);
|
||||
|
||||
|
||||
//Called to change the scale of the rendered fonts
|
||||
static void setScale(GLFontScale scale);
|
||||
|
||||
@@ -113,14 +113,9 @@ public:
|
||||
//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);
|
||||
|
||||
//Public drawing font, 8 bit char version.
|
||||
void drawString(const std::string& str, float xpos, float ypos, Align hAlign = GLFONT_ALIGN_LEFT, Align vAlign = GLFONT_ALIGN_TOP, int vpx = 0, int vpy = 0, bool cacheable = false);
|
||||
|
||||
private:
|
||||
|
||||
|
||||
std::wstring nextParam(std::wistringstream &str);
|
||||
std::wstring getParamKey(const std::wstring& param_str);
|
||||
std::wstring getParamValue(const std::wstring& param_str);
|
||||
@@ -128,17 +123,17 @@ private:
|
||||
//Repository of all loaded fonts
|
||||
static GLFont fonts[GLFontSize::GLFONT_SIZE_MAX];
|
||||
|
||||
//Map of user requested font to internal font, which changes
|
||||
//changes with the requested scale.
|
||||
// this is rebuilt by the user calling setScale(GLFontScale) and changed atomically,
|
||||
//which map a user-requested font to a final one depending of the zoom level.
|
||||
static GLFontSize userFontZoomMapping[GLFontSize::GLFONT_SIZE_MAX];
|
||||
|
||||
static std::atomic<GLFontScale> currentScale;
|
||||
|
||||
//load a given font file, (lazy loading)
|
||||
void loadFontOnce();
|
||||
|
||||
//private drawing font, 16 bit char version, called by Drawer object
|
||||
void drawString(const std::wstring& str, int pxHeight, float xpos, float ypos, Align hAlign = GLFONT_ALIGN_LEFT, Align vAlign = GLFONT_ALIGN_TOP, int vpx = 0, int vpy = 0, bool cacheable = false);
|
||||
|
||||
//private drawing font, 8 bit char version, called by Drawer object
|
||||
void drawString(const std::string& str, int pxHeight, float xpos, float ypos, Align hAlign = GLFONT_ALIGN_LEFT, Align vAlign = GLFONT_ALIGN_TOP, int vpx = 0, int vpy = 0, bool cacheable = false);
|
||||
|
||||
GLFontStringCache *cacheString(const std::wstring& str, int pxHeight, int vpx, int vpy);
|
||||
void drawCacheString(GLFontStringCache *fc, float xpos, float ypos, Align hAlign, Align vAlign);
|
||||
|
||||
@@ -147,7 +142,7 @@ private:
|
||||
|
||||
//force GC of all available fonts
|
||||
static void clearAllCaches();
|
||||
|
||||
|
||||
float getStringWidth(const std::wstring& str, float size, float viewAspect);
|
||||
|
||||
//the string cache is per-front (internal font)
|
||||
@@ -171,5 +166,33 @@ private:
|
||||
int gcCounter;
|
||||
std::mutex cache_busy;
|
||||
|
||||
static std::mutex g_userFontZoomMappingMutex;
|
||||
public:
|
||||
|
||||
//Proxy class computing and caching the selection of the underlying fonts
|
||||
//depending of the user input and requested scale for the fonts.
|
||||
class Drawer {
|
||||
|
||||
private:
|
||||
|
||||
//result of the computation
|
||||
int renderingFontIndex = 0;
|
||||
|
||||
double renderingFontScaleFactor = 1.0;
|
||||
|
||||
public:
|
||||
|
||||
Drawer(int basicFontSize, double scaleFactor);
|
||||
|
||||
//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);
|
||||
|
||||
//Public drawing font, 8 bit char version.
|
||||
void drawString(const std::string& str, float xpos, float ypos, Align hAlign = GLFONT_ALIGN_LEFT, Align vAlign = GLFONT_ALIGN_TOP, int vpx = 0, int vpy = 0, bool cacheable = false);
|
||||
|
||||
}; //end class Drawer
|
||||
|
||||
//The User request a font of size requestedSize to display, with an additional
|
||||
//optional scale factor scaleFactor.
|
||||
static GLFont::Drawer getFont(int requestedSize, double scaleFactor = 1.0);
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user