Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit af7ec83

Browse files
committed
[[ SVG ]] Simplify Opcodes
This patch makes a number of changes to the drawing format opcodes to simplify it. The relative path commands have been removed as the relative commands don't offer any advantage after compilation than their non-relative counterparts. This change reduces the number of path opcodes by half, allowing future use of the top 4-bits of the opcode for a repeat count. The paint and transform opcodes now define a single object, rather than a sequence. This better reflects the current static nature of the drawing format, where transforms can always be flattened to a single matrix and there can only be one paint defined at a time. Finally, a RoundedRectangle opcode has been added, better distinguishing between a rectangle with or without rounded corners.
1 parent 3bc83a2 commit af7ec83

File tree

2 files changed

+287
-364
lines changed

2 files changed

+287
-364
lines changed

extensions/script-libraries/drawing/drawing.livecodescript

Lines changed: 102 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ private command _svgCompileLine @xContext, pElement
11391139
put pElement["features"]["y1"] into tY1
11401140
put pElement["features"]["x2"] into tX2
11411141
put pElement["features"]["y2"] into tY2
1142-
_svgCompileShape xContext, "line", __svgBoxLine(tX1, tY1, tX2, tY2), tX1, tY1, tX2, tY2
1142+
_svgCompileShape xContext, "line", _svgBoxLine(tX1, tY1, tX2, tY2), tX1, tY1, tX2, tY2
11431143
end _svgCompileLine
11441144

11451145
private command _svgCompilePolyline @xContext, pElement
@@ -1157,6 +1157,7 @@ end _svgCompilePolygon
11571157
private command _svgCompilePath @xContext, pElement
11581158
local tD
11591159
put pElement["features"]["d"] into tD
1160+
11601161
_svgCompileShape xContext, "path", _svgBoxPath(tD), tD["array"]
11611162
end _svgCompilePath
11621163

@@ -1291,17 +1292,18 @@ constant kMCGDrawingOpcodeStrokeDashArray = 10
12911292
constant kMCGDrawingOpcodeStrokeDashOffset = 11
12921293
constant kMCGDrawingOpcodeStrokeMiterLimit = 12
12931294
constant kMCGDrawingOpcodeRectangle = 13
1294-
constant kMCGDrawingOpcodeCircle = 14
1295-
constant kMCGDrawingOpcodeEllipse = 15
1296-
constant kMCGDrawingOpcodeLine = 16
1297-
constant kMCGDrawingOpcodePolyline = 17
1298-
constant kMCGDrawingOpcodePolygon = 18
1299-
constant kMCGDrawingOpcodePath = 19
1300-
1301-
constant kMCGDrawingTransformOpcodeEnd = 0
1295+
constant kMCGDrawingOpcodeRoundedRectangle = 14
1296+
constant kMCGDrawingOpcodeCircle = 15
1297+
constant kMCGDrawingOpcodeEllipse = 16
1298+
constant kMCGDrawingOpcodeLine = 17
1299+
constant kMCGDrawingOpcodePolyline = 18
1300+
constant kMCGDrawingOpcodePolygon = 19
1301+
constant kMCGDrawingOpcodePath = 20
1302+
1303+
constant kMCGDrawingTransformOpcodeIdentity = 0
13021304
constant kMCGDrawingTransformOpcodeAffine = 1
13031305

1304-
constant kMCGDrawingPaintOpcodeEnd = 0
1306+
constant kMCGDrawingPaintOpcodeNone = 0
13051307
constant kMCGDrawingPaintOpcodeSolidColor = 1
13061308
constant kMCGDrawingPaintOpcodeLinearGradient = 2
13071309
constant kMCGDrawingPaintOpcodeRadialGradient = 3
@@ -1324,30 +1326,18 @@ constant kMCGDrawingStrokeLineCapOpcodeSquare = 2
13241326

