From 2dfdffd6753043cb8097f4c698e5c411ddc9855b Mon Sep 17 00:00:00 2001 From: Arcade Wise Date: Thu, 5 Oct 2023 21:11:31 -0400 Subject: Add changing fonts as an option to text rendering --- virtual-programs/display.folk | 33 ++++++++++++++++++++++++++++----- virtual-programs/label.folk | 9 +++++++-- 2 files changed, 35 insertions(+), 7 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/display.folk b/virtual-programs/display.folk index f7b70959..daa819e9 100644 --- a/virtual-programs/display.folk +++ b/virtual-programs/display.folk @@ -112,7 +112,6 @@ On process { namespace export * namespace ensemble create } - variable font [font load "PTSans-Regular"] variable glyphMsd [Gpu::fn {sampler2D atlas vec4 atlasGlyphBounds vec2 glyphUv} vec4 { vec2 atlasUv = mix(atlasGlyphBounds.xw, atlasGlyphBounds.zy, glyphUv); return texture(atlas, vec2(atlasUv.x, 1.0-atlasUv.y)); @@ -214,8 +213,26 @@ On process { Gpu::draw $circle [list $x $y] $radius $thickness [getColor $color] [ne $filled "false"] } - proc textExtent {text scale} { - variable font + set ::FontCache [dict create] + + # load all fonts into the fontCache + foreach fontPath [list {*}[glob {*}vendor/fonts/*.png]] { + set fontName "" + regexp {vendor/fonts/(.*).png} $fontPath whole_match fontName + if {!($fontName eq "")} { + puts "Loaded $fontName into font cache" + set fontdata [font load $fontName] + dict set ::FontCache $fontName $fontdata + } + } + + proc textExtent {text scale {font "PTSans-Regular"}} { + if {!([dict exists $::FontCache $font])} { + throw {DISPLAY FONT {font doesn't exist}} "$font doesn't exist" + return + } + set font [dict get $::FontCache $font] + set em [* $scale 25.0] set x 0; set y 0 set width 0 @@ -236,8 +253,14 @@ On process { } return [list $width [+ $y $em]] } - proc text {x0 y0 scale text radians} { - variable font + proc text {x0 y0 scale text radians {font ""}} { + if {!([dict exists $::FontCache $font])} { + throw {DISPLAY FONT {font doesn't exist}} "$font doesn't exist" + return + } + set font [dict get $::FontCache $font] + + variable glyph set fontAtlas [font gpuAtlasImage $font] set fontAtlasSize [list [::image width [font atlasImage $font]] \ diff --git a/virtual-programs/label.folk b/virtual-programs/label.folk index ad5e406f..18c6e139 100644 --- a/virtual-programs/label.folk +++ b/virtual-programs/label.folk @@ -4,15 +4,20 @@ When /thing/ has region /region/ { # set height [region height $region] set radians [region angle $region] - When the collected matches for [list /someone/ wishes $thing is labelled /text/] are /matches/ { + When the collected matches for [list /someone/ wishes $thing is labelled /text/ with font /font/] are /matches/ { set text [join [lmap match $matches {dict get $match text}] "\n"] if {$text eq ""} { return } set scale 1 - Display::text $x $y $scale $text $radians + Display::text $x $y $scale $text $radians [dict get $match font] } } +When /someone/ wishes /thing/ is labelled /text/ { + # Set the default font + Wish $thing is labelled $text with font "PTSans-Regular" +} + fn text {coords text angle} { Display::text [lindex $coords 0] [lindex $coords 1] 2 $text $angle } -- cgit v1.2.3 From 35e329acac3246006345ce6efeedc3d7710215cc Mon Sep 17 00:00:00 2001 From: Arcade Wise Date: Thu, 5 Oct 2023 21:13:40 -0400 Subject: oops, forgot to add to the 2nd function --- virtual-programs/display.folk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'virtual-programs') diff --git a/virtual-programs/display.folk b/virtual-programs/display.folk index daa819e9..4bce8a00 100644 --- a/virtual-programs/display.folk +++ b/virtual-programs/display.folk @@ -253,7 +253,7 @@ On process { } return [list $width [+ $y $em]] } - proc text {x0 y0 scale text radians {font ""}} { + proc text {x0 y0 scale text radians {font "PTSans-Regular"}} { if {!([dict exists $::FontCache $font])} { throw {DISPLAY FONT {font doesn't exist}} "$font doesn't exist" return -- cgit v1.2.3 From cc5864480e41322e384d702df0d6814348a25467 Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Thu, 5 Oct 2023 21:42:27 -0400 Subject: display: Catch errors, so we don't hard crash if font doesn't exist --- virtual-programs/display.folk | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'virtual-programs') diff --git a/virtual-programs/display.folk b/virtual-programs/display.folk index 4bce8a00..d7c0f2b1 100644 --- a/virtual-programs/display.folk +++ b/virtual-programs/display.folk @@ -475,7 +475,10 @@ On process { set renderTime [baretime { Display::start - foreach command $displayCommands { {*}$command } + foreach command $displayCommands { + try { {*}$command } \ + on error e { puts stderr $::errorInfo } + } Display::end }] -- cgit v1.2.3 From 5a662a3df4e4a64210065fe3601fe87e63053bce Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Thu, 5 Oct 2023 21:44:40 -0400 Subject: display: Make image cache shrink when more fonts are loaded --- virtual-programs/display.folk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'virtual-programs') diff --git a/virtual-programs/display.folk b/virtual-programs/display.folk index d7c0f2b1..f92afb67 100644 --- a/virtual-programs/display.folk +++ b/virtual-programs/display.folk @@ -359,7 +359,7 @@ On process { return vec4(0.0, 0.0, 0.0, 0.0); }] variable imCache [dict create] - variable IMCACHE_MAX_IMAGES [- $Gpu::ImageManager::GPU_MAX_IMAGES 1] + variable IMCACHE_MAX_IMAGES [- $Gpu::ImageManager::GPU_MAX_IMAGES [dict size $::FontCache]] proc checkImCacheAndCopyIfNeeded {imDrawSet} { variable imCache variable IMCACHE_MAX_IMAGES -- cgit v1.2.3