Class: PSD::TypeTool
- Defined in:
- lib/psd/layer_info/typetool.rb
Overview
Parses and provides information about text areas within layers in the document.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from LayerInfo
Instance Method Summary collapse
-
#colors ⇒ Object
Return all colors used for text in this layer.
- #engine_data ⇒ Object
-
#font ⇒ Object
Gets all of the basic font information for this text area.
-
#fonts ⇒ Object
Returns all fonts listed for this layer, since fonts are defined on a per-character basis.
- #method_missing(method, *args, &block) ⇒ Object
-
#parse ⇒ Object
Parse all of the text data in the layer.
- #parser ⇒ Object
-
#sizes ⇒ Object
Return all font sizes for this layer.
-
#text_value ⇒ Object
(also: #to_s)
Extracts the text within the text area.
- #to_hash ⇒ Object
Methods inherited from LayerInfo
Constructor Details
This class inherits a constructor from PSD::LayerInfo
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
114 115 116 117 |
# File 'lib/psd/layer_info/typetool.rb', line 114 def method_missing(method, *args, &block) return @data[method] if @data.has_key?(method) return super end |
Instance Method Details
#colors ⇒ Object
Return all colors used for text in this layer. The colors are returned in RGB format as an array of arrays.
> [[255, 0, 0], [0, 0, 255]]
84 85 86 87 88 89 90 91 92 |
# File 'lib/psd/layer_info/typetool.rb', line 84 def colors return [] if engine_data.nil? engine_data.EngineDict.StyleRun.RunArray.map do |r| next unless r.StyleSheet.StyleSheetData.key?('FillColor') r.StyleSheet.StyleSheetData.FillColor.Values.map do |v| (v * 255).round end end.uniq end |
#engine_data ⇒ Object
94 95 96 |
# File 'lib/psd/layer_info/typetool.rb', line 94 def engine_data @data[:engine_data] end |
#font ⇒ Object
Gets all of the basic font information for this text area. This assumes that the first font is the only one you want.
56 57 58 59 60 61 62 63 |
# File 'lib/psd/layer_info/typetool.rb', line 56 def font { name: fonts.first, sizes: sizes, colors: colors, css: parser.to_css } end |
#fonts ⇒ Object
Returns all fonts listed for this layer, since fonts are defined on a per-character basis.
67 68 69 70 |
# File 'lib/psd/layer_info/typetool.rb', line 67 def fonts return [] if engine_data.nil? engine_data.ResourceDict.FontSet.map(&:Name) end |
#parse ⇒ Object
Parse all of the text data in the layer.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/psd/layer_info/typetool.rb', line 11 def parse version = @file.read_short parse_transform_info text_version = @file.read_short descriptor_version = @file.read_int @data[:text] = Descriptor.new(@file).parse @data[:text]['EngineData'] .encode!('UTF-8', 'MacRoman') .delete!("\000") @data[:engine_data] = nil begin parser.parse! @data[:engine_data] = parser.result rescue Exception => e puts e. end warpVersion = @file.read_short descriptor_version = @file.read_int @data[:warp] = Descriptor.new(@file).parse [:left, :top, :right, :bottom].each do |pos| @data[pos] = @file.read_int end return self end |
#parser ⇒ Object
98 99 100 |
# File 'lib/psd/layer_info/typetool.rb', line 98 def parser @parser ||= PSD::EngineData.new(@data[:text]['EngineData']) end |
#sizes ⇒ Object
Return all font sizes for this layer.
73 74 75 76 77 78 |
# File 'lib/psd/layer_info/typetool.rb', line 73 def sizes return [] if engine_data.nil? engine_data.EngineDict.StyleRun.RunArray.map do |r| r.StyleSheet.StyleSheetData.FontSize end.uniq end |
#text_value ⇒ Object Also known as: to_s
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.
44 45 46 47 48 49 50 51 |
# File 'lib/psd/layer_info/typetool.rb', line 44 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 end |
#to_hash ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/psd/layer_info/typetool.rb', line 102 def to_hash { value: text_value, font: font, left: left, top: top, right: right, bottom: bottom, transform: transform } end |