From f85dff6783ba99f68235dd7f08a2998e01c223c5 Mon Sep 17 00:00:00 2001 From: Omikhleia Date: Sat, 16 Dec 2023 18:06:40 +0100 Subject: [PATCH] fixup! feat: Support more rough features --- .../rough-lua/fillers/dashed-filler.lua | 7 +- .../framebox/rough-lua/fillers/dot-filler.lua | 4 +- .../rough-lua/fillers/hachure-fill.lua | 24 ++----- .../rough-lua/fillers/hachure-filler.lua | 4 +- .../rough-lua/fillers/hatch-filler.lua | 5 +- .../rough-lua/fillers/scan-line-hachure.lua | 7 +- packages/framebox/rough-lua/generator.lua | 8 ++- packages/framebox/rough-lua/geometry.lua | 2 +- packages/framebox/rough-lua/renderer.lua | 64 +++++-------------- 9 files changed, 37 insertions(+), 88 deletions(-) diff --git a/packages/framebox/rough-lua/fillers/dashed-filler.lua b/packages/framebox/rough-lua/fillers/dashed-filler.lua index a66772e..f76bd3b 100644 --- a/packages/framebox/rough-lua/fillers/dashed-filler.lua +++ b/packages/framebox/rough-lua/fillers/dashed-filler.lua @@ -10,6 +10,9 @@ local polygonHachureLines = require("packages.framebox.rough-lua.fillers.scan-line-hachure").polygonHachureLines local lineLength = require("packages.framebox.rough-lua.geometry").lineLength +local jsshims = require("packages.framebox.rough-lua.jsshims") +local array_concat = jsshims.array_concat + local DashedFiller = pl.class() function DashedFiller:_init (helper) @@ -42,9 +45,7 @@ function DashedFiller:dashedLine (lines, o) local start = { p1[1] + (lstart * math.cos(alpha)) + (startOffset * math.cos(alpha)), p1[2] + lstart * math.sin(alpha) + (startOffset * math.sin(alpha)) } local end_ = { p1[1] + (lend * math.cos(alpha)) + (startOffset * math.cos(alpha)), p1[2] + (lend * math.sin(alpha)) + (startOffset * math.sin(alpha)) } local t = self.helper.doubleLineOps(start[1], start[2], end_[1], end_[2], o) - for _, v in ipairs(t) do - ops[#ops + 1] = v - end + pl.tablex.insertvalues(ops, t) -- = JS ops.push(...t) end end return ops diff --git a/packages/framebox/rough-lua/fillers/dot-filler.lua b/packages/framebox/rough-lua/fillers/dot-filler.lua index f821fbf..7b0f7a4 100644 --- a/packages/framebox/rough-lua/fillers/dot-filler.lua +++ b/packages/framebox/rough-lua/fillers/dot-filler.lua @@ -47,9 +47,7 @@ function DotFiller:dotsOnLines (lines, o) local cx = (x - ro) + math.random() * 2 * ro local cy = (y - ro) + math.random() * 2 * ro local el = self.helper.ellipse(cx, cy, fweight, fweight, o) - for _, v in ipairs(el.ops) do - ops[#ops + 1] = v - end + pl.tablex.insertvalues(ops, el.ops) -- = JS ops.push(...el.ops) end end return { type = 'fillSketch', ops = ops } diff --git a/packages/framebox/rough-lua/fillers/hachure-fill.lua b/packages/framebox/rough-lua/fillers/hachure-fill.lua index 6e170a4..523278e 100644 --- a/packages/framebox/rough-lua/fillers/hachure-fill.lua +++ b/packages/framebox/rough-lua/fillers/hachure-fill.lua @@ -12,25 +12,9 @@ -- Copyright (c) 2023 Preet Shihn -- --- quick Lua shim... -local function table_splice (tbl, start, length) -- from xlua - length = length or 1 - start = start or 1 - local endd = start + length - local spliced = {} - local remainder = {} - for i, elt in ipairs(tbl) do - if i < start or i >= endd then - table.insert(spliced, elt) - else - table.insert(remainder, elt) - end - end - return spliced, remainder -end -local function math_round (x) -- quick Lua shim - return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5) -end +local jsshims = require("packages.framebox.rough-lua.jsshims") +local array_splice = jsshims.array_splice +local math_round = jsshims.math_round local rotatePoints = function (points, center, degrees) if points and #points > 0 then @@ -135,7 +119,7 @@ local function straightHachureLines (polygons, gap, hachureStepOffset) ix = i end local removed - edges, removed = table_splice(edges, 1, ix) + edges, removed = array_splice(edges, 1, ix) for _, edge in ipairs(removed) do activeEdges[#activeEdges + 1] = { s = y, edge = edge } end diff --git a/packages/framebox/rough-lua/fillers/hachure-filler.lua b/packages/framebox/rough-lua/fillers/hachure-filler.lua index 43be9a5..d113aa7 100644 --- a/packages/framebox/rough-lua/fillers/hachure-filler.lua +++ b/packages/framebox/rough-lua/fillers/hachure-filler.lua @@ -25,9 +25,7 @@ function HachureFiller:renderLines (lines, o) local ops = {} for _, line in ipairs(lines) do local t = self.helper.doubleLineOps(line[1][1], line[1][2], line[2][1], line[2][2], o) - for _, v in ipairs(t) do - ops[#ops + 1] = v - end + pl.tablex.insertvalues(ops, t) -- = JS ops.push(...t) end return ops end diff --git a/packages/framebox/rough-lua/fillers/hatch-filler.lua b/packages/framebox/rough-lua/fillers/hatch-filler.lua index 52ecc80..299a7a1 100644 --- a/packages/framebox/rough-lua/fillers/hatch-filler.lua +++ b/packages/framebox/rough-lua/fillers/hatch-filler.lua @@ -7,7 +7,6 @@ -- License MIT -- Copyright (c) 2019 Preet Shihn -- -local polygonHachureLines = require("packages.framebox.rough-lua.fillers.scan-line-hachure").polygonHachureLines local HachureFiller = require("packages.framebox.rough-lua.fillers.hachure-filler").HachureFiller local HatchFiller = pl.class(HachureFiller) @@ -17,9 +16,7 @@ function HatchFiller:fillPolygons (polygonList, o) local o2 = pl.tablex.copy(o) o2.hachureAngle = o.hachureAngle + 90 local set2 = self._base.fillPolygons(self, polygonList, o2) - for _, v in ipairs(set2.ops) do - set.ops[#set.ops + 1] = v - end + pl.tablex.insertvalues(set.ops, set2.ops) return set end diff --git a/packages/framebox/rough-lua/fillers/scan-line-hachure.lua b/packages/framebox/rough-lua/fillers/scan-line-hachure.lua index c5bd201..f4e284a 100644 --- a/packages/framebox/rough-lua/fillers/scan-line-hachure.lua +++ b/packages/framebox/rough-lua/fillers/scan-line-hachure.lua @@ -9,13 +9,12 @@ -- local hachureLines = require("packages.framebox.rough-lua.fillers.hachure-fill").hachureLines +local jsshims = require("packages.framebox.rough-lua.jsshims") +local math_round = jsshims.math_round + local PRNG = require("packages.framebox.graphics.prng") local prng = PRNG() -local function math_round (x) -- quick Lua shim - return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5) -end - local function polygonHachureLines (polygonList, o) local angle = o.hachureAngle + 90 local gap = o.hachureGap diff --git a/packages/framebox/rough-lua/generator.lua b/packages/framebox/rough-lua/generator.lua index 7e9f8cd..77e1e75 100644 --- a/packages/framebox/rough-lua/generator.lua +++ b/packages/framebox/rough-lua/generator.lua @@ -161,11 +161,13 @@ local RoughGenerator = pl.class({ local pointsList = type(p1[1]) == 'number' and { inputPoints } or inputPoints for _, points in ipairs(pointsList) do if #points < 3 then - polyPoints[#polyPoints + 1] = points + pl.tablex.insertvalues(polyPoints, points) -- = JS polyPoints.push(...points) elseif #points == 3 then - polyPoints[#polyPoints + 1] = pointsOnBezierCurves(curveToBezier({ points[1], points[1], points[2], points[3] }), 10, (1 + o.roughness) / 2) + local t = pointsOnBezierCurves(curveToBezier({ points[1], points[1], points[2], points[3] }), 10, (1 + o.roughness) / 2) + pl.tablex.insertvalues(polyPoints, t) -- = JS polyPoints.push(...t) else - polyPoints[#polyPoints + 1] = pointsOnBezierCurves(curveToBezier(points), 10, (1 + o.roughness) / 2) + local t = pointsOnBezierCurves(curveToBezier(points), 10, (1 + o.roughness) / 2) + pl.tablex.insertvalues(polyPoints, t) -- = JS polyPoints.push(...t) end end end diff --git a/packages/framebox/rough-lua/geometry.lua b/packages/framebox/rough-lua/geometry.lua index 184653a..c4b5d08 100644 --- a/packages/framebox/rough-lua/geometry.lua +++ b/packages/framebox/rough-lua/geometry.lua @@ -10,7 +10,7 @@ local function lineLength(line) local p1 = line[1] local p2 = line[2] - return math.sqrt(math.pow(p1[1] - p2[1], 2) + math.pow(p1[2] - p2[2], 2)) + return math.sqrt((p1[1] - p2[1])^2 + (p1[2] - p2[2])^2) end return { diff --git a/packages/framebox/rough-lua/renderer.lua b/packages/framebox/rough-lua/renderer.lua index a18cc88..8b6bed6 100644 --- a/packages/framebox/rough-lua/renderer.lua +++ b/packages/framebox/rough-lua/renderer.lua @@ -8,6 +8,9 @@ -- Copyright (c) 2019 Preet Shihn -- +local jsshims = require("packages.framebox.rough-lua.jsshims") +local array_concat = jsshims.array_concat + local PRNG = require("packages.framebox.graphics.prng") local prng = PRNG() @@ -122,18 +125,7 @@ local function _doubleLine(x1, y1, x2, y2, o, filling) return o1 end local o2 = _line(x1, y1, x2, y2, o, true, true) - -- fusing arrays - local t = {} - local n = 0 - for _, v in ipairs(o1) do - n = n + 1 - t[n] = v - end - for _, v in ipairs(o2) do - n = n + 1 - t[n] = v - end - return t + return array_concat(o1, o2) end local function _curve(points, closePoint, o) @@ -181,9 +173,7 @@ local function _curve(points, closePoint, o) } elseif len == 2 then local t = _line(points[1][1], points[1][2], points[2][1], points[2][2], o, true, true) - for _, item in ipairs(t) do - ops[#ops+1] = item - end + pl.tablex.insertvalues(ops, t) -- = JS ops.push(...t) end return ops end @@ -300,7 +290,6 @@ end local function _bezierTo(x1, y1, x2, y2, x, y, current, o) local ops = {} local ros = {o.maxRandomnessOffset or 1, (o.maxRandomnessOffset or 1) + 0.3} - local f = {0, 0} local iterations = o.disableMultiStroke and 1 or 2 local preserveVertices = o.preserveVertices for i = 1, iterations do @@ -318,7 +307,7 @@ local function _bezierTo(x1, y1, x2, y2, x, y, current, o) } } end - f = preserveVertices and { x, y } or { x + _offsetOpt(ros[i], o), y + _offsetOpt(ros[i], o) } + local f = preserveVertices and { x, y } or { x + _offsetOpt(ros[i], o), y + _offsetOpt(ros[i], o) } ops[#ops+1] = { op = 'bcurveTo', data = { @@ -346,15 +335,11 @@ local function linearPath(points, close, o) local ops = {} for i = 1, len - 1 do local t = _doubleLine(points[i][1], points[i][2], points[i + 1][1], points[i + 1][2], o) - for k = 1, #t do - ops[#ops+1] = t[k] - end + pl.tablex.insertvalues(ops, t) -- = JS ops.push(...t) end if close then local t = _doubleLine(points[len][1], points[len][2], points[1][1], points[1][2], o) - for k = 1, #t do - ops[#ops+1] = t[k] - end + pl.tablex.insertvalues(ops, t) -- = JS ops.push(...t) end return { type = 'path', ops = ops } elseif len == 2 then @@ -400,14 +385,13 @@ local function curve(inputPoints, o) end end end - return { type = 'path', ops = o1 + o2 } + return { type = 'path', ops = array_concat(o1, o2) } end return { type = 'path', ops = {} } end local function generateEllipseParams(width, height, o) local psq = math.sqrt(math.pi * 2 * math.sqrt(((width / 2)^2 + (height / 2)^2) / 2)) - local stepCount = math.ceil(math.max(o.curveStepCount, (o.curveStepCount / math.sqrt(200)) * psq)) local increment = (math.pi * 2) / stepCount local rx = math.abs(width / 2) @@ -424,9 +408,7 @@ local function ellipseWithParams(x, y, o, ellipseParams) if not o.disableMultiStroke and o.roughness ~= 0 then local ap2 = _computeEllipsePoints(ellipseParams.increment, x, y, ellipseParams.rx, ellipseParams.ry, 1.5, 0, o) local o2 = _curve(ap2, nil, o) - for _, item in ipairs(o2) do - o1[#o1+1] = item - end + pl.tablex.insertvalues(o1, o2) -- JS version used array_concat but seems avoidable here end return { estimatedPoints = cp1, opset = { type = 'path', ops = o1 } } end @@ -458,20 +440,14 @@ local function arc(x, y, width, height, start, stop, closed, roughClosure, o) local ops = _arc(arcInc, cx, cy, rx, ry, strt, stp, 1, o) if not o.disableMultiStroke then local o2 = _arc(arcInc, cx, cy, rx, ry, strt, stp, 1.5, o) - for _, item in ipairs(o2) do - ops[#ops+1] = item - end + pl.tablex.insertvalues(ops, o2) -- = JS ops.push(...o2) end if closed then if roughClosure then local t = _doubleLine(cx, cy, cx + rx * math.cos(strt), cy + ry * math.sin(strt), o) - for _, item in ipairs(t) do - ops[#ops+1] = item - end + pl.tablex.insertvalues(ops, t) -- = JS ops.push(...t) t = _doubleLine(cx, cy, cx + rx * math.cos(stp), cy + ry * math.sin(stp), o) - for _, item in ipairs(t) do - ops[#ops+1] = item - end + pl.tablex.insertvalues(ops, t) -- = JS ops.push(...t) else ops[#ops+1] = { op = 'lineTo', data = {cx, cy} } ops[#ops+1] = { op = 'lineTo', data = {cx + rx * math.cos(strt), cy + ry * math.sin(strt)} } @@ -493,22 +469,16 @@ local function svgPath(path, o) first = {data[1], data[2]} elseif key == 'L' then local t = _doubleLine(current[1], current[2], data[1], data[2], o) - for _, item in ipairs(t) do - ops[#ops+1] = item - end - current = {data[1], data[2]} + pl.tablex.insertvalues(ops, t) -- = JS ops.push(...t) + current = { data[1], data[2] } elseif key == 'C' then local x1, y1, x2, y2, x, y = data[1], data[2], data[3], data[4], data[5], data[6] local t = _bezierTo(x1, y1, x2, y2, x, y, current, o) - for _, item in ipairs(t) do - ops[#ops+1] = item - end + pl.tablex.insertvalues(ops, t) -- = JS ops.push(...t) current = {x, y} elseif key == 'Z' then local t = _doubleLine(current[1], current[2], first[1], first[2], o) - for _, item in ipairs(t) do - ops[#ops+1] = item - end + pl.tablex.insertvalues(ops, t) -- = JS ops.push(...t) current = {first[1], first[2]} end end