psd 3.4.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/README.md +196 -149
  2. package/dist/descriptor.d.ts +20 -0
  3. package/dist/descriptor.d.ts.map +1 -0
  4. package/dist/descriptor.js +114 -0
  5. package/dist/descriptor.js.map +1 -0
  6. package/dist/engine-data.d.ts +11 -0
  7. package/dist/engine-data.d.ts.map +1 -0
  8. package/dist/engine-data.js +82 -0
  9. package/dist/engine-data.js.map +1 -0
  10. package/dist/image.d.ts +52 -0
  11. package/dist/image.d.ts.map +1 -0
  12. package/dist/image.js +301 -0
  13. package/dist/image.js.map +1 -0
  14. package/dist/index.d.ts +16 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +10 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/layer-info.d.ts +494 -0
  19. package/dist/layer-info.d.ts.map +1 -0
  20. package/dist/layer-info.js +529 -0
  21. package/dist/layer-info.js.map +1 -0
  22. package/dist/layer-styles.d.ts +160 -0
  23. package/dist/layer-styles.d.ts.map +1 -0
  24. package/dist/layer-styles.js +183 -0
  25. package/dist/layer-styles.js.map +1 -0
  26. package/dist/layer.d.ts +143 -0
  27. package/dist/layer.d.ts.map +1 -0
  28. package/dist/layer.js +249 -0
  29. package/dist/layer.js.map +1 -0
  30. package/dist/nodes.d.ts +233 -0
  31. package/dist/nodes.d.ts.map +1 -0
  32. package/dist/nodes.js +326 -0
  33. package/dist/nodes.js.map +1 -0
  34. package/dist/path-record.d.ts +34 -0
  35. package/dist/path-record.d.ts.map +1 -0
  36. package/dist/path-record.js +44 -0
  37. package/dist/path-record.js.map +1 -0
  38. package/dist/png.d.ts +10 -0
  39. package/dist/png.d.ts.map +1 -0
  40. package/dist/png.js +85 -0
  41. package/dist/png.js.map +1 -0
  42. package/dist/psd.d.ts +49 -0
  43. package/dist/psd.d.ts.map +1 -0
  44. package/dist/psd.js +106 -0
  45. package/dist/psd.js.map +1 -0
  46. package/dist/reader.d.ts +41 -0
  47. package/dist/reader.d.ts.map +1 -0
  48. package/dist/reader.js +100 -0
  49. package/dist/reader.js.map +1 -0
  50. package/dist/resources.d.ts +105 -0
  51. package/dist/resources.d.ts.map +1 -0
  52. package/dist/resources.js +173 -0
  53. package/dist/resources.js.map +1 -0
  54. package/dist/type-tool.d.ts +142 -0
  55. package/dist/type-tool.d.ts.map +1 -0
  56. package/dist/type-tool.js +222 -0
  57. package/dist/type-tool.js.map +1 -0
  58. package/package.json +26 -32
  59. package/src/descriptor.ts +141 -0
  60. package/src/engine-data.ts +86 -0
  61. package/src/image.ts +338 -0
  62. package/src/index.ts +38 -0
  63. package/src/layer-info.ts +578 -0
  64. package/src/layer-styles.ts +339 -0
  65. package/src/layer.ts +334 -0
  66. package/src/nodes.ts +411 -0
  67. package/src/path-record.ts +69 -0
  68. package/src/png.ts +92 -0
  69. package/src/psd.ts +133 -0
  70. package/src/reader.ts +125 -0
  71. package/src/resources.ts +273 -0
  72. package/src/type-tool.ts +270 -0
  73. package/.travis.yml +0 -5
  74. package/Cakefile +0 -61
  75. package/DEVELOPMENT.md +0 -27
  76. package/bower.json +0 -26
  77. package/docs/docco.css +0 -284
  78. package/docs/lib/psd/blend_mode.coffee.html +0 -72
  79. package/docs/lib/psd/channel_image.coffee.html +0 -81
  80. package/docs/lib/psd/color.coffee.html +0 -13
  81. package/docs/lib/psd/descriptor.coffee.html +0 -173
  82. package/docs/lib/psd/file.coffee.html +0 -78
  83. package/docs/lib/psd/header.coffee.html +0 -71
  84. package/docs/lib/psd/image.coffee.html +0 -94
  85. package/docs/lib/psd/image_export.coffee.html +0 -5
  86. package/docs/lib/psd/image_exports/png.coffee.html +0 -19
  87. package/docs/lib/psd/image_format.coffee.html +0 -8
  88. package/docs/lib/psd/image_formats/layer_raw.coffee.html +0 -9
  89. package/docs/lib/psd/image_formats/layer_rle.coffee.html +0 -10
  90. package/docs/lib/psd/image_formats/raw.coffee.html +0 -6
  91. package/docs/lib/psd/image_formats/rle.coffee.html +0 -37
  92. package/docs/lib/psd/image_mode.coffee.html +0 -7
  93. package/docs/lib/psd/image_modes/cmyk.coffee.html +0 -36
  94. package/docs/lib/psd/image_modes/greyscale.coffee.html +0 -17
  95. package/docs/lib/psd/image_modes/rgb.coffee.html +0 -32
  96. package/docs/lib/psd/init.coffee.html +0 -22
  97. package/docs/lib/psd/layer/blend_modes.coffee.html +0 -22
  98. package/docs/lib/psd/layer/blending_ranges.coffee.html +0 -29
  99. package/docs/lib/psd/layer/channel_image.coffee.html +0 -13
  100. package/docs/lib/psd/layer/helpers.coffee.html +0 -19
  101. package/docs/lib/psd/layer/info.coffee.html +0 -69
  102. package/docs/lib/psd/layer/mask.coffee.html +0 -11
  103. package/docs/lib/psd/layer/name.coffee.html +0 -15
  104. package/docs/lib/psd/layer/position_channels.coffee.html +0 -24
  105. package/docs/lib/psd/layer.coffee.html +0 -64
  106. package/docs/lib/psd/layer_info/blend_clipping_elements.coffee.html +0 -11
  107. package/docs/lib/psd/layer_info/blend_interior_elements.coffee.html +0 -11
  108. package/docs/lib/psd/layer_info/fill_opacity.coffee.html +0 -10
  109. package/docs/lib/psd/layer_info/gradient_fill.coffee.html +0 -12
  110. package/docs/lib/psd/layer_info/layer_id.coffee.html +0 -10
  111. package/docs/lib/psd/layer_info/layer_name_source.coffee.html +0 -10
  112. package/docs/lib/psd/layer_info/legacy_typetool.coffee.html +0 -82
  113. package/docs/lib/psd/layer_info/locked.coffee.html +0 -24
  114. package/docs/lib/psd/layer_info/metadata.coffee.html +0 -30
  115. package/docs/lib/psd/layer_info/nested_section_divider.coffee.html +0 -23
  116. package/docs/lib/psd/layer_info/object_effects.coffee.html +0 -12
  117. package/docs/lib/psd/layer_info/section_divider.coffee.html +0 -41
  118. package/docs/lib/psd/layer_info/solid_color.coffee.html +0 -24
  119. package/docs/lib/psd/layer_info/typetool.coffee.html +0 -121
  120. package/docs/lib/psd/layer_info/unicode_name.coffee.html +0 -14
  121. package/docs/lib/psd/layer_info/vector_mask.coffee.html +0 -37
  122. package/docs/lib/psd/layer_info/vector_origination.coffee.html +0 -12
  123. package/docs/lib/psd/layer_info/vector_stroke.coffee.html +0 -12
  124. package/docs/lib/psd/layer_info/vector_stroke_content.coffee.html +0 -12
  125. package/docs/lib/psd/layer_info.coffee.html +0 -11
  126. package/docs/lib/psd/layer_mask.coffee.html +0 -73
  127. package/docs/lib/psd/lazy_execute.coffee.html +0 -80
  128. package/docs/lib/psd/mask.coffee.html +0 -47
  129. package/docs/lib/psd/node.coffee.html +0 -100
  130. package/docs/lib/psd/nodes/ancestry.coffee.html +0 -47
  131. package/docs/lib/psd/nodes/build_preview.coffee.html +0 -6
  132. package/docs/lib/psd/nodes/group.coffee.html +0 -19
  133. package/docs/lib/psd/nodes/layer.coffee.html +0 -17
  134. package/docs/lib/psd/nodes/root.coffee.html +0 -61
  135. package/docs/lib/psd/nodes/search.coffee.html +0 -23
  136. package/docs/lib/psd/path_record.coffee.html +0 -73
  137. package/docs/lib/psd/resource.coffee.html +0 -20
  138. package/docs/lib/psd/resource_section.coffee.html +0 -16
  139. package/docs/lib/psd/resources/layer_comps.coffee.html +0 -31
  140. package/docs/lib/psd/resources.coffee.html +0 -44
  141. package/docs/lib/psd/util.coffee.html +0 -24
  142. package/docs/lib/psd.coffee.html +0 -85
  143. package/index.js +0 -2
  144. package/lib/psd/blend_mode.coffee +0 -78
  145. package/lib/psd/channel_image.coffee +0 -87
  146. package/lib/psd/color.coffee +0 -10
  147. package/lib/psd/descriptor.coffee +0 -189
  148. package/lib/psd/file.coffee +0 -88
  149. package/lib/psd/header.coffee +0 -83
  150. package/lib/psd/image.coffee +0 -107
  151. package/lib/psd/image_export.coffee +0 -2
  152. package/lib/psd/image_exports/png.coffee +0 -28
  153. package/lib/psd/image_format.coffee +0 -5
  154. package/lib/psd/image_formats/layer_raw.coffee +0 -6
  155. package/lib/psd/image_formats/layer_rle.coffee +0 -7
  156. package/lib/psd/image_formats/raw.coffee +0 -3
  157. package/lib/psd/image_formats/rle.coffee +0 -38
  158. package/lib/psd/image_mode.coffee +0 -4
  159. package/lib/psd/image_modes/cmyk.coffee +0 -36
  160. package/lib/psd/image_modes/greyscale.coffee +0 -14
  161. package/lib/psd/image_modes/rgb.coffee +0 -39
  162. package/lib/psd/init.coffee +0 -21
  163. package/lib/psd/layer/blend_modes.coffee +0 -20
  164. package/lib/psd/layer/blending_ranges.coffee +0 -28
  165. package/lib/psd/layer/channel_image.coffee +0 -10
  166. package/lib/psd/layer/helpers.coffee +0 -16
  167. package/lib/psd/layer/info.coffee +0 -76
  168. package/lib/psd/layer/mask.coffee +0 -8
  169. package/lib/psd/layer/name.coffee +0 -12
  170. package/lib/psd/layer/position_channels.coffee +0 -22
  171. package/lib/psd/layer.coffee +0 -64
  172. package/lib/psd/layer_info/artboard.coffee +0 -16
  173. package/lib/psd/layer_info/blend_clipping_elements.coffee +0 -8
  174. package/lib/psd/layer_info/blend_interior_elements.coffee +0 -8
  175. package/lib/psd/layer_info/fill_opacity.coffee +0 -7
  176. package/lib/psd/layer_info/gradient_fill.coffee +0 -9
  177. package/lib/psd/layer_info/layer_id.coffee +0 -7
  178. package/lib/psd/layer_info/layer_name_source.coffee +0 -7
  179. package/lib/psd/layer_info/legacy_typetool.coffee +0 -80
  180. package/lib/psd/layer_info/locked.coffee +0 -21
  181. package/lib/psd/layer_info/metadata.coffee +0 -27
  182. package/lib/psd/layer_info/nested_section_divider.coffee +0 -22
  183. package/lib/psd/layer_info/object_effects.coffee +0 -9
  184. package/lib/psd/layer_info/section_divider.coffee +0 -38
  185. package/lib/psd/layer_info/solid_color.coffee +0 -21
  186. package/lib/psd/layer_info/typetool.coffee +0 -172
  187. package/lib/psd/layer_info/unicode_name.coffee +0 -11
  188. package/lib/psd/layer_info/vector_mask.coffee +0 -35
  189. package/lib/psd/layer_info/vector_origination.coffee +0 -9
  190. package/lib/psd/layer_info/vector_stroke.coffee +0 -9
  191. package/lib/psd/layer_info/vector_stroke_content.coffee +0 -9
  192. package/lib/psd/layer_info.coffee +0 -8
  193. package/lib/psd/layer_mask.coffee +0 -73
  194. package/lib/psd/lazy_execute.coffee +0 -86
  195. package/lib/psd/mask.coffee +0 -52
  196. package/lib/psd/node.coffee +0 -150
  197. package/lib/psd/nodes/ancestry.coffee +0 -44
  198. package/lib/psd/nodes/build_preview.coffee +0 -3
  199. package/lib/psd/nodes/group.coffee +0 -16
  200. package/lib/psd/nodes/layer.coffee +0 -14
  201. package/lib/psd/nodes/root.coffee +0 -59
  202. package/lib/psd/nodes/search.coffee +0 -20
  203. package/lib/psd/path_record.coffee +0 -72
  204. package/lib/psd/resource.coffee +0 -17
  205. package/lib/psd/resource_section.coffee +0 -16
  206. package/lib/psd/resources/guides.coffee +0 -25
  207. package/lib/psd/resources/layer_comps.coffee +0 -28
  208. package/lib/psd/resources/layer_links.coffee +0 -17
  209. package/lib/psd/resources/resolution_info.coffee +0 -26
  210. package/lib/psd/resources.coffee +0 -41
  211. package/lib/psd/util.coffee +0 -23
  212. package/lib/psd.coffee +0 -92
  213. package/output/Logo_Glyph.png +0 -0
  214. package/output/Make a change and save..png +0 -0
  215. package/output/Matte.png +0 -0
  216. package/webpack.config.js +0 -23