13251327
constant kMCGDrawingPathOpcodeEnd = 0
13261328
constant kMCGDrawingPathOpcodeMoveTo = 1
1327-
constant kMCGDrawingPathOpcodeRelativeMoveTo = 2
1328-
constant kMCGDrawingPathOpcodeLineTo = 3
1329-
constant kMCGDrawingPathOpcodeRelativeLineTo = 4
1330-
constant kMCGDrawingPathOpcodeHorizontalTo = 5
1331-
constant kMCGDrawingPathOpcodeRelativeHorizontalTo = 6
1332-
constant kMCGDrawingPathOpcodeVerticalTo = 7
1333-
constant kMCGDrawingPathOpcodeRelativeVerticalTo = 8
1334-
constant kMCGDrawingPathOpcodeCubicTo = 9
1335-
constant kMCGDrawingPathOpcodeRelativeCubicTo = 10
1336-
constant kMCGDrawingPathOpcodeSmoothCubicTo = 11
1337-
constant kMCGDrawingPathOpcodeRelativeSmoothCubicTo = 12
1338-
constant kMCGDrawingPathOpcodeQuadraticTo= 13
1339-
constant kMCGDrawingPathOpcodeRelativeQuadraticTo = 14
1340-
constant kMCGDrawingPathOpcodeSmoothQuadraticTo = 15
1341-
constant kMCGDrawingPathOpcodeRelativeSmoothQuadraticTo = 16
1342-
constant kMCGDrawingPathOpcodeArcTo = 17
1343-
constant kMCGDrawingPathOpcodeRelativeArcTo = 18
1344-
constant kMCGDrawingPathOpcodeReflexArcTo = 19
1345-
constant kMCGDrawingPathOpcodeRelativeReflexArcTo = 20
1346-
constant kMCGDrawingPathOpcodeReverseArcTo = 21
1347-
constant kMCGDrawingPathOpcodeRelativeReverseArcTo = 22
1348-
constant kMCGDrawingPathOpcodeReverseReflexArcTo = 23
1349-
constant kMCGDrawingPathOpcodeRelativeReverseReflexArcTo = 24
1350-
constant kMCGDrawingPathOpcodeCloseSubpath = 25
1329+
constant kMCGDrawingPathOpcodeLineTo = 2
1330+
constant kMCGDrawingPathOpcodeHorizontalTo = 3
1331+
constant kMCGDrawingPathOpcodeVerticalTo = 4
1332+
constant kMCGDrawingPathOpcodeCubicTo = 5
1333+
constant kMCGDrawingPathOpcodeSmoothCubicTo = 6
1334+
constant kMCGDrawingPathOpcodeQuadraticTo= 7
1335+
constant kMCGDrawingPathOpcodeSmoothQuadraticTo = 8
1336+
constant kMCGDrawingPathOpcodeArcTo = 9
1337+
constant kMCGDrawingPathOpcodeReflexArcTo = 10
1338+
constant kMCGDrawingPathOpcodeReverseArcTo = 11
1339+
constant kMCGDrawingPathOpcodeReverseReflexArcTo = 12
1340+
constant kMCGDrawingPathOpcodeCloseSubpath = 13
13511341

13521342
private command _svgEncode @xContext, @rDrawing
13531343
put empty into xContext["opcodes"]
@@ -1426,7 +1416,11 @@ private command _svgEncode @xContext, @rDrawing
14261416
_svgEncodeOp xContext, kMCGDrawingOpcodeStrokeMiterLimit, tArgument
14271417
break
14281418
case "rect"
1429-
_svgEncodeOp xContext, kMCGDrawingOpcodeRectangle, tArgument[1], tArgument[2], tArgument[3], tArgument[4], tArgument[5], tArgument[6]
1419+
if tArgument[5] is 0 and tArgument[6] is 0 then
1420+
_svgEncodeOp xContext, kMCGDrawingOpcodeRectangle, tArgument[1], tArgument[2], tArgument[3], tArgument[4]
1421+
else
1422+
_svgEncodeOp xContext, kMCGDrawingOpcodeRoundedRectangle, tArgument[1], tArgument[2], tArgument[3], tArgument[4], tArgument[5], tArgument[6]
1423+
end if
14301424
break
14311425
case "circle"
14321426
_svgEncodeOp xContext, kMCGDrawingOpcodeCircle, tArgument[1], tArgument[2], tArgument[3]
@@ -1521,6 +1515,7 @@ end _svgEncodeScalars
15211515

