Skip to content

Commit

Permalink
fixup! feat: Support more rough features
Browse files Browse the repository at this point in the history
  • Loading branch information
Omikhleia authored and Didier Willis committed Dec 16, 2023
1 parent cdbbb07 commit f85dff6
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 88 deletions.
7 changes: 4 additions & 3 deletions packages/framebox/rough-lua/fillers/dashed-filler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions packages/framebox/rough-lua/fillers/dot-filler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
24 changes: 4 additions & 20 deletions packages/framebox/rough-lua/fillers/hachure-fill.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions packages/framebox/rough-lua/fillers/hachure-filler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions packages/framebox/rough-lua/fillers/hatch-filler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
7 changes: 3 additions & 4 deletions packages/framebox/rough-lua/fillers/scan-line-hachure.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions packages/framebox/rough-lua/generator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/framebox/rough-lua/geometry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
64 changes: 17 additions & 47 deletions packages/framebox/rough-lua/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)} }
Expand All @@ -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
Expand Down

0 comments on commit f85dff6

Please sign in to comment.