diff options
author | Richard Stöckl <richard.stoeckl@aon.at> | 2019-08-28 16:46:46 +0200 |
---|---|---|
committer | Richard Stöckl <richard.stoeckl@aon.at> | 2019-08-28 16:46:46 +0200 |
commit | 7b84d27f314deae8fab88961e3b835cea57f13cd (patch) | |
tree | 2851f75260d7efcea9969018eec781c16b97ea6b | |
parent | 5336f2c016d4b1b5e9399a84aa4657d374dd5cb3 (diff) |
added SVG support for horizontal and vertical lines
-rw-r--r-- | svgbasic.go | 21 | ||||
-rw-r--r-- | svgwrite.go | 19 |
2 files changed, 38 insertions, 2 deletions
diff --git a/svgbasic.go b/svgbasic.go index cc35073..6900afa 100644 --- a/svgbasic.go +++ b/svgbasic.go @@ -31,7 +31,9 @@ func init() { pathCmdSub = strings.NewReplacer(",", " ", "L", " L ", "l", " l ", "C", " C ", "c", " c ", - "M", " M ", "m", " m ") + "M", " M ", "m", " m ", + "H", " H ", "h", " h ", + "V", " V ", "v", " v ") } // SVGBasicSegmentType describes a single curve or position segment @@ -79,6 +81,19 @@ func absolutizePath(segs []SVGBasicSegmentType) { segPtr.Cmd = 'C' x = segPtr.Arg[4] y = segPtr.Arg[5] + case 'H': + x = seg.Arg[0] + case 'h': + segPtr.Arg[0] += x + segPtr.Cmd = 'H' + x += seg.Arg[0] + case 'V': + y = seg.Arg[0] + case 'v': + segPtr.Arg[0] += y + segPtr.Cmd = 'V' + y += seg.Arg[0] + } } } @@ -127,8 +142,12 @@ func pathParse(pathStr string) (segs []SVGBasicSegmentType, err error) { setup(2) case 'C', 'c': // Absolute/relative Bézier curve: cx0, cy0, cx1, cy1, x1, y1 setup(6) + case 'H', 'h': // Absolute/relative horizontal line to: x + setup(1) case 'L', 'l': // Absolute/relative lineto: x, y setup(2) + case 'V', 'v': // Absolute/relative vertical line to: x + setup(1) case 'Z', 'z': // closepath instruction (takes no arguments) break default: diff --git a/svgwrite.go b/svgwrite.go index 7e455fb..a612539 100644 --- a/svgwrite.go +++ b/svgwrite.go @@ -29,8 +29,17 @@ func (f *Fpdf) SVGBasicWrite(sb *SVGBasicType, scale float64) { var cx0, cy0, cx1, cy1 float64 var path []SVGBasicSegmentType var seg SVGBasicSegmentType + sval := func(origin float64, arg int) float64 { + return origin + scale*seg.Arg[arg] + } + xval := func(arg int) float64 { + return sval(originX, arg) + } + yval := func(arg int) float64 { + return sval(originY, arg) + } val := func(arg int) (float64, float64) { - return originX + scale*seg.Arg[arg], originY + scale*seg.Arg[arg+1] + return xval(arg), yval(arg + 1) } for j := 0; j < len(sb.Segments) && f.Ok(); j++ { path = sb.Segments[j] @@ -50,6 +59,14 @@ func (f *Fpdf) SVGBasicWrite(sb *SVGBasicType, scale float64) { newX, newY = val(4) f.CurveCubic(x, y, cx0, cy0, newX, newY, cx1, cy1, "D") x, y = newX, newY + case 'H': + newX = xval(0) + f.Line(x, y, newX, y) + x = newX + case 'V': + newY = yval(0) + f.Line(x, y, x, newY) + y = newY case 'Z': f.Line(x, y, originX, originY) default: |