15221516
private command _svgEncodePaint @xContext, pPaint
15231517
if pPaint[1] is "none" then
1518+
_svgEncodeOp xContext, kMCGDrawingPaintOpcodeNone
15241519
else if pPaint[1] is "color" then
15251520
_svgEncodeOp xContext, kMCGDrawingPaintOpcodeSolidColor, pPaint[2], pPaint[3], pPaint[4], pPaint[5]
15261521
else if pPaint[1] is "linear" or pPaint[1] is "radial" or pPaint[1] is "conical" then
@@ -1552,23 +1547,33 @@ private command _svgEncodePaint @xContext, pPaint
15521547
else
15531548
_InternalError format("unknown paint '%s'", pPaint[1])
15541549
end if
1555-
_svgEncodeOp xContext, kMCGDrawingPaintOpcodeEnd
15561550
end _svgEncodePaint
15571551

15581552
private command _svgEncodeTransform @xContext, pTransform
1559-
repeat for each element tTransform in pTransform
1553+
if the number of elements in pTransform > 1 then
1554+
throw "internal error - transform list length is not 1"
1555+
end if
1556+
1557+
if the number of elements in pTransform is 0 then
1558+
_svgEncodeOp xContext, kMCGDrawingTransformOpcodeIdentity
1559+
else
1560+
local tTransform
1561+
put pTransform[1] into tTransform
15601562
switch tTransform[1]
15611563
case "matrix"
15621564
_svgEncodeOp xContext, kMCGDrawingTransformOpcodeAffine, tTransform[2], tTransform[3], tTransform[4], tTransform[5], tTransform[6], tTransform[7]
15631565
break
15641566
default
15651567
_InternalError format("unknown transform '%s'", tTransform[1])
15661568
end switch
1567-
end repeat
1568-
_svgEncodeOp xContext, kMCGDrawingTransformOpcodeEnd
1569+
end if
15691570
end _svgEncodeTransform
15701571

15711572
private command _svgEncodePath @xContext, pPath
1573+
local tLastX, tLastY
1574+
put 0 into tLastX
1575+
put 0 into tLastY
1576+
15721577
local tScalarIndex
15731578
set the caseSensitive to true
15741579
put 1 into tScalarIndex
@@ -1579,58 +1584,90 @@ private command _svgEncodePath @xContext, pPath
15791584

