Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions builtin-programs/decorations/label.folk
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,44 @@ When /thing/ has resolved geometry /geom/ {
set text [join [lmap result $results {dict get $result text}] "\n"]
if {$text eq ""} { return }

# Split text into lines and find the longest line.
set lines [split $text "\n"]
set width [dict get $geom width]
set height [dict get $geom height]

set maxLength 0
foreach line $lines {
foreach line [split $text "\n"] {
set lineLength [string length $line]
if {$lineLength > $maxLength} {
set maxLength $lineLength
}
}

if {$maxLength == 0} { return }
set scale [::math::min 0.02 [/ 0.45 $maxLength]]

# Set default scale based on longest line length.
# Scale inversely with length to keep text readable.
set defaultScale [::math::min 0.02 [/ 0.45 $maxLength]]
set position [list [expr {$width / 2.0}] [expr {$height / 2.0}]]
set options [dict create \
position $position \
scale $scale \
anchor center \
font "PTSans-Regular"]

set x [/ $geom(width) 2.0]
try {
set y $($geom(top) + $geom(tagSize) + $geom(bottom)/2.0)
} on error e {
set y [/ $geom(height) 2.0]
if {[dict exists $geom top] &&
[dict exists $geom tagSize] &&
[dict exists $geom bottom]} {
dict set options position \
[list [expr {$width / 2.0}] \
[expr {[dict get $geom top] + [dict get $geom tagSize] + [dict get $geom bottom] / 2.0}]]
}
set options [dict create x $x y $y scale $defaultScale]

# FIXME: support per-label options; right now, this just

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably still need this FIXME comment?

# applies an arbitrary label's options to all of them
# together.
set options [dict merge $options [dict get $result options]]
# mashes them all together. (E.g. if one label wants to
# be red and another wants to be blue, they'll both be
# whatever the last label wants to be.)
foreach result $results {
set options [dict merge $options [dict get $result options]]
}
dict set options text $text
Wish to draw text onto $thing with {*}$options
}
Expand Down
39 changes: 0 additions & 39 deletions builtin-programs/display/arc.folk

This file was deleted.

135 changes: 0 additions & 135 deletions builtin-programs/display/curve.folk

This file was deleted.

53 changes: 53 additions & 0 deletions builtin-programs/draw/arc.folk
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Wish the GPU compiles pipeline "arc" [list \
{vec2 viewport mat3 surfaceToClip
vec2 center float radius float thickness float start float arclen vec4 color} {
float r = radius + thickness;
vec2 vertices[6] = vec2[6](
center - r,
vec2(center.x + r, center.y - r),
vec2(center.x - r, center.y + r),
vec2(center.x + r, center.y - r),
center + r,
vec2(center.x - r, center.y + r)
);
vec3 v = surfaceToClip * vec3(vertices[gl_VertexIndex], 1.0);
return vec4(v.xy / v.z, 0.0, 1.0);
} [subst {
vec2 clipXy = (gl_FragCoord.xy / viewport) * 2.0 - 1.0;
vec3 surfaceXy = inverse(surfaceToClip) * vec3(clipXy, 1.0);
surfaceXy /= surfaceXy.z;
const float TAU = $::TAU;
float c_start = clamp(start, 0.0, TAU);
float c_arclen = clamp(arclen, 0.0, TAU);
float dist = length(surfaceXy.xy - center) - radius;
float angle = atan(-(surfaceXy.y - center.y), surfaceXy.x - center.x);
angle = (angle < 0.0) ? (angle + TAU) : angle;
float end = c_start + c_arclen;
if (dist < thickness && dist > 0.0) {
if ((end < TAU && angle > c_start && angle < end) ||
(end >= TAU && (angle > c_start || angle < end - TAU))) {
return color;
}
}
return vec4(0.0);
}]]

When the color map is /colorMap/ &\
/p/ has canvas /id/ with /...wiOptions/ &\
/p/ has canvas projection /surfaceToClip/ &\
/someone/ wishes to draw an arc onto /p/ with /...options/ {
set center [dict getdef $options center ""]
if {$center eq ""} { set center [list [dict get $options x] [dict get $options y]] }
set radius [dict get $options radius]
set thickness [dict get $options thickness]
set start [dict get $options start]
set arclen [dict get $options arclen]
set color [dict get $options color]
set color [dict getdef $colorMap $color $color]
set layer [dict getdef $options layer 0]
set wiResolution [list [dict get $wiOptions width] [dict get $wiOptions height]]
Wish the GPU draws pipeline "arc" onto canvas $id with arguments \
[list $wiResolution $surfaceToClip \
$center $radius $thickness $start $arclen $color] \
layer $layer
}
84 changes: 46 additions & 38 deletions builtin-programs/draw/circle.folk
Original file line number Diff line number Diff line change
@@ -1,47 +1,55 @@
Wish the GPU compiles pipeline "circle" {
{vec2 viewport mat3 surfaceToClip
vec2 center float radius float thickness vec4 color int filled} {
float r = radius + thickness;
vec2 vertices[6] = vec2[6](
center - r,
vec2(center.x + r, center.y - r),
vec2(center.x - r, center.y + r),
vec2 center float innerRadius float outerRadius vec4 color} {
float r = outerRadius;
vec2 vertices[6] = vec2[6](
vec2(center.x - r, center.y - r),
vec2(center.x + r, center.y - r),
vec2(center.x - r, center.y + r),

vec2(center.x + r, center.y - r),
center + r,
vec2(center.x - r, center.y + r)
);
vec3 v = surfaceToClip * vec3(vertices[gl_VertexIndex], 1.0);
return vec4(v.xy/v.z, 0.0, 1.0);
} {
vec2 clipXy = (gl_FragCoord.xy / viewport) * 2.0 - 1.0;
vec3 surfaceXy = inverse(surfaceToClip) * vec3(clipXy, 1.0);
surfaceXy /= surfaceXy.z;
vec2(center.x + r, center.y - r),
vec2(center.x + r, center.y + r),
vec2(center.x - r, center.y + r)
);
vec3 v = surfaceToClip * vec3(vertices[gl_VertexIndex], 1.0);
return vec4(v.xy/v.z, 0.0, 1.0);
} {
vec2 clipXy = (gl_FragCoord.xy / viewport) * 2.0 - 1.0;
vec3 surfaceXy = inverse(surfaceToClip) * vec3(clipXy, 1.0);
surfaceXy /= surfaceXy.z;

float dist = length(surfaceXy.xy - center) - radius;
if (filled == 1) {
return (dist < thickness) ? color : vec4(0.0);
} else {
return (dist < thickness && dist > 0.0) ? color : vec4(0.0);
float dist = length(surfaceXy.xy - center);
return (dist > innerRadius && dist < outerRadius) ? color : vec4(0.0);
}
}
}

When the color map is /colorMap/ &\
/p/ has canvas /id/ with /...wiOptions/ &\
/p/ has canvas projection /surfaceToClip/ &\
/someone/ wishes to draw a circle onto /p/ with /...options/ {
When the color map is /colorMap/ &\
/p/ has canvas /id/ with /...wiOptions/ &\
/p/ has canvas projection /surfaceToClip/ &\
/someone/ wishes to draw a circle onto /p/ with /...options/ {
set wiResolution [list [dict get $wiOptions width] [dict get $wiOptions height]]

set center [dict getdef $options center ""]
if {$center eq ""} {
set center [list [dict get $options x] [dict get $options y]]
}

set center [dict getdef $options center ""]
if {$center eq ""} { set center [list [dict get $options x] [dict get $options y]] }
set radius [dict get $options radius]
set thickness [dict get $options thickness]
set color [dict get $options color]
set color [dict getdef $colorMap $color $color]
set filled [dict getdef $options filled false]
set radius [dict get $options radius]
set thickness [dict getdef $options thickness 0]
set color [dict get $options color]
set color [dict getdef $colorMap $color $color]
set filled [dict getdef $options filled false]
set layer [dict getdef $options layer 0]

set wiResolution [list [dict get $wiOptions width] [dict get $wiOptions height]]
Wish the GPU draws pipeline "circle" onto canvas $id with arguments \
[list $wiResolution $surfaceToClip \
$center $radius $thickness $color [expr {$filled eq false ? 0 : 1}]]
}
if {$filled} {
set innerRadius 0.0
} else {
set innerRadius $radius
}
set outerRadius [expr {$radius + $thickness}]

Wish the GPU draws pipeline "circle" onto canvas $id with arguments \
[list $wiResolution $surfaceToClip \
$center $innerRadius $outerRadius $color] \
layer $layer
}
Loading