diff --git a/build.js b/build.js index 82b2408a..b91d79aa 100644 --- a/build.js +++ b/build.js @@ -164,6 +164,7 @@ var filesToInclude = [ ifSpecifiedInclude('easing', 'src/util/anim_ease.js'), ifSpecifiedInclude('parser', 'src/parser.js'), + ifSpecifiedInclude('parser', 'src/elements_parser.js'), 'src/point.class.js', 'src/intersection.class.js', diff --git a/dist/all.js b/dist/all.js index b9912892..707a7956 100644 --- a/dist/all.js +++ b/dist/all.js @@ -4613,47 +4613,7 @@ fabric.util.string = { * @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created. */ function parseElements(elements, callback, options, reviver) { - var instances = new Array(elements.length), i = elements.length; - - function checkIfDone() { - if (--i === 0) { - instances = instances.filter(function(el) { - return el != null; - }); - resolveGradients(instances); - callback(instances); - } - } - - for (var index = 0, el, len = elements.length; index < len; index++) { - el = elements[index]; - var klass = fabric[capitalize(el.tagName)]; - if (klass && klass.fromElement) { - try { - if (klass.async) { - klass.fromElement(el, (function(index, el) { - return function(obj) { - reviver && reviver(el, obj); - instances.splice(index, 0, obj); - checkIfDone(); - }; - })(index, el), options); - } - else { - var obj = klass.fromElement(el, options); - reviver && reviver(el, obj); - instances.splice(index, 0, obj); - checkIfDone(); - } - } - catch(err) { - fabric.log(err); - } - } - else { - checkIfDone(); - } - } + fabric.ElementsParser.parse(elements, callback, options, reviver); } /** @@ -5054,12 +5014,82 @@ fabric.util.string = { createSVGFontFacesMarkup: createSVGFontFacesMarkup, createSVGRefElementsMarkup: createSVGRefElementsMarkup, - getGradientDefs: getGradientDefs + getGradientDefs: getGradientDefs, + resolveGradients: resolveGradients }); })(typeof exports !== 'undefined' ? exports : this); +fabric.ElementsParser = { + + parse: function(elements, callback, options, reviver) { + + this.elements = elements; + this.callback = callback; + this.options = options; + this.reviver = reviver; + + this.instances = new Array(elements.length); + this.numElements = elements.length; + + this.createObjects(); + }, + + createObjects: function() { + for (var i = 0, len = this.elements.length; i < len; i++) { + this.createObject(this.elements[i], i); + } + }, + + createObject: function(el, index) { + var klass = fabric[fabric.util.string.capitalize(el.tagName)]; + if (klass && klass.fromElement) { + try { + this._createObject(klass, el, index); + } + catch(err) { + fabric.log(err); + } + } + else { + this.checkIfDone(); + } + }, + + _createObject: function(klass, el, index) { + if (klass.async) { + klass.fromElement(el, this.createCallback(index, el), this.options); + } + else { + var obj = klass.fromElement(el, this.options); + this.reviver && this.reviver(el, obj); + this.instances.splice(index, 0, obj); + this.checkIfDone(); + } + }, + + createCallback: function(index, el) { + var _this = this; + return function(obj) { + _this.reviver && _this.reviver(el, obj); + _this.instances.splice(index, 0, obj); + _this.checkIfDone(); + }; + }, + + checkIfDone: function() { + if (--this.numElements === 0) { + this.instances = this.instances.filter(function(el) { + return el != null; + }); + fabric.resolveGradients(this.instances); + this.callback(this.instances); + } + } +}; + + (function(global) { "use strict"; diff --git a/dist/all.min.js b/dist/all.min.js index 8ab58397..de75d2ac 100644 --- a/dist/all.min.js +++ b/dist/all.min.js @@ -1,7 +1,7 @@ /* build: `node build.js modules=ALL exclude=gestures minifier=uglifyjs` *//*! Fabric.js Copyright 2008-2013, Printio (Juriy Zaytsev, Maxim Chernyak) */var fabric=fabric||{version:"1.3.9"};typeof exports!="undefined"&&(exports.fabric=fabric),typeof document!="undefined"&&typeof window!="undefined"?(fabric.document=document,fabric.window=window):(fabric.document=require("jsdom").jsdom("
"),fabric.window=fabric.document.createWindow()),fabric.isTouchSupported="ontouchstart"in fabric.document.documentElement,fabric.isLikelyNode=typeof Buffer!="undefined"&&typeof window=="undefined",fabric.SHARED_ATTRIBUTES=["transform","fill","fill-opacity","fill-rule","opacity","stroke","stroke-dasharray","stroke-linecap","stroke-linejoin","stroke-miterlimit","stroke-opacity","stroke-width"];var Cufon=function(){function r(e){var t=this.face=e.face;this.glyphs=e.glyphs,this.w=e.w,this.baseSize=parseInt(t["units-per-em"],10),this.family=t["font-family"].toLowerCase(),this.weight=t["font-weight"],this.style=t["font-style"]||"normal",this.viewBox=function(){var e=t.bbox.split(/\s+/),n={minX:parseInt(e[0],10),minY:parseInt(e[1],10),maxX:parseInt(e[2],10),maxY:parseInt(e[3],10)};return n.width=n.maxX-n.minX,n.height=n.maxY-n.minY,n.toString=function(){return[this.minX,this.minY,this.width,this.height].join(" ")},n}(),this.ascent=-parseInt(t.ascent,10),this.descent=-parseInt(t.descent,10),this.height=-this.ascent+this.descent}function i(){var e={},t={oblique:"italic",italic:"oblique"};this.add=function(t){(e[t.style]||(e[t.style]={}))[t.weight]=t},this.get=function(n,r){var i=e[n]||e[t[n]]||e.normal||e.italic||e.oblique;if(!i)return null;r={normal:400,bold:700}[r]||parseInt(r,10);if(i[r])return i[r];var s={1:1,99:0}[r%100],o=[],u,a;s===undefined&&(s=r>400),r==500&&(r=400);for(var f in i){f=parseInt(f,10);if(!u||fa)a=f;o.push(f)}return ra&&(r=a),o.sort(function(e,t){return(s?e>r&&t>r?e-1},complexity:function(){return this.getObjects().reduce(function(e,t){return e+=t.complexity?t.complexity():0,e},0)}},function(e){function r(e,t){var n=e.indexOf(t);return n!==-1&&e.splice(n,1),e}function i(e,t){return Math.floor(Math.random()*(t-e+1))+e}function o(e){return e*s}function u(e){return e/s}function a(e,t,n){var r=Math.sin(n),i=Math.cos(n);e.subtractEquals(t);var s=e.x*i-e.y*r,o=e.x*r+e.y*i;return(new fabric.Point(s,o)).addEquals(t)}function f(e,t){return parseFloat(Number(e).toFixed(t))}function l(){return!1}function c(e,t){return e=fabric.util.string.camelize(e.charAt(0).toUpperCase()+e.slice(1)),h(t)[e]}function h(t){if(!t)return fabric;var n=t.split("."),r=n.length,i=e||fabric.window;for(var s=0;s =t})}function r(e,t){return i(e,t,function(e,t){return e>>0,n=0,r;if(arguments.length>1)r=arguments[1];else do{if(n in this){r=this[n++];break}if(++n>=t)throw new TypeError}while(!0);for(;n0&&(i.status="Intersection"),i},t.Intersection.intersectPolygonPolygon=function(e,t){var r=new n,i=e.length;for(var s=0;s0&&(r.status="Intersection"),r},t.Intersection.intersectPolygonRectangle=function(e,r,i){var s=r.min(i),o=r.max(i),u=new t.Point(o.x,s.y),a=new t.Point(s.x,o.y),f=n.intersectLinePolygon(s,u,e),l=n.intersectLinePolygon(u,o,e),c=n.intersectLinePolygon(o,a,e),h=n.intersectLinePolygon(a,s,e),p=new n;return p.appendPoints(f.points),p.appendPoints(l.points),p.appendPoints(c.points),p.appendPoints(h.points),p.points.length>0&&(p.status="Intersection"),p}}(typeof exports!="undefined"?exports:this),function(e){"use strict";function n(e){e?this._tryParsingColor(e):this.setSource([0,0,0,1])}function r(e,t,n){return n<0&&(n+=1),n>1&&(n-=1),n<1/6?e+(t-e)*6*n:n<.5?t:n<2/3?e+(t-e)*(2/3-n)*6:e}var t=e.fabric||(e.fabric={});if(t.Color){t.warn("fabric.Color is already defined.");return}t.Color=n,t.Color.prototype={_tryParsingColor:function(e){var t;e in n.colorNameMap&&(e=n.colorNameMap[e]),t=n.sourceFromHex(e),t||(t=n.sourceFromRgb(e)),t||(t=n.sourceFromHsl(e)),t&&this.setSource(t)},_rgbToHsl:function(e,n,r){e/=255,n/=255,r/=255;var i,s,o,u=t.util.array.max([e,n,r]),a=t.util.array.min([e,n,r]);o=(u+a)/2;if(u===a)i=s=0;else{var f=u-a;s=o>.5?f/(2-u-a):f/(u+a);switch(u){case e:i=(n-r)/f+(n