浏览代码

TTF fonts will now be rendered correctly in upscaling mode

Note that TTF rendering bypasses xBRZ - TTF fonts are rendered at larger
point size
Ivan Savenko 1 年之前
父节点
当前提交
020a825ecd

+ 16 - 7
client/render/IFont.cpp

@@ -11,9 +11,18 @@
 #include "StdInc.h"
 #include "IFont.h"
 
+#include "../gui/CGuiHandler.h"
+
+#include "../render/IScreenHandler.h"
+
 #include "../../lib/Point.h"
 #include "../../lib/texts/TextOperations.h"
 
+int IFont::getScalingFactor() const
+{
+	return GH.screenHandler().getScalingFactor();
+}
+
 size_t IFont::getStringWidth(const std::string & data) const
 {
 	size_t width = 0;
@@ -32,13 +41,13 @@ void IFont::renderTextLeft(SDL_Surface * surface, const std::string & data, cons
 
 void IFont::renderTextRight(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
 {
-	Point size((int)getStringWidth(data), (int)getLineHeight());
+	Point size = Point((int)getStringWidth(data), (int)getLineHeight()) * getScalingFactor();
 	renderText(surface, data, color, pos - size);
 }
 
 void IFont::renderTextCenter(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
 {
-	Point size((int)getStringWidth(data), (int)getLineHeight());
+	Point size = Point((int)getStringWidth(data), (int)getLineHeight()) * getScalingFactor();
 	renderText(surface, data, color, pos - size / 2);
 }
 
@@ -49,31 +58,31 @@ void IFont::renderTextLinesLeft(SDL_Surface * surface, const std::vector<std::st
 	for(const std::string & line : data)
 	{
 		renderTextLeft(surface, line, color, currPos);
-		currPos.y += (int)getLineHeight();
+		currPos.y += getLineHeight() * getScalingFactor();
 	}
 }
 
 void IFont::renderTextLinesRight(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
 {
 	Point currPos = pos;
-	currPos.y -= (int)data.size() * (int)getLineHeight();
+	currPos.y -= (int)data.size() * (int)getLineHeight() * getScalingFactor();
 
 	for(const std::string & line : data)
 	{
 		renderTextRight(surface, line, color, currPos);
-		currPos.y += (int)getLineHeight();
+		currPos.y += getLineHeight() * getScalingFactor();
 	}
 }
 
 void IFont::renderTextLinesCenter(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
 {
 	Point currPos = pos;
-	currPos.y -= (int)data.size() * (int)getLineHeight() / 2;
+	currPos.y -= (int)data.size() * (int)getLineHeight() / 2 * getScalingFactor();
 
 	for(const std::string & line : data)
 	{
 		renderTextCenter(surface, line, color, currPos);
-		currPos.y += (int)getLineHeight();
+		currPos.y += getLineHeight() * getScalingFactor();
 	}
 }
 

+ 2 - 0
client/render/IFont.h

@@ -22,6 +22,8 @@ protected:
 	/// Internal function to render font, see renderTextLeft
 	virtual void renderText(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const = 0;
 
+	int getScalingFactor() const;
+
 public:
 	virtual ~IFont()
 	{}

+ 2 - 0
client/render/IScreenHandler.h

@@ -44,6 +44,8 @@ public:
 	/// Dimensions of logical output. Can be different if scaling is used
 	virtual Point getLogicalResolution() const = 0;
 
+	virtual int getScalingFactor() const = 0;
+
 	/// Window has focus
 	virtual bool hasFocus() = 0;
 };

+ 7 - 8
client/renderSDL/CTrueTypeFont.cpp

@@ -29,7 +29,9 @@ std::pair<std::unique_ptr<ui8[]>, ui64> CTrueTypeFont::loadData(const JsonNode &
 
 TTF_Font * CTrueTypeFont::loadFont(const JsonNode &config)
 {
-	int pointSize = static_cast<int>(config["size"].Float());
+	int pointSizeBase = static_cast<int>(config["size"].Float());
+	int scalingFactor = getScalingFactor();
+	int pointSize = pointSizeBase * scalingFactor;
 
 	if(!TTF_WasInit() && TTF_Init()==-1)
 		throw std::runtime_error(std::string("Failed to initialize true type support: ") + TTF_GetError() + "\n");
@@ -74,7 +76,7 @@ size_t CTrueTypeFont::getLineHeight() const
 	if (fallbackFont)
 		return fallbackFont->getLineHeight();
 
-	return TTF_FontHeight(font.get());
+	return TTF_FontHeight(font.get()) / getScalingFactor();
 }
 
 size_t CTrueTypeFont::getGlyphWidth(const char *data) const
@@ -83,19 +85,16 @@ size_t CTrueTypeFont::getGlyphWidth(const char *data) const
 		return fallbackFont->getGlyphWidth(data);
 
 	return getStringWidth(std::string(data, TextOperations::getUnicodeCharacterSize(*data)));
-	int advance;
-	TTF_GlyphMetrics(font.get(), *data, nullptr, nullptr, nullptr, nullptr, &advance);
-	return advance;
 }
 
 size_t CTrueTypeFont::getStringWidth(const std::string & data) const
 {
 	if (fallbackFont && fallbackFont->canRepresentString(data))
-		return fallbackFont->getStringWidth(data);
+		return fallbackFont->getStringWidth(data) / getScalingFactor();
 
 	int width;
 	TTF_SizeUTF8(font.get(), data.c_str(), &width, nullptr);
-	return width;
+	return width / getScalingFactor();
 }
 
 void CTrueTypeFont::renderText(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
@@ -107,7 +106,7 @@ void CTrueTypeFont::renderText(SDL_Surface * surface, const std::string & data,
 	}
 
 	if (dropShadow && color.r != 0 && color.g != 0 && color.b != 0) // not black - add shadow
-		renderText(surface, data, Colors::BLACK, pos + Point(1,1));
+		renderText(surface, data, Colors::BLACK, pos + Point(1,1) * getScalingFactor());
 
 	if (!data.empty())
 	{

+ 2 - 2
client/renderSDL/ScreenHandler.h

@@ -69,8 +69,6 @@ class ScreenHandler final : public IScreenHandler
 
 	/// Performs validation of settings and updates them to valid values if necessary
 	void validateSettings();
-
-	int getScalingFactor() const;
 public:
 
 	/// Creates and initializes screen, window and SDL state
@@ -93,6 +91,8 @@ public:
 
 	Point getLogicalResolution() const final;
 
+	int getScalingFactor() const final;
+
 	std::vector<Point> getSupportedResolutions() const final;
 	std::vector<Point> getSupportedResolutions(int displayIndex) const;
 	std::tuple<int, int> getSupportedScalingRange() const final;