AGS: Force readjust TTF fonts if the OPT_ANTIALIASFONTS changes

From upstream 115119eeb009fc7f2c5820b47bb866acad6498ec
This commit is contained in:
Paul Gilbert 2022-03-26 22:11:44 -07:00
parent f34cf7ce67
commit f4935dbf39
10 changed files with 44 additions and 10 deletions

View File

@ -52,6 +52,7 @@
#include "ags/engine/ac/system.h"
#include "ags/engine/debugging/debugger.h"
#include "ags/engine/debugging/debug_log.h"
#include "ags/shared/font/fonts.h"
#include "ags/engine/gui/gui_dialog.h"
#include "ags/engine/main/engine.h"
#include "ags/engine/main/game_run.h"
@ -425,6 +426,8 @@ int SetGameOption(int opt, int setting) {
} else if (opt == OPT_PORTRAITSIDE) {
if (setting == 0) // set back to Left
_GP(play).swap_portrait_side = 0;
} else if (opt == OPT_ANTIALIASFONTS) {
adjust_fonts_for_render_mode(setting != 0);
}
return oldval;

View File

@ -43,6 +43,7 @@
#include "ags/engine/debugging/debugger.h"
#include "ags/shared/debugging/out.h"
#include "ags/engine/device/mouse_w32.h"
#include "ags/shared/font/fonts.h"
#include "ags/shared/gfx/bitmap.h"
#include "ags/engine/gfx/ddb.h"
#include "ags/engine/gfx/graphics_driver.h"
@ -598,6 +599,8 @@ HSaveError DoAfterRestore(const PreservedParams &pp, const RestoredData &r_data)
}
update_directional_sound_vol();
adjust_fonts_for_render_mode(_GP(game).options[OPT_ANTIALIASFONTS]);
recreate_overlay_ddbs();
GUI::MarkAllGUIForUpdate();

View File

