| | |
| | | svgedit.utilities.toXml = function(str) { |
| | | return $('<p/>').text(str).html(); |
| | | }; |
| | | |
| | | |
| | | // Function: svgedit.utilities.fromXml |
| | | // Converts XML entities in a string to single characters. |
| | | // Converts XML entities in a string to single characters. |
| | | // Example: "&" becomes "&" |
| | | // |
| | | // Parameters: |
| | | // str - The string to be converted |
| | | // |
| | | // Returns: |
| | | // Returns: |
| | | // The converted string |
| | | svgedit.utilities.fromXml = function(str) { |
| | | return $('<p/>').html(str).text(); |
| | |
| | | // else if (c > 127) { |
| | | // if (c < 2048){ |
| | | // output += String.fromCharCode((c >> 6) | 192); |
| | | // } |
| | | // } |
| | | // else { |
| | | // output += String.fromCharCode((c >> 12) | 224) + String.fromCharCode((c >> 6) & 63 | 128); |
| | | // } |
| | |
| | | // return output; |
| | | // }, |
| | | |
| | | // Function: svgedit.utilities.convertToXMLReferences |
| | | // Function: svgedit.utilities.convertToXMLReferences |
| | | // Converts a string to use XML references |
| | | svgedit.utilities.convertToXMLReferences = function(input) { |
| | | var output = ''; |
| | |
| | | try{ |
| | | var dXML = (window.DOMParser)?new DOMParser():new ActiveXObject("Microsoft.XMLDOM"); |
| | | dXML.async = false; |
| | | } catch(e){ |
| | | throw new Error("XML Parser could not be instantiated"); |
| | | } catch(e){ |
| | | throw new Error("XML Parser could not be instantiated"); |
| | | }; |
| | | try{ |
| | | if(dXML.loadXML) out = (dXML.loadXML(sXML))?dXML:false; |
| | |
| | | |
| | | // Function: svgedit.utilities.bboxToObj |
| | | // Converts a SVGRect into an object. |
| | | // |
| | | // |
| | | // Parameters: |
| | | // bbox - a SVGRect |
| | | // |
| | | // |
| | | // Returns: |
| | | // An object with properties names x, y, width, height. |
| | | svgedit.utilities.bboxToObj = function(bbox) { |
| | |
| | | }; |
| | | |
| | | // Function: svgedit.utilities.getUrlFromAttr |
| | | // Extracts the URL from the url(...) syntax of some attributes. |
| | | // Extracts the URL from the url(...) syntax of some attributes. |
| | | // Three variants: |
| | | // * <circle fill="url(someFile.svg#foo)" /> |
| | | // * <circle fill="url('someFile.svg#foo')" /> |
| | |
| | | // |
| | | // Parameters: |
| | | // attrVal - The attribute value as a string |
| | | // |
| | | // |
| | | // Returns: |
| | | // String with just the URL, like someFile.svg#foo |
| | | svgedit.utilities.getUrlFromAttr = function(attrVal) { |
| | | if (attrVal) { |
| | | if (attrVal) { |
| | | // url("#somegrad") |
| | | if (attrVal.indexOf('url("') === 0) { |
| | | return attrVal.substring(5,attrVal.indexOf('"',6)); |
| | |
| | | // Get correct BBox for a path in Webkit |
| | | // Converted from code found here: |
| | | // http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html |
| | | // |
| | | // |
| | | // Parameters: |
| | | // path - The path DOM element to get the BBox for |
| | | // |
| | |
| | | svgedit.utilities.getPathBBox = function(path) { |
| | | var seglist = path.pathSegList; |
| | | var tot = seglist.numberOfItems; |
| | | |
| | | |
| | | var bounds = [[], []]; |
| | | var start = seglist.getItem(0); |
| | | var P0 = [start.x, start.y]; |
| | |
| | | // Add actual points to limits |
| | | bounds[0].push(P0[0]); |
| | | bounds[1].push(P0[1]); |
| | | |
| | | |
| | | if(seg.x1) { |
| | | var P1 = [seg.x1, seg.y1], |
| | | P2 = [seg.x2, seg.y2], |
| | |
| | | for(var j=0; j < 2; j++) { |
| | | |
| | | var calc = function(t) { |
| | | return Math.pow(1-t,3) * P0[j] |
| | | return Math.pow(1-t,3) * P0[j] |
| | | + 3 * Math.pow(1-t,2) * t * P1[j] |
| | | + 3 * (1-t) * Math.pow(t,2) * P2[j] |
| | | + Math.pow(t,3) * P3[j]; |
| | |
| | | var b = 6 * P0[j] - 12 * P1[j] + 6 * P2[j]; |
| | | var a = -3 * P0[j] + 9 * P1[j] - 9 * P2[j] + 3 * P3[j]; |
| | | var c = 3 * P1[j] - 3 * P0[j]; |
| | | |
| | | |
| | | if(a == 0) { |
| | | if(b == 0) { |
| | | continue; |
| | |
| | | } |
| | | continue; |
| | | } |
| | | |
| | | |
| | | var b2ac = Math.pow(b,2) - 4 * c * a; |
| | | if(b2ac < 0) continue; |
| | | var t1 = (-b + Math.sqrt(b2ac))/(2 * a); |
| | |
| | | bounds[1].push(seg.y); |
| | | } |
| | | } |
| | | |
| | | |
| | | var x = Math.min.apply(null, bounds[0]); |
| | | var w = Math.max.apply(null, bounds[0]) - x; |
| | | var y = Math.min.apply(null, bounds[1]); |
| | |
| | | // Note that performance is currently terrible, so some way to improve would |
| | | // be great. |
| | | // |
| | | // Parameters: |
| | | // Parameters: |
| | | // selected - Container or <use> DOM element |
| | | function groupBBFix(selected) { |
| | | if(svgedit.browser.supportsHVLineContainerBBox()) { |
| | | try { return selected.getBBox();} catch(e){} |
| | | try { return selected.getBBox();} catch(e){} |
| | | } |
| | | var ref = $.data(selected, 'ref'); |
| | | var matched = null; |
| | | |
| | | |
| | | if(ref) { |
| | | var copy = $(ref).children().clone().attr('visibility', 'hidden'); |
| | | $(svgroot_).append(copy); |
| | |
| | | } else { |
| | | matched = $(selected).find('line, path'); |
| | | } |
| | | |
| | | |
| | | var issue = false; |
| | | if(matched.length) { |
| | | matched.each(function() { |
| | |
| | | if (elem.nodeType != 1) return null; |
| | | var ret = null; |
| | | var elname = selected.nodeName; |
| | | |
| | | |
| | | switch ( elname ) { |
| | | case 'text': |
| | | if(selected.textContent === '') { |
| | |
| | | if(elname === 'use') { |
| | | ret = groupBBFix(selected, true); |
| | | } |
| | | |
| | | |
| | | if(elname === 'use') { |
| | | if(!ret) ret = selected.getBBox(); |
| | | //if(!svgedit.browser.isWebkit()) { |
| | |
| | | // ret = bb; |
| | | //} |
| | | } else if(~visElems_arr.indexOf(elname)) { |
| | | try { ret = selected.getBBox();} |
| | | catch(e) { |
| | | try { ret = selected.getBBox();} |
| | | catch(e) { |
| | | // Check if element is child of a foreignObject |
| | | var fo = $(selected).closest("foreignObject"); |
| | | if(fo.length) { |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | if(ret) { |
| | | ret = svgedit.utilities.bboxToObj(ret); |
| | | } |
| | |
| | | // xpath lookup |
| | | return domdoc_.evaluate( |
| | | 'svg:svg[@id="svgroot"]//svg:*[@id="'+id+'"]', |
| | | domcontainer_, |
| | | domcontainer_, |
| | | function() { return "http://www.w3.org/2000/svg"; }, |
| | | 9, |
| | | null).singleNodeValue; |
| | |
| | | // Function: assignAttributes |
| | | // Assigns multiple attributes to an element. |
| | | // |
| | | // Parameters: |
| | | // Parameters: |
| | | // node - DOM element to apply new attribute values to |
| | | // attrs - Object with attribute keys/values |
| | | // suspendLength - Optional integer of milliseconds to suspend redraw |
| | |
| | | svgedit.utilities.assignAttributes = function(node, attrs, suspendLength, unitCheck) { |
| | | |
| | | for (var i in attrs) { |
| | | var ns = (i.substr(0,4) === "xml:" ? XMLNS : |
| | | var ns = (i.substr(0,4) === "xml:" ? XMLNS : |
| | | i.substr(0,6) === "xlink:" ? XLINKNS : null); |
| | | |
| | | |
| | | if(ns) { |
| | | node.setAttributeNS(ns, i, attrs[i]); |
| | | } else if(!unitCheck) { |
| | |
| | | } else { |
| | | svgedit.units.setUnitAttr(node, i, attrs[i]); |
| | | } |
| | | |
| | | |
| | | } |
| | | }; |
| | | |
| | |
| | | 'rx':0, |
| | | 'ry':0 |
| | | } |
| | | |
| | | |
| | | for(var attr in defaults) { |
| | | var val = defaults[attr]; |
| | | if(element.getAttribute(attr) == val) { |
| | | element.removeAttribute(attr); |
| | | } |
| | | } |
| | | |
| | | |
| | | }; |
| | | |
| | | |
| | | })(); |
| | | })(); |