Commit 8e965273 authored by Ryan LeFevre's avatar Ryan LeFevre

Add node searching and fix the browserify compile

parent c567b01d
fs = require 'fs'
browserify = require 'browserify'
task 'compile', 'Compile with browserify for the web', ->
browserify
noParse: [
'fs'
]
.transform('coffeeify')
.require('./shims/png.coffee', expose: './image_exports/png.coffee')
.add('./lib/psd.coffee')
.bundle (err, src) ->
throw err if err?
fs.writeFile './dist/psd.js', src, ->
console.log "Compiled to ./dist/psd.js"
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
module.exports = class Descriptor
constructor: (@file) ->
@data = {}
parse: ->
@data.class = @parseClass()
numItems = @file.readInt()
for i in [0...numItems]
id, value = @parseKeyItem()
@data[id] = value
@data
parseClass: ->
name: @file.readUnicodeString()
id: @parseId()
parseId: ->
len = @file.readInt()
if len is 0 then @file.readString(4) else @file.readString(len)
parseKeyItem: ->
id = @parseId()
value = @parseItem()
return id, value
parseItem: (type = null) ->
type = @file.readString(4) unless type?
switch type
when 'bool' then @parseBoolean()
when 'type', 'GlbC' then @parseClass()
when 'Objc', 'GlbO' then new Descriptor(@file).parse()
when 'doub' then @parseDouble()
when 'enum' then @parseEnum()
when 'alis' then @parseAlias()
when 'Pth' then @parseFilePath()
when 'long' then @parseInteger()
when 'comp' then @parseLargeInteger()
when 'VlLs' then @parseList()
when 'ObAr' then @parseObjectArray()
when 'tdta' then @parseRawData()
when 'obj ' then @parseReference()
when 'TEXT' then @file.readUnicodeString()
when 'UntF' then @parseUnitDouble()
when 'UnFl' then @parseUnitFloat()
parseBoolean: -> @file.readBoolean()
parseDouble: -> @file.readDouble()
parseInteger: -> @file.readInt()
parseLargeInteger: -> @file.readLongLong()
parseIdentifier: -> @file.readInt()
parseIndex: -> @file.readInt()
parseOffset: -> @file.readInt()
parseProperty: ->
class: @parseClass()
id: @parseId()
parseEnum: ->
type: @parseId()
value: @parseId()
parseEnumReference: ->
class: @parseClass()
type: @parseId()
value: @parseId()
parseAlias: ->
len = @file.readInt()
@file.readString(len)
parseFilePath: ->
len = @file.readInt()
sig = @file.readString(4)
# Little endian
pathSize = @file.read('<i')
numChars = @file.read('<i')
path = @file.readUnicodeString(numChars)
sig: sig
path: path
parseList: ->
count = @file.readInt()
items = []
for i in [0...count]
items.push @parseItem()
items
parseObjectArray: ->
\ No newline at end of file
...@@ -21,6 +21,9 @@ module.exports = class File ...@@ -21,6 +21,9 @@ module.exports = class File
Double: Double:
code: '>d' code: '>d'
length: 8 length: 8
LongLong:
code: '>q'
length: 8
for own format, info of FORMATS then do (format, info) => for own format, info of FORMATS then do (format, info) =>
@::["read#{format}"] = -> @readf(info.code, info.length)[0] @::["read#{format}"] = -> @readf(info.code, info.length)[0]
...@@ -47,4 +50,5 @@ module.exports = class File ...@@ -47,4 +50,5 @@ module.exports = class File
.join('') .join('')
.replace(/\u0000/g, '') .replace(/\u0000/g, '')
readByte: -> @read(1)[0] readByte: -> @read(1)[0]
\ No newline at end of file readBoolean: -> readByte() isnt 0
\ No newline at end of file
_ = require 'lodash'
module.exports =
childrenAtPath: (path, opts = {}) ->
unless Array.isArray(path)
path = path.split('/').filter((p) -> p.length > 0)
path = _.clone(path)
query = path.shift()
matches = @children().filter (c) ->
if opts.caseSensitive
c.name is query
else
c.name.toLowerCase() is query.toLowerCase()
if path.length is 0
return matches
else
return _.flatten matches.map (m) ->
m.childrenAtPath(_.clone(path), opts)
\ No newline at end of file
...@@ -4,18 +4,13 @@ ...@@ -4,18 +4,13 @@
"dependencies": { "dependencies": {
"coffee-script": "~ 1.7.1", "coffee-script": "~ 1.7.1",
"jspack": "~ 0.0.3", "jspack": "~ 0.0.3",
"coffeescript-module": "~ 0.0.2", "coffeescript-module": "~ 0.1.1",
"png": "~ 3.0.3", "png": "~ 3.0.3",
"rsvp": "~ 3.0.6", "rsvp": "~ 3.0.6",
"lodash": "~ 2.4" "lodash": "~ 2.4"
}, },
"devDependencies": { "devDependencies": {
"coffeeify": "~ 0.6.0" "coffeeify": "~ 0.6.0",
}, "browserify": "~ 3.46.0"
"browserify": {
"transform": ["coffeeify"]
},
"scripts": {
"compile": "browserify ./lib/psd.coffee > ./dist/psd.js"
} }
} }
\ No newline at end of file
module.exports =
toPng: ->
new RSVP.Promise (resolve, reject) =>
# Draw the pixels to the canvas
canvas = document.createElement('canvas')
canvas.width = @width()
canvas.height = @height()
context = canvas.getContext('2d')
imageData = context.getImageData(0, 0, @width(), @height())
pixelData = imageData.data
pixelData[i] = pixel for pixel, i in @pixelData
# Create the image and set the source to the
# canvas data URL.
image = new Image()
image.width = @width()
image.height = @height()
image.src = canvas.toDataURL 'image/png'
resolve(image)
saveAsPng: ->
throw "Not available in the browser. Use toPng() instead."
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment