Class: RRTF::ImageNode
Overview
This class represents an image within a RTF document. Currently only the PNG, JPEG and Windows Bitmap formats are supported. Efforts are made to identify the file type but these are not guaranteed to work.
Constant Summary
- TYPE_DICTIONARY =
Supported image types.
{ :png => 'pngblip', :jpeg => 'jpegblip', :bmp => 'dibitmap0' # device independent bitmap }.freeze
- SIZING_MODE_DICTIONARY =
Supported sizing modes.
{ # Size the image absolutely according to the given width and height. "ABSOLUTE" => "ABSOLUTE", # Fit the image in the box specified by the given width and height, # preserving the aspect ratio. "FIX_ASPECT_RATIO" => "FIX_ASPECT_RATIO" }.freeze
Instance Attribute Summary collapse
- #border ⇒ Object
- #displayed_height ⇒ Object
- #displayed_width ⇒ Object
- #height ⇒ Object readonly
- #sizing_mode ⇒ Object
- #type ⇒ Object readonly
- #width ⇒ Object readonly
Attributes inherited from Node
Class Method Summary collapse
-
.inspect(source) ⇒ Array<Object>
Attempts to extract the type, width, and height of an image using FastImage.
-
.parse_border_array(border) ⇒ Object
inspect().
Instance Method Summary collapse
-
#initialize(parent, source, id, options = {}) ⇒ ImageNode
constructor
This is the constructor for the ImageNode class.
-
#to_rtf ⇒ Object
This method generates the RTF for an ImageNode object.
Methods inherited from Node
#is_root?, #next_node, #previous_node, #root
Constructor Details
#initialize(parent, source, id, options = {}) ⇒ ImageNode
This is the constructor for the ImageNode class.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rrtf/node/image_node.rb', line 74 def initialize(parent, source, id, = {}) super(parent) @source = source @id = id # load default options = { "width" => nil, "height" => nil, "sizing_mode" => "ABSOLUTE", "border" => nil }.merge() # extract options @displayed_width = Utilities.value2twips(.delete("width")) @displayed_height = Utilities.value2twips(.delete("height")) @sizing_mode = SIZING_MODE_DICTIONARY[.delete("sizing_mode")] @border = self.class.parse_border_array(.delete("border")) # store border colours in colour table @border.each do |b| b.push_colours(root.colours) end # Store path to image. if @source.is_a?(String) begin @source = open(@source) rescue OpenURI::HTTPError => error response = error.io RTFError.fire("Could not open '#{@source}'. Server responded with #{response.status.join(',')}.") rescue Exception => error RTFError.fire("Could not open '#{@source}'. #{error.}.") end # rescue block elsif !@source.respond_to?(:each_byte) RTFError.fire("A string or object that responds to :each_byte must be supplied - '#{@source}' given.") end # unless # Attempt to determine image type and dimensions. @type, @width, @height = self.class.inspect(@source) if @type.nil? RTFError.fire("The #{File.basename(@source)} file contains an unknown or unsupported image type.") elsif @width.nil? || @height.nil? RTFError.fire("Could not determine the dimensions of #{File.basename(@source)}.") end # if @displayed_width, @displayed_height = size_image end |
Instance Attribute Details
#border ⇒ Object
28 29 30 |
# File 'lib/rrtf/node/image_node.rb', line 28 def border @border end |
#displayed_height ⇒ Object
28 29 30 |
# File 'lib/rrtf/node/image_node.rb', line 28 def displayed_height @displayed_height end |
#displayed_width ⇒ Object
28 29 30 |
# File 'lib/rrtf/node/image_node.rb', line 28 def displayed_width @displayed_width end |
#height ⇒ Object (readonly)
28 29 30 |
# File 'lib/rrtf/node/image_node.rb', line 28 def height @height end |
#sizing_mode ⇒ Object
28 29 30 |
# File 'lib/rrtf/node/image_node.rb', line 28 def sizing_mode @sizing_mode end |
#type ⇒ Object (readonly)
28 29 30 |
# File 'lib/rrtf/node/image_node.rb', line 28 def type @type end |
#width ⇒ Object (readonly)
28 29 30 |
# File 'lib/rrtf/node/image_node.rb', line 28 def width @width end |
Class Method Details
.inspect(source) ⇒ Array<Object>
Attempts to extract the type, width, and height of an image using FastImage.
37 38 39 40 41 42 43 44 |
# File 'lib/rrtf/node/image_node.rb', line 37 def self.inspect(source) type, width, height = nil type = TYPE_DICTIONARY[FastImage.type(source)] width, height = FastImage.size(source) unless type.nil? [type, width, height] end |
.parse_border_array(border) ⇒ Object
inspect()
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rrtf/node/image_node.rb', line 46 def self.parse_border_array(border) case border when nil [] when BorderStyle [border] when Hash [BorderStyle.new(border)] when Array border.collect{ |b| parse_border_array(b) }.flatten.compact else RTFError.fire("Invalid border #{b}.") end end |
Instance Method Details
#to_rtf ⇒ Object
This method generates the RTF for an ImageNode object.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/rrtf/node/image_node.rb', line 124 def to_rtf text = StringIO.new count = 0 text << '{\pict' @border.each{ |b| text << " #{b.prefix(self.root)}" } text << "\\picwgoal#{@displayed_width}" if @displayed_width != nil text << "\\pichgoal#{@displayed_height}" if @displayed_height != nil text << "\\picw#{@width}\\pich#{@height}\\bliptag#{@id}" text << "\\#{@type}\n" @source.each_byte do |byte| hex_str = byte.to_s(16) hex_str.insert(0,'0') if hex_str.length == 1 text << hex_str count += 1 if count == 40 text << "\n" count = 0 end # if end # each_byte text << "\n}" text.string end |