@@ -1,172 +0,0 @@
1
- _ = require 'lodash'
2
- parseEngineData = require 'parse-engine-data'
3
- LayerInfo = require '../layer_info.coffee'
4
- Descriptor = require '../descriptor.coffee'
5
-
6
- module.exports = class TextElements extends LayerInfo
7
- @shouldParse: (key) -> key is 'TySh'
8
-
9
- TRANSFORM_VALUE = ['xx', 'xy', 'yx', 'yy', 'tx', 'ty']
10
- COORDS_VALUE = ['left', 'top', 'right', 'bottom']
11
-
12
- constructor: (layer, length) ->
13
- super(layer, length)
14
-
15
- @version = null
16
- @transform = {}
17
- @textVersion = null
18
- @descriptorVersion = null
19
- @textData = null
20
- @engineData = null
21
- @textValue = null
22
- @warpVersion = null
23
- @descriptorVersion = null
24
- @warpData = null
25
- @coords = {}
26
-
27
- parse: ->
28
- @version = @file.readShort()
29
-
30
- @parseTransformInfo()
31
-
32
- @textVersion = @file.readShort()
33
- @descriptorVersion = @file.readInt()
34
-
35
- @textData = new Descriptor(@file).parse()
36
- @textValue = @textData['Txt ']
37
- @engineData = parseEngineData(@textData.EngineData)
38
-
39
- @warpVersion = @file.readShort()
40
-
41
- @descriptorVersion = @file.readInt()
42
-
43
- @warpData = new Descriptor(@file).parse()
44
-
45
- for name, index in COORDS_VALUE
46
- @coords[name] = @file.readInt()
47
-
48
- parseTransformInfo: ->
49
- for name, index in TRANSFORM_VALUE
50
- @transform[name] = @file.readDouble()
51
-
52
- fonts: ->
53
- return [] unless @engineData?
54
- @engineData.ResourceDict.FontSet.map (f) -> f.Name
55
-
56
- lengthArray: ->
57
- arr = @engineData.EngineDict.StyleRun.RunLengthArray
58
- sum = _.reduce(arr, (m, o) -> m + o)
59
- arr[arr.length - 1] = arr[arr.length - 1] - 1 if sum - @textValue.length == 1
60
- return arr
61
-
62
- fontStyles: ->
63
- data = @engineData.EngineDict.StyleRun.RunArray.map (r) ->
64
- r.StyleSheet.StyleSheetData
65
- data.map (f) ->
66
- if f.FauxItalic
67
- style = 'italic'
68
- else
69
- style = 'normal'
70
- return style
71
-
72
- fontWeights: ->
73
- data = @engineData.EngineDict.StyleRun.RunArray.map (r) ->
74
- r.StyleSheet.StyleSheetData
75
- data.map (f) ->
76
- if f.FauxBold
77
- weight = 'bold'
78
- else
79
- weight = 'normal'
80
- return weight
81
-
82
- textDecoration: ->
83
- data = @engineData.EngineDict.StyleRun.RunArray.map (r) ->
84
- r.StyleSheet.StyleSheetData
85
- data.map (f) ->
86
- if f.Underline
87
- decoration = 'underline'
88
- else
89
- decoration = 'none'
90
- return decoration
91
-
92
- leading: ->
93
- data = @engineData.EngineDict.StyleRun.RunArray.map (r) ->
94
- r.StyleSheet.StyleSheetData
95
- data.map (f) ->
96
- if f.Leading
97
- leading = f.Leading
98
- else
99
- leading = 'auto'
100
- return leading
101
-
102
- sizes: ->
103
- return [] if not @engineData? and not @styles().FontSize?
104
- @styles().FontSize
105
-
106
- alignment: ->
107
- return [] unless @engineData?
108
- alignments = ['left', 'right', 'center', 'justify']
109
- @engineData.EngineDict.ParagraphRun.RunArray.map (s) ->
110
- alignments[Math.min(parseInt(s.ParagraphSheet.Properties.Justification, 10), 3)]
111
-
112
- # Return all colors used for text in this layer. The colors are returned in RGBA
113
- # format as an array of arrays.
114
- colors: ->
115
- # If the color is opaque black, this field is sometimes omitted.
116
- return [[0, 0, 0, 255]] if not @engineData? or not @styles().FillColor?
117
-
118
- @styles().FillColor.map (s) ->
119
- values = s.Values.map (v) -> Math.round(v * 255)
120
- values.push values.shift() # Change ARGB -> RGBA for consistency
121
- values
122
-
123
- styles: ->
124
- return {} unless @engineData?
125
- return @_styles if @_styles?
126
-
127
- data = @engineData.EngineDict.StyleRun.RunArray.map (r) ->
128
- r.StyleSheet.StyleSheetData
129
-
130
- @_styles = _.reduce(data, (m, o) ->
131
- for own k, v of o
132
- m[k] or= []
133
- m[k].push v
134
- m
135
- , {})
136
-
137
- # Creates the CSS string and returns it. Each property is newline separated
138
- # and not all properties may be present depending on the document.
139
- #
140
- # Colors are returned in rgba() format and fonts may include some internal
141
- # Photoshop fonts.
142
- toCSS: ->
143
- definition =
144
- 'font-family': @fonts().join(', ')
145
- 'font-size': "#{@sizes()[0]}pt"
146
- 'color': "rgba(#{@colors()[0].join(', ')})"
147
- 'text-align': @alignment()[0]
148
-
149
- css = []
150
- for k, v of definition
151
- continue unless v?
152
- css.push "#{k}: #{v};"
153
-
154
- css.join("\n")
155
-
156
- export: ->
157
- value: @textValue
158
- font:
159
- lengthArray: @lengthArray()
160
- styles: @fontStyles()
161
- weights: @fontWeights()
162
- names: @fonts()
163
- sizes: @sizes()
164
- colors: @colors()
165
- alignment: @alignment()
166
- textDecoration: @textDecoration()
167
- leading: @leading()
168
- left: @coords.left
169
- top: @coords.top
170
- right: @coords.right
171
- bottom: @coords.bottom
172
- transform: @transform
@@ -1,11 +0,0 @@
1
- LayerInfo = require '../layer_info.coffee'
2
-
3
- module.exports = class UnicodeName extends LayerInfo
4
- @shouldParse: (key) -> key is 'luni'
5
-
6
- parse: ->
7
- pos = @file.tell()
8
- @data = @file.readUnicodeString()
9
-
10
- @file.seek pos + @length
11
- return @
@@ -1,35 +0,0 @@
1
- LayerInfo = require '../layer_info.coffee'
2
- PathRecord = require '../path_record.coffee'
3
-
4
- module.exports = class VectorMask extends LayerInfo
5
- @shouldParse: (key) -> key in ['vmsk', 'vsms']
6
-
7
- constructor: (layer, length) ->
8
- super(layer, length)
9
-
10
- @invert = null
11
- @notLink = null
12
- @disable = null
13
- @paths = []
14
-
15
- parse: ->
16
- @file.seek 4, true # version
17
- tag = @file.readInt()
18
-
19
- @invert = (tag & 0x01) > 0
20
- @notLink = (tag & (0x01 << 1)) > 0
21
- @disable = (tag & (0x01 << 2)) > 0
22
-
23
- # I haven't figured out yet why this is 10 and not 8.
24
- numRecords = (@length - 10) / 26
25
- for i in [0...numRecords]
26
- record = new PathRecord(@file)
27
- record.parse()
28
-
29
- @paths.push record
30
-
31
- export: ->
32
- invert: @invert
33
- notLink: @notLink
34
- disable: @disable
35
- paths: @paths.map (p) -> p.export()
@@ -1,9 +0,0 @@
1
- LayerInfo = require '../layer_info.coffee'
2
- Descriptor = require '../descriptor.coffee'
3
-
4
- module.exports = class VectorOrigination extends LayerInfo
5
- @shouldParse: (key) -> key is 'vogk'
6
-
7
- parse: ->
8
- @file.seek 8, true
9
- @data = new Descriptor(@file).parse()
@@ -1,9 +0,0 @@
1
- LayerInfo = require '../layer_info.coffee'
2
- Descriptor = require '../descriptor.coffee'
3
-
4
- module.exports = class VectorStroke extends LayerInfo
5
- @shouldParse: (key) -> key is 'vstk'
6
-
7
- parse: ->
8
- @file.seek 4, true
9
- @data = new Descriptor(@file).parse()
@@ -1,9 +0,0 @@
1
- LayerInfo = require '../layer_info.coffee'
2
- Descriptor = require '../descriptor.coffee'
3
-
4
- module.exports = class VectorStrokeContent extends LayerInfo
5
- @shouldParse: (key) -> key is 'vscg'
6
-
7
- parse: ->
8
- @file.seek 8, true
9
- @data = new Descriptor(@file).parse()
@@ -1,8 +0,0 @@
1
- module.exports = class LayerInfo
2
- constructor: (@layer, @length) ->
3
- @file = @layer.file
4
- @section_end = @file.tell() + @length
5
- @data = {}
6
-
7
- skip: -> @file.seek @section_end
8
- parse: -> @skip() # skip by default
@@ -1,73 +0,0 @@
1
- _ = require 'lodash'
2
- Util = require './util.coffee'
3
- Layer = require './layer.coffee'
4
-
5
- # The layer mask is the overarching data structure that describes both
6
- # the layers/groups in the PSD document, and the global mask.
7
- # This part of the document is ordered as such:
8
- #
9
- # * Layers
10
- # * Layer images
11
- # * Global Mask
12
- #
13
- # The file does not need to have a global mask. If there is none, then
14
- # its length will be zero.
15
- module.exports = class LayerMask
16
- constructor: (@file, @header) ->
17
- @layers = []
18
- @mergedAlpha = false
19
- @globalMask = null
20
-
21
- skip: -> @file.seek @file.readInt(), true
22
-
23
- parse: ->
24
- maskSize = @file.readInt()
25
- finish = maskSize + @file.tell()
26
-
27
- return if maskSize <= 0
28
-
29
- @parseLayers()
30
- @parseGlobalMask()
31
-
32
- # The layers are stored in the reverse order that we would like them. In other
33
- # words, they're stored bottom to top and we want them top to bottom.
34
- @layers.reverse()
35
-
36
- @file.seek finish
37
-
38
- parseLayers: ->
39
- layerInfoSize = Util.pad2 @file.readInt()
40
-
41
- if layerInfoSize > 0
42
- layerCount = @file.readShort()
43
-
44
- if layerCount < 0
45
- layerCount = Math.abs layerCount
46
- @mergedAlpha = true
47
-
48
- for i in [0...layerCount]
49
- @layers.push new Layer(@file, @header).parse()
50
-
51
- layer.parseChannelImage() for layer in @layers
52
-
53
- parseGlobalMask: ->
54
- length = @file.readInt()
55
- return if length <= 0
56
-
57
- maskEnd = @file.tell() + length
58
-
59
- @globalMask = _({}).tap (mask) =>
60
- mask.overlayColorSpace = @file.readShort()
61
- mask.colorComponents = [
62
- @file.readShort() >> 8
63
- @file.readShort() >> 8
64
- @file.readShort() >> 8
65
- @file.readShort() >> 8
66
- ]
67
-
68
- mask.opacity = @file.readShort() / 16.0
69
-
70
- # 0 = color selected, 1 = color protected, 128 = use value per layer
71
- mask.kind = @file.readByte()
72
-
73
- @file.seek maskEnd
@@ -1,86 +0,0 @@
1
- # LazyExecute is very important when it comes to speed. Because some PSD documents
2
- # can be extremely large and hold a LOT of data, we can significantly speed up
3
- # parsing by temporarily skipping over chunks of the PSD. Chances are that you aren't
4
- # going to use every piece of data in the document. This means, when you do request
5
- # some data that's proxied through LazyExecute, we can parse it on the fly. This overhead
6
- # should be incredibly minimal.
7
- #
8
- # While not as elegant as the PSD.rb counterpart, it gets the job done. We look at the
9
- # object we need to proxy, and define proxies for every item on its prototype on this
10
- # one. The proxy checks to see if the object has been loaded first before passing on
11
- # the call to the object.
12
- #
13
- # If the object has not been loaded yet, we record our current position in the file, jump
14
- # to the known start position of the data, parse it by calling a set method on the object,
15
- # jump back to the original position in the file, and then call the proxied property.
16
- #
17
- # ## Example
18
- # ``` coffeescript
19
- # obj = new SomeObject()
20
- # data = new LazyExecute(obj, file)
21
- # .now('skip')
22
- # .later('parse')
23
- # .ignore('foo', 'bar')
24
- # .get()
25
- # ```
26
- module.exports = class LazyExecute
27
- constructor: (@obj, @file) ->
28
- @startPos = @file.tell()
29
- @loaded = false
30
- @loadMethod = null
31
- @loadArgs = []
32
- @passthru = []
33
-
34
- # This describes the method that we want to run at object instantiation. Typically this
35
- # will skip over the data that we will parse on-demand later. We can pass any arguments
36
- # we want to the method as well.
37
- now: (method, args...) ->
38
- @obj[method].apply(@obj, args)
39
- return @
40
-
41
- # Here we describe the method we want to run when the first method/property on the object
42
- # is accessed. We can also define any arguments that need to be passed to the function.
43
- later: (method, args...) ->
44
- @loadMethod = method
45
- @loadArgs = args
46
- return @
47
-
48
- # Sometimes we don't have to parse the data in order to get some important information.
49
- # For example, we can get the widht/height from the full preview image without parsing the
50
- # image itself, since that data comes from the header. Purely convenience, but helps to
51
- # optimize usage.
52
- #
53
- # The arguments are a list of method/property names we don't want to trigger on-demand parsing.
54
- ignore: (args...) ->
55
- @passthru.concat args
56
- return @
57
-
58
- # This is called once all of the paramters of the proxy have been set up, i.e. now, later, and skip.
59
- # This defines all items on the proxied objects prototype on this object, and checks to make sure
60
- # the proxied object has been loaded before passing on the call.
61
- get: ->
62
- for key, val of @obj then do (key, val) =>
63
- return if @[key]?
64
- Object.defineProperty @, key,
65
- get: ->
66
- @load() if not @loaded and not (key in @passthru)
67
- @obj[key]
68
-
69
- @
70
-
71
- # If we are accessing a property for the first time, then this will call the load method, which
72
- # was defined during setup with `later()`. The steps this performs are:
73
- #
74
- # 1. Records the current file position.
75
- # 2. Jumps to the recorded start position for the proxied data.
76
- # 3. Calls the load method, which was defined with `later()`.
77
- # 4. Jumps back to the original file position.
78
- # 5. Sets the `@loaded` flag to true so we know this object has been parsed.
79
- load: ->
80
- origPos = @file.tell()
81
- @file.seek @startPos
82
-
83
- @obj[@loadMethod].apply(@obj, @loadArgs)
84
-
85
- @file.seek origPos
86
- @loaded = true
@@ -1,52 +0,0 @@
1
- # Each layer/group in the PSD document can have a mask, which is
2
- # represented by this class. The mask describes which parts of the
3
- # layer are visible and which are hidden.
4
- module.exports = class Mask
5
- constructor: (@file) ->
6
- @top = 0
7
- @right = 0
8
- @bottom = 0
9
- @left = 0
10
-
11
- parse: ->
12
- # If there is no mask, then this section will have a size of zero
13
- # and we can move on to the next.
14
- @size = @file.readInt()
15
- return @ if @size is 0
16
-
17
- maskEnd = @file.tell() + @size
18
-
19
- # First, we parse the coordinates of the mask.
20
- @top = @file.readInt()
21
- @left = @file.readInt()
22
- @bottom = @file.readInt()
23
- @right = @file.readInt()
24
-
25
- # We can then easily derive the dimensions from the box coordinates.
26
- @width = @right - @left
27
- @height = @bottom - @top
28
-
29
- # Each mask defines a couple of flags that are used as extra metadata.
30
- @relative = (@flags & 0x01) > 0
31
- @disabled = (@flags & (0x01 << 1)) > 0
32
- @invert = (@flags & (0x01 << 2)) > 0
33
-
34
- @defaultColor = @file.readByte()
35
- @flags = @file.readByte()
36
-
37
- @file.seek maskEnd
38
- return @
39
-
40
- export: ->
41
- return {} if @size is 0
42
-
43
- top: @top
44
- left: @left
45
- bottom: @bottom
46
- right: @right
47
- width: @width
48
- height: @height
49
- defaultColor: @defaultColor
50
- relative: @relative
51
- disabled: @disabled
52
- invert: @invert
@@ -1,150 +0,0 @@
1
- # The Node abstraction is one of the most important in PSD.js. It's the base for the
2
- # tree representation of the document structure. Every layer and group is a node in
3
- # the document tree. All common functionality is here, and both layers and groups extend
4
- # this class with specialized functionality.
5
- #
6
- # While you can access the layer data directly, the Node interface provides a somewhat
7
- # higher-level API that makes it easier and less verbose to access the wealth of
8
- # information that's stored in each PSD.
9
- _ = require 'lodash'
10
- {Module} = require 'coffeescript-module'
11
-
12
- module.exports = class Node extends Module
13
- # We have a couple of important mixins that provide some really cool functionality.
14
- @includes require('./nodes/ancestry.coffee')
15
- @includes require('./nodes/search.coffee')
16
- @includes require('./nodes/build_preview.coffee')
17
-
18
- # There are some common properties that are shared by all Node types. We define them
19
- # here to DRY up the code a little, especially when exporting data.
20
- @PROPERTIES: ['name', 'left', 'right', 'top', 'bottom', 'height', 'width']
21
-
22
- # Each Node subclass defines a type, which makes it easier to idenfity what we're
23
- # dealing with, since `constructor.name` can get mangled during minification and
24
- # wreak havoc.
25
- type: 'node'
26
-
27
- # Every node gets a reference to the layer/group and its parent, which allows us to
28
- # traverse the tree structure. It also builds references to all of its children.
29
- constructor: (@layer, @parent = null) ->
30
- @layer.node = @
31
- @_children = []
32
-
33
- # We go ahead and copy the layer name to the node since it's such a commonly
34
- # accessed property.
35
- @name = @layer.name
36
-
37
- @forceVisible = null
38
-
39
- # We also store the coordinates on the node, especially since we'll eventually be
40
- # able to modify them based on layer comp data.
41
- @coords =
42
- top: @layer.top
43
- bottom: @layer.bottom
44
- left: @layer.left
45
- right: @layer.right
46
-
47
- @topOffset = 0
48
- @leftOffset = 0
49
-
50
- @createProperties()
51
-
52
- createProperties: ->
53
- # The coordinate properties take into consideration any offsets that might be set
54
- # by the current layer comp. This is not implemented yet, but will be in the future.
55
- Object.defineProperty @, 'top',
56
- get: -> @coords.top + @topOffset
57
- set: (val) -> @coords.top = val
58
-
59
- Object.defineProperty @, 'right',
60
- get: -> @coords.right + @leftOffset
61
- set: (val) -> @coords.right = val
62
-
63
- Object.defineProperty @, 'bottom',
64
- get: -> @coords.bottom + @topOffset
65
- set: (val) -> @coords.bottom = val
66
-
67
- Object.defineProperty @, 'left',
68
- get: -> @coords.left + @leftOffset
69
- set: (val) -> @coords.left = val
70
-
71
- # We take the tiny overhead of recalculating this every time since the offset could
72
- # change based on the layer comp.
73
- Object.defineProperty @, 'width', get: -> @right - @left
74
- Object.defineProperty @, 'height', get: -> @bottom - @top
75
-
76
- # **All properties should be accessed through `get()`**. While many things can be
77
- # accessed without it, using `get()` provides 2 things:
78
- #
79
- # * Consistency
80
- # * Access to both data on the Node and the Layer through the same interface.
81
- #
82
- # This makes it much cleaner to access stuff like layer info blocks, since you just
83
- # give the name of the block you want to access. For example:
84
- #
85
- # ``` coffeescript
86
- # node.get('typeTool').export()
87
- #
88
- # # vs
89
- #
90
- # node.layer.typeTool().export()
91
- # ```
92
- get: (prop) ->
93
- value = if @[prop]? then @[prop] else @layer[prop]
94
- if typeof value is 'function' then value() else value
95
-
96
- # Is this layer/group visible? This checks all possible places that could define
97
- # whether or not this is true, e.g. clipping masks. It also checks the current
98
- # layer comp visibility override (not implemented yet).
99
- visible: ->
100
- return false if @layer.clipped and not @clippingMask().visible()
101
- if @forceVisible? then @forceVisible else @layer.visible
102
-
103
- hidden: -> not @visible()
104
-
105
- isLayer: -> @type is 'layer'
106
- isGroup: -> @type is 'group'
107
- isRoot: -> @type is 'root'
108
-
109
- # Retrieves the clipping mask for this node. Because a clipping mask can be applied
110
- # to multiple layers, we have to traverse the tree until we find the first node that
111
- # does not have the `clipped` flag. We can do it this way because all layers that
112
- # the clipping node affects must be siblings and in sequence.
113
- clippingMask: ->
114
- return null unless @layer.clipped
115
- @clippingMaskCached or= (
116
- maskNode = @nextSibling()
117
- maskNode = maskNode.nextSibling() while maskNode.clipped
118
- maskNode
119
- )
120
-
121
- clippedBy: -> @clippingMask()
122
-
123
- # We can export the most important information about this node as a plain object.
124
- # If we're exporting a group, it will recursively export itself and all of it's descendants as well.
125
- export: ->
126
- hash =
127
- type: null
128
- visible: @visible()
129
- opacity: @layer.opacity / 255.0
130
- blendingMode: @layer.blendingMode()
131
-
132
- hash[prop] = @[prop] for prop in Node.PROPERTIES
133
- hash
134
-
135
- # While the PSD document does not define explicit dimensions for groups, we can generate
136
- # them based on the bounding boxes of their layer children. When we build the tree structure,
137
- # we update the dimensions of the group whenever a layer is added so that we finish with
138
- # the actual bounding box of the group's contents.
139
- updateDimensions: ->
140
- return if @isLayer()
141
-
142
- child.updateDimensions() for child in @_children
143
-
144
- return if @isRoot()
145
-
146
- nonEmptyChildren = @_children.filter((c) -> not c.isEmpty())
147
- @left = _.min(nonEmptyChildren.map((c) -> c.left)) or 0
148
- @top = _.min(nonEmptyChildren.map((c) -> c.top)) or 0
149
- @bottom = _.max(nonEmptyChildren.map((c) -> c.bottom)) or 0
150
- @right = _.max(nonEmptyChildren.map((c) -> c.right)) or 0
@@ -1,44 +0,0 @@
1
- _ = require 'lodash'
2
-
3
- module.exports =
4
- root: ->
5
- return @ if @isRoot()
6
- return @parent.root()
7
-
8
- isRoot: -> @depth() is 0
9
-
10
- children: -> @_children
11
-
12
- ancestors: ->
13
- return [] if not @parent? or @parent.isRoot()
14
- return @parent.ancestors().concat [@parent]
15
-
16
- hasChildren: -> @_children.length > 0
17
- childless: -> not @hasChildren()
18
-
19
- siblings: ->
20
- return [] unless @parent?
21
- @parent.children()
22
-
23
- nextSibling: ->
24
- return null unless @parent?
25
- index = @siblings().indexOf(@)
26
- @siblings()[index + 1]
27
-
28
- prevSibling: ->
29
- return null unless @parent?
30
- index = @siblings().indexOf(@)
31
- @siblings()[index - 1]
32
-
33
- hasSiblings: -> @siblings().length > 1
34
- onlyChild: -> not @hasSiblings()
35
-
36
- descendants: -> _.flatten @_children.map((n) -> n.subtree())
37
-
38
- subtree: -> [@].concat @descendants()
39
-
40
- depth: -> @ancestors().length + 1
41
-
42
- path: (asArray = false) ->
43
- path = @ancestors().map((n) -> n.name).concat([@name])
44
- if asArray then path else path.join('/')
@@ -1,3 +0,0 @@
1
- module.exports =
2
- toPng: -> @layer.image.toPng()
3
- saveAsPng: (output) -> @layer.image.saveAsPng(output)
@@ -1,16 +0,0 @@
1
- _ = require 'lodash'
2
- Node = require '../node.coffee'
3
-
4
- module.exports = class Group extends Node
5
- type: 'group'
6
-
7
- passthruBlending: ->
8
- @get('blendingMode') is 'passthru'
9
-
10
- isEmpty: ->
11
- return false unless child.isEmpty() for child in @_children
12
-
13
- export: ->
14
- _.merge super(),
15
- type: 'group'
16
- children: @_children.map((c) -> c.export())