Skip to content

Commit

Permalink
Add option to set a font size multiplier for ttf fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
MjnMixael committed Aug 28, 2024
1 parent 39565d3 commit ceed5be
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 3 deletions.
18 changes: 16 additions & 2 deletions code/graphics/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,12 +814,26 @@ void gr_string(float sx, float sy, const char* s, int resize_mode, size_t in_len
GR_DEBUG_SCOPE("Render TTF string");

auto path = beginDrawing(resize_mode);
path->translate(sx, sy);

auto nvgFont = static_cast<NVGFont*>(currentFont);

float scale_factor = Font_Scale_Factor;
if (!nvgFont->getScaleBehavior()) {
scale_factor = 1.0f;
}

float originalSize = nvgFont->getSize();
float scaledSize = originalSize * scale_factor;

// Calculate the offset to center the text
float offsetX = 0.0f;
// Not sure if we should do this.. kinda depends on if the text is drawn at the top of the screen or the bottom
float offsetY = (scaledSize - originalSize) * 0.5f;

path->translate(sx - offsetX, sy - offsetY);

path->fontFaceId(nvgFont->getHandle());
path->fontSize(nvgFont->getSize());
path->fontSize(scaledSize);
path->textLetterSpacing(nvgFont->getLetterSpacing());
path->textAlign(static_cast<TextAlign>(ALIGN_TOP | ALIGN_LEFT));

Expand Down
10 changes: 10 additions & 0 deletions code/graphics/software/FSFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ namespace font
{
}

void FSFont::setScaleBehavior(bool scale)
{
this->canScale = scale;
}

void FSFont::setBottomOffset(float offset)
{
this->offsetBottom = offset;
Expand All @@ -30,6 +35,11 @@ namespace font
this->filename = newName;
}

bool FSFont::getScaleBehavior() const
{
return this->canScale;
}

float FSFont::getBottomOffset() const
{
return this->offsetBottom;
Expand Down
19 changes: 19 additions & 0 deletions code/graphics/software/FSFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace font
protected:
float offsetTop; //!< The offset at the top of a line of text
float offsetBottom; //!< The offset at the bottom of a line of text
bool canScale = false; //!< If the font is allowed to scale with the user font multiplier

float _height;
float _ascender;
Expand Down Expand Up @@ -142,6 +143,15 @@ namespace font
virtual void getStringSize(const char *text, size_t textLen = std::numeric_limits<size_t>::max(),
int resize_mode = -1, float *width = NULL, float *height = NULL) const = 0;

/**
* @brief Gets the scaling behavior of this font
*
* @date 28.8.2024
*
* @return The scaling behavior
*/
bool getScaleBehavior() const;

/**
* @brief Gets the offset of this font from the top of the drawing line
*
Expand All @@ -160,6 +170,15 @@ namespace font
*/
float getBottomOffset() const;

/**
* @brief Sets the scaling behavior
*
* @date 28.8.2024
*
* @param scale whether or not this font can scale with the font multiplier
*/
void setScaleBehavior(bool scale);


/**
* @brief Sets the top offset for this font
Expand Down
21 changes: 20 additions & 1 deletion code/graphics/software/NVGFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@
#include "graphics/paths/PathRenderer.h"

#include "mod_table/mod_table.h"
#include "options/Option.h"

#include "localization/localize.h"

#include <limits>

float Font_Scale_Factor = 1.0;

static auto FontScaleFactor __UNUSED = options::OptionBuilder<float>("Game.FontScaleFactor",
std::pair<const char*, int>{"Font Scale Factor", 1859}, // Update localize.cpp!! If this is still here, do not pass review
std::pair<const char*, int>{"Sets a multipler to scale fonts by. Only works on fonts the mod has explicitely allowed", 1860})
.category(std::make_pair("Game", 1824))
.range(0.2f, 4.0f) // Upper limit is somewhat arbitrary
.level(options::ExpertLevel::Advanced)
.default_val(1.0)
.bind_to(&Font_Scale_Factor)
.importance(55)
.finish();

namespace
{
const char* const TOKEN_SEPARATORS = "\n\t\r";
Expand Down Expand Up @@ -130,8 +144,13 @@ namespace font
path->saveState();
path->resetState();

float scale_factor = Font_Scale_Factor;
if (!canScale) {
scale_factor = 1.0f;
}

path->fontFaceId(m_handle);
path->fontSize(m_size);
path->fontSize(m_size * scale_factor);
path->textLetterSpacing(m_letterSpacing);
path->textAlign(static_cast<TextAlign>(ALIGN_TOP | ALIGN_LEFT));

Expand Down
2 changes: 2 additions & 0 deletions code/graphics/software/NVGFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "globalincs/pstypes.h"
#include "graphics/software/FSFont.h"

extern float Font_Scale_Factor;

namespace font
{
struct font;
Expand Down
8 changes: 8 additions & 0 deletions code/graphics/software/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ namespace
return;
}

if (optional_string("+Can Scale:")) {
bool temp;

stuff_boolean(&temp);

nvgFont->setScaleBehavior(temp);
}

if (optional_string("+Top offset:"))
{
float temp;
Expand Down

0 comments on commit ceed5be

Please sign in to comment.