Skip to content

Commit

Permalink
feat: Add untested parts of the roughjs port
Browse files Browse the repository at this point in the history
These are not needed (yet) in our framebox use, but let's have
them in an "untested" folder. This will help to get started if we
later decide to extract the port as a separate Lua rocks.
  • Loading branch information
Omikhleia authored and Didier Willis committed Jan 14, 2024
1 parent 15fcfd8 commit 8c53b80
Show file tree
Hide file tree
Showing 9 changed files with 816 additions and 16 deletions.
21 changes: 20 additions & 1 deletion ptable.sile-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,27 @@ build = {
["sile.packages.parbox"] = "packages/parbox/init.lua",
["sile.packages.ptable"] = "packages/ptable/init.lua",
["sile.packages.framebox"] = "packages/framebox/init.lua",
["sile.packages.framebox.graphics.prng"] = "packages/framebox/graphics/prng.lua",
["sile.packages.framebox.graphics.renderer"] = "packages/framebox/graphics/renderer.lua",
["sile.packages.framebox.graphics.rough"] = "packages/framebox/graphics/rough.lua",
["prng-prigarin"] = "prng-prigarin/init.lua",
["rough-lua.rough.jsshims"] = "rough-lua/rough/jsshims.lua",
["rough-lua.rough.generator"] = "rough-lua/rough/generator.lua",
["rough-lua.rough.renderer"] = "rough-lua/rough/renderer.lua",
["rough-lua.rough.fillers.hachure-filler"] = "rough-lua/rough/fillers/hachure-filler.lua",
["rough-lua.rough.fillers.zigzag-filler"] = "rough-lua/rough/fillers/zigzag-filler.lua",
["rough-lua.rough.fillers.zigzag-line-filler"] = "rough-lua/rough/fillers/zigzag-line-filler.lua",
["rough-lua.rough.fillers.dot-filler"] = "rough-lua/rough/fillers/dot-filler.lua",
["rough-lua.rough.fillers.dashed-filler"] = "rough-lua/rough/fillers/dashed-filler.lua",
["rough-lua.rough.fillers.scan-line-hachure"] = "rough-lua/rough/fillers/scan-line-hachure.lua",
["rough-lua.rough.fillers.hatch-filler"] = "rough-lua/rough/fillers/hatch-filler.lua",
["rough-lua.rough.fillers.hachure-fill"] = "rough-lua/rough/fillers/hachure-fill.lua",
["rough-lua.rough.fillers.filler"] = "rough-lua/rough/fillers/filler.lua",
["rough-lua.rough.geometry"] = "rough-lua/rough/geometry.lua",
["rough-lua.untested.path-data-parser"] = "rough-lua/untested/path-data-parser/init.lua",
["rough-lua.untested.path-data-parser.parser"] = "rough-lua/untested/path-data-parser/parser.lua",
["rough-lua.untested.path-data-parser.normalize"] = "rough-lua/untested/path-data-parser/normalize.lua",
["rough-lua.untested.path-data-parser.absolutize"] = "rough-lua/untested/path-data-parser/absolutize.lua",
["rough-lua.untested.points-on-curve"] = "rough-lua/untested/points-on-curve/init.lua",
["rough-lua.untested.points-on-curve.curve-to-bezier"] = "rough-lua/untested/points-on-curve/curve-to-bezier.lua",
}
}
11 changes: 3 additions & 8 deletions rough-lua/rough/generator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ local line, rectangle,
renderer.arc, renderer.curve, renderer.linearPath,
renderer.svgPath,
renderer.patternFillArc, renderer.patternFillPolygons, renderer.solidFillPolygon
-- PORTING NOTE:
-- I ported the module but haven't tested it for now
-- local curveToBezier = require("rough-lua.points-on-curve.curve-to-bezier").curveToBezier
-- local pointsOnPath = require("rough-lua.points-on-curve").pointsOnPath
-- local pointsOnBezierCurves = require("rough-lua.points-on-curve").pointsOnBezierCurves
local pointsOnPath = function () error("Not implemented") end
local curveToBezier = function () error("Not implemented") end
local pointsOnBezierCurves = function () error("Not implemented") end
local curveToBezier = require("rough-lua.untested.points-on-curve.curve-to-bezier").curveToBezier
local pointsOnPath = require("rough-lua.untested.points-on-curve").pointsOnPath
local pointsOnBezierCurves = require("rough-lua.untested.points-on-curve").pointsOnBezierCurves


