1
0
Fork 0

canvas/svg.nas add optional basic support for linked images

canvas/api.nas add alternative signature for ellipse and circle
This commit is contained in:
Henning Stahlke 2018-12-11 23:38:29 +01:00
parent 07ac341b70
commit a91ebe4676
2 changed files with 42 additions and 5 deletions

View file

@ -1005,11 +1005,17 @@ var Path = {
#
# @param rx radius x
# @param ry radius y
# @param cx (optional) center x coordinate
# @param cx (optional) center x coordinate or vector [cx, cy]
# @param cy (optional) center y coordinate
ellipse: func(rx, ry, cx = nil, cy = nil) {
cx = num(cx) or 0;
cy = num(cy) or 0;
if (typeof(cx) == "vector") {
cy = cx[1];
cx = cx[0];
}
else {
cx = num(cx) or 0;
cy = num(cy) or 0;
}
me.moveTo(cx - rx, cy)
.arcSmallCW(rx, ry, 0, 2*rx, 0)
.arcSmallCW(rx, ry, 0, -2*rx, 0);
@ -1020,7 +1026,7 @@ var Path = {
# Add a circle to the path
#
# @param r radius
# @param cx (optional) center x coordinate
# @param cx (optional) center x coordinate or vector [cx, cy]
# @param cy (optional) center y coordinate
circle: func(r, cx = nil, cy = nil) {
return me.ellipse(r, r, cx, cy);

View file

@ -3,6 +3,8 @@
# @param group The canvas.Group instance to append the parsed elements to
# @param path The path of the svg file (absolute or relative to FG_ROOT)
# @param options Optional hash of options
# font-mapper func
# parse_images bool
var parsesvg = func(group, path, options = nil)
{
if( !isa(group, Group) )
@ -572,7 +574,7 @@ var parsesvg = func(group, path, options = nil)
if( dash and size(dash) > 3 )
# at least 2 comma separated values...
stack[-1].setStrokeDashArray(split(',', dash));
}
} #end path/rect/ellipse/circle
else if( name == "use" )
{
var ref = attr["xlink:href"];
@ -595,6 +597,35 @@ var parsesvg = func(group, path, options = nil)
append(defs_stack, "defs");
return;
}
else if (name == "image" and options["parse_images"])
{
var ref = attr["xlink:href"];
# ref must not be missing and shall not contain Windows path separator
# find("\\") is correct, backslash is control character and must be escaped
# by adding another backslash - otherwise parse error anywhere below
if (ref == nil or find("\\", ref) > -1)
{
return printlog("info", "Invalid or missing href in image tag: '" ~ ref ~ "'");
}
if (substr(ref, 0, 5) == "data:") {
return printlog("info", "Unsupported embedded image");
}
elsif (substr(ref, 0, 5) != "file:") {
# absolute paths seem to start with "file:"
# prepend relative paths with the path of SVG file and hope the image is there
# file access limitations apply
ref = io.dirname(path) ~ ref;
}
pushElement("image", attr["id"]);
if (attr["x"] != nil and attr["y"] != nil) {
stack[-1].setTranslation(attr["x"], attr["y"]);
}
if (attr["width"] != nil and attr["height"] != nil) {
stack[-1].setSize(attr["width"], attr["height"]);
}
stack[-1].setFile(ref);
}
else
{
printlog("info", "Skipping unknown element '" ~ name ~ "'");