Merge pull request #39 from guzba/master

support ellipse
This commit is contained in:
treeform 2020-12-20 10:23:02 -08:00 committed by GitHub
commit 645590fb1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 10 deletions

View file

@ -186,22 +186,35 @@ proc draw(
if ctx.stroke != ColorRGBA() and ctx.strokeWidth > 0:
img.strokePath(path, ctx.stroke, ctx.strokeWidth, ctx.transform)
of "circle":
of "circle", "ellipse":
# Reference for magic constant:
# https://dl3.pushbulletusercontent.com/a3fLVC8boTzRoxevD1OgCzRzERB9z2EZ/unknown.png
let
ctx = decodeCtx(ctxStack[^1], node)
let ctx = decodeCtx(ctxStack[^1], node)
var cx, cy: float32 # Default to 0.0 unless set by cx and cy on node
if node.attr("cx") != "":
cx = parseFloat(node.attr("cx"))
if node.attr("cy") != "":
cy = parseFloat(node.attr("cy"))
r = parseFloat(node.attr("r"))
magic = (4.0 * (-1.0 + sqrt(2.0)) / 3) * r
var rx, ry: float32
if node.tag == "circle":
rx = parseFloat(node.attr("r"))
ry = rx
else:
rx = parseFloat(node.attr("rx"))
ry = parseFloat(node.attr("ry"))
let
magicX = (4.0 * (-1.0 + sqrt(2.0)) / 3) * rx
magicY = (4.0 * (-1.0 + sqrt(2.0)) / 3) * ry
let path = newPath()
path.moveTo(cx + r, cy)
path.bezierCurveTo(cx + r, cy + magic, cx + magic, cy + r, cx, cy + r)
path.bezierCurveTo(cx - magic, cy + r, cx - r, cy + magic, cx - r, cy)
path.bezierCurveTo(cx - r, cy - magic, cx - magic, cy - r, cx, cy - r)
path.bezierCurveTo(cx + magic, cy - r, cx + r, cy - magic, cx + r, cy)
path.moveTo(cx + rx, cy)
path.bezierCurveTo(cx + rx, cy + magicY, cx + magicX, cy + ry, cx, cy + ry)
path.bezierCurveTo(cx - magicX, cy + ry, cx - rx, cy + magicY, cx - rx, cy)
path.bezierCurveTo(cx - rx, cy - magicY, cx - magicX, cy - ry, cx, cy - ry)
path.bezierCurveTo(cx + magicX, cy - ry, cx + rx, cy - magicY, cx + rx, cy)
path.closePath()
let d = $path

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View file

@ -0,0 +1,20 @@
<?xml version="1.0" standalone="no"?>
<svg width="12cm" height="4cm" viewBox="0 0 1200 400"
xmlns="http://www.w3.org/2000/svg" version="1.1">
<desc>Example ellipse01 - examples of ellipses</desc>
<!-- Show outline of viewport using 'rect' element -->
<rect x="1" y="1" width="1198" height="398"
fill="none" stroke="blue" stroke-width="2" />
<g transform="translate(300 200)">
<ellipse rx="250" ry="100"
fill="red" />
</g>
<ellipse transform="translate(900 200) rotate(-30)"
rx="250" ry="100"
fill="none" stroke="blue" stroke-width="20" />
</svg>

After

Width:  |  Height:  |  Size: 609 B

View file

@ -7,6 +7,7 @@ const files = [
"rect01",
"rect02",
"circle01",
"ellipse01",
"triangle01",
"quad01",
"Ghostscript_Tiger"