Got text rendering to work with sokol_gfx (learngl text rendering article #1)
Need to todo the altas article next then the optimizing vod
This commit is contained in:
@ -1,10 +1,13 @@
|
||||
/*
|
||||
Yet another port of fontstash.
|
||||
|
||||
I decided t use this instead of the odin port as it already deviated from the original impl.
|
||||
I decided to use this instead of the odin port as it deviated from the original, making it difficult to sift through.
|
||||
So The code was small enough that I mine as well learn it by porting for my use case.
|
||||
|
||||
Original copyright for fonstash.h:
|
||||
TODO(Ed): Add docs here and throughout
|
||||
TODO(Ed): This is unfinished...
|
||||
|
||||
Original author's copyright for fonstash.h:
|
||||
------------------------------------------------------------------------------
|
||||
Copyright (c) 2009-2013 Mikko Mononen memon@inside.org
|
||||
|
||||
@ -27,6 +30,8 @@ package sectr
|
||||
|
||||
import stbtt "vendor:stb/truetype"
|
||||
|
||||
FStash_Use_stb_truetype :: #config(FSTASH_USE_STB_TRUE_TYPE, true)
|
||||
|
||||
Range2_i16 :: struct #raw_union {
|
||||
using pts : Vec2_i16,
|
||||
using xy : struct {
|
||||
@ -36,26 +41,26 @@ Range2_i16 :: struct #raw_union {
|
||||
|
||||
Vec2_i16 :: [2]i16
|
||||
|
||||
FSTASH_Invalid :: -1
|
||||
FStash_Invalid :: -1
|
||||
|
||||
FSTASH_Hash_Lut_Size :: 256
|
||||
FSTASH_Max_Fallbacks :: 20
|
||||
FSTASH_Max_States :: 20
|
||||
FSTASH_Vertex_Count :: 1024
|
||||
FSTASH_Init_Atlas_Nodes :: 256
|
||||
FStash_Hash_Lut_Size :: 256
|
||||
FStash_Max_Fallbacks :: 20
|
||||
FStash_Max_States :: 20
|
||||
FStash_Vertex_Count :: 1024
|
||||
FStash_Init_Atlas_Nodes :: 256
|
||||
|
||||
FSTASH_FontLuts :: [FSTASH_Hash_Lut_Size]i32
|
||||
FSTASH_FontFallbacks :: [FSTASH_Max_Fallbacks]i32
|
||||
FStash_FontLuts :: [FStash_Hash_Lut_Size]i32
|
||||
FStash_FontFallbacks :: [FStash_Max_Fallbacks]i32
|
||||
|
||||
FSTASH_HandleErrorProc :: #type proc( uptr : rawptr, error, val : i32 )
|
||||
FStash_HandleErrorProc :: #type proc( uptr : rawptr, error, val : i32 )
|
||||
|
||||
FSTASH_RenderCreateProc :: #type proc( uptr : rawptr, width, height : i32 )
|
||||
FSTASH_RenderResizeProc :: #type proc( uptr : rawptr, width, height : i32 )
|
||||
FSTASH_RenderUpdateProc :: #type proc( uptr : rawptr, rect : ^i32, data : ^u8 )
|
||||
FSTASH_RenderDrawProc :: #type proc( uptr : rawptr, verts : ^f32, tcoords : ^f32, colors : ^i32, num_verts : i32 )
|
||||
FSTASH_RenderDelete :: #type proc( uptr : rawptr )
|
||||
FStash_RenderCreateProc :: #type proc( uptr : rawptr, width, height : i32 )
|
||||
FStash_RenderResizeProc :: #type proc( uptr : rawptr, width, height : i32 )
|
||||
FStash_RenderUpdateProc :: #type proc( uptr : rawptr, rect : ^i32, data : ^u8 )
|
||||
FStash_RenderDrawProc :: #type proc( uptr : rawptr, verts : ^f32, tcoords : ^f32, colors : ^i32, num_verts : i32 )
|
||||
FStash_RenderDelete :: #type proc( uptr : rawptr )
|
||||
|
||||
FSTASH_AlignFlag :: enum u32 {
|
||||
FStash_AlignFlag :: enum u32 {
|
||||
Left,
|
||||
Center,
|
||||
Right,
|
||||
@ -64,51 +69,42 @@ FSTASH_AlignFlag :: enum u32 {
|
||||
Bottom,
|
||||
Baseline,
|
||||
}
|
||||
FSTASH_AlignFlags :: bit_set[ FSTASH_AlignFlag; u32 ]
|
||||
FStash_AlignFlags :: bit_set[ FStash_AlignFlag; u32 ]
|
||||
|
||||
// FONSflags
|
||||
FSTASH_QuadLocation :: enum u32 {
|
||||
FStash_QuadLocation :: enum u32 {
|
||||
Top_Left = 1,
|
||||
Bottom_Left = 2,
|
||||
}
|
||||
|
||||
FSTASH_Atlas :: struct {
|
||||
FStash_Atlas :: struct {
|
||||
dud : i32,
|
||||
}
|
||||
|
||||
FSTASH_AtlasNode :: struct {
|
||||
FStash_AtlasNode :: struct {
|
||||
x, y, width : i16,
|
||||
}
|
||||
|
||||
FSTASH_ErrorCode :: enum u32 {
|
||||
FStash_ErrorCode :: enum u32 {
|
||||
Atlas_Full,
|
||||
Scratch_Full,
|
||||
States_Overflow,
|
||||
States_Underflow,
|
||||
}
|
||||
|
||||
FSTASH_Quad :: struct {
|
||||
FStash_Quad :: struct {
|
||||
x0, y0, s0, t0 : f32,
|
||||
x1, y1, s1, t1 : f32,
|
||||
}
|
||||
|
||||
FSTASH_Font :: struct {
|
||||
info : stbtt.fontinfo,
|
||||
name : string,
|
||||
data : []byte,
|
||||
free_data : bool,
|
||||
|
||||
ascender : f32,
|
||||
descender : f32,
|
||||
line_height : f32,
|
||||
|
||||
glyphs : Array(FSTASH_Glyph),
|
||||
lut : FSTASH_FontLuts,
|
||||
fallbacks : FSTASH_FontFallbacks,
|
||||
num_fallbacks : i32,
|
||||
when FStash_Use_stb_truetype
|
||||
{
|
||||
FStash_FontParserData :: struct {
|
||||
stbtt_info : stbtt.fontinfo,
|
||||
}
|
||||
}
|
||||
|
||||
FSTASH_Glyph :: struct {
|
||||
FStash_Glyph :: struct {
|
||||
codepoint : rune,
|
||||
index, next : i32,
|
||||
size, blur : i16,
|
||||
@ -117,17 +113,33 @@ FSTASH_Glyph :: struct {
|
||||
offset : Vec2_i16,
|
||||
}
|
||||
|
||||
FSTASH_Params :: struct {
|
||||
width, height : i32,
|
||||
quad_location : FSTASH_QuadLocation, // (flags)
|
||||
render_create : FSTASH_RenderCreateProc,
|
||||
render_resize : FSTASH_RenderResizeProc,
|
||||
render_update : FSTASH_RenderUpdateProc,
|
||||
render_draw : FSTASH_RenderDrawProc,
|
||||
render_delete : FSTASH_RenderDelete,
|
||||
FStash_Font :: struct {
|
||||
parser_data : FStash_FontParserData,
|
||||
name : string,
|
||||
data : []byte,
|
||||
free_data : bool,
|
||||
|
||||
ascender : f32,
|
||||
descender : f32,
|
||||
line_height : f32,
|
||||
|
||||
glyphs : Array(FStash_Glyph),
|
||||
lut : FStash_FontLuts,
|
||||
fallbacks : FStash_FontFallbacks,
|
||||
num_fallbacks : i32,
|
||||
}
|
||||
|
||||
FSTASH_State :: struct {
|
||||
FStash_Params :: struct {
|
||||
width, height : i32,
|
||||
quad_location : FStash_QuadLocation, // (flags)
|
||||
render_create : FStash_RenderCreateProc,
|
||||
render_resize : FStash_RenderResizeProc,
|
||||
render_update : FStash_RenderUpdateProc,
|
||||
render_draw : FStash_RenderDrawProc,
|
||||
render_delete : FStash_RenderDelete,
|
||||
}
|
||||
|
||||
FStash_State :: struct {
|
||||
font : i32,
|
||||
alignment : i32,
|
||||
size : f32,
|
||||
@ -136,14 +148,14 @@ FSTASH_State :: struct {
|
||||
spacing : f32,
|
||||
}
|
||||
|
||||
FSTASH_TextIter :: struct {
|
||||
FStash_TextIter :: struct {
|
||||
x, y : f32,
|
||||
next_x, next_y : f32,
|
||||
scale, spacing : f32,
|
||||
|
||||
isize, iblur : i16,
|
||||
|
||||
font : ^FSTASH_Font,
|
||||
font : ^FStash_Font,
|
||||
prev_glyph_id : i32,
|
||||
|
||||
codepoint : rune,
|
||||
@ -154,28 +166,73 @@ FSTASH_TextIter :: struct {
|
||||
end : string,
|
||||
}
|
||||
|
||||
FSTASH_Context :: struct {
|
||||
params : FSTASH_Params,
|
||||
FStash_Context :: struct {
|
||||
params : FStash_Params,
|
||||
|
||||
// Atlas
|
||||
atlas : Array(FSTASH_AtlasNode),
|
||||
atlas : Array(FStash_AtlasNode),
|
||||
texture_data : []byte,
|
||||
width, height : i32,
|
||||
// ----
|
||||
|
||||
normalized_size : Vec2,
|
||||
|
||||
verts : [FSTASH_Vertex_Count * 2]f32,
|
||||
tcoords : [FSTASH_Vertex_Count * 2]f32,
|
||||
colors : [FSTASH_Vertex_Count ]f32,
|
||||
verts : [FStash_Vertex_Count * 2]f32,
|
||||
tcoords : [FStash_Vertex_Count * 2]f32,
|
||||
colors : [FStash_Vertex_Count ]f32,
|
||||
|
||||
states : [FSTASH_Max_States]FSTASH_State,
|
||||
states : [FStash_Max_States]FStash_State,
|
||||
num_states : i32,
|
||||
|
||||
handle_error : FSTASH_HandleErrorProc,
|
||||
handle_error : FStash_HandleErrorProc,
|
||||
error_uptr : rawptr,
|
||||
}
|
||||
|
||||
when FStash_Use_stb_truetype
|
||||
{
|
||||
fstash_tt_init :: proc( ctx : ^FStash_Context ) -> i32 { return 1 }
|
||||
|
||||
fstash_tt_load_font :: proc( ctx : ^FStash_Context, parser_data : ^FStash_FontParserData, data : []byte ) -> b32
|
||||
{
|
||||
parser_data.stbtt_info.userdata = ctx
|
||||
stb_error := stbtt.InitFont( & parser_data.stbtt_info, & data[0], 0 )
|
||||
return stb_error
|
||||
}
|
||||
|
||||
fstash_tt_get_font_metrics :: proc( parser_data : ^FStash_FontParserData, ascent, descent, line_gap : ^i32 ) {
|
||||
stbtt.GetFontVMetrics( & parser_data.stbtt_info, ascent, descent, line_gap )
|
||||
}
|
||||
|
||||
fstash_tt_get_pixel_height_scale :: proc( parser_data : ^FStash_FontParserData, size : f32 ) -> f32
|
||||
{
|
||||
return stbtt.ScaleForPixelHeight( & parser_data.stbtt_info, size )
|
||||
}
|
||||
|
||||
fstash_tt_get_glyph_index :: proc( parser_data : ^FStash_FontParserData, codepoint : rune ) -> i32
|
||||
{
|
||||
return stbtt.FindGlyphIndex( & parser_data.stbtt_info, codepoint )
|
||||
}
|
||||
|
||||
fstash_tt_build_glyph_bitmap :: proc( parser_data : ^FStash_FontParserData, glyph_index : i32,
|
||||
size, scale : f32, advance, left_side_bearing, x0, y0, x1, y1 : ^i32 ) -> i32
|
||||
{
|
||||
stbtt.GetGlyphHMetrics( & parser_data.stbtt_info, glyph_index, advance, left_side_bearing )
|
||||
stbtt.GetGlyphBitmapBox( & parser_data.stbtt_info, glyph_index, scale, scale, x0, y0, x1, y1 )
|
||||
return 1
|
||||
}
|
||||
|
||||
fstash_tt_render_glyph_bitmap :: proc( parser_data : ^FStash_FontParserData, output : [^]byte,
|
||||
out_width, out_height, out_stride : i32, scale_x, scale_y : f32, glyph_index : i32 )
|
||||
{
|
||||
stbtt.MakeGlyphBitmap( & parser_data.stbtt_info, output, out_width, out_height, out_stride, scale_x, scale_y, glyph_index )
|
||||
}
|
||||
|
||||
fstash_tt_get_glyph_kern_advance :: proc( parser_data : ^FStash_FontParserData, glyph_1, glyph_2 : i32 ) -> i32
|
||||
{
|
||||
return stbtt.GetGlyphKernAdvance( & parser_data.stbtt_info, glyph_1, glyph_2 )
|
||||
}
|
||||
} // when FStash_Use_stb_true-type
|
||||
|
||||
fstash_decode_utf8 :: proc( state : ^rune, codepoint : ^rune, to_decode : byte ) -> bool
|
||||
{
|
||||
UTF8_Accept :: 0
|
||||
@ -214,12 +271,12 @@ fstash_decode_utf8 :: proc( state : ^rune, codepoint : ^rune, to_decode : byte )
|
||||
return (state^) == UTF8_Accept
|
||||
}
|
||||
|
||||
fstash_atlas_delete :: proc ( ctx : ^FSTASH_Context ) {
|
||||
fstash_atlas_delete :: proc ( ctx : ^FStash_Context ) {
|
||||
using ctx
|
||||
array_free( ctx.atlas )
|
||||
}
|
||||
|
||||
fstash_atlas_expand :: proc( ctx : ^FSTASH_Context, width, height : i32 )
|
||||
fstash_atlas_expand :: proc( ctx : ^FStash_Context, width, height : i32 )
|
||||
{
|
||||
if width > ctx.width {
|
||||
fstash_atlas_insert( ctx, ctx.atlas.num, ctx.width, 0, width - ctx.width )
|
||||
@ -229,39 +286,39 @@ fstash_atlas_expand :: proc( ctx : ^FSTASH_Context, width, height : i32 )
|
||||
ctx.height = height
|
||||
}
|
||||
|
||||
fstash_atlas_init :: proc( ctx : ^FSTASH_Context, width, height : i32, num_nodes : u32 = FSTASH_Init_Atlas_Nodes )
|
||||
fstash_atlas_init :: proc( ctx : ^FStash_Context, width, height : i32, num_nodes : u32 = FStash_Init_Atlas_Nodes )
|
||||
{
|
||||
error : AllocatorError
|
||||
ctx.atlas, error = array_init_reserve( FSTASH_AtlasNode, context.allocator, u64(num_nodes), dbg_name = "font atlas" )
|
||||
ctx.atlas, error = array_init_reserve( FStash_AtlasNode, context.allocator, u64(num_nodes), dbg_name = "font atlas" )
|
||||
ensure(error != AllocatorError.None, "Failed to allocate font atlas")
|
||||
|
||||
ctx.width = width
|
||||
ctx.height = height
|
||||
|
||||
array_append( & ctx.atlas, FSTASH_AtlasNode{ width = i16(width)} )
|
||||
array_append( & ctx.atlas, FStash_AtlasNode{ width = i16(width)} )
|
||||
}
|
||||
|
||||
fstash_atlas_insert :: proc( ctx : ^FSTASH_Context, id : u64, x, y, width : i32 ) -> (error : AllocatorError)
|
||||
fstash_atlas_insert :: proc( ctx : ^FStash_Context, id : u64, x, y, width : i32 ) -> (error : AllocatorError)
|
||||
{
|
||||
error = array_append_at( & ctx.atlas, FSTASH_AtlasNode{ i16(x), i16(y), i16(width) }, id )
|
||||
error = array_append_at( & ctx.atlas, FStash_AtlasNode{ i16(x), i16(y), i16(width) }, id )
|
||||
return
|
||||
}
|
||||
|
||||
fstash_atlas_remove :: proc( ctx : ^FSTASH_Context, id : u64 )
|
||||
fstash_atlas_remove :: proc( ctx : ^FStash_Context, id : u64 )
|
||||
{
|
||||
array_remove_at( ctx.atlas, id )
|
||||
}
|
||||
|
||||
fstash_atlas_reset :: proc( ctx : ^FSTASH_Context, width, height : i32 )
|
||||
fstash_atlas_reset :: proc( ctx : ^FStash_Context, width, height : i32 )
|
||||
{
|
||||
ctx.width = width
|
||||
ctx.height = height
|
||||
array_clear( ctx.atlas )
|
||||
|
||||
array_append( & ctx.atlas, FSTASH_AtlasNode{ width = i16(width)} )
|
||||
array_append( & ctx.atlas, FStash_AtlasNode{ width = i16(width)} )
|
||||
}
|
||||
|
||||
fstash_atlas_add_skyline_level :: proc (ctx : ^FSTASH_Context, id : u64, x, y, width, height : i32 ) -> (error : AllocatorError)
|
||||
fstash_atlas_add_skyline_level :: proc (ctx : ^FStash_Context, id : u64, x, y, width, height : i32 ) -> (error : AllocatorError)
|
||||
{
|
||||
insert :: fstash_atlas_insert
|
||||
remove :: fstash_atlas_remove
|
||||
@ -289,7 +346,6 @@ fstash_atlas_add_skyline_level :: proc (ctx : ^FSTASH_Context, id : u64, x, y, w
|
||||
sky_id -= 1
|
||||
}
|
||||
|
||||
|
||||
// Merge same height skyline segments that are next to each other.
|
||||
for sky_id := id; sky_id < ctx.atlas.num - 1;
|
||||
{
|
||||
@ -307,7 +363,7 @@ fstash_atlas_add_skyline_level :: proc (ctx : ^FSTASH_Context, id : u64, x, y, w
|
||||
return
|
||||
}
|
||||
|
||||
fstash_atlas_rect_fits :: proc( ctx : ^FSTASH_Context, location, width, height : i32 ) -> (max_height : i32)
|
||||
fstash_atlas_rect_fits :: proc( ctx : ^FStash_Context, location, width, height : i32 ) -> (max_height : i32)
|
||||
{
|
||||
// Checks if there is enough space at the location of skyline span 'i',
|
||||
// and return the max height of all skyline spans under that at that location,
|
||||
@ -347,7 +403,7 @@ fstash_atlas_rect_fits :: proc( ctx : ^FSTASH_Context, location, width, height :
|
||||
return
|
||||
}
|
||||
|
||||
fstash_atlas_add_rect :: proc( ctx : ^FSTASH_Context, )
|
||||
fstash_atlas_add_rect :: proc( ctx : ^FStash_Context, )
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,12 @@
|
||||
package sectr
|
||||
|
||||
import "core:os"
|
||||
import "core:strings"
|
||||
import "core:unicode"
|
||||
import sokol_gfx "thirdparty:sokol/gfx"
|
||||
import "thirdparty:freetype"
|
||||
|
||||
Font_Provider_Use_Freetype :: true
|
||||
|
||||
Font_Largest_Px_Size :: 32
|
||||
|
||||
@ -29,16 +35,41 @@ FontTag :: struct {
|
||||
point_size : f32
|
||||
}
|
||||
|
||||
FontDef :: struct {
|
||||
path_file : string,
|
||||
when Font_Provider_Use_Freetype
|
||||
{
|
||||
FontParserFontData :: struct {
|
||||
using face : freetype.Face,
|
||||
}
|
||||
FontParserData :: struct {
|
||||
lib : freetype.Library,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
FontGlyph :: struct {
|
||||
size : Vec2i,
|
||||
bearing : Vec2i,
|
||||
advance : u32,
|
||||
texture : sokol_gfx.Image,
|
||||
bindings : sokol_gfx.Bindings,
|
||||
}
|
||||
|
||||
FontDef :: struct {
|
||||
path_file : string,
|
||||
parser_info : FontParserFontData,
|
||||
glyphs : [256]FontGlyph,
|
||||
}
|
||||
|
||||
FontProviderData :: struct {
|
||||
font_cache : HMapChainedPtr(FontDef),
|
||||
font_cache : HMapChainedPtr(FontDef),
|
||||
parser : FontParserData,
|
||||
gfx_bindings : sokol_gfx.Bindings,
|
||||
gfx_pipeline : sokol_gfx.Pipeline,
|
||||
gfx_vbuffer : sokol_gfx.Buffer,
|
||||
gfx_sampler : sokol_gfx.Sampler,
|
||||
}
|
||||
|
||||
Font_Provider_Ggfx_Buffer_Size :: 6 * 4 * size_of(f32) * Kilobyte * 32
|
||||
|
||||
font_provider_startup :: proc()
|
||||
{
|
||||
profile(#procedure)
|
||||
@ -50,6 +81,76 @@ font_provider_startup :: proc()
|
||||
verify( font_cache_alloc_error == AllocatorError.None, "Failed to allocate font_cache" )
|
||||
|
||||
log("font_cache created")
|
||||
|
||||
when Font_Provider_Use_Freetype
|
||||
{
|
||||
result := freetype.init_free_type( & font_provider_data.parser.lib )
|
||||
if result != freetype.Error.Ok {
|
||||
fatal( "font_provider_setup: Failed to initialize freetype" )
|
||||
}
|
||||
}
|
||||
|
||||
// Setup Graphics Pipeline
|
||||
{
|
||||
using font_provider_data
|
||||
backend := sokol_gfx.query_backend()
|
||||
learngl_font_glyph_shader := sokol_gfx.make_shader(learngl_font_glyph_shader_desc(backend))
|
||||
|
||||
gfx_vbuffer = sokol_gfx.make_buffer( sokol_gfx.Buffer_Desc {
|
||||
size = Font_Provider_Ggfx_Buffer_Size, // (6 verts, 4 f32 each) * 32 kilos
|
||||
usage = sokol_gfx.Usage.DYNAMIC,
|
||||
type = sokol_gfx.Buffer_Type.VERTEXBUFFER,
|
||||
})
|
||||
|
||||
gfx_sampler = sokol_gfx.make_sampler( sokol_gfx.Sampler_Desc {
|
||||
min_filter = sokol_gfx.Filter.LINEAR,
|
||||
mag_filter = sokol_gfx.Filter.LINEAR,
|
||||
mipmap_filter = sokol_gfx.Filter.NONE,
|
||||
wrap_u = sokol_gfx.Wrap.CLAMP_TO_EDGE,
|
||||
wrap_v = sokol_gfx.Wrap.CLAMP_TO_EDGE,
|
||||
// min_lod = 1.0,
|
||||
// max_lod = 1.0,
|
||||
border_color = sokol_gfx.Border_Color.OPAQUE_BLACK,
|
||||
})
|
||||
|
||||
glyph_vs_layout : sokol_gfx.Vertex_Layout_State
|
||||
glyph_vs_layout.attrs[ATTR_glyph_vs_vertex] = sokol_gfx.Vertex_Attr_State {
|
||||
format = sokol_gfx.Vertex_Format.FLOAT4,
|
||||
offset = 0,
|
||||
}
|
||||
glyph_vs_layout.buffers[0] = sokol_gfx.Vertex_Buffer_Layout_State {
|
||||
stride = size_of(f32) * 4, // Total stride ( pos2 + tex2 )
|
||||
step_func = sokol_gfx.Vertex_Step.PER_VERTEX,
|
||||
}
|
||||
|
||||
gfx_pipeline = sokol_gfx.make_pipeline(
|
||||
{
|
||||
shader = learngl_font_glyph_shader,
|
||||
layout = glyph_vs_layout,
|
||||
colors ={
|
||||
0 = sokol_gfx.Color_Target_State \
|
||||
{
|
||||
// pixel_format = sokol_gfx.Pixel_Format.R8,
|
||||
// write_mask = sokol_gfx.Color_Mask.R,
|
||||
blend = sokol_gfx.Blend_State {
|
||||
enabled = true,
|
||||
src_factor_rgb = sokol_gfx.Blend_Factor.SRC_ALPHA,
|
||||
dst_factor_rgb = sokol_gfx.Blend_Factor.ONE_MINUS_SRC_ALPHA,
|
||||
// op_rgb = sokol_gfx.Blend_Op.ADD,
|
||||
|
||||
src_factor_alpha= sokol_gfx.Blend_Factor.ONE,
|
||||
dst_factor_alpha = sokol_gfx.Blend_Factor.ZERO,
|
||||
// src_factor_alpha = sokol_gfx.Blend_Factor.ONE,
|
||||
// dst_factor_alpha = sokol_gfx.Blend_Factor.ZERO,
|
||||
// op_alpha = sokol_gfx.Blend_Op.DEFAULT,
|
||||
}
|
||||
},
|
||||
},
|
||||
color_count = 1,
|
||||
sample_count = 1,
|
||||
})
|
||||
}
|
||||
|
||||
log("font_provider initialized")
|
||||
}
|
||||
|
||||
@ -103,10 +204,70 @@ font_load :: proc(path_file : string,
|
||||
def, set_error := hmap_chained_set(font_cache, key, FontDef{})
|
||||
verify( set_error == AllocatorError.None, "Failed to add new font entry to cache" )
|
||||
|
||||
def.path_file = path_file
|
||||
def.path_file = path_file
|
||||
// def.default_size = i32(points_to_pixels(default_size))
|
||||
|
||||
face_index :: 0
|
||||
freetype.new_memory_face( font_provider_data.parser.lib, raw_data(font_data), cast(i32) len(font_data), face_index, & def.parser_info.face )
|
||||
|
||||
// Hardcoding to 24 pt for testing (until we have a proper cached atlas)
|
||||
freetype.set_pixel_sizes( def.parser_info.face, 0, 72 )
|
||||
|
||||
return {}
|
||||
for ascii_code in 0 ..< 128 {
|
||||
load_error := freetype.load_char(def.parser_info.face, u32(ascii_code), {freetype.Load_Flag.Render})
|
||||
verify( load_error == .Ok, "Failed to load character using freetype" )
|
||||
|
||||
using def.parser_info
|
||||
|
||||
codepoint := rune(ascii_code)
|
||||
|
||||
if ! unicode.is_print(codepoint) || face.glyph.bitmap.width <= 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
glyph_data : sokol_gfx.Image_Data
|
||||
glyph_data.subimage[0][0] = sokol_gfx.Range {
|
||||
face.glyph.bitmap.buffer,
|
||||
u64(face.glyph.bitmap.width * face.glyph.bitmap.rows)
|
||||
}
|
||||
desc := sokol_gfx.Image_Desc {
|
||||
type = sokol_gfx.Image_Type._2D,
|
||||
render_target = false,
|
||||
width = i32(face.glyph.bitmap.width),
|
||||
height = i32(face.glyph.bitmap.rows),
|
||||
num_slices = 1,
|
||||
num_mipmaps = 1,
|
||||
usage = sokol_gfx.Usage.IMMUTABLE,
|
||||
pixel_format = sokol_gfx.Pixel_Format.R8,
|
||||
sample_count = 0,
|
||||
data = glyph_data,
|
||||
label = strings.clone_to_cstring(str_fmt("font_ascii %v", ascii_code))
|
||||
}
|
||||
width := i32(face.glyph.bitmap.width)
|
||||
rows := i32(face.glyph.bitmap.rows)
|
||||
logf("font_ascii : %v", ascii_code )
|
||||
logf("font_ascii glyph: %v", rune(ascii_code) )
|
||||
rhi_img := sokol_gfx.make_image( desc )
|
||||
verify( sokol_gfx.query_image_state(rhi_img) != sokol_gfx.Resource_State.INVALID,
|
||||
"Failed to create image on sokol gfx" );
|
||||
|
||||
def_bindings := sokol_gfx.Bindings {
|
||||
vertex_buffers = { ATTR_glyph_vs_vertex = gfx_vbuffer, },
|
||||
fs = {
|
||||
images = { SLOT_glyph_bitmap = rhi_img, },
|
||||
samplers = { SLOT_glyph_bitmap_sampler = gfx_sampler }
|
||||
},
|
||||
}
|
||||
|
||||
def.glyphs[ascii_code] = FontGlyph {
|
||||
size = { i32(face.glyph.bitmap.width), i32(face.glyph.bitmap.rows) },
|
||||
bearing = { face.glyph.bitmap_left, face.glyph.bitmap_top },
|
||||
texture = rhi_img,
|
||||
bindings = def_bindings,
|
||||
advance = u32(face.glyph.advance.x),
|
||||
}
|
||||
}
|
||||
|
||||
fid := FontID { key, desired_id }
|
||||
return fid
|
||||
}
|
||||
|
Reference in New Issue
Block a user