Add assignable allocator support to stb_truetype

This commit is contained in:
2025-01-10 22:44:39 -05:00
parent 87ab22c207
commit c64f8132dc
11 changed files with 18882 additions and 46 deletions

View File

@@ -17,7 +17,7 @@ Features:
* Robust quality of life features:
* Tracks text layers!
* Push and pop stack for font, font_size, colour, view, position, scale and zoom!
* Enforce even only font-sizing (useful for linear-zoom) [TODO]
* Enforce even only font-sizing (useful for linear-zoom)
* Snap-positining to view for better hinting
* Basic or advanced text shaping via Harfbuzz
* All rendering is real-time, triangulation done on the CPU, vertex rendering and texture blitting on the gpu.
@@ -31,8 +31,9 @@ Features:
Upcoming:
* Support for ear-clipping triangulation
* Support for ear-clipping triangulation, or just better triangulation..
* Support for which triangulation method used on a by font basis?
* https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf
* Multi-threading supported job queue.
* Lift heavy-lifting portion of the library's context into a thread context.
* Synchronize threads by merging their generated layered draw list into a finished draw-list for processing on the user's render thread.

View File

@@ -224,16 +224,16 @@ init :: proc "c" ()
}
glyph_draw_opts := ve.Init_Glyph_Draw_Params_Default
glyph_draw_opts.snap_glyph_height = false
glyph_draw_opts.snap_glyph_height = true
shaper_opts := ve.Init_Shaper_Params_Default
shaper_opts.snap_glyph_position = false
shaper_opts.snap_glyph_position = true
ve.startup( & demo_ctx.ve_ctx, .STB_TrueType, allocator = context.allocator,
glyph_draw_params = glyph_draw_opts,
shaper_params = shaper_opts,
px_scalar = 1.89,
alpha_sharpen = 0.1,
px_scalar = 1.25,
alpha_sharpen = 0.0,
)
ve_sokol.setup_gfx_objects( & demo_ctx.render_ctx, & demo_ctx.ve_ctx, vert_cap = 256 * 1024, index_cap = 512 * 1024 )
@@ -388,10 +388,10 @@ Glyphs are first rendered to an intermediate 2k x 512px R8 texture. This allows
4 x 4 = 16x supersampling, and 8 Region C glyphs similarly. A simple 16-tap box downsample shader is then used to blit from this
intermediate texture to the final atlas location.`
draw_text("How it works", demo_ctx.font_title, { 0.2, current_scroll - (section_start + 0.06) })
draw_text(how_it_works, demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.1) })
draw_text(caching_strategy, demo_ctx.font_mono, { 0.28, current_scroll - (section_start + 0.32) })
draw_text(how_it_works2, demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.82) })
draw_text("How it works", demo_ctx.font_title, { 0.2, current_scroll - (section_start + 0.06) }, size = 92)
draw_text(how_it_works, demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.1) }, size = 19)
draw_text(caching_strategy, demo_ctx.font_mono, { 0.28, current_scroll - (section_start + 0.32) }, size = 21)
draw_text(how_it_works2, demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.82) }, size = 19)
}
// Showcase section
@@ -408,43 +408,43 @@ etiam dignissim diam quis enim. Convallis convallis tellus id interdum.`
draw_text("Sans serif", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.28) }, size = 19)
draw_text(font_family_test, demo_ctx.font_demo_sans, { 0.3, current_scroll - (section_start + 0.28) }, size = 18)
draw_text("Serif", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.36) })
draw_text(font_family_test, demo_ctx.font_demo_serif, { 0.3, current_scroll - (section_start + 0.36) })
draw_text("Serif", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.36) }, size = 19)
draw_text(font_family_test, demo_ctx.font_demo_serif, { 0.3, current_scroll - (section_start + 0.36) }, size = 18)
draw_text("Script", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.44) })
draw_text(font_family_test, demo_ctx.font_demo_script, { 0.3, current_scroll - (section_start + 0.44) })
draw_text("Script", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.44) }, size = 19)
draw_text(font_family_test, demo_ctx.font_demo_script, { 0.3, current_scroll - (section_start + 0.44) }, size = 22)
draw_text("Monospace", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.52) })
draw_text(font_family_test, demo_ctx.font_demo_mono, { 0.3, current_scroll - (section_start + 0.52) })
draw_text("Monospace", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.52) }, size = 19)
draw_text(font_family_test, demo_ctx.font_demo_mono, { 0.3, current_scroll - (section_start + 0.52) }, size = 22)
draw_text("Small", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.60) })
draw_text(font_family_test, demo_ctx.font_small, { 0.3, current_scroll - (section_start + 0.60) })
draw_text("Small", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.60) }, size = 19)
draw_text(font_family_test, demo_ctx.font_small, { 0.3, current_scroll - (section_start + 0.60) }, size = 10)
draw_text("Greek", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.72) })
draw_text("Ήταν απλώς θέμα χρόνου.", demo_ctx.font_demo_sans, { 0.3, current_scroll - (section_start + 0.72) })
draw_text("Greek", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.72) }, size = 19)
draw_text("Ήταν απλώς θέμα χρόνου.", demo_ctx.font_demo_sans, { 0.3, current_scroll - (section_start + 0.72) }, size = 18)
draw_text("Vietnamese", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.76) })
draw_text("Bầu trời trong xanh thăm thẳm, không một gợn mây.", demo_ctx.font_demo_sans, { 0.3, current_scroll - (section_start + 0.76) })
draw_text("Vietnamese", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.76) }, size = 19)
draw_text("Bầu trời trong xanh thăm thẳm, không một gợn mây.", demo_ctx.font_demo_sans, { 0.3, current_scroll - (section_start + 0.76) }, size = 18)
draw_text("Thai", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.80) })
draw_text("การเดินทางขากลับคงจะเหงา", demo_ctx.font_demo_thai, { 0.3, current_scroll - (section_start + 0.80) })
draw_text("Thai", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.80) }, size = 19)
draw_text("การเดินทางขากลับคงจะเหงา", demo_ctx.font_demo_thai, { 0.3, current_scroll - (section_start + 0.80) }, size = 24)
draw_text("Chinese", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.84) })
draw_text("床前明月光 疑是地上霜 举头望明月 低头思故乡", demo_ctx.font_demo_chinese, {0.3, current_scroll - (section_start + 0.84) })
draw_text("Chinese", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.84) }, size = 19)
draw_text("床前明月光 疑是地上霜 举头望明月 低头思故乡", demo_ctx.font_demo_chinese, {0.3, current_scroll - (section_start + 0.84) }, size = 24)
draw_text("Japanese", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.88) })
draw_text("ぎょしょうとナレズシの研究 モンスーン・アジアの食事文化", demo_ctx.font_demo_japanese, { 0.3, current_scroll - (section_start + 0.88) })
draw_text("Japanese", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.88) }, size = 19)
draw_text("ぎょしょうとナレズシの研究 モンスーン・アジアの食事文化", demo_ctx.font_demo_japanese, { 0.3, current_scroll - (section_start + 0.88) }, size = 24)
draw_text("Korean", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.92) })
draw_text("그들의 장비와 기구는 모두 살아 있다.", demo_ctx.font_demo_korean, { 0.3, current_scroll - (section_start + 0.92) })
draw_text("Korean", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.92) }, size = 19)
draw_text("그들의 장비와 기구는 모두 살아 있다.", demo_ctx.font_demo_korean, { 0.3, current_scroll - (section_start + 0.92) }, size = 36)
draw_text("Needs harfbuzz to work:", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.96)})
draw_text("Needs harfbuzz to work:", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 0.96)}, size = 14)
draw_text("Arabic", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 1.00) })
draw_text("حب السماء لا تمطر غير الأحلام.", demo_ctx.font_demo_arabic, { 0.3, current_scroll - (section_start + 1.00) })
draw_text("Arabic", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 1.00) }, size = 19)
draw_text("حب السماء لا تمطر غير الأحلام.", demo_ctx.font_demo_arabic, { 0.3, current_scroll - (section_start + 1.00) }, size = 24)
draw_text("Hebrew", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 1.04) })
draw_text("אז הגיע הלילה של כוכב השביט הראשון.", demo_ctx.font_demo_hebrew, { 0.3, current_scroll - (section_start + 1.04) })
draw_text("Hebrew", demo_ctx.font_print, { 0.2, current_scroll - (section_start + 1.04) }, size = 19)
draw_text("אז הגיע הלילה של כוכב השביט הראשון.", demo_ctx.font_demo_hebrew, { 0.3, current_scroll - (section_start + 1.04) }, size = 22)
}
// Zoom Test
@@ -486,13 +486,10 @@ etiam dignissim diam quis enim. Convallis convallis tellus id interdum.`
zoomed_text_base_size : f32 = 12.0
zoom_adjust_size := zoomed_text_base_size * current_zoom
// ve_id, resolved_size := font_resolve_draw_id( font_firacode, zoom_adjust_size * OVER_SAMPLE_ZOOM )
resolved_size := zoom_adjust_size
resolved_size, _ := ve.resolve_zoom_size_scale(current_zoom, zoomed_text_base_size, 1.0, 2, 2, 999.0, demo_ctx.screen_size)
current_zoom_text := fmt.tprintf("Current Zoom : %.2f x\nCurrent Resolved Size: %v px", current_zoom, resolved_size )
draw_text(current_zoom_text, demo_ctx.font_firacode, { 0.2, zoom_info_y })
// ve.configure_snap( & demo_ctx.ve_ctx, u32(0), u32(0) )
size := measure_text_size( zoom_text, demo_ctx.font_firacode, zoomed_text_base_size, 0 ) * current_zoom
x_offset := (size.x / demo_ctx.screen_size.x) * 0.5
zoomed_text_pos := Vec2 { 0.5 - x_offset, zoomed_text_y }
@@ -567,8 +564,6 @@ etiam dignissim diam quis enim. Convallis convallis tellus id interdum.`
draw_text(codes[grid[y * GRID_W + x]], demo_ctx.font_demo_raincode, { pos_x, pos_y }, size = 20, color = code_colour)
}
// ve.set_colour(&ve_ctx, {1.0, 1.0, 1.0, 1.0})
}
// Cache pressure test

