123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- if(!dojo._hasResource["dojox.gfx.utils"]){
- dojo._hasResource["dojox.gfx.utils"] = true;
- dojo.provide("dojox.gfx.utils");
- dojo.require("dojox.gfx");
- (function(){
- var d = dojo, g = dojox.gfx, gu = g.utils;
- dojo.mixin(gu, {
- forEach: function(
- /* dojox.gfx.Surface || dojox.gfx.Shape */ object,
- /*Function|String|Array*/ f, /*Object?*/ o
- ){
- o = o || d.global;
- f.call(o, object);
- if(object instanceof g.Surface || object instanceof g.Group){
- d.forEach(object.children, function(shape){
- gu.forEach(shape, f, o);
- });
- }
- },
- serialize: function(
- /* dojox.gfx.Surface || dojox.gfx.Shape */ object
- ){
- var t = {}, v, isSurface = object instanceof g.Surface;
- if(isSurface || object instanceof g.Group){
- t.children = d.map(object.children, gu.serialize);
- if(isSurface){
- return t.children;
- }
- }else{
- t.shape = object.getShape();
- }
- if(object.getTransform){
- v = object.getTransform();
- if(v){ t.transform = v; }
- }
- if(object.getStroke){
- v = object.getStroke();
- if(v){ t.stroke = v; }
- }
- if(object.getFill){
- v = object.getFill();
- if(v){ t.fill = v; }
- }
- if(object.getFont){
- v = object.getFont();
- if(v){ t.font = v; }
- }
- return t;
- },
- toJson: function(
- /* dojox.gfx.Surface || dojox.gfx.Shape */ object,
- /* Boolean? */ prettyPrint
- ){
- return d.toJson(gu.serialize(object), prettyPrint);
- },
- deserialize: function(
- /* dojox.gfx.Surface || dojox.gfx.Shape */ parent,
- /* dojox.gfx.Shape || Array */ object
- ){
- if(object instanceof Array){
- return d.map(object, d.hitch(null, gu.deserialize, parent));
- }
- var shape = ("shape" in object) ? parent.createShape(object.shape) : parent.createGroup();
- if("transform" in object){
- shape.setTransform(object.transform);
- }
- if("stroke" in object){
- shape.setStroke(object.stroke);
- }
- if("fill" in object){
- shape.setFill(object.fill);
- }
- if("font" in object){
- shape.setFont(object.font);
- }
- if("children" in object){
- d.forEach(object.children, d.hitch(null, gu.deserialize, shape));
- }
- return shape;
- },
- fromJson: function(
- /* dojox.gfx.Surface || dojox.gfx.Shape */ parent,
- /* String */ json){
- return gu.deserialize(parent, d.fromJson(json));
- },
- toSvg: function(/*GFX object*/surface){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var deferred = new dojo.Deferred();
-
- if(dojox.gfx.renderer === "svg"){
-
- try{
- var svg = gu._cleanSvg(gu._innerXML(surface.rawNode));
- deferred.callback(svg);
- }catch(e){
- deferred.errback(e);
- }
- }else{
-
-
- if (!gu._initSvgSerializerDeferred) {
- gu._initSvgSerializer();
- }
- var jsonForm = dojox.gfx.utils.toJson(surface);
- var serializer = function(){
- try{
- var sDim = surface.getDimensions();
- var width = sDim.width;
- var height = sDim.height;
-
- var node = gu._gfxSvgProxy.document.createElement("div");
- gu._gfxSvgProxy.document.body.appendChild(node);
-
- dojo.withDoc(gu._gfxSvgProxy.document, function() {
- dojo.style(node, "width", width);
- dojo.style(node, "height", height);
- }, this);
-
- var ts = gu._gfxSvgProxy[dojox._scopeName].gfx.createSurface(node, width, height);
-
-
- var draw = function(surface) {
- try{
- gu._gfxSvgProxy[dojox._scopeName].gfx.utils.fromJson(surface, jsonForm);
-
- var svg = gu._cleanSvg(node.innerHTML);
- surface.clear();
- surface.destroy();
- gu._gfxSvgProxy.document.body.removeChild(node);
- deferred.callback(svg);
- }catch(e){
- deferred.errback(e);
- }
- };
- ts.whenLoaded(null,draw);
- }catch (ex) {
- deferred.errback(ex);
- }
- };
-
-
- if(gu._initSvgSerializerDeferred.fired > 0){
- serializer();
- }else{
- gu._initSvgSerializerDeferred.addCallback(serializer);
- }
- }
- return deferred;
- },
-
- _gfxSvgProxy: null,
-
- _initSvgSerializerDeferred: null,
- _svgSerializerInitialized: function() {
-
-
-
-
- gu._initSvgSerializerDeferred.callback(true);
- },
- _initSvgSerializer: function(){
-
-
-
-
-
- if(!gu._initSvgSerializerDeferred){
- gu._initSvgSerializerDeferred = new dojo.Deferred();
- var f = dojo.doc.createElement("iframe");
- dojo.style(f, {
- display: "none",
- position: "absolute",
- width: "1em",
- height: "1em",
- top: "-10000px"
- });
- var intv;
- if(dojo.isIE){
- f.onreadystatechange = function(){
- if(f.contentWindow.document.readyState == "complete"){
- f.onreadystatechange = function() {};
- intv = setInterval(function() {
- if(f.contentWindow[dojo._scopeName] &&
- f.contentWindow[dojox._scopeName].gfx &&
- f.contentWindow[dojox._scopeName].gfx.utils){
- clearInterval(intv);
- f.contentWindow.parent[dojox._scopeName].gfx.utils._gfxSvgProxy = f.contentWindow;
- f.contentWindow.parent[dojox._scopeName].gfx.utils._svgSerializerInitialized();
- }
- }, 50);
- }
- };
- }else{
- f.onload = function(){
- f.onload = function() {};
- intv = setInterval(function() {
- if(f.contentWindow[dojo._scopeName] &&
- f.contentWindow[dojox._scopeName].gfx &&
- f.contentWindow[dojox._scopeName].gfx.utils){
- clearInterval(intv);
- f.contentWindow.parent[dojox._scopeName].gfx.utils._gfxSvgProxy = f.contentWindow;
- f.contentWindow.parent[dojox._scopeName].gfx.utils._svgSerializerInitialized();
- }
- }, 50);
- };
- }
-
- var uri = (dojo.config["dojoxGfxSvgProxyFrameUrl"]||dojo.moduleUrl("dojox", "gfx/resources/gfxSvgProxyFrame.html"));
- f.setAttribute("src", uri);
- dojo.body().appendChild(f);
- }
- },
- _innerXML: function(/*Node*/node){
-
-
-
-
-
-
- if(node.innerXML){
- return node.innerXML;
- }else if(node.xml){
- return node.xml;
- }else if(typeof XMLSerializer != "undefined"){
- return (new XMLSerializer()).serializeToString(node);
- }
- return null;
- },
- _cleanSvg: function(svg) {
-
-
-
-
- if(svg){
-
- if(svg.indexOf("xmlns=\"http://www.w3.org/2000/svg\"") == -1){
- svg = svg.substring(4, svg.length);
- svg = "<svg xmlns=\"http://www.w3.org/2000/svg\"" + svg;
- }
-
-
- svg = svg.replace(/\bdojoGfx\w*\s*=\s*(['"])\w*\1/g, "");
- }
- return svg;
- }
- });
- })();
- }
|