Skip to content

Commit

Permalink
Fix encoding issues with text data
Browse files Browse the repository at this point in the history
  • Loading branch information
meltingice committed Apr 23, 2014
1 parent bbc3f2b commit a8b2b6b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 31 deletions.
26 changes: 26 additions & 0 deletions examples/table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require './lib/psd'
require 'terminal-table'
require 'colorize'

file = ARGV[0] || 'examples/images/example.psd'
layer_comp = ARGV[1]

psd = PSD.new(file)
psd.parse!

tree = layer_comp ? psd.tree.filter_by_comp(layer_comp) : psd.tree

title = "#{File.basename(file)} / #{layer_comp || "Last Document State"}"
header = ["Visible?", "Position", "Dimensions", "Path"]
rows = tree.descendants.map do |node|
[
node.visible?.to_s.colorize(node.visible? ? :green : :red),
"(#{node.left}, #{node.top})",
"#{node.width}x#{node.height}",
{ value: node.path, width: 90 }
]
end

table = Terminal::Table.new rows: rows, headings: header, title: title

puts table
23 changes: 3 additions & 20 deletions examples/tree.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
require './lib/psd'
require 'terminal-table'
require 'colorize'
require 'pp'

psd = nil
file = ARGV[0] || 'examples/images/example.psd'
layer_comp = ARGV[1]

psd = PSD.new(file)
psd.parse!

tree = layer_comp ? psd.tree.filter_by_comp(layer_comp) : psd.tree

title = "#{File.basename(file)} / #{layer_comp || "Last Document State"}"
header = ["Visible?", "Position", "Dimensions", "Path"]
rows = tree.descendants.map do |node|
[
node.visible?.to_s.colorize(node.visible? ? :green : :red),
"(#{node.left}, #{node.top})",
"#{node.width}x#{node.height}",
{ value: node.path, width: 90 }
]
end

table = Terminal::Table.new rows: rows, headings: header, title: title

puts table
pp psd.tree.to_hash
5 changes: 4 additions & 1 deletion lib/psd/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def read_byte
# Reads a unicode string, which is double the length of a normal string and encoded as UTF-16.
def read_unicode_string(length=nil)
length ||= read_int if length.nil?
!length.nil? && length > 0 ? read(length * 2).encode('UTF-8', 'UTF-16BE').delete("\000") : ''
return '' if length.nil? || length <= 0
read(length * 2)
.encode('UTF-8', 'UTF-16BE', universal_newline: true)
.delete("\000")
end

# Reads a boolean value.
Expand Down
10 changes: 1 addition & 9 deletions lib/psd/layer_info/typetool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ def parse
descriptor_version = @file.read_int

@data[:text] = Descriptor.new(@file).parse
@data[:text]['EngineData']
.encode!('UTF-8', 'MacRoman', invalid: :replace, undef: :replace)
.delete!("\000")

@data[:engine_data] = nil
begin
Expand All @@ -44,12 +41,7 @@ def parse
# Extracts the text within the text area. In the event that psd-enginedata fails
# for some reason, we attempt to extract the text using some rough regex.
def text_value
if engine_data.nil?
# Something went wrong, lets hack our way through.
/\/Text \(˛ˇ(.*)\)$/.match(@data[:text]['EngineData'])[1].gsub /\r/, "\n"
else
engine_data.EngineDict.Editor.Text
end
@data[:text]['Txt ']
end
alias :to_s :text_value

Expand Down
2 changes: 1 addition & 1 deletion psd.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]

gem.add_dependency 'rake'
gem.add_dependency 'psd-enginedata', '~> 1.0'
gem.add_dependency 'psd-enginedata', '~> 1'
gem.add_dependency 'chunky_png'
gem.add_dependency 'activesupport', '~> 3.2.7'
gem.add_dependency 'xmp'
Expand Down

0 comments on commit a8b2b6b

Please sign in to comment.