View File

@@ -67,6 +67,15 @@ push-location $path_thirdparty
}
pop-location
$path_stb_truetype = join-path $path_thirdparty 'stb\src'
push-location $path_stb_truetype
$devshell = Join-Path $PSScriptRoot 'helpers/devshell.ps1'
. $devshell -arch amd64
& .\build.bat
pop-location
$odin_compiler_defs = join-path $PSScriptRoot 'helpers/odin_compiler_defs.ps1'
. $odin_compiler_defs

Binary file not shown.

10824
thirdparty/stb/src/gb/gb.h vendored Normal file

File diff suppressed because it is too large Load Diff

2
thirdparty/stb/src/stb_image.c vendored Normal file
View File

@@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

7897
thirdparty/stb/src/stb_image.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -412,6 +412,43 @@ int main(int arg, char **argv)
}
#endif
#pragma region ODIN: CUSTOM ALLOCATOR
#ifdef STB_TRUETYPE_IMPLEMENTATION
#define GB_IMPLEMENTATION
#endif
#include "gb/gb.h"
#ifdef STBTT_STATIC
#define STBTT_DEF static
#else
#define STBTT_DEF extern
#endif
#ifdef __cplusplus
extern "C" {
#endif
STBTT_DEF void stbtt_SetAllocator( gbAllocator allocator );
#ifdef __cplusplus
}
#endif
#ifndef STBTT_malloc
#define STBTT_malloc(x,u) ((void)(u), gb_alloc(stbtt__allocator, x))
#define STBTT_free(x,u) ((void)(u), gb_free(stbtt__allocator, x))
#endif
#ifdef STB_TRUETYPE_IMPLEMENTATION
gb_global gbAllocator stbtt__allocator = { gb_heap_allocator_proc, NULL };
STBTT_DEF void stbtt_SetAllocator( gbAllocator allocator ) {
stbtt__allocator = allocator;
}
#endif
#pragma endregion ODIN: CUSTOM ALLOCATOR
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

