forgot the baseline

font_atlas
Alessandro Mauri 2 weeks ago
parent 2356d165fe
commit 61556d0a2c
  1. 10
      src/main.c3
  2. 20
      src/ugui_font.c3
  3. 10
      src/ugui_text.c3

@ -27,7 +27,7 @@ fn int main(String[] args)
{
ugui::Ctx ui;
ui.init()!!;
ui.font.load("/usr/share/fonts/TTF/FreeSans.ttf", 16)!!;
ui.font.load("/usr/share/fonts/TTF/FreeSans.ttf", 16, scale: 1.5)!!;
short width = 800;
short height = 450;
@ -168,10 +168,16 @@ fn int main(String[] args)
};
rl::Vector2 position = {
.x = cmd.sprite.rect.x,
.y = (float)cmd.sprite.rect.y - cmd.sprite.rect.h,
.y = cmd.sprite.rect.y,
};
rl::draw_texture_rec(font_texture, source, position, rl::WHITE);
//rl::draw_rectangle(cmd.sprite.rect.x,
// cmd.sprite.rect.y,
// cmd.sprite.rect.w,
// cmd.sprite.rect.h,
// rl::WHITE
//);
default:
io::printfn("Unknown cmd type: %s", cmd.type);
}

@ -3,6 +3,7 @@ module ugui;
import schrift;
import std::collections::map;
import std::core::mem;
import std::io;
// unicode code point, different type for a different hash
def Codepoint = uint;
@ -15,9 +16,9 @@ def Codepoint = uint;
* | |oy | | |
* | v | | |
* | .ii. | | |
* | @@@@@@. |<->| |
* | V@Mio@@o |adv| |h
* | :i. V@V | | |
* | @@@@@@. | | |
* | V@Mio@@o | | |
* | :i. V@V | | h
* | :oM@@M | | |
* | :@@@MM@M | | |
* | @@o o@M | | |
@ -25,8 +26,8 @@ def Codepoint = uint;
* |ox @@@o@@@@ | | |
* | :M@@V:@@.| | v
* +-------------*---+ -
* |<------------->|
* w
* |<---- w ---->|
* |<------ adv ---->|
*/
struct Glyph {
Codepoint code;
@ -137,11 +138,12 @@ fn void! Font.load(&font, String path, uint height, float scale = 1)
font.ascender = (float)lmetrics.ascender;
font.descender = (float)lmetrics.descender;
font.linegap = (float)lmetrics.lineGap;
//io::printfn("ascender:%d, descender:%d, linegap:%d", font.ascender, font.descender, font.linegap);
// TODO: allocate buffer based on FONT_CACHED and the size of a sample letter
// like the letter 'A'
font.atlas = mem::new_array(AtlasBW, 1);
ushort size = (ushort)font.size*512;
ushort size = (ushort)font.size*256;
font.atlas[0].new(size, size)!;
// preallocate the ASCII range
@ -191,9 +193,11 @@ fn Glyph*! Font.get_glyph(&font, Codepoint code, bool* is_new = null)
glyph.code = code;
glyph.w = (ushort)img.width;
glyph.h = (ushort)img.height;
glyph.ox = (short)-gmtx.leftSideBearing;
glyph.oy = (short)-gmtx.yOffset;
glyph.ox = (short)gmtx.leftSideBearing;
glyph.oy = (short)gmtx.yOffset;
glyph.adv = (short)gmtx.advanceWidth;
//io::printfn("code=%c, w=%d, h=%d, ox=%d, oy=%d, adv=%d",
// glyph.code, glyph.w, glyph.h, glyph.ox, glyph.oy, glyph.adv);
Point uv = font.atlas[0].place(pixels, glyph.w, glyph.h)!;
glyph.idx = 0;

@ -15,6 +15,8 @@ fn void! Ctx.text_unbounded(&ctx, String label, String text)
// this resets the flags
c_elem.type = ETYPE_TEXT;
short line_height = (short)ctx.font.ascender - (short)ctx.font.descender;
short baseline = (short)ctx.font.ascender;
bool update_atlas;
// if the element is new or the parent was updated then redo layout
if (c_elem.flags.is_new || parent.flags.updated) {
@ -26,8 +28,8 @@ fn void! Ctx.text_unbounded(&ctx, String label, String text)
Codepoint cp = (Codepoint)c;
bool n;
gp = ctx.font.get_glyph(cp, &n)!;
text_size.w += gp.w + gp.ox + gp.adv;
text_size.h += gp.h + gp.oy;
text_size.w += gp.adv;
text_size.h += line_height;
if (n) { update_atlas = true; }
}
@ -64,7 +66,7 @@ fn void! Ctx.text_unbounded(&ctx, String label, String text)
.type = CMD_SPRITE,
.sprite.rect = {
.x = orig.x + gp.ox,
.y = orig.y + gp.oy,
.y = orig.y + gp.oy + baseline,
.w = gp.w,
.h = gp.h,
},
@ -75,7 +77,7 @@ fn void! Ctx.text_unbounded(&ctx, String label, String text)
.h = gp.h,
},
};
orig.x += gp.w + gp.ox;
orig.x += gp.adv;
ctx.cmd_queue.enqueue(&cmd)!;
}
}

Loading…
Cancel
Save