diff options
| author | Andrés Cuervo <acwervo@gmail.com> | 2023-11-28 23:35:35 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-28 23:35:35 +0000 |
| commit | e7e2bcc548a244817b68ef3c5ea5cd2eea49f7aa (patch) | |
| tree | a4cd73f2c6582f201f6a34d0b170d8f9bc0f340a | |
| parent | Merge pull request #119 from FolkComputer/nm/clear-mailbox (diff) | |
| parent | Fix logic (diff) | |
| download | folk-e7e2bcc548a244817b68ef3c5ea5cd2eea49f7aa.tar.gz folk-e7e2bcc548a244817b68ef3c5ea5cd2eea49f7aa.zip | |
Merge pull request #118 from FolkComputer/nm/text-anchor
Support arbitrary anchor within text extent
| -rw-r--r-- | virtual-programs/display/text.folk | 53 |
1 files changed, 17 insertions, 36 deletions
diff --git a/virtual-programs/display/text.folk b/virtual-programs/display/text.folk index b55cfeea..5eb33bb7 100644 --- a/virtual-programs/display/text.folk +++ b/virtual-programs/display/text.folk @@ -68,37 +68,18 @@ On process "display" { return (vec2f) { width, y }; } $cc proc textShape {Font* font char* text - float x0 float y0 float scale int halign int valign float radians Tcl_Obj* color} Tcl_Obj* { + float x0 float y0 float scale float anchorx float anchory float radians Tcl_Obj* color} Tcl_Obj* { Tcl_Obj* gpuAtlasImageSize = Tcl_ObjPrintf("%d %d", font->atlasImage.width, font->atlasImage.height); vec2f extent = textExtent(font, text, scale); float em = scale * 25.0; - float x = 0; - float y = 0; - switch (halign) { - case -1: // left align - x = 0; - break; - case 0: // center - x = extent.x/2.0; - break; - case 1: // right align - x = extent.x; - break; - } - - switch (valign) { - case -1: // top align - y = -em; - break; - case 0: // center - y = extent.y/2.0; - break; - case 1: // bottom align - y = extent.y; - break; - } + // The anchor origin is the top left of the text + float x = anchorx * extent.x; + float y = anchory * extent.y; + + // Text rendering starts from the baseline + if (anchory == 0) { y = -em; } vec2f offset = vec2f_rotate((vec2f){x, y}, radians); vec2f p0 = (vec2f) { x0 - offset.x, y0 - offset.y }; @@ -229,23 +210,23 @@ On process "display" { set color [getColor [dict_getdef $options color white]] if {$anchor == "topleft"} { - set align [list -1 -1] + set anchor [list 0 0] } elseif {$anchor == "top"} { - set align [list 0 -1] + set anchor [list 0.5 0] } elseif {$anchor == "topright"} { - set align [list 1 -1] + set anchor [list 1.0 0] } elseif {$anchor == "left"} { - set align [list -1 0] + set anchor [list 0 0.5] } elseif {$anchor == "center"} { - set align [list 0 0] + set anchor [list 0.5 0.5] } elseif {$anchor == "right"} { - set align [list 1 0] + set anchor [list 1.0 0.5] } elseif {$anchor == "bottomleft"} { - set align [list -1 1] + set anchor [list 0 1] } elseif {$anchor == "bottom"} { - set align [list 0 1] + set anchor [list 0.5 1] } elseif {$anchor == "bottomright"} { - set align [list 1 1] + set anchor [list 1 1] } if {!([dict exists $::FontCache $font])} { @@ -253,7 +234,7 @@ On process "display" { } set font [dict get $::FontCache $font] - set instances [font textShape $font $text $x0 $y0 $scale {*}$align $radians $color] + set instances [font textShape $font $text $x0 $y0 $scale {*}$anchor $radians $color] # We need to batch into one wish so we don't deal with n^2 # checks for existing statements for n glyphs. |