View File

@@ -36,6 +36,37 @@ when ODIN_ARCH == .wasm32 || ODIN_ARCH == .wasm64p32 {
#assert(size_of(c.int) == size_of(rune))
#assert(size_of(c.int) == size_of(b32))
//-----------------------------------------------------------------------------
// CUSTOM: ODIN COMPATIBLE ALLOCATOR
//-----------------------------------------------------------------------------
gbAllocationType :: enum(i32) {
Alloc,
Free,
FreeAll,
Resize,
}
gbAllocatorProc :: #type proc(allocator_data: rawptr, type: gbAllocationType,
size: c.ssize_t, alignment: c.ssize_t,
old_memory: rawptr, old_size: c.ssize_t,
flags : c.ulonglong
) -> rawptr
gbAllocator :: struct {
procedure: gbAllocatorProc,
data: rawptr,
}
@(default_calling_convention="c", link_prefix="stbtt_")
foreign stbtt {
SetAllocator :: proc(allocator : gbAllocator) ---
}
//-----------------------------------------------------------------------------
// END CUSTOM: ODIN COMPATIBLE ALLOCATOR
//-----------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////
//
// TEXTURE BAKING API

View File

@@ -596,6 +596,7 @@ batch_generate_glyphs_draw_list :: proc ( draw_list : ^Draw_List,
error : Allocator_Error
glyph_pack[pack_id].shape, error = parser_get_glyph_shape(entry.parser_info, shape.glyph[pack_id])
assert(error == .None)
assert(glyph_pack[pack_id].shape != nil)
}
for id, index in oversized
{
@@ -633,7 +634,10 @@ batch_generate_glyphs_draw_list :: proc ( draw_list : ^Draw_List,
}
flush_glyph_buffer_draw_list(draw_list, & glyph_buffer.draw_list, & glyph_buffer.clear_draw_list, & glyph_buffer.allocated_x)
for id, index in oversized do parser_free_shape(entry.parser_info, glyph_pack[id].shape)
for pack_id, index in oversized {
assert(glyph_pack[pack_id].shape != nil)
parser_free_shape(entry.parser_info, glyph_pack[pack_id].shape)
}
}
profile_end()
@@ -666,6 +670,7 @@ batch_generate_glyphs_draw_list :: proc ( draw_list : ^Draw_List,
error : Allocator_Error
glyph_pack[pack_id].shape, error = parser_get_glyph_shape(entry.parser_info, shape.glyph[pack_id])
assert(error == .None)
assert(glyph_pack[pack_id].shape != nil)
}
for id, index in to_cache
@@ -731,7 +736,10 @@ batch_generate_glyphs_draw_list :: proc ( draw_list : ^Draw_List,
}
flush_glyph_buffer_draw_list(draw_list, & glyph_buffer.draw_list, & glyph_buffer.clear_draw_list, & glyph_buffer.allocated_x)
for id, index in to_cache do parser_free_shape(entry.parser_info, glyph_pack[id].shape)
for pack_id, index in to_cache {
assert(glyph_pack[pack_id].shape != nil)
parser_free_shape(entry.parser_info, glyph_pack[pack_id].shape)
}
profile_begin("gen_cached_draw_list: to_cache")
when ENABLE_DRAW_TYPE_VISUALIZATION {

View File

@@ -18,6 +18,7 @@ Already wanted to do so anyway to evaluate the shape generation implementation.
import "base:runtime"
import "core:c"
import "core:math"
import "core:mem"
import "core:slice"
import stbtt "thirdparty:stb/truetype"
// import freetype "thirdparty:freetype"
@@ -57,13 +58,44 @@ Parser_Glyph_Vertex :: struct {
Parser_Glyph_Shape :: [dynamic]Parser_Glyph_Vertex
Parser_Context :: struct {
kind : Parser_Kind,
lib_backing : Allocator,
kind : Parser_Kind,
// ft_library : freetype.Library,
}
parser_init :: proc( ctx : ^Parser_Context, kind : Parser_Kind )
parser_stbtt_allocator_proc :: proc(
allocator_data : rawptr,
type : stbtt.gbAllocationType,
size : c.ssize_t,
alignment : c.ssize_t,
old_memory : rawptr,
old_size : c.ssize_t,
flags : c.ulonglong
) -> rawptr
{
ctx.kind = kind
allocator := transmute(^Allocator) allocator_data
result, error := allocator.procedure( allocator.data, cast(mem.Allocator_Mode) type, cast(int) size, cast(int) alignment, old_memory, cast(int) old_size )
assert(error == .None)
if type == .Alloc || type == .Resize {
return transmute(rawptr) & result[0]
}
else do return nil
}
parser_init :: proc( ctx : ^Parser_Context, kind : Parser_Kind, allocator := context.allocator )
{
ctx.kind = kind
ctx.lib_backing = allocator
stbtt_allocator := stbtt.gbAllocator { parser_stbtt_allocator_proc, & ctx.lib_backing }
stbtt.SetAllocator( stbtt_allocator )
}
parser_reload :: proc( ctx : ^Parser_Context, allocator := context.allocator) {
ctx.lib_backing = allocator
stbtt_allocator := stbtt.gbAllocator { parser_stbtt_allocator_proc, & ctx.lib_backing }
stbtt.SetAllocator( stbtt_allocator )
}
parser_shutdown :: proc( ctx : ^Parser_Context ) {