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; ugui::Ctx ui;
ui.init()!!; 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 width = 800;
short height = 450; short height = 450;
@ -168,10 +168,16 @@ fn int main(String[] args)
}; };
rl::Vector2 position = { rl::Vector2 position = {
.x = cmd.sprite.rect.x, .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_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: default:
io::printfn("Unknown cmd type: %s", cmd.type); io::printfn("Unknown cmd type: %s", cmd.type);
} }

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

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

Loading…
Cancel
Save