From 8dd6b9ff3321e539f5c7e8d118b1f0e2fd204d34 Mon Sep 17 00:00:00 2001 From: Naveen Michaud-Agrawal Date: Thu, 5 Oct 2023 23:31:03 +0000 Subject: Support other objects having programs --- virtual-programs/programs.folk | 22 ++++++++++++++++++++++ virtual-programs/tags-and-calibration.folk | 23 +---------------------- 2 files changed, 23 insertions(+), 22 deletions(-) create mode 100644 virtual-programs/programs.folk (limited to 'virtual-programs') diff --git a/virtual-programs/programs.folk b/virtual-programs/programs.folk new file mode 100644 index 00000000..f42e1360 --- /dev/null +++ b/virtual-programs/programs.folk @@ -0,0 +1,22 @@ + + +When (non-capturing) /type/ /obj/ has a program { + puts "Added $type $obj" + On unmatch { puts "Removed $type $obj" } + + try { + set tempPath "$::env(HOME)/folk-printed-programs/$obj.folk.temp" + + if {[file exists $tempPath]} { + set path [open "$::env(HOME)/folk-printed-programs/$obj.folk.temp" r] + } else { + set path [open "$::env(HOME)/folk-printed-programs/$obj.folk" r] + } + set code [read $path] + close $path + + Claim $obj has program code $code + } on error error { + puts stderr "No code for $type $obj" + } +} diff --git a/virtual-programs/tags-and-calibration.folk b/virtual-programs/tags-and-calibration.folk index b219b541..9ad63a07 100644 --- a/virtual-programs/tags-and-calibration.folk +++ b/virtual-programs/tags-and-calibration.folk @@ -114,28 +114,6 @@ When camera /camera/ has width /cameraWidth/ height /cameraHeight/ { } } -When (non-capturing) tag /tag/ has center /c/ size /size/ { - Claim tag $tag is a tag -} - -When (non-capturing) tag /tag/ is a tag { - puts "Added tag $tag" - On unmatch { puts "Removed tag $tag" } - - set tempPath "$::env(HOME)/folk-printed-programs/$tag.folk.temp" - - if {[file exists $tempPath]} { - set path [open "$::env(HOME)/folk-printed-programs/$tag.folk.temp" r] - } else { - set path [open "$::env(HOME)/folk-printed-programs/$tag.folk" r] - } - set code [read $path] - close $path - - # Wish $tag is outlined green - Claim $tag has program code $code -} - When (non-capturing) tag /tag/ has corners /corners/ { set tagCorners [lmap p $corners {::cameraToProjector $p}] @@ -156,4 +134,5 @@ When (non-capturing) tag /tag/ has corners /corners/ { set region [region create $corners $edges $angle] Claim $tag has region $region + Claim tag $tag has a program } -- cgit v1.2.3 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 From 58111948f9656591852f3e38b09630cf4b8a38df Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Fri, 6 Oct 2023 12:09:05 -0400 Subject: images: Fix saving images with stride --- virtual-programs/images.folk | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/images.folk b/virtual-programs/images.folk index bfa5daef..e748cd11 100644 --- a/virtual-programs/images.folk +++ b/virtual-programs/images.folk @@ -48,7 +48,7 @@ namespace eval ::image { #include void - jpeg(FILE* dest, uint8_t* data, uint32_t components, uint32_t width, uint32_t height, int quality) + jpeg(FILE* dest, uint8_t* data, uint32_t components, uint32_t bytesPerRow, uint32_t width, uint32_t height, int quality) { JSAMPARRAY image; if (components == 1) { @@ -56,9 +56,9 @@ namespace eval ::image { for (size_t i = 0; i < height; i++) { image[i] = calloc(width * 3, sizeof (JSAMPLE)); for (size_t j = 0; j < width; j++) { - image[i][j * 3 + 0] = data[(i * width + j)]; - image[i][j * 3 + 1] = data[(i * width + j)]; - image[i][j * 3 + 2] = data[(i * width + j)]; + image[i][j * 3 + 0] = data[(i*bytesPerRow + j)]; + image[i][j * 3 + 1] = data[(i*bytesPerRow + j)]; + image[i][j * 3 + 2] = data[(i*bytesPerRow + j)]; } } } else if (components == 3) { @@ -66,9 +66,9 @@ namespace eval ::image { for (size_t i = 0; i < height; i++) { image[i] = calloc(width * 3, sizeof (JSAMPLE)); for (size_t j = 0; j < width; j++) { - image[i][j * 3 + 0] = data[(i * width + j) * 3]; - image[i][j * 3 + 1] = data[(i * width + j) * 3 + 1]; - image[i][j * 3 + 2] = data[(i * width + j) * 3 + 2]; + image[i][j * 3 + 0] = data[i*bytesPerRow + j*3]; + image[i][j * 3 + 1] = data[i*bytesPerRow + j*3 + 1]; + image[i][j * 3 + 2] = data[i*bytesPerRow + j*3 + 2]; } } } else { exit(1); } @@ -96,7 +96,7 @@ namespace eval ::image { free(image); } - void png(FILE* dest, uint8_t* data, uint32_t components, uint32_t width, uint32_t height) { + void png(FILE* dest, uint8_t* data, uint32_t components, uint32_t bytesPerRow, uint32_t width, uint32_t height) { png_structp png_w = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_infop info_w = png_create_info_struct(png_w); @@ -104,14 +104,15 @@ namespace eval ::image { png_set_IHDR(png_w, info_w, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); - else + else if (components == 1) png_set_IHDR(png_w, info_w, width, height, 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); + else exit(1); png_bytep* row_pointers = (png_bytep *)malloc(sizeof(png_bytep) * height); for (int i = 0; i < height; i++) { - row_pointers[i] = data + i * width * components; + row_pointers[i] = data + i * bytesPerRow; } png_init_io(png_w, dest); @@ -124,12 +125,12 @@ namespace eval ::image { } $cc proc saveAsJpeg {image_t im char* filename} void { FILE* out = fopen(filename, "w"); - jpeg(out, im.data, im.components, im.width, im.height, 100); + jpeg(out, im.data, im.components, im.bytesPerRow, im.width, im.height, 100); fclose(out); } $cc proc saveAsPng {image_t im char* filename} void { FILE* out = fopen(filename, "wb"); - png(out, im.data, im.components, im.width, im.height); + png(out, im.data, im.components, im.bytesPerRow, im.width, im.height); fclose(out); } # Given the four corners of a region in an image, warp it to a new image of a given width and height -- cgit v1.2.3