diff options
author | meltingice <meltingice8917@gmail.com> | 2012-07-17 22:56:58 -0400 |
---|---|---|
committer | meltingice <meltingice8917@gmail.com> | 2012-07-17 22:56:58 -0400 |
commit | a26a711e80ac891e1e9bd92a7cad79ec95c3bc64 (patch) | |
tree | cf736050f1da37b71a6bc01231b5f78c3a4270c5 | |
parent | Simplify pad4 function (diff) | |
download | psd.js-a26a711e80ac891e1e9bd92a7cad79ec95c3bc64.tar.xz psd.js-a26a711e80ac891e1e9bd92a7cad79ec95c3bc64.zip |
Clean up string parsing function
-rw-r--r-- | lib/psd.js | 9 | ||||
-rw-r--r-- | lib/psd.min.js | 3 | ||||
-rwxr-xr-x | src/psdfile.coffee | 5 |
3 files changed, 6 insertions, 11 deletions
@@ -1335,12 +1335,9 @@ var jspack = new JSPack(); ; }; PSDFile.prototype.readString = function(length) { - var i, ret, _i; - ret = []; - for (i = _i = 0; 0 <= length ? _i < length : _i > length; i = 0 <= length ? ++_i : --_i) { - ret[i] = String.fromCharCode(this.read(1)[0]); - } - return ret.join('').replace(/\u0000/g, ""); + var ret; + ret = String.fromCharCode.apply(null, this.read(length)); + return ret.replace(/\u0000/g, ""); }; PSDFile.prototype.readUnicodeString = function() { diff --git a/lib/psd.min.js b/lib/psd.min.js index 1234357..071043a 100644 --- a/lib/psd.min.js +++ b/lib/psd.min.js @@ -133,8 +133,7 @@ return obj;};PSDDescriptor.prototype.parseObjectArray=function(){var i,id,num,ty return values;};PSDDescriptor.prototype.parseRawData=function(){var len;len=this.file.readInt();return this.file.read(len);};PSDDescriptor.prototype.parseReference=function(){var form,klass,value;form=this.file.readString(4);klass=this.parseClass();value=(function(){switch(form){case"Clss":return null;case"Enmr":return this.parseEnum();case"Idnt":return this.parseIdentifier();case"indx":return this.parseIndex();case"name":return this.file.readUnicodeString();case"rele":return this.parseOffset();case"prop":return this.parseProperty();}}).call(this);return value;};PSDDescriptor.prototype.parseUnitDouble=function(){var unit,unitID,value;unitID=this.file.parseString(4);unit=(function(){switch(unitID){case"#Ang":return"Angle";case"#Rsl":return"Density";case"#Rlt":return"Distance";case"#Nne":return"None";case"#Prc":return"Percent";case"#Pxl":return"Pixels";case"#Mlm":return"Millimeters";case"#Pnt":return"Points";}})();value=this.file.readDouble();return{id:unitID,unit:unit,value:value};};return PSDDescriptor;})();PSDFile=(function(){PSDFile.prototype.unicodeRegex=/\\u([\d\w]{4})/gi;function PSDFile(data){this.data=data;this.pos=0;} PSDFile.prototype.tell=function(){return this.pos;};PSDFile.prototype.read=function(bytes){var i,_i,_results;_results=[];for(i=_i=0;0<=bytes?_i<bytes:_i>bytes;i=0<=bytes?++_i:--_i){_results.push(this.data[this.pos++]);} return _results;};PSDFile.prototype.seek=function(amount,rel){if(rel==null){rel=true;} -if(rel){return this.pos+=amount;}else{return this.pos=amount;}};PSDFile.prototype.readInt=function(){var int;int=this.readUInt();if(int>=0x80000000){return int-0x100000000;}else{return int;}};PSDFile.prototype.readUInt=function(){var b1,b2,b3,b4;b1=this.read(1)[0]<<24;b2=this.read(1)[0]<<16;b3=this.read(1)[0]<<8;b4=this.read(1)[0];return b1|b2|b3|b4;};PSDFile.prototype.readShortInt=function(){var int;int=this.readShortUInt();if(int>=0x8000){return int-0x10000;}else{return int;}};PSDFile.prototype.readShortUInt=function(){var b1,b2;b1=this.read(1)[0]<<8;b2=this.read(1)[0];return b1|b2;};PSDFile.prototype.readLongInt=function(){return this.readf(">l")[0];};PSDFile.prototype.readLongUInt=function(){return this.readf(">L")[0];};PSDFile.prototype.readDouble=function(){return this.readf(">d")[0];};PSDFile.prototype.readBoolean=function(){return this.read(1)[0]!==0;};PSDFile.prototype.readLongLong=function(){return this.read(8);};PSDFile.prototype.readULongLong=function(){return this.read(8);};PSDFile.prototype.readString=function(length){var i,ret,_i;ret=[];for(i=_i=0;0<=length?_i<length:_i>length;i=0<=length?++_i:--_i){ret[i]=String.fromCharCode(this.read(1)[0]);} -return ret.join('').replace(/\u0000/g,"");};PSDFile.prototype.readUnicodeString=function(){var len,str;len=this.readInt()*2;str=this.readf(">"+len+"s")[0];str=str.replace(this.unicodeRegex,function(match,grp){return String.fromCharCode(parseInt(grp,16));});return str.replace(/\u0000/g,"");};PSDFile.prototype.readLengthWithString=function(defaultLen){var length,str;if(defaultLen==null){defaultLen=4;} +if(rel){return this.pos+=amount;}else{return this.pos=amount;}};PSDFile.prototype.readInt=function(){var int;int=this.readUInt();if(int>=0x80000000){return int-0x100000000;}else{return int;}};PSDFile.prototype.readUInt=function(){var b1,b2,b3,b4;b1=this.read(1)[0]<<24;b2=this.read(1)[0]<<16;b3=this.read(1)[0]<<8;b4=this.read(1)[0];return b1|b2|b3|b4;};PSDFile.prototype.readShortInt=function(){var int;int=this.readShortUInt();if(int>=0x8000){return int-0x10000;}else{return int;}};PSDFile.prototype.readShortUInt=function(){var b1,b2;b1=this.read(1)[0]<<8;b2=this.read(1)[0];return b1|b2;};PSDFile.prototype.readLongInt=function(){return this.readf(">l")[0];};PSDFile.prototype.readLongUInt=function(){return this.readf(">L")[0];};PSDFile.prototype.readDouble=function(){return this.readf(">d")[0];};PSDFile.prototype.readBoolean=function(){return this.read(1)[0]!==0;};PSDFile.prototype.readLongLong=function(){return this.read(8);};PSDFile.prototype.readULongLong=function(){return this.read(8);};PSDFile.prototype.readString=function(length){var ret;ret=String.fromCharCode.apply(null,this.read(length));return ret.replace(/\u0000/g,"");};PSDFile.prototype.readUnicodeString=function(){var len,str;len=this.readInt()*2;str=this.readf(">"+len+"s")[0];str=str.replace(this.unicodeRegex,function(match,grp){return String.fromCharCode(parseInt(grp,16));});return str.replace(/\u0000/g,"");};PSDFile.prototype.readLengthWithString=function(defaultLen){var length,str;if(defaultLen==null){defaultLen=4;} length=this.read(1)[0];if(length===0){str=this.readString(defaultLen);}else{str=this.readString(length);} return str;};PSDFile.prototype.readBytesList=function(size){return this.read(size);};PSDFile.prototype.readSpaceColor=function(){var colorComponent,colorSpace,i,_i;colorSpace=this.readShortInt();colorComponent=[];for(i=_i=0;_i<4;i=++_i){colorComponent.push(this.readShortInt()>>8);} return PSDColor.colorSpaceToARGB(colorSpace,colorComponent);};PSDFile.prototype.readf=function(format){return jspack.Unpack(format,this.read(jspack.CalcLength(format)));};PSDFile.prototype.skipBlock=function(desc){var n;if(desc==null){desc="unknown";} diff --git a/src/psdfile.coffee b/src/psdfile.coffee index 577a368..083b0d4 100755 --- a/src/psdfile.coffee +++ b/src/psdfile.coffee @@ -71,9 +71,8 @@ class PSDFile # null-byte padded, we strip out these null bytes since they are of no # use to us in Javascript. readString: (length) -> - ret = [] - ret[i] = String.fromCharCode(@read(1)[0]) for i in [0...length] - ret.join('').replace /\u0000/g, "" + ret = String.fromCharCode.apply null, @read(length) + ret.replace /\u0000/g, "" readUnicodeString: -> len = @readInt() * 2 |