Made sure alpha sharpen is only applied when the color alpha is at or above 1.0

This commit is contained in:
2025-01-11 08:47:11 -05:00
parent d56e1d608c
commit dbe97a7176
4 changed files with 85 additions and 41 deletions

View File

@@ -18,7 +18,7 @@ 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)
* Snap-positining to view for better hinting
* Snap-positioning 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.
* Can hand thousands of draw text calls with very large or small shapes.

View File

@@ -1,25 +1,6 @@
# Interface
TODO: OUTDATED
Notes
---
The freetype setup is not finished. Specifically due to cache_glyph_freetype not parsing the glyph outline data structure properly.
Freetype supports specifying a FT_Memory handle which is a pointer to a FT_MemoryRect. This can be used to define an allocator for the parser. Currently this library does not wrap this interface (yet). If using freetype its recommend to update `parser_init` with the necessary changes to wrap the context's backing allocator for freetype to utilize.
```c
struct FT_MemoryRec_
{
void* user;
FT_Alloc_Func alloc;
FT_Free_Func free;
FT_Realloc_Func realloc;
};
```
This library (seems) to perform best if the text commands are fed in 'whitespace aware chunks', where instead of feeding it entire blobs of text, the user identfies "words" in the text and feeding the visible and whitespce chunks derived from this to draw_text as separate calls. It improves the caching of the text shapes. The downside is there has to be a time where the text is parsed into tokens beforehand so that the this iteration does not have to occur continously.
## Lifetime
### startup
@@ -33,21 +14,72 @@ Much of the data structures within the context struct are not fixed-capacity all
The library supports being used in a dynamically loaded module. If its hot-reloaded simply make sure to call this procedure with a reference to the backing allocator provided during startup as all dynamic containers tend to lose a proper reference to the allocator's procedure.
Call clear_atlas_region_caches & clear_shape_cache to reset the library's shape and glyph cache state if doing tuning of the library.
Call clear_atlas_region_caches & clear_shape_cache to reset the library's shape and glyph cache state to force a re-render.
### shutdown
Release resources from the context.
### load_font
Will load an instance of a font. The user needs to load the file's bytes themselves, the font entry (Entry :: struct) will by tracked by the library. The user will be given a font_id which is a direct index for the entry in the tracked array.
### unload_font
Will free an entry, (parser and shaper resources also freed)
## Scoping Context interface
These are a set of push & pop pairs of functions that operator ont he context's stack containers. They are used with the draw_shape and draw_text procedures. This mainly for quick scratch usage where the user wants to directly compose a large amount of text without having a UI framework directly handle the text backend.
* font
* font_size
* colour: Linear colour.
* view: Width and height of the 2D area the text will be drawn within.
* position: Uses relative positioning will offset the incoming position by the given amount.
* scale: Uses relative scaling, will scale the procedures incoming scale by the given amount.
* zoom: Affects scaling, will scale the procedure's incoming font size & scale based on an *UX canvas camera's* notion of it.
Procedure types:
* `scope_<stack_option>`: push with a defer pop
* `push_<stack_option>`
* `pop_<stack_option>`
## Miscellaneous
Stuff used by the draw list generation interface or just getters and setters.
### get_cursor_pos
Will provide the current cursor_pos for the resulting text drawn.
### get_normalized_position_scale
Will normalize the value of the position and scale based on the provided view.
Position will also be snapped to the nearest pixel via ceil.
Does nothing if view is 1 or 0
This is used by draw via view relative space procedures to normalize it to the intended space for the render pass.
## resolve_draw_px_size
Used to constrain the px_size used in draw calls.
The view relative space and scoping stack-based procedures support zoom. When utilizing zoom their is a nasty jitter that will occur if the user smoothly goes across different font sizes because the spacing can drastically change between even and odd font-sizes. This is applied to enforce the font sticks to a specific interval.
For the provided procedures that utilize it, they reference the context's zoom_px_interval. It can be set with `set_zoom_px_interval` and the default value is 2.
## resolve_zoom_size_scale
### configure_snap
You'll find this used immediately in draw_text it acts as a way to snap the position of the text to the nearest pixel for the width and height specified.
If snapping is not desired, set the snap_width and height before calling draw_text to 0.
### get_cursor_pos
Will provide the current cursor_pos for the resulting text drawn.
### set_color

View File

@@ -235,8 +235,8 @@ init :: proc "c" ()
ve.startup( & demo_ctx.ve_ctx, .STB_TrueType, allocator = context.allocator,
glyph_draw_params = glyph_draw_opts,
shaper_params = shaper_opts,
px_scalar = 1.8,
alpha_sharpen = 0.05,
px_scalar = 1.6,
alpha_sharpen = 0.4,
)
ve_sokol.setup_gfx_objects( & demo_ctx.render_ctx, & demo_ctx.ve_ctx, vert_cap = 256 * 1024, index_cap = 512 * 1024 )

