From 42508d4e25148e8951d67ee8806eb5cef65aa2b7 Mon Sep 17 00:00:00 2001 From: s-ol Date: Sat, 27 Sep 2025 17:51:07 +0200 Subject: lib/love: add text-wrap --- alv-lib/love.moon | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/alv-lib/love.moon b/alv-lib/love.moon index 9d3c64c..e18ea3f 100644 --- a/alv-lib/love.moon +++ b/alv-lib/love.moon @@ -227,6 +227,38 @@ Create a shape that draws the text `str` with font `font` (or the default font). height = fnt\getHeight! love.graphics.print str, fnt, math.floor(width*wm), math.floor(-height/2) +text_wrap = Constant.meta + meta: + name: 'text' + summary: "create a text shape, wrapping beyond a certain width." + examples: { '(love/text-wrap str width [align] [font])' } + description: " +Create a shape that draws the text `str` with font `font` (or the default font). +`align` should be on of the following strings: +- `center` (the default) +- `left` +- `right`" + + value: class extends PureOp + pattern: any.str + any.num + -any.str + -any['love/font'] + type: T['love/shape'] + + tick: => + { str, width, align, fnt } = @unwrap_all! + + wm = switch align or 'center' + when 'left' then 0 + when 'center' then -0.5 + when 'right' then -1 + else + error Error 'argument', "unknown text alignment '#{align}'" + + @out\set -> + fnt or= love.graphics.getFont! + _, lines = fnt\getWrap str, width + height = #lines * fnt\getHeight! + love.graphics.printf str, fnt, math.floor(width*wm), math.floor(-height/2), width, align + color = Constant.meta meta: name: 'color' @@ -631,7 +663,8 @@ macro [->>][]: 'no-shape': no_shape :font - :circle, :ellipse, :line, :rectangle, :text + :circle, :ellipse, :line, :rectangle + :text, 'text-wrap': text_wrap :translate, :rotate, :scale, :shear :color, 'line-width': line_width -- cgit v1.2.3