@ -413,7 +413,7 @@ int alfont_set_font_size_ex(ALFONT_FONT *f, int h, int flags) {
int error, test_h, direction;
int real_height;
/* check the font doesn't already use that w and h */
if (h == f->face_h)
if ((h == f->face_h) && (flags & ALFONT_FLG_FORCE_RESIZE) == 0)
return ALFONT_OK;
else if (h <= 0)
return ALFONT_ERROR;

View File

@ -42,8 +42,10 @@ namespace AGS3 {
/* font adjustment flags (AGS compatibility hacks) */
// Force reupdate the font even if the face size matches the request
#define ALFONT_FLG_FORCE_RESIZE 0x01
// Make ascender equal to formal font height
#define ALFONT_FLG_ASCENDER_EQ_HEIGHT 0x01
#define ALFONT_FLG_ASCENDER_EQ_HEIGHT 0x02
/* structs */
typedef struct ALFONT_FONT ALFONT_FONT;

View File

@ -67,6 +67,8 @@ public:
FontMetrics *metrics) = 0;
// Gets font's name; must return an empty string if no name is available
virtual const char *GetName(int fontNumber) = 0;
// Perform any necessary adjustments when the AA mode is toggled
virtual void AdjustFontForAntiAlias(int fontNumber, bool aa_mode) = 0;
protected:
IAGSFontRenderer2() {}
~IAGSFontRenderer2() {}

View File

@ -455,6 +455,13 @@ void alloc_font_outline_buffers(size_t font_number,
*outline_stencil = &f.OutlineStencilSub;
}
void adjust_fonts_for_render_mode(bool aa_mode) {
for (size_t i = 0; i < _GP(fonts).size(); ++i) {
if (_GP(fonts)[i].Renderer2 != nullptr)
_GP(fonts)[i].Renderer2->AdjustFontForAntiAlias(i, aa_mode);
}
}
void wfreefont(size_t fontNumber) {
if (fontNumber >= _GP(fonts).size())
return;

View File

@ -135,6 +135,8 @@ void wgtprintf(Shared::Bitmap *ds, int xxx, int yyy, size_t fontNumber, color_t
void alloc_font_outline_buffers(size_t font_number,
Shared::Bitmap **text_stencil, Shared::Bitmap **outline_stencil,
int text_width, int text_height, int color_depth);
// Perform necessary adjustments on all fonts in case the text render mode changed (anti-aliasing etc)
void adjust_fonts_for_render_mode(bool aa_mode);
// Free particular font's data
void wfreefont(size_t fontNumber);
// Free all fonts data

View File

@ -78,6 +78,16 @@ bool TTFFontRenderer::IsBitmapFont() {
return false;
}
static int GetAlfontFlags() {
int flags = ALFONT_FLG_FORCE_RESIZE;
// Compatibility: font ascender is always adjusted to the formal font's height;
// EXCEPTION: not if it's a game made before AGS 3.4.1 with TTF anti-aliasing
// (the reason is uncertain, but this is to emulate old engine's behavior).
if (!(ShouldAntiAliasText() && (_G(loaded_game_file_version) < kGameVersion_341)))
flags |= ALFONT_FLG_ASCENDER_EQ_HEIGHT;
return flags;
}
bool TTFFontRenderer::LoadFromDiskEx(int fontNumber, int fontSize,
const FontRenderParams *params, FontMetrics *metrics) {
String file_name = String::FromFormat("agsfnt%d.ttf", fontNumber);
@ -102,13 +112,8 @@ bool TTFFontRenderer::LoadFromDiskEx(int fontNumber, int fontSize,
fontSize = 8; // compatibility fix
if (params && params->SizeMultiplier > 1)
fontSize *= params->SizeMultiplier;
// Compatibility: font ascender is always adjusted to the formal font's height;
// EXCEPTION: not if it's a game made before AGS 3.4.1 with TTF anti-aliasing
// (the reason is uncertain, but this is to emulate old engine's behavior).
int alfont_flags = 0;
if (!(ShouldAntiAliasText() && (_G(loaded_game_file_version) < kGameVersion_341)))
alfont_flags |= ALFONT_FLG_ASCENDER_EQ_HEIGHT;
alfont_set_font_size_ex(alfptr, fontSize, alfont_flags);
alfont_set_font_size_ex(alfptr, fontSize, GetAlfontFlags());
_fontData[fontNumber].AlFont = alfptr;
_fontData[fontNumber].Params = params ? *params : FontRenderParams();
@ -116,7 +121,7 @@ bool TTFFontRenderer::LoadFromDiskEx(int fontNumber, int fontSize,
if (metrics) {
metrics->Height = alfont_get_font_height(alfptr);
metrics->RealHeight = alfont_get_font_real_height(alfptr);
}
}
return true;
}
@ -124,6 +129,14 @@ const char *TTFFontRenderer::GetName(int fontNumber) {
return alfont_get_name(_fontData[fontNumber].AlFont);
}
void TTFFontRenderer::AdjustFontForAntiAlias(int fontNumber, bool aa_mode) {
if (_G(loaded_game_file_version) < kGameVersion_341) {
ALFONT_FONT *alfptr = _fontData[fontNumber].AlFont;
int old_height = alfont_get_font_height(alfptr);
alfont_set_font_size_ex(alfptr, old_height, GetAlfontFlags());
}
}
void TTFFontRenderer::FreeMemory(int fontNumber) {
alfont_destroy_font(_fontData[fontNumber].AlFont);
_fontData.erase(fontNumber);

View File

@ -50,6 +50,7 @@ public:
bool LoadFromDiskEx(int fontNumber, int fontSize, const FontRenderParams *params,
FontMetrics *metrics) override;
const char *GetName(int fontNumber) override;
void AdjustFontForAntiAlias(int fontNumber, bool aa_mode) override;
private:
struct FontData {

View File

@ -48,6 +48,7 @@ public:
bool LoadFromDiskEx(int fontNumber, int fontSize,
const FontRenderParams *params, FontMetrics *metrics) override;
const char *GetName(int fontNumber) override { return ""; }
void AdjustFontForAntiAlias(int fontNumber, bool aa_mode) override { /* do nothing */ }
private:
struct FontData {