add FontStylesScheme
This commit is contained in:
+42
-10
@@ -1,5 +1,6 @@
|
||||
#include "Font.hpp"
|
||||
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
#include "Texture.hpp"
|
||||
#include "Batch2D.hpp"
|
||||
@@ -64,7 +65,7 @@ static inline void draw_glyph(
|
||||
-0.2f * style.italic,
|
||||
16,
|
||||
c,
|
||||
batch.getColor()
|
||||
batch.getColor() * style.color
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -87,7 +88,7 @@ static inline void draw_glyph(
|
||||
0.5f,
|
||||
16,
|
||||
c,
|
||||
batch.getColor()
|
||||
batch.getColor() * style.color
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -100,17 +101,33 @@ static inline void draw_text(
|
||||
const glm::vec3& pos,
|
||||
const glm::vec3& right,
|
||||
const glm::vec3& up,
|
||||
float glyphInterval
|
||||
float glyphInterval,
|
||||
const FontStylesScheme* styles
|
||||
) {
|
||||
static FontStylesScheme defStyles {
|
||||
{{std::numeric_limits<size_t>::max()}},
|
||||
};
|
||||
if (styles == nullptr) {
|
||||
styles = &defStyles;
|
||||
}
|
||||
|
||||
uint page = 0;
|
||||
uint next = MAX_CODEPAGES;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
|
||||
FontStyle style {};
|
||||
|
||||
do {
|
||||
for (uint c : text){
|
||||
size_t entryIndex = 0;
|
||||
int styleCharsCounter = -1;
|
||||
const FontStyle* style = &styles->palette.at(entryIndex);
|
||||
|
||||
for (uint c : text) {
|
||||
styleCharsCounter++;
|
||||
if (styleCharsCounter > style->n &&
|
||||
entryIndex + 1 < styles->palette.size()) {
|
||||
style = &styles->palette.at(++entryIndex);
|
||||
styleCharsCounter = -1;
|
||||
}
|
||||
if (!font.isPrintableChar(c)) {
|
||||
x++;
|
||||
continue;
|
||||
@@ -119,7 +136,14 @@ static inline void draw_text(
|
||||
if (charpage == page){
|
||||
batch.texture(font.getPage(charpage));
|
||||
draw_glyph(
|
||||
batch, pos, glm::vec2(x, y), c, right, up, glyphInterval, style
|
||||
batch,
|
||||
pos,
|
||||
glm::vec2(x, y),
|
||||
c,
|
||||
right,
|
||||
up,
|
||||
glyphInterval,
|
||||
*style
|
||||
);
|
||||
}
|
||||
else if (charpage > page && charpage < next){
|
||||
@@ -145,20 +169,27 @@ const Texture* Font::getPage(int charpage) const {
|
||||
}
|
||||
|
||||
void Font::draw(
|
||||
Batch2D& batch, std::wstring_view text, int x, int y, float scale
|
||||
Batch2D& batch,
|
||||
std::wstring_view text,
|
||||
int x,
|
||||
int y,
|
||||
const FontStylesScheme* styles,
|
||||
float scale
|
||||
) const {
|
||||
draw_text(
|
||||
*this, batch, text,
|
||||
glm::vec3(x, y, 0),
|
||||
glm::vec3(glyphInterval*scale, 0, 0),
|
||||
glm::vec3(0, lineHeight*scale, 0),
|
||||
glyphInterval/static_cast<float>(lineHeight)
|
||||
glyphInterval/static_cast<float>(lineHeight),
|
||||
styles
|
||||
);
|
||||
}
|
||||
|
||||
void Font::draw(
|
||||
Batch3D& batch,
|
||||
std::wstring_view text,
|
||||
const FontStylesScheme* styles,
|
||||
const glm::vec3& pos,
|
||||
const glm::vec3& right,
|
||||
const glm::vec3& up
|
||||
@@ -167,6 +198,7 @@ void Font::draw(
|
||||
*this, batch, text, pos,
|
||||
right * static_cast<float>(glyphInterval),
|
||||
up * static_cast<float>(lineHeight),
|
||||
glyphInterval/static_cast<float>(lineHeight)
|
||||
glyphInterval/static_cast<float>(lineHeight),
|
||||
styles
|
||||
);
|
||||
}
|
||||
|
||||
@@ -12,11 +12,16 @@ class Batch3D;
|
||||
class Camera;
|
||||
|
||||
struct FontStyle {
|
||||
size_t n = -1;
|
||||
bool bold = false;
|
||||
bool italic = false;
|
||||
glm::vec4 color {1, 1, 1, 1};
|
||||
};
|
||||
|
||||
struct FontStylesScheme {
|
||||
std::vector<FontStyle> palette;
|
||||
};
|
||||
|
||||
class Font {
|
||||
int lineHeight;
|
||||
int yoffset;
|
||||
@@ -45,12 +50,20 @@ public:
|
||||
/// @brief Check if character is visible (non-whitespace)
|
||||
/// @param codepoint character unicode codepoint
|
||||
bool isPrintableChar(uint codepoint) const;
|
||||
|
||||
void draw(Batch2D& batch, std::wstring_view text, int x, int y, float scale=1) const;
|
||||
|
||||
void draw(
|
||||
Batch2D& batch,
|
||||
std::wstring_view text,
|
||||
int x,
|
||||
int y,
|
||||
const FontStylesScheme* styles,
|
||||
float scale = 1
|
||||
) const;
|
||||
|
||||
void draw(
|
||||
Batch3D& batch,
|
||||
std::wstring_view text,
|
||||
const FontStylesScheme* styles,
|
||||
const glm::vec3& pos,
|
||||
const glm::vec3& right={1, 0, 0},
|
||||
const glm::vec3& up={0, 1, 0}
|
||||
|
||||
Reference in New Issue
Block a user