add FontStyle 'bold' and 'italic' properties to 2D text

This commit is contained in:
MihailRis 2024-12-04 17:08:04 +03:00
parent 28f49ac948
commit b15725913e
4 changed files with 76 additions and 25 deletions

View File

@ -261,6 +261,24 @@ void Batch2D::rect(
vertex(x+w, y+h, u+tx, v, r,g,b,a);
}
void Batch2D::parallelogram(
float x, float y, float w, float h, float skew,
float u, float v, float tx, float ty,
float r, float g, float b, float a
){
if (index + 6*B2D_VERTEX_SIZE >= capacity) {
flush();
}
setPrimitive(DrawPrimitive::triangle);
vertex(x-skew*w, y, u, v+ty, r,g,b,a);
vertex(x+(1+skew)*w, y+h, u+tx, v, r,g,b,a);
vertex(x+skew*w, y+h, u, v, r,g,b,a);
vertex(x-skew*w, y, u, v+ty, r,g,b,a);
vertex(x+w-skew*w, y, u+tx, v+ty, r,g,b,a);
vertex(x+(1+skew)*w, y+h, u+tx, v, r,g,b,a);
}
void Batch2D::rect(
float x, float y, float w, float h,
float r0, float g0, float b0,
@ -336,6 +354,22 @@ void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index
rect(x, y, w, h, u, v, scale, scale, tint.r, tint.g, tint.b, tint.a);
}
void Batch2D::sprite(
float x,
float y,
float w,
float h,
float skew,
int atlasRes,
int index,
glm::vec4 tint
) {
float scale = 1.0f / (float)atlasRes;
float u = (index % atlasRes) * scale;
float v = 1.0f - ((index / atlasRes) * scale) - scale;
parallelogram(x, y, w, h, skew, u, v, scale, scale, tint.r, tint.g, tint.b, tint.a);
}
void Batch2D::flush() {
if (index == 0)
return;

View File

@ -45,6 +45,7 @@ public:
void setRegion(UVRegion region);
void sprite(float x, float y, float w, float h, const UVRegion& region, glm::vec4 tint);
void sprite(float x, float y, float w, float h, int atlasRes, int index, glm::vec4 tint);
void sprite(float x, float y, float w, float h, float skew, int atlasRes, int index, glm::vec4 tint);
void point(float x, float y, float r, float g, float b, float a);
inline void setColor(glm::vec4 color) {
@ -79,6 +80,12 @@ public:
float r, float g, float b, float a
);
void parallelogram(
float x, float y, float w, float h, float skew,
float u, float v, float tx, float ty,
float r, float g, float b, float a
);
void rect(
float x, float y, float w, float h,
float r0, float g0, float b0,

View File

@ -52,17 +52,21 @@ static inline void draw_glyph(
uint c,
const glm::vec3& right,
const glm::vec3& up,
float glyphInterval
float glyphInterval,
const FontStyle& style
) {
batch.sprite(
pos.x + offset.x * right.x,
pos.y + offset.y * right.y,
right.x / glyphInterval,
up.y,
16,
c,
batch.getColor()
);
for (int i = 0; i <= style.bold; i++) {
batch.sprite(
pos.x + (offset.x + i / (right.x/glyphInterval/2.0f)) * right.x,
pos.y + offset.y * right.y,
right.x / glyphInterval,
up.y,
-0.2f * style.italic,
16,
c,
batch.getColor()
);
}
}
static inline void draw_glyph(
@ -72,17 +76,20 @@ static inline void draw_glyph(
uint c,
const glm::vec3& right,
const glm::vec3& up,
float glyphInterval
float glyphInterval,
const FontStyle& style
) {
batch.sprite(
pos + right * offset.x + up * offset.y,
up, right / glyphInterval,
0.5f,
0.5f,
16,
c,
batch.getColor()
);
for (int i = 0; i <= style.bold; i++) {
batch.sprite(
pos + right * (offset.x + i) + up * offset.y,
up, right / glyphInterval,
0.5f,
0.5f,
16,
c,
batch.getColor()
);
}
}
template <class Batch>
@ -99,6 +106,9 @@ static inline void draw_text(
uint next = MAX_CODEPAGES;
int x = 0;
int y = 0;
FontStyle style {};
do {
for (uint c : text){
if (!font.isPrintableChar(c)) {
@ -109,7 +119,7 @@ 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
batch, pos, glm::vec2(x, y), c, right, up, glyphInterval, style
);
}
else if (charpage > page && charpage < next){

View File

@ -11,10 +11,10 @@ class Batch2D;
class Batch3D;
class Camera;
enum class FontStyle {
none,
shadow,
outline
struct FontStyle {
bool bold = false;
bool italic = false;
glm::vec4 color {1, 1, 1, 1};
};
class Font {