local RoughGenerator = pl.class({
Expand Down
10 changes: 3 additions & 7 deletions rough-lua/rough/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ local jsshims = require("rough-lua.rough.jsshims")
local array_concat = jsshims.array_concat
local PRNG = require("prng-prigarin")

-- PORTING NOTE:
-- I ported path-data-parser but haven't tested it for now
-- local pathDataParser = require("rough-lua.path-data-parser")
-- local parsePath, normalize, absolutize = pathDataParser.parsePath, pathDataParser.normalize, pathDataParser.absolutize
local normalize = function () error("Not yet implemented") end
local absolutize = function () error("Not yet implemented") end
local parsePath = function () error("Not yet implemented") end
local pathDataParser = require("rough-lua.untested.path-data-parser")
local parsePath, normalize, absolutize
= pathDataParser.parsePath, pathDataParser.normalize, pathDataParser.absolutize

local getFiller = require("rough-lua.rough.fillers.filler").getFiller

Expand Down
95 changes: 95 additions & 0 deletions rough-lua/untested/path-data-parser/absolutize.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
--
-- License: MIT
-- Copyright (c) 2023, Didier Willis
--
-- This is a straightforward port of the path-data-parser JavaScript library.
-- (https://github.com/pshihn/path-data-parser/)
-- License: MIT
-- Copyright (c) 2019 Preet Shihn
--
local function absolutize(segments)
local cx, cy = 0, 0
local subx, suby = 0, 0
local out = {}
for _, segment in ipairs(segments) do
local key, data = segment.key, segment.data
if key == 'M' then
out[#out + 1] = { key = 'M', data = pl.tablex.copy(data) }
cx, cy = data[1], data[2]
subx, suby = data[1], data[2]
elseif key == 'm' then
cx = cx + data[1]
cy = cy + data[2]
out[#out + 1] = { key = 'M', data = { cx, cy } }
subx, suby = cx, cy
elseif key == 'L' then
out[#out + 1] = { key = 'L', data = pl.tablex.copy(data) }
cx, cy = data[1], data[2]
elseif key == 'l' then
cx = cx + data[1]
cy = cy + data[2]
out[#out + 1] = { key = 'L', data = { cx, cy } }
elseif key == 'C' then
out[#out + 1] = { key = 'C', data = pl.tablex.copy(data) }
cx, cy = data[5], data[6]
elseif key == 'c' then
local newdata = pl.tablex.map(data, function (d, i)
return (i % 2) == 0 and (d + cx) or (d + cy)
end)
out[#out + 1] = { key = 'C', data = newdata }
cx, cy = newdata[5], newdata[6]
elseif key == 'Q' then
out[#out + 1] = { key = 'Q', data = pl.tablex.copy(data) }
cx, cy = data[2], data[3]
elseif key == 'q' then
local newdata = pl.tablex.map(data, function (d, i)
return (i % 2) == 0 and (d + cx) or (d + cy)
end)
out[#out + 1] = { key = 'Q', data = newdata }
cx, cy = newdata[2], newdata[3]
elseif key == 'A' then
out[#out + 1] = { key = 'A', data = pl.tablex.copy(data) }
cx, cy = data[5], data[6]
elseif key == 'a' then
cx = cx + data[5]
cy = cy + data[6]
out[#out + 1] = { key = 'A', data = { data[1], data[2], data[3], data[4], data[5], cx, cy } }
elseif key == 'H' then
out[#out + 1] = { key = 'H', data = pl.tablex.copy(data) }
cx = data[1]
elseif key == 'h' then
cx = cx + data[1]
out[#out + 1] = { key = 'H', data = { cx } }
elseif key == 'V' then
out[#out + 1] = { key = 'V', data = pl.tablex.copy(data) }
cy = data[1]
elseif key == 'v' then
cy = cy + data[1]
out[#out + 1] = { key = 'V', data = { cy } }
elseif key == 'S' then
out[#out + 1] = { key = 'S', data = pl.tablex.copy(data) }
cx, cy = data[2], data[3]
elseif key == 's' then
local newdata = pl.tablex.map(data, function (d, i)
return (i % 2) == 0 and (d + cx) or (d + cy)
end)
out[#out + 1] = { key = 'S', data = newdata }
cx, cy = newdata[2], newdata[3]
elseif key == 'T' then
out[#out + 1] = { key = 'T', data = pl.tablex.copy(data) }
cx, cy = data[1], data[2]
elseif key == 't' then
cx = cx + data[1]
cy = cy + data[2]
out[#out + 1] = { key = 'T', data = { cx, cy } }
elseif key == 'Z' or key == 'z' then
out[#out + 1] = { key = 'Z', data = {} }
cx, cy = subx, suby
end
end
return out
end

return {
absolutize = absolutize,
}
20 changes: 20 additions & 0 deletions rough-lua/untested/path-data-parser/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--
-- License: MIT
-- Copyright (c) 2023, Didier Willis
--
-- This is a straightforward port of the path-data-parser JavaScript library.
-- (https://github.com/pshihn/path-data-parser/)
-- License: MIT
-- Copyright (c) 2019 Preet Shihn
--
local normalize = require("rough-lua.untested.path-data-parser.normalize").normalize
local absolutize = require("rough-lua.untested.path-data-parser.absolutize").absolutize
local parsePath = require("rough-lua.untested.path-data-parser.parser").parsePath
local serialize = require("rough-lua.untested.path-data-parser.parser").serialize

return {
parsePath = parsePath,
serialize = serialize,
absolutize = absolutize,
normalize = normalize,
}
Loading

0 comments on commit 8c53b80

Please sign in to comment.