15801585
local tOpcode
15811586
switch tCommand
1587+
case "m"
1588+
add tLastX to tScalars[1]
1589+
add tLastY to tScalars[2]
15821590
case "M"
15831591
put kMCGDrawingPathOpcodeMoveTo into tOpcode
1592+
put tScalars[1] into tLastX
1593+
put tScalars[2] into tLastY
15841594
break
1585-
case "m"
1586-
put kMCGDrawingPathOpcodeRelativeMoveTo into tOpcode
1587-
break
1595+
15881596
case "Z"
15891597
case "z"
15901598
put kMCGDrawingPathOpcodeCloseSubpath into tOpcode
15911599
break
1600+
1601+
case "l"
1602+
add tLastX to tScalars[1]
1603+
add tLastY to tScalars[2]
15921604
case "L"
15931605
put kMCGDrawingPathOpcodeLineTo into tOpcode
1606+
put tScalars[1] into tLastX
1607+
put tScalars[2] into tLastY
15941608
break
1595-
case "l"
1596-
put kMCGDrawingPathOpcodeRelativeLineTo into tOpcode
1597-
break
1609+
1610+
case "h"
1611+
add tLastX to tScalars[1]
15981612
case "H"
15991613
put kMCGDrawingPathOpcodeHorizontalTo into tOpcode
1614+
put tScalars[1] into tLastX
16001615
break
1601-
case "h"
1602-
put kMCGDrawingPathOpcodeRelativeHorizontalTo into tOpcode
1603-
break
1616+
1617+
case "v"
1618+
add tLastY to tScalars[1]
16041619
case "V"
16051620
put kMCGDrawingPathOpcodeVerticalTo into tOpcode
1621+
put tScalars[1] into tLastY
16061622
break
1607-
case "v"
1608-
put kMCGDrawingPathOpcodeRelativeVerticalTo into tOpcode
1609-
break
1623+
1624+
case "c"
1625+
add tLastX to tScalars[1]
1626+
add tLastY to tScalars[2]
1627+
add tLastX to tScalars[3]
1628+
add tLastY to tScalars[4]
1629+
add tLastX to tScalars[5]
1630+
add tLastY to tScalars[6]
16101631
case "C"
16111632
put kMCGDrawingPathOpcodeCubicTo into tOpcode
1633+
put tScalars[5] into tLastX
1634+
put tScalars[6] into tLastY
16121635
break
1613-
case "c"
1614-
put kMCGDrawingPathOpcodeRelativeCubicTo into tOpcode
1615-
break
1636+
1637+
case "s"
1638+
add tLastX to tScalars[1]
1639+
add tLastY to tScalars[2]
1640+
add tLastX to tScalars[3]
1641+
add tLastY to tScalars[4]
16161642
case "S"
16171643
put kMCGDrawingPathOpcodeSmoothCubicTo into tOpcode
1644+
put tScalars[3] into tLastX
1645+
put tScalars[4] into tLastY
16181646
break
1619-
case "s"
1620-
put kMCGDrawingPathOpcodeRelativeSmoothCubicTo into tOpcode
1621-
break
1647+
1648+
case "q"
1649+
add tLastX to tScalars[1]
1650+
add tLastY to tScalars[2]
1651+
add tLastX to tScalars[3]
1652+
add tLastY to tScalars[4]
16221653
case "Q"
16231654
put kMCGDrawingPathOpcodeQuadraticTo into tOpcode
1655+
put tScalars[3] into tLastX
1656+
put tScalars[4] into tLastY
16241657
break
1625-
case "q"
1626-
put kMCGDrawingPathOpcodeRelativeQuadraticTo into tOpcode
1627-
break
1658+
1659+
case "t"
1660+
add tLastX to tScalars[1]
1661+
add tLastY to tScalars[2]
16281662
case "T"
16291663
put kMCGDrawingPathOpcodeSmoothQuadraticTo into tOpcode
1664+
put tScalars[1] into tLastX
1665+
put tScalars[2] into tLastY
16301666
break
1631-
case "t"
1632-
put kMCGDrawingPathOpcodeRelativeSmoothQuadraticTo into tOpcode
1633-
break
1667+
1668+
case "a"
1669+
add tLastX to tScalars[6]
1670+
add tLastY to tScalars[7]
16341671
case "A"
16351672
if tScalars[4] is "0" then
16361673
if tScalars[5] is "0" then
@@ -1649,25 +1686,8 @@ private command _svgEncodePath @xContext, pPath
16491686
put tScalars[7] into tScalars[5]
16501687
delete variable tScalars[6]
16511688
delete variable tScalars[7]
1652-
break
1653-
case "a"
1654-
if tScalars[4] is "0" then
1655-
if tScalars[5] is "0" then
1656-
put kMCGDrawingPathOpcodeRelativeArcTo into tOpcode
1657-
else
1658-
put kMCGDrawingPathOpcodeRelativeReverseArcTo into tOpcode
1659-
end if
1660-
else
1661-
if tScalars[5] is "0" then
1662-
put kMCGDrawingPathOpcodeRelativeReflexArcTo into tOpcode
1663-
else
1664-
put kMCGDrawingPathOpcodeRelativeReverseReflexArcTo into tOpcode
1665-
end if
1666-
end if
1667-
put tScalars[6] into tScalars[4]
1668-
put tScalars[7] into tScalars[5]
1669-
delete variable tScalars[6]
1670-
delete variable tScalars[7]
1689+
put tScalars[4] into tLastX
1690+
put tScalars[5] into tLastY
16711691
break
16721692
end switch
16731693
_svgEncodeOpV xContext, tOpcode, tScalars

0 commit comments

Comments
 (0)