我喜欢 Egor 的解决方案,但它不适用于小数和字母 V 和 H,所以:
local function parsePath (input)
input = input:gsub("([^%s,;])([%a])", "%1 %2") -- Convert "100D" to "100 D"
input = input:gsub("([%a])([^%s,;])", "%1 %2") -- Convert "D100" to "D 100"
local output, line = {}
for v in input:gmatch("([^%s,;]+)") do
if tonumber(v) then
line[#line+1] = math.floor(tonumber(v)+0.5)
else
line = {v}
output[#output+1] = line
end
end
return output
end
运行:
local ds = {
"M40,360H-40", -- line
"M -40,480 H 40", -- line
"M 840,1000 V 920 M 720,920 V 1000", -- two lines
"M 1280.1,-39.9 1200.1,39.9", -- line with decimals
"M 1320,40 1400,-40",
"M 40,480 H 360",
"M 760,360 400,360",
"M 1120,240 1320,40",
"M 400,360 40,360",
"M 840,920 C 840,680 1040,320 1120,240", -- cubic bezier
"M 1200,40 C 1160,80 1120,120 1080,160",
"M 1080,160 C 920,320 520,360 400,360",
"M 360,480 C 520,480 720,760 720,920",
"M 760,360 C 640,360 560,520 640,600",
"M 640,600 C 720,680 840,640 880,560",
"M 880,560 C 920,480 880,360 760,360",
"M 1080,160 C 1040,200 920,360 760,360",
"M 360,480 C 480,480 560,520 640,600",
"M 640,600 C 720,680 720,840 720,920",
"M 840,920 C 840,800 840,640 880,560",
"M 880,560 C 920,480 1120,240 1120,240",
"M 0,600 H 360 L 600,840 V 960 H 0",
"M 1440,0 H 1920 V 960 H 960 V 720",
"M 0,0 H 1080 L 840,240 H 0"
}
for i, d in ipairs (ds) do
local parsedPath = parsePath (d)
local str = '{'
for i, component in ipairs (list) do
str = str .. '{'.. table.concat(component, ',') ..'},'
end
str = str:sub(1, -2) -- remove last comma
str = str .. '}'
print (str)
end
结果:
{{M,40,360},{H,-40}}
{{M,-40,480},{H,40}}
{{M,840,1000},{V,920}}
{{M,720,920},{V,1000}}
{{M,1200,-40},{V,40}}
{{M,1320,40},{V,-40}}
{{M,840,920},{C,840,800,1000,560,1080,480},{M,1080,480},{C,1160,400,1320,240,1320,40}}
{{M,1200,40},{C,1200,120,1160,160,1080,200},{M,1080,200},{C,920,280,720,360,480,360},{M,480,360},{C,3360,360,120,360,40,360}}
{{M,40,480},{C,40,480,240,480,360,480,520,480,720,760,720,920}}
{{M,760,400},{C,640,400,560,520,640,600}}
{{M,640,600},{C,720,680,800,640,840,600}}
{{M,840,600},{C,880,560,880,400,760,400}}
{{M,1080,200},{C,920,280,920,400,760,400}}
{{M,760,400},{C,640,400,600,360,480,360}}
{{M,360,480},{C,480,480,560,520,640,600}}
{{M,640,600},{C,720,680,720,840,720,920}}
{{M,840,920},{C,840,800,760,680,840,600}}
{{M,840,600},{C,880,560,1040,520,1080,480}}
{{M,0,0},{V,240},{H,720},{C,720,240,1080,120,1080,120},{V,0}}
{{M,0,0},{H,1080},{V,120},{L,840,240},{H,0}}
{{M,0,600},{H,360},{L,600,840},{V,960},{H,0}}
{{M,1440,0},{H,1920},{V,960},{H,960},{V,840},{L,1200,800,1400,600,1440,360}}