2024-02-13 14:16:39 -08:00
|
|
|
package sectr
|
|
|
|
|
|
|
|
import "core:fmt"
|
|
|
|
import "core:math"
|
|
|
|
import "core:mem"
|
|
|
|
import "core:path/filepath"
|
|
|
|
import "core:os"
|
|
|
|
|
|
|
|
import rl "vendor:raylib"
|
|
|
|
|
2024-03-10 23:05:18 -07:00
|
|
|
Font_Largest_Px_Size :: 32
|
|
|
|
|
|
|
|
Font_Size_Interval :: 2
|
2024-02-13 14:16:39 -08:00
|
|
|
|
2024-02-13 15:50:22 -08:00
|
|
|
// Font_Default :: ""
|
2024-02-27 06:32:26 -08:00
|
|
|
Font_Default :: FontID { 0, "" }
|
2024-02-13 14:16:39 -08:00
|
|
|
Font_Default_Point_Size :: 18.0
|
|
|
|
|
|
|
|
Font_TTF_Default_Chars_Padding :: 4
|
|
|
|
|
|
|
|
Font_Load_Use_Default_Size :: -1
|
|
|
|
Font_Load_Gen_ID :: ""
|
|
|
|
|
|
|
|
Font_Atlas_Packing_Method :: enum u32 {
|
2024-02-27 06:32:26 -08:00
|
|
|
Raylib_Basic = 0, // Basic packing algo
|
2024-02-13 14:16:39 -08:00
|
|
|
Skyeline_Rect = 1, // stb_pack_rect
|
|
|
|
}
|
|
|
|
|
2024-02-13 23:29:08 -08:00
|
|
|
// TODO(Ed) : These are currently i32, I wanted them to be string ids for debug ease of use.
|
|
|
|
// There is an issue with the hash map type preventing me from doing so. Its allocator reference breaks.
|
2024-02-13 15:50:22 -08:00
|
|
|
// FontID :: distinct string
|
2024-02-27 06:32:26 -08:00
|
|
|
FontID :: struct {
|
|
|
|
key : u64,
|
|
|
|
label : string,
|
|
|
|
}
|
2024-02-13 14:16:39 -08:00
|
|
|
FontTag :: struct {
|
|
|
|
key : FontID,
|
|
|
|
point_size : f32
|
|
|
|
}
|
|
|
|
|
|
|
|
FontGlyphsRender :: struct {
|
|
|
|
size : i32,
|
|
|
|
count : i32,
|
|
|
|
padding : i32,
|
|
|
|
texture : rl.Texture2D,
|
|
|
|
recs : [^]rl.Rectangle, // Characters rectangles in texture
|
|
|
|
glyphs : [^]rl.GlyphInfo, // Characters info data
|
|
|
|
}
|
|
|
|
|
|
|
|
FontDef :: struct {
|
|
|
|
path_file : string,
|
2024-03-08 00:34:21 -08:00
|
|
|
|
|
|
|
// TODO(Ed) : you may have to store font data in the future if we render on demand
|
|
|
|
// data : []u8,
|
|
|
|
|
2024-02-13 14:16:39 -08:00
|
|
|
default_size : i32,
|
2024-03-10 23:05:18 -07:00
|
|
|
size_table : [Font_Largest_Px_Size / Font_Size_Interval] FontGlyphsRender,
|
2024-02-13 14:16:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
FontProviderData :: struct {
|
2024-02-27 06:32:26 -08:00
|
|
|
font_cache : HMapZPL(FontDef),
|
2024-02-13 14:16:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
font_provider_startup :: proc()
|
|
|
|
{
|
2024-03-10 23:05:18 -07:00
|
|
|
profile(#procedure)
|
2024-03-07 15:29:49 -08:00
|
|
|
state := get_state()
|
2024-02-13 14:16:39 -08:00
|
|
|
font_provider_data := & get_state().font_provider_data; using font_provider_data
|
|
|
|
|
2024-02-27 06:32:26 -08:00
|
|
|
font_cache_alloc_error : AllocatorError
|
2024-03-10 07:31:21 -07:00
|
|
|
font_cache, font_cache_alloc_error = zpl_hmap_init_reserve( FontDef, persistent_slab_allocator(), 2 )
|
2024-02-27 06:32:26 -08:00
|
|
|
verify( font_cache_alloc_error == AllocatorError.None, "Failed to allocate font_cache" )
|
|
|
|
|
2024-02-13 14:16:39 -08:00
|
|
|
log("font_cache created")
|
|
|
|
log("font_provider initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
font_provider_shutdown :: proc()
|
|
|
|
{
|
|
|
|
font_provider_data := & get_state().font_provider_data; using font_provider_data
|
|
|
|
|
2024-02-27 06:32:26 -08:00
|
|
|
for id in 0 ..< font_cache.entries.num
|
|
|
|
{
|
|
|
|
def := & font_cache.entries.data[id].value
|
|
|
|
|
2024-02-13 15:50:22 -08:00
|
|
|
for & px_render in def.size_table {
|
2024-02-13 14:16:39 -08:00
|
|
|
using px_render
|
|
|
|
rl.UnloadFontData( glyphs, count )
|
|
|
|
rl.UnloadTexture ( texture )
|
|
|
|
rl.MemFree( recs )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-23 06:36:23 -08:00
|
|
|
font_load :: proc( path_file : string,
|
2024-02-13 14:16:39 -08:00
|
|
|
default_size : f32 = Font_Load_Use_Default_Size,
|
2024-02-13 15:50:22 -08:00
|
|
|
desired_id : string = Font_Load_Gen_ID
|
2024-02-13 14:16:39 -08:00
|
|
|
) -> FontID
|
|
|
|
{
|
2024-03-10 23:05:18 -07:00
|
|
|
profile(#procedure)
|
2024-02-13 14:16:39 -08:00
|
|
|
font_provider_data := & get_state().font_provider_data; using font_provider_data
|
|
|
|
|
2024-03-08 00:34:21 -08:00
|
|
|
font_data, read_succeded : = os.read_entire_file( path_file, context.temp_allocator )
|
2024-02-27 04:50:57 -08:00
|
|
|
verify( b32(read_succeded), str_fmt_tmp("Failed to read font file for: %v", path_file) )
|
2024-02-13 14:16:39 -08:00
|
|
|
font_data_size := cast(i32) len(font_data)
|
|
|
|
|
|
|
|
desired_id := desired_id
|
|
|
|
// Use file name as key
|
|
|
|
if len(desired_id) == 0 {
|
|
|
|
// NOTE(Ed): This should never be used except for laziness so I'll be throwing a warning everytime.
|
2024-02-27 06:32:26 -08:00
|
|
|
log("desired_key not provided, using file name. Give it a proper name!", LogLevel.Warning)
|
2024-02-13 15:50:22 -08:00
|
|
|
// desired_id = cast(FontID) file_name_from_path(path_file)
|
|
|
|
desired_id = file_name_from_path(path_file)
|
2024-02-13 14:16:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
default_size := default_size
|
|
|
|
if default_size == Font_Load_Use_Default_Size {
|
|
|
|
default_size = Font_Default_Point_Size
|
|
|
|
}
|
|
|
|
|
2024-03-10 23:05:18 -07:00
|
|
|
key := cast(u64) crc32( transmute([]byte) desired_id )
|
2024-02-27 06:32:26 -08:00
|
|
|
def, set_error := zpl_hmap_set( & font_cache, key,FontDef {} )
|
|
|
|
verify( set_error == AllocatorError.None, "Failed to add new font entry to cache" )
|
|
|
|
|
2024-02-13 14:16:39 -08:00
|
|
|
def.path_file = path_file
|
2024-03-08 00:34:21 -08:00
|
|
|
// def.data = font_data
|
2024-02-13 14:16:39 -08:00
|
|
|
def.default_size = i32(points_to_pixels(default_size))
|
|
|
|
|
2024-03-07 15:29:49 -08:00
|
|
|
// TODO(Ed): this is slow & eats quite a bit of memory early on. Setup a more on demand load for a specific size.
|
|
|
|
// Also, we need to eventually switch to a SDF shader for rendering
|
|
|
|
|
2024-02-13 14:16:39 -08:00
|
|
|
// Render all sizes at once
|
2024-02-13 23:29:08 -08:00
|
|
|
// Note(Ed) : We only generate textures for even multiples of the font.
|
2024-03-10 23:05:18 -07:00
|
|
|
for font_size : i32 = Font_Size_Interval; font_size <= Font_Largest_Px_Size; font_size += Font_Size_Interval
|
2024-02-13 14:16:39 -08:00
|
|
|
{
|
2024-03-10 23:05:18 -07:00
|
|
|
profile("font size render")
|
|
|
|
id := (font_size / Font_Size_Interval) + (font_size % Font_Size_Interval)
|
2024-02-27 06:32:26 -08:00
|
|
|
|
|
|
|
px_render := & def.size_table[id - 1]
|
2024-02-13 14:16:39 -08:00
|
|
|
using px_render
|
2024-02-27 06:32:26 -08:00
|
|
|
size = font_size
|
2024-02-13 14:16:39 -08:00
|
|
|
count = 95 // This is the default codepoint count from raylib when loading a font.
|
|
|
|
padding = Font_TTF_Default_Chars_Padding
|
|
|
|
glyphs = rl.LoadFontData( raw_data(font_data), font_data_size,
|
|
|
|
fontSize = size,
|
|
|
|
codepoints = nil,
|
|
|
|
codepointCount = count,
|
|
|
|
type = rl.FontType.DEFAULT )
|
2024-02-27 04:50:57 -08:00
|
|
|
verify( glyphs != nil, str_fmt_tmp("Failed to load glyphs for font: %v at desired size: %v", desired_id, size ) )
|
2024-02-13 14:16:39 -08:00
|
|
|
|
|
|
|
atlas := rl.GenImageFontAtlas( glyphs, & recs, count, size, padding, i32(Font_Atlas_Packing_Method.Raylib_Basic) )
|
|
|
|
texture = rl.LoadTextureFromImage( atlas )
|
|
|
|
|
|
|
|
// glyphs_slice := slice_ptr( glyphs, count )
|
|
|
|
// for glyph in glyphs_slice {
|
|
|
|
// TODO(Ed) : See if above can properly reference
|
|
|
|
|
|
|
|
// NOTE(raylib): Update glyphs[i].image to use alpha, required to be used on image_draw_text()
|
|
|
|
for glyph_id : i32 = 0; glyph_id < count; glyph_id += 1 {
|
|
|
|
glyph := & glyphs[glyph_id]
|
|
|
|
|
|
|
|
rl.UnloadImage( glyph.image )
|
|
|
|
glyph.image = rl.ImageFromImage( atlas, recs[glyph_id] )
|
|
|
|
}
|
|
|
|
rl.UnloadImage( atlas )
|
|
|
|
}
|
|
|
|
|
2024-03-08 20:20:49 -08:00
|
|
|
free_all( context.temp_allocator )
|
2024-02-27 06:32:26 -08:00
|
|
|
return { key, desired_id }
|
2024-02-13 14:16:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Font_Use_Default_Size :: f32(0.0)
|
|
|
|
|
2024-03-05 07:15:56 -08:00
|
|
|
to_rl_Font :: proc( id : FontID, size := Font_Use_Default_Size ) -> rl.Font
|
|
|
|
{
|
2024-02-13 14:16:39 -08:00
|
|
|
font_provider_data := & get_state().font_provider_data; using font_provider_data
|
|
|
|
|
2024-03-10 23:05:18 -07:00
|
|
|
even_size := math.round(size * (1.0/f32(Font_Size_Interval))) * f32(Font_Size_Interval)
|
|
|
|
size := clamp( i32( even_size), 4, Font_Largest_Px_Size )
|
2024-02-27 06:32:26 -08:00
|
|
|
def := zpl_hmap_get( & font_cache, id.key )
|
2024-02-13 14:16:39 -08:00
|
|
|
size = size if size != i32(Font_Use_Default_Size) else def.default_size
|
2024-02-27 06:32:26 -08:00
|
|
|
|
2024-03-10 23:05:18 -07:00
|
|
|
id := (size / Font_Size_Interval) + (size % Font_Size_Interval)
|
2024-02-27 06:32:26 -08:00
|
|
|
px_render := & def.size_table[ id - 1 ]
|
2024-02-13 14:16:39 -08:00
|
|
|
|
2024-02-13 23:29:08 -08:00
|
|
|
// This is free for now perf wise... may have to move this out to on a setting change later.
|
2024-03-10 23:05:18 -07:00
|
|
|
if id <= 8 {
|
|
|
|
rl.SetTextureFilter( px_render.texture, rl.TextureFilter.POINT )
|
|
|
|
}
|
|
|
|
else if id <= 14 {
|
|
|
|
rl.SetTextureFilter( px_render.texture, rl.TextureFilter.POINT )
|
|
|
|
}
|
|
|
|
else if id <= 48 {
|
|
|
|
rl.SetTextureFilter( px_render.texture, rl.TextureFilter.POINT )
|
|
|
|
}
|
|
|
|
else if id > 48 {
|
|
|
|
rl.SetTextureFilter( px_render.texture, rl.TextureFilter.POINT )
|
|
|
|
}
|
2024-02-13 15:50:22 -08:00
|
|
|
|
2024-02-13 14:16:39 -08:00
|
|
|
rl_font : rl.Font
|
|
|
|
rl_font.baseSize = px_render.size
|
2024-03-06 13:44:01 -08:00
|
|
|
rl_font.glyphCount = px_render.count
|
|
|
|
rl_font.glyphPadding = px_render.padding
|
2024-02-13 14:16:39 -08:00
|
|
|
rl_font.glyphs = px_render.glyphs
|
|
|
|
rl_font.recs = px_render.recs
|
|
|
|
rl_font.texture = px_render.texture
|
|
|
|
return rl_font
|
|
|
|
}
|