pixel perfect font size

This commit is contained in:
jrkb 2023-04-13 07:24:16 +02:00
parent 48c64c9326
commit b49d3cf459

View file

@ -700,7 +700,7 @@ class Font {
} }
void draw(float x, float y, float z, const std::string & text, bool vFlip = false){ void draw(float x, float y, float z, const std::string & text, bool vFlip = false, float fontSize_px = 42){
float originalX = x; float originalX = x;
glBindVertexArray(vao); glBindVertexArray(vao);
@ -710,6 +710,7 @@ class Font {
FT_UInt previous = 0; FT_UInt previous = 0;
for(const char * textIt = text.c_str(); *textIt != '\0';){ for(const char * textIt = text.c_str(); *textIt != '\0';){
float letterFontSize_px = fontSize_px; // can be individual
uint32_t charcode = decodeCharcode(&textIt); uint32_t charcode = decodeCharcode(&textIt);
if(charcode == '\r'){ if(charcode == '\r'){
@ -746,18 +747,18 @@ class Font {
float v1 = (float)(glyph.bearingY + d) / emSize; float v1 = (float)(glyph.bearingY + d) / emSize;
float x0 = x + u0 * worldSize; float x0 = x + u0 * worldSize * letterFontSize_px;
float y0 = y + v0 * worldSize; float y0 = y + v0 * worldSize * letterFontSize_px;
float x1 = x + u1 * worldSize; float x1 = x + u1 * worldSize * letterFontSize_px;
float y1 = y + v1 * worldSize; float y1 = y + v1 * worldSize * letterFontSize_px;
if(vFlip){ if(vFlip){
float _v = v0; float _v = v0;
v0 = v1; v0 = v1;
v1 = _v; v1 = _v;
y0 = y - v0 * worldSize; y0 = y - v0 * worldSize * letterFontSize_px;
y1 = y - v1 * worldSize; y1 = y - v1 * worldSize * letterFontSize_px;
} }
int32_t base = static_cast <int32_t>(vertices.size()); int32_t base = static_cast <int32_t>(vertices.size());
@ -769,7 +770,7 @@ class Font {
indices.insert(indices.end(), {base, base + 1, base + 2, base + 2, base + 3, base}); indices.insert(indices.end(), {base, base + 1, base + 2, base + 2, base + 3, base});
} }
x += (float)glyph.advance / emSize * worldSize; x += ((float)glyph.advance * letterFontSize_px) / emSize * worldSize;
previous = glyph.index; previous = glyph.index;
} }
@ -919,7 +920,7 @@ static void tryUpdateMainFont(FT_Library & library,
Font::BoundingBox & bb){ Font::BoundingBox & bb){
{ {
cout << "tryUpdateMainFont" << endl; cout << "tryUpdateMainFont" << endl;
auto font = loadFont(library, filename, 0.05f); auto font = loadFont(library, filename, 1.0f);
if(!font){ if(!font){
return; return;
} }