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); 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( void Batch2D::rect(
float x, float y, float w, float h, float x, float y, float w, float h,
float r0, float g0, float b0, 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); 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() { void Batch2D::flush() {
if (index == 0) if (index == 0)
return; return;

View File

@ -45,6 +45,7 @@ public:
void setRegion(UVRegion region); 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, 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, 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); void point(float x, float y, float r, float g, float b, float a);
inline void setColor(glm::vec4 color) { inline void setColor(glm::vec4 color) {
@ -79,6 +80,12 @@ public:
float r, float g, float b, float a 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( void rect(
float x, float y, float w, float h, float x, float y, float w, float h,
float r0, float g0, float b0, float r0, float g0, float b0,

View File

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

View File

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