Merge branch 'master' into master

This commit is contained in:
Jeroen van Rijn
2022-07-28 16:01:18 +02:00
committed by GitHub
619 changed files with 122592 additions and 30625 deletions
+114 -27
View File
@@ -1,32 +1,105 @@
package stb_easy_font
// Source port of stb_easy_font.h
/*
Source port of stb_easy_font.h
Original port: gingerBill
Bugfixes: Florian Behr & Jeroen van Rijn
Additions: Jeroen van Rijn
Changelog:
2022-04-03
Bug fixes
Add `print(x, y, text, color, quad_buffer)` version that takes `[]quad`.
(Same internal memory layout as []u8 API, but more convenient for the caller.)
Add optional `scale := f32(1.0)` param to `print` to embiggen the glyph quads.
2021-09-14
Original Odin version
*/
/*
// Example for use with vendor:raylib
quads: [999]easy_font.Quad = ---
color := rl.GREEN
c := transmute(easy_font.Color)color
num_quads := easy_font.print(10, 60, TEXT, c, quads[:])
for q in quads[:num_quads] {
tl := q.tl.v
br := q.br.v
color = transmute(rl.Color)q.tl.c
r := rl.Rectangle{x = tl.x, y = tl.y, width = br.x - tl.x, height = br.y - tl.y}
// Yes, we could just use the `color` from above, but this shows how to get it back from the vertex.
// And in practice this code will likely not live as close to the `easy_font` call.
rl.DrawRectangleRec(r, color)
}
*/
import "core:math"
import "core:mem"
color :: struct {
c: [4]u8,
Color :: [4]u8
Vertex :: struct {
v: [3]f32,
c: Color,
}
#assert(size_of(Vertex) == 16)
draw_segs :: proc(x, y: f32, segs: []u8, vertical: bool, c: color, vbuf: []byte, offset: int) -> int {
x, y, offset := x, y, offset
for i in 0..<len(segs) {
n := segs[i] & 7
x += f32((segs[i]>>31) & 1)
if n != 0 && offset+64 <= len(vbuf) {
y0 := y + f32(segs[i]>>4)
for j in 0..<4 {
(^f32)(&vbuf[offset+0])^ = x + ((vertical ? 1 : len) if j==1 || j==2 else 0)
(^f32)(&vbuf[offset+4])^ = y0 + ((vertical ? len : 1) if j >= 2 else 0)
(^f32)(&vbuf[offset+8])^ = 0
(^color)(&vbuf[offset+12])^ = c
offset += 16
}
Quad :: struct #packed {
tl: Vertex,
tr: Vertex,
br: Vertex,
bl: Vertex,
}
#assert(size_of(Quad) == 64)
// Same memory layout, but takes a []quad instead of []byte
draw_segs_quad_buffer :: proc(x, y: f32, segs: []u8, vertical: bool, c: Color, buf: []Quad, start_offset: int, scale := f32(1.0)) -> (quads: int) {
x, y := x, y
quads = start_offset
for seg in segs {
stroke_length := f32(seg & 7) * scale
x += f32((seg >> 3) & 1) * scale
if stroke_length != 0 && quads + 1 <= len(buf) {
y0 := y + (f32(seg >> 4) * scale)
horz := scale if vertical else stroke_length
vert := stroke_length if vertical else scale
buf[quads].tl.c = c
buf[quads].tl.v = { x, y0, 0 }
buf[quads].tr.c = c
buf[quads].tr.v = { x + horz, y0, 0 }
buf[quads].br.c = c
buf[quads].br.v = { x + horz, y0 + vert, 0 }
buf[quads].bl.c = c
buf[quads].bl.v = { x, y0 + vert, 0 }
quads += 1
}
}
return quads
}
// Compatible with original C API
draw_segs_vertex_buffer :: proc(x, y: f32, segs: []u8, vertical: bool, c: Color, vbuf: []byte, start_offset: int, scale := f32(1.0)) -> (offset: int) {
buf := mem.slice_data_cast([]Quad, vbuf)
offset = draw_segs_quad_buffer(x, y, segs, vertical, c, buf, start_offset / size_of(Quad), scale) * size_of(Quad)
return offset
}
draw_segs :: proc{ draw_segs_quad_buffer, draw_segs_vertex_buffer }
@(private)
_spacing_val := f32(0)
@@ -34,13 +107,17 @@ font_spacing :: proc(spacing: f32) {
_spacing_val = spacing
}
print :: proc(x, y: f32, text: string, color: color, vertex_buffer: []byte) -> int {
x, y := x, y
// Same memory layout, but takes a []quad instead of []byte
print_quad_buffer :: proc(x, y: f32, text: string, color: Color, quad_buffer: []Quad, scale := f32(1.0)) -> (quads: int) {
x, y, color := x, y, color
text := text
start_x := x
offset := 0
for len(text) != 0 && offset < len(vertex_buffer) {
if color == {} {
color = {255, 255, 255, 255}
}
for len(text) != 0 && quads < len(quad_buffer) {
c := text[0]
if c == '\n' {
y += 12
@@ -52,17 +129,27 @@ print :: proc(x, y: f32, text: string, color: color, vertex_buffer: []byte) -> i
v_seg := charinfo[c-32].v_seg
num_h := charinfo[c-32 + 1].h_seg - h_seg
num_v := charinfo[c-32 + 1].v_seg - v_seg
offset = draw_segs(x, y_ch, hseg[h_seg:][:num_h], false, color, vertex_buffer, offset)
offset = draw_segs(x, y_ch, hseg[v_seg:][:num_v], true, color, vertex_buffer, offset)
x += f32(advance & 15)
x += _spacing_val
quads = draw_segs(x, y_ch, hseg[h_seg:][:num_h], false, color, quad_buffer, quads, scale)
quads = draw_segs(x, y_ch, vseg[v_seg:][:num_v], true, color, quad_buffer, quads, scale)
x += f32(advance & 15) * scale
x += _spacing_val * scale
}
text = text[1:]
}
return offset/64
return
}
// Compatible with original C API
print_vertex_buffer :: proc(x, y: f32, text: string, color: Color, vertex_buffer: []byte, scale := f32(1.0)) -> int {
buf := mem.slice_data_cast([]Quad, vertex_buffer)
return print_quad_buffer(x, y, text, color, buf, scale)
}
print :: proc{ print_quad_buffer, print_vertex_buffer }
width :: proc(text: string) -> int {
length := f32(0)
max_length := f32(0)
+3 -2
View File
@@ -75,8 +75,9 @@ foreign stbi {
info_from_memory :: proc(buffer: [^]byte, len: c.int, x, y, comp: ^c.int) -> c.int ---
info_from_callbacks :: proc(clbk: ^Io_Callbacks, user: rawptr, x, y, comp: ^c.int) -> c.int ---
is_16_bit :: proc(filename: cstring) -> b32 ---
is_16_bit_from_file :: proc(f: ^c.FILE) -> b32 ---
is_16_bit :: proc(filename: cstring) -> b32 ---
is_16_bit_from_file :: proc(f: ^c.FILE) -> b32 ---
is_16_bit_from_memory :: proc(buffer: [^]byte, len: c.int) -> c.int ---
// for image formats that explicitly notate that they have premultiplied alpha,
// we just return the colors as stored in the file. set this flag to force
+3 -2
View File
@@ -4,8 +4,9 @@ import c "core:c/libc"
#assert(size_of(b32) == size_of(c.int))
when ODIN_OS == "windows" { foreign import lib "../lib/stb_rect_pack.lib" }
when ODIN_OS == "linux" { foreign import lib "../lib/stb_rect_pack.a" }
when ODIN_OS == .Windows { foreign import lib "../lib/stb_rect_pack.lib" }
when ODIN_OS == .Linux { foreign import lib "../lib/stb_rect_pack.a" }
when ODIN_OS == .Darwin { foreign import lib "../lib/stb_rect_pack.a" }
Coord :: distinct c.int
_MAXVAL :: max(Coord)
+13 -13
View File
@@ -1,16 +1,16 @@
all:
mkdir -p ../lib
gcc -c -O2 -Os -fPIC stb_image.c stb_image_write.c stb_image_resize.c stb_truetype.c stb_rect_pack.c stb_vorbis.c
ar rcs ../lib/stb_image.a stb_image.o
ar rcs ../lib/stb_image_write.a stb_image_write.o
ar rcs ../lib/stb_image_resize.a stb_image_resize.o
ar rcs ../lib/stb_truetype.a stb_truetype.o
ar rcs ../lib/stb_rect_pack.a stb_rect_pack.o
#ar rcs ../lib/stb_vorbis_pack.a stb_vorbis_pack.o
#gcc -fPIC -shared -Wl,-soname=stb_image.so -o ../lib/stb_image.so stb_image.o
#gcc -fPIC -shared -Wl,-soname=stb_image_write.so -o ../lib/stb_image_write.so stb_image_write.o
#gcc -fPIC -shared -Wl,-soname=stb_image_resize.so -o ../lib/stb_image_resize.so stb_image_resize.o
#gcc -fPIC -shared -Wl,-soname=stb_truetype.so -o ../lib/stb_truetype.so stb_image_truetype.o
#gcc -fPIC -shared -Wl,-soname=stb_rect_pack.so -o ../lib/stb_rect_pack.so stb_rect_packl.o
#gcc -fPIC -shared -Wl,-soname=stb_vorbis.so -o ../lib/stb_vorbis.so stb_vorbisl.o
$(CC) -c -O2 -Os -fPIC stb_image.c stb_image_write.c stb_image_resize.c stb_truetype.c stb_rect_pack.c stb_vorbis.c
$(AR) rcs ../lib/stb_image.a stb_image.o
$(AR) rcs ../lib/stb_image_write.a stb_image_write.o
$(AR) rcs ../lib/stb_image_resize.a stb_image_resize.o
$(AR) rcs ../lib/stb_truetype.a stb_truetype.o
$(AR) rcs ../lib/stb_rect_pack.a stb_rect_pack.o
#$(AR) rcs ../lib/stb_vorbis_pack.a stb_vorbis_pack.o
#$(CC) -fPIC -shared -Wl,-soname=stb_image.so -o ../lib/stb_image.so stb_image.o
#$(CC) -fPIC -shared -Wl,-soname=stb_image_write.so -o ../lib/stb_image_write.so stb_image_write.o
#$(CC) -fPIC -shared -Wl,-soname=stb_image_resize.so -o ../lib/stb_image_resize.so stb_image_resize.o
#$(CC) -fPIC -shared -Wl,-soname=stb_truetype.so -o ../lib/stb_truetype.so stb_image_truetype.o
#$(CC) -fPIC -shared -Wl,-soname=stb_rect_pack.so -o ../lib/stb_rect_pack.so stb_rect_packl.o
#$(CC) -fPIC -shared -Wl,-soname=stb_vorbis.so -o ../lib/stb_vorbis.so stb_vorbisl.o
rm *.o
+4 -3
View File
@@ -3,8 +3,9 @@ package stb_truetype
import c "core:c"
import stbrp "vendor:stb/rect_pack"
when ODIN_OS == "windows" { foreign import stbtt "../lib/stb_truetype.lib" }
when ODIN_OS == "linux" { foreign import stbtt "../lib/stb_truetype.a" }
when ODIN_OS == .Windows { foreign import stbtt "../lib/stb_truetype.lib" }
when ODIN_OS == .Linux { foreign import stbtt "../lib/stb_truetype.a" }
when ODIN_OS == .Darwin { foreign import stbtt "../lib/stb_truetype.a" }
///////////////////////////////////////////////////////////////////////////////
@@ -225,7 +226,7 @@ foreign stbtt {
// (.ttf) files only contain one font. The number of fonts can be used for
// indexing with the previous function where the index is between zero and one
// less than the total fonts. If an error occurs, -1 is returned.
GetNumberOfFonts :: proc(data: [^]byte) -> b32 ---
GetNumberOfFonts :: proc(data: [^]byte) -> c.int ---
// Each .ttf/.ttc file may have more than one font. Each font has a sequential
// index number starting from 0. Call this function to get the font offset for
+3 -2
View File
@@ -3,8 +3,9 @@ package stb_vorbis
import c "core:c/libc"
when ODIN_OS == "windows" { foreign import lib "../lib/stb_vorbis.lib" }
when ODIN_OS == "linux" { foreign import lib "../lib/stb_vorbis.a" }
when ODIN_OS == .Windows { foreign import lib "../lib/stb_vorbis.lib" }
when ODIN_OS == .Linux { foreign import lib "../lib/stb_vorbis.a" }
when ODIN_OS == .Darwin { foreign import lib "../lib/stb_vorbis.a" }