diff --git a/Readme.md b/Readme.md index ca1b76d..c04ac70 100644 --- a/Readme.md +++ b/Readme.md @@ -37,7 +37,6 @@ Note: freetype and harfbuzz could technically be gutted if the user removes thei ### Additional Features: * Support for freetype (WIP, Untested) -* Support for harfbuzz (WIP, Untested) * Add ability to conditionally compile dependencies (so that the user may not need to resolve those packages). * Related to usage of //+build tags? * Ability to set a draw transform, viewport and projection diff --git a/examples/sokol_demo/sokol_demo.odin b/examples/sokol_demo/sokol_demo.odin index b2dc49a..8c5d8a7 100644 --- a/examples/sokol_demo/sokol_demo.odin +++ b/examples/sokol_demo/sokol_demo.odin @@ -138,7 +138,7 @@ font_firacode : FontID Font_Use_Default_Size :: f32(0.0) -font_provider_resolve_draw_id :: proc( id : FontID, size := Font_Use_Default_Size ) -> ( ve_id : ve.FontID, resolved_size : i32 ) +font_resolve_draw_id :: proc( id : FontID, size := Font_Use_Default_Size ) -> ( ve_id : ve.FontID, resolved_size : i32 ) { def := demo_ctx.font_ids[ id.label ] size := size == 0.0 ? f32(def.default_size) : size @@ -152,14 +152,14 @@ font_provider_resolve_draw_id :: proc( id : FontID, size := Font_Use_Default_Siz measure_text_size :: proc( text : string, font : FontID, font_size := Font_Use_Default_Size, spacing : f32 ) -> Vec2 { - ve_id, size := font_provider_resolve_draw_id( font, font_size ) + ve_id, size := font_resolve_draw_id( font, font_size ) measured := ve.measure_text_size( & demo_ctx.ve_ctx, ve_id, text ) return measured } get_font_vertical_metrics :: #force_inline proc ( font : FontID, font_size := Font_Use_Default_Size ) -> ( ascent, descent, line_gap : f32 ) { - ve_id, size := font_provider_resolve_draw_id( font, font_size ) + ve_id, size := font_resolve_draw_id( font, font_size ) ascent, descent, line_gap = ve.get_font_vertical_metrics( & demo_ctx.ve_ctx, ve_id ) return } @@ -170,7 +170,7 @@ draw_text_string_pos_norm :: proc( content : string, id : FontID, size : f32, po width := demo_ctx.screen_size.x height := demo_ctx.screen_size.y - ve_id, resolved_size := font_provider_resolve_draw_id( id, size ) + ve_id, resolved_size := font_resolve_draw_id( id, size ) color_norm := normalize_rgba8(color) ve.set_colour( & demo_ctx.ve_ctx, color_norm ) @@ -198,7 +198,7 @@ draw_text_zoomed_norm :: proc(content : string, id : FontID, size : f32, pos : V zoom_adjust_size *= OVER_SAMPLE_ZOOM - ve_id, resolved_size := font_provider_resolve_draw_id(id, zoom_adjust_size) + ve_id, resolved_size := font_resolve_draw_id(id, zoom_adjust_size) text_scale := screen_scale { @@ -271,7 +271,7 @@ init :: proc "c" () case .DUMMY: fmt.println(">> using dummy backend") } - ve.startup( & demo_ctx.ve_ctx, .STB_TrueType, allocator = context.allocator, snap_shape_position = false, use_advanced_text_shaper = true ) + ve.startup( & demo_ctx.ve_ctx, .Freetype, allocator = context.allocator, snap_shape_position = false, use_advanced_text_shaper = true ) ve_sokol.setup_gfx_objects( & demo_ctx.render_ctx, & demo_ctx.ve_ctx, vert_cap = 1024 * 1024, index_cap = 1024 * 1024 ) error : mem.Allocator_Error @@ -477,11 +477,13 @@ etiam dignissim diam quis enim. Convallis convallis tellus id interdum.` draw_text_string_pos_norm("Korean", font_print, 19, {0.2, current_scroll - (section_start + 0.92)}, COLOR_WHITE) draw_text_string_pos_norm("그들의 장비와 기구는 모두 살아 있다.", font_demo_korean, 36, {0.3, current_scroll - (section_start + 0.92)}, COLOR_WHITE) - draw_text_string_pos_norm("Arabic", font_print, 19, {0.2, current_scroll - (section_start + 0.96)}, COLOR_WHITE) - draw_text_string_pos_norm("حب السماء لا تمطر غير الأحلام. (This one needs HarfBuzz to work!)", font_demo_arabic, 24, {0.3, current_scroll - (section_start + 0.96)}, COLOR_WHITE) + draw_text_string_pos_norm("Needs harfbuzz to work:", font_print, 14, {0.2, current_scroll - (section_start + 0.96)}, COLOR_WHITE) - draw_text_string_pos_norm("Hebrew", font_print, 19, {0.2, current_scroll - (section_start + 1.0)}, COLOR_WHITE) - draw_text_string_pos_norm("אז הגיע הלילה של כוכב השביט הראשון. (This one needs HarfBuzz to work!)", font_demo_hebrew, 22, {0.3, current_scroll - (section_start + 1.0)}, COLOR_WHITE) + draw_text_string_pos_norm("Arabic", font_print, 19, {0.2, current_scroll - (section_start + 1.00)}, COLOR_WHITE) + draw_text_string_pos_norm("حب السماء لا تمطر غير الأحلام.", font_demo_arabic, 24, {0.3, current_scroll - (section_start + 1.00)}, COLOR_WHITE) + + draw_text_string_pos_norm("Hebrew", font_print, 19, {0.2, current_scroll - (section_start + 1.04)}, COLOR_WHITE) + draw_text_string_pos_norm("אז הגיע הלילה של כוכב השביט הראשון.", font_demo_hebrew, 22, {0.3, current_scroll - (section_start + 1.04)}, COLOR_WHITE) } // Zoom Test @@ -523,7 +525,7 @@ 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_provider_resolve_draw_id( font_firacode, zoom_adjust_size * OVER_SAMPLE_ZOOM ) + ve_id, resolved_size := font_resolve_draw_id( font_firacode, zoom_adjust_size * OVER_SAMPLE_ZOOM ) current_zoom_text := fmt.tprintf("Current Zoom : %.2f x\nCurrent Resolved Size: %v px", current_zoom, resolved_size ) draw_text_string_pos_norm(current_zoom_text, font_firacode, 19, {0.2, zoom_info_y}, COLOR_WHITE) diff --git a/scripts/build_sokol_demo.ps1 b/scripts/build_sokol_demo.ps1 index 3f309fc..6ca8021 100644 --- a/scripts/build_sokol_demo.ps1 +++ b/scripts/build_sokol_demo.ps1 @@ -94,10 +94,10 @@ function build-SokolBackendDemo # $build_args += $flag_micro_architecture_native $build_args += $flag_use_separate_modules $build_args += $flag_thread_count + $CoreCount_Physical - # $build_args += $flag_optimize_none + $build_args += $flag_optimize_none # $build_args += $flag_optimize_minimal # $build_args += $flag_optimize_speed - $build_args += $falg_optimize_aggressive + # $build_args += $falg_optimize_aggressive $build_args += $flag_debug $build_args += $flag_pdb_name + $pdb $build_args += $flag_subsystem + 'windows' diff --git a/vefontcache/parser.odin b/vefontcache/parser.odin index ca63dd8..92a084a 100644 --- a/vefontcache/parser.odin +++ b/vefontcache/parser.odin @@ -56,9 +56,9 @@ ParserContext :: struct { // fonts : HMapChained(ParserFontInfo), } -parser_init :: proc( ctx : ^ParserContext ) +parser_init :: proc( ctx : ^ParserContext, kind : ParserKind ) { - switch ctx.kind + switch kind { case .Freetype: result := freetype.init_free_type( & ctx.ft_library ) @@ -68,6 +68,8 @@ parser_init :: proc( ctx : ^ParserContext ) // Do nothing intentional } + ctx.kind = kind + // error : AllocatorError // ctx.fonts, error = make( HMapChained(ParserFontInfo), 256 ) // assert( error == .None, "VEFontCache.parser_init: Failed to allocate fonts array" ) @@ -99,6 +101,7 @@ parser_load_font :: proc( ctx : ^ParserContext, label : string, data : []byte ) font.label = label font.data = data + font.kind = ctx.kind return } @@ -190,6 +193,19 @@ parser_get_font_vertical_metrics :: #force_inline proc "contextless" ( font : ^P switch font.kind { case .Freetype: + info := font.freetype_info + ascent = i32(info.ascender) + descent = i32(info.descender) + line_gap = i32(info.height) - (ascent - descent) + + // FreeType stores these values in font units, so we need to convert them to pixels + units_per_em := i32(info.units_per_em) + + if units_per_em != 0 { + ascent = (ascent * i32(info.size.metrics.y_ppem)) / units_per_em + descent = (descent * i32(info.size.metrics.y_ppem)) / units_per_em + line_gap = (line_gap * i32(info.size.metrics.y_ppem)) / units_per_em + } case .STB_TrueType: stbtt.GetFontVMetrics( & font.stbtt_info, & ascent, & descent, & line_gap ) diff --git a/vefontcache/vefontcache.odin b/vefontcache/vefontcache.odin index ad2dc31..234349b 100644 --- a/vefontcache/vefontcache.odin +++ b/vefontcache/vefontcache.odin @@ -32,7 +32,6 @@ Entry_Default :: Entry { Context :: struct { backing : Allocator, - parser_kind : ParserKind, parser_ctx : ParserContext, shaper_ctx : ShaperContext, @@ -267,7 +266,7 @@ startup :: proc( ctx : ^Context, parser_kind : ParserKind, assert( error == .None, "VEFontCache.init : Failed to allocate vertices array for clear_draw_list" ) } - parser_init( & parser_ctx ) + parser_init( & parser_ctx, parser_kind ) shaper_init( & shaper_ctx ) } @@ -355,10 +354,7 @@ load_font :: proc( ctx : ^Context, label : string, data : []byte, size_px : f32, used = true parser_info = parser_load_font( & parser_ctx, label, data ) - // assert( parser_info != nil, "VEFontCache.load_font: Failed to load font info from parser" ) - shaper_info = shaper_load_font( & shaper_ctx, label, data, transmute(rawptr) id ) - // assert( shaper_info != nil, "VEFontCache.load_font: Failed to load font from shaper") size = size_px size_scale = size_px < 0.0 ? \