Jelajahi Sumber

Added option to define true type fonts size for each xbrz mode

Ivan Savenko 1 tahun lalu
induk
melakukan
8367729235
3 mengubah file dengan 18 tambahan dan 7 penghapusan
  1. 11 5
      client/renderSDL/CTrueTypeFont.cpp
  2. 2 1
      client/renderSDL/CTrueTypeFont.h
  3. 5 1
      config/fonts.json

+ 11 - 5
client/renderSDL/CTrueTypeFont.cpp

@@ -27,19 +27,25 @@ std::pair<std::unique_ptr<ui8[]>, ui64> CTrueTypeFont::loadData(const JsonNode &
 	return CResourceHandler::get()->load(ResourcePath(filename, EResType::TTF_FONT))->readAll();
 }
 
-TTF_Font * CTrueTypeFont::loadFont(const JsonNode &config)
+int CTrueTypeFont::getPointSize(const JsonNode & config) const
 {
-	int pointSizeBase = static_cast<int>(config["size"].Float());
 	int scalingFactor = getScalingFactor();
-	int pointSize = pointSizeBase * scalingFactor;
 
+	if (config.isNumber())
+		return config.Integer() * scalingFactor;
+	else
+		return config[scalingFactor-1].Integer();
+}
+
+TTF_Font * CTrueTypeFont::loadFont(const JsonNode &config)
+{
 	if(!TTF_WasInit() && TTF_Init()==-1)
 		throw std::runtime_error(std::string("Failed to initialize true type support: ") + TTF_GetError() + "\n");
 
-	return TTF_OpenFontRW(SDL_RWFromConstMem(data.first.get(), (int)data.second), 1, pointSize);
+	return TTF_OpenFontRW(SDL_RWFromConstMem(data.first.get(), data.second), 1, getPointSize(config["size"]));
 }
 
-int CTrueTypeFont::getFontStyle(const JsonNode &config)
+int CTrueTypeFont::getFontStyle(const JsonNode &config) const
 {
 	const JsonVector & names = config["style"].Vector();
 	int ret = 0;

+ 2 - 1
client/renderSDL/CTrueTypeFont.h

@@ -30,7 +30,8 @@ class CTrueTypeFont final : public IFont
 
 	std::pair<std::unique_ptr<ui8[]>, ui64> loadData(const JsonNode & config);
 	TTF_Font * loadFont(const JsonNode & config);
-	int getFontStyle(const JsonNode & config);
+	int getPointSize(const JsonNode & config) const;
+	int getFontStyle(const JsonNode & config) const;
 
 	void renderText(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const override;
 public:

+ 5 - 1
config/fonts.json

@@ -19,7 +19,11 @@
 	// Should be in format:
 	// <replaced bitmap font name, case-sensetive> : <true type font description>
 	// "file" - file to load font from, must be in data/ directory
-	// "size" - point size of font
+	// "size" - point size of font. Can be defined in two forms:
+	// a) single number, e.g. 10. In this case, game will automatically multiply font size by upscaling factor when xBRZ is in use, 
+	//    so xbrz 2x will use 20px, xbrz 3x will use 30px, etc
+	// b) list of scaling factors for each scaling mode, e.g. [ 10, 16, 22, 26]. In this case game will select point size according to xBRZ scaling factor
+	//    so unscaled mode will use 10px, xbrz2 will use 16px, and xbrz3 will use 22
 	// "style" - italic and\or bold, indicates font style
 	// "blend" - if set to true, font will be antialiased
 	"trueType":