Commit 1bd1167c authored by Ryan LeFevre's avatar Ryan LeFevre

Add layer info parsing and unicode names

parent 28ac2075
{jspack} = require('jspack') {jspack} = require 'jspack'
Util = require './util.coffee'
module.exports = class File module.exports = class File
FORMATS = FORMATS =
...@@ -38,4 +39,12 @@ module.exports = class File ...@@ -38,4 +39,12 @@ module.exports = class File
seek: (amt, rel = false) -> if rel then @pos += amt else @pos = amt seek: (amt, rel = false) -> if rel then @pos += amt else @pos = amt
readString: (length) -> @read(length).toString().replace /\u0000/g, '' readString: (length) -> @read(length).toString().replace /\u0000/g, ''
readUnicodeString: (length = null) ->
length or= @readInt()
@read(length * 2)
.toJSON()
.map((c) -> Util.getUnicodeCharacter(c))
.join('')
.replace(/\u0000/g, '')
readByte: -> @read(1)[0] readByte: -> @read(1)[0]
\ No newline at end of file
...@@ -6,6 +6,7 @@ module.exports = class Layer extends Module ...@@ -6,6 +6,7 @@ module.exports = class Layer extends Module
@includes require('./layer/mask.coffee') @includes require('./layer/mask.coffee')
@includes require('./layer/blending_ranges.coffee') @includes require('./layer/blending_ranges.coffee')
@includes require('./layer/name.coffee') @includes require('./layer/name.coffee')
@includes require('./layer/info.coffee')
# @includes require('./layer/channel_image.coffee') # @includes require('./layer/channel_image.coffee')
constructor: (@file, @header) -> constructor: (@file, @header) ->
...@@ -28,6 +29,7 @@ module.exports = class Layer extends Module ...@@ -28,6 +29,7 @@ module.exports = class Layer extends Module
@parseMaskData() @parseMaskData()
@parseBlendingRanges() @parseBlendingRanges()
@parseLegacyLayerName() @parseLegacyLayerName()
@parseLayerInfo()
@file.seek @layerEnd @file.seek @layerEnd
return @ return @
......
LazyExecute = require '../lazy_execute.coffee'
Util = require '../util.coffee'
LAYER_INFO = {
name: require('../layer_info/unicode_name.coffee')
}
module.exports =
parseLayerInfo: ->
while @file.tell() < @layerEnd
@file.seek 4, true # sig
key = @file.readString(4)
length = Util.pad2 @file.readInt()
pos = @file.tell()
keyParseable = false
for own name, klass of LAYER_INFO
continue unless klass.shouldParse(key)
i = new klass(@, length)
@adjustments[name] = new LazyExecute(i, @file)
.now('skip')
.later('parse')
.get()
keyParseable = true
break
@file.seek length, true if not keyParseable
\ No newline at end of file
module.exports = class LayerInfo
constructor: (@layer, @length) ->
@file = @layer.file
@section_end = @file.tell() + @length
@data = {}
skip: -> @file.seek @section_end
parse: -> @skip() # skip by default
\ No newline at end of file
LayerInfo = require '../layer_info.coffee'
module.exports = class UnicodeName extends LayerInfo
@shouldParse: (key) -> key is 'luni'
parse: ->
pos = @file.tell()
@data = @file.readUnicodeString()
@file.seek pos + @length
return @
\ No newline at end of file
module.exports = class Util module.exports = class Util
@pad2: (i) -> (i + 1) & ~0x01 @pad2: (i) -> (i + 1) & ~0x01
@pad4: (i) -> ((i + 4) & ~0x03) - 1 @pad4: (i) -> ((i + 4) & ~0x03) - 1
@getUnicodeCharacter: (cp) ->
if cp >= 0 and cp <= 0xD7FF or cp >= 0xE000 and cp <= 0xFFFF
return String.fromCharCode(cp)
else if cp >= 0x10000 and cp <= 0x10FFFF
# we substract 0x10000 from cp to get a 20-bits number
# in the range 0..0xFFFF
cp -= 0x10000
# we add 0xD800 to the number formed by the first 10 bits
# to give the first byte
first = ((0xffc00 & cp) >> 10) + 0xD800
# we add 0xDC00 to the number formed by the low 10 bits
# to give the second byte
second = (0x3ff & cp) + 0xDC00
String.fromCharCode(first) + String.fromCharCode(second)
\ 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