View File

@@ -697,7 +697,7 @@ auto_pop_vpz :: #force_inline proc( ctx : ^Context, camera : VPZ_Transform ) {
//#endregion("scoping")
//#region("draw_list generation")
//#region("misc")
get_cursor_pos :: #force_inline proc "contextless" ( ctx : Context ) -> Vec2 { return ctx.cursor_pos }
@@ -759,6 +759,10 @@ set_snap_glyph_render_height :: #force_inline proc( ctx : ^Context, should_snap
ctx.glyph_buffer.snap_glyph_height = cast(f32) i32(should_snap)
}
//#endreigon("misc")
//#region("draw_list generation")
/* The most fundamental interface-level draw shape procedure.
Context's stack is not used. Only modifications for alpha sharpen and px_scalar are applied.
view, position, and scale are expected to be in unsigned normalized space:
@@ -799,8 +803,9 @@ draw_text_shape_normalized_space :: #force_inline proc( ctx : ^Context,
entry := ctx.entries[ font ]
adjusted_colour := colour
adjusted_colour.a += ctx.alpha_sharpen
should_alpha_sharpen := cast(f32) cast(i32) (colour.a >= 1.0)
adjusted_colour := colour
adjusted_colour.a += ctx.alpha_sharpen * should_alpha_sharpen
target_px_size := px_size * ctx.px_scalar
target_scale := scale * (1 / ctx.px_scalar)
@@ -860,8 +865,9 @@ draw_text_normalized_space :: proc( ctx : ^Context,
ctx.cursor_pos = {}
entry := ctx.entries[ font ]
adjusted_colour := colour
adjusted_colour.a += ctx.alpha_sharpen
should_alpha_sharpen := cast(f32) cast(i32) (colour.a >= 1.0)
adjusted_colour := colour
adjusted_colour.a += ctx.alpha_sharpen * should_alpha_sharpen
// Does nothing when px_scalar is 1.0
target_px_size := px_size * ctx.px_scalar
@@ -930,8 +936,9 @@ draw_text_shape_view_space :: #force_inline proc( ctx : ^Context,
entry := ctx.entries[ font ]
adjusted_colour := colour
adjusted_colour.a = 1.0 + ctx.alpha_sharpen
should_alpha_sharpen := cast(f32) cast(i32) (colour.a >= 1.0)
adjusted_colour := colour
adjusted_colour.a += ctx.alpha_sharpen * should_alpha_sharpen
resolved_size, zoom_scale := resolve_zoom_size_scale( zoom, px_size, scale, ctx.zoom_px_interval, 2, 999.0, view )
target_position, norm_scale := get_normalized_position_scale( position, zoom_scale, view )
@@ -998,8 +1005,9 @@ draw_text_view_space :: proc(ctx : ^Context,
ctx.cursor_pos = {}
entry := ctx.entries[ font ]
adjusted_colour := colour
adjusted_colour.a += ctx.alpha_sharpen
should_alpha_sharpen := cast(f32) cast(i32) (colour.a >= 1.0)
adjusted_colour := colour
adjusted_colour.a += ctx.alpha_sharpen * should_alpha_sharpen
resolved_size, zoom_scale := resolve_zoom_size_scale( zoom, px_size, scale, ctx.zoom_px_interval, 2, 999.0, view )
target_position, norm_scale := get_normalized_position_scale( position, zoom_scale, view )
@@ -1074,8 +1082,10 @@ draw_shape :: proc( ctx : ^Context, position, scale : Vec2, shape : Shaped_Text
ctx.cursor_pos = {}
entry := ctx.entries[ font ]
adjusted_colour := peek(stack.colour)
adjusted_colour.a += ctx.alpha_sharpen
colour := peek(stack.colour)
should_alpha_sharpen := cast(f32) cast(i32) (colour.a >= 1.0)
adjusted_colour := colour
adjusted_colour.a += ctx.alpha_sharpen * should_alpha_sharpen
px_size := peek(stack.font_size)
zoom := peek(stack.zoom)
@@ -1152,8 +1162,10 @@ draw_text :: proc( ctx : ^Context, position, scale : Vec2, text_utf8 : string,
ctx.cursor_pos = {}
entry := ctx.entries[ font ]
adjusted_colour := peek(stack.colour)
adjusted_colour.a += ctx.alpha_sharpen
colour := peek(stack.colour)
should_alpha_sharpen := cast(f32) cast(i32) (colour.a >= 1.0)
adjusted_colour := colour
adjusted_colour.a += ctx.alpha_sharpen * should_alpha_sharpen
px_size := peek(stack.font_size)
zoom := peek(stack.zoom)