From a91ebe4676fe7adca45ed5f90551f983776ccc3a Mon Sep 17 00:00:00 2001 From: Henning Stahlke Date: Tue, 11 Dec 2018 23:38:29 +0100 Subject: [PATCH] canvas/svg.nas add optional basic support for linked images canvas/api.nas add alternative signature for ellipse and circle --- Nasal/canvas/api.nas | 14 ++++++++++---- Nasal/canvas/svg.nas | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/Nasal/canvas/api.nas b/Nasal/canvas/api.nas index a00a9e6cf..6dcfc26b6 100644 --- a/Nasal/canvas/api.nas +++ b/Nasal/canvas/api.nas @@ -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); diff --git a/Nasal/canvas/svg.nas b/Nasal/canvas/svg.nas index ca6e42696..e1914bbdd 100644 --- a/Nasal/canvas/svg.nas +++ b/Nasal/canvas/svg.nas @@ -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 ~ "'");