Class: RTF::ImageNode

Inherits:
Node
  • Object
show all
Defined in:
lib/rtf/node.rb

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 collapse

PNG =

A definition for an image type constant.

:pngblip
JPEG =

A definition for an image type constant.

:jpegblip
BITMAP =

A definition for an image type constant.

:dibitmap0
LITTLE_ENDIAN =

A definition for an architecture endian constant.

:little
BIG_ENDIAN =

A definition for an architecture endian constant.

:big

Instance Attribute Summary collapse

Attributes inherited from Node

#parent

Instance Method Summary collapse

Methods inherited from Node

#is_root?, #next_node, #previous_node, #root

Constructor Details

#initialize(parent, source, id) ⇒ ImageNode

This is the constructor for the ImageNode class.

Parameters

parent

A reference to the node that owns the new image node.

source

A reference to the image source. This must be a String or a File.

id

The unique identifier for the image node.

Exceptions

RTFError

Generated whenever the image specified is not recognised as a supported image type, something other than a String or File or IO is passed as the source parameter or if the specified source does not exist or cannot be accessed.



1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
# File 'lib/rtf/node.rb', line 1132

def initialize(parent, source, id)
   super(parent)
   @source = nil
   @id     = id
   @read   = []
   @type   = nil
   @x_scaling = @y_scaling = nil
   @top_crop = @right_crop = @bottom_crop = @left_crop = nil
   @width = @height = nil

   # Check what we were given.
   src = source
   src.binmode if src.instance_of?(File)
   src = File.new(source, 'rb') if source.instance_of?(String)
   if src.instance_of?(File)
      # Check the files existence and accessibility.
      if !File.exist?(src.path)
         RTFError.fire("Unable to find the #{File.basename(source)} file.")
      end
      if !File.readable?(src.path)
         RTFError.fire("Access to the #{File.basename(source)} file denied.")
      end
      @source = src
   else
      RTFError.fire("Unrecognised source specified for ImageNode.")
   end

   @type = get_file_type(src)
   if @type == nil
      RTFError.fire("The #{File.basename(source)} file contains an "\
                    "unknown or unsupported image type.")
   end

   @width, @height = get_dimensions
end

Instance Attribute Details

#bottom_cropObject

Attribute accessor.



1111
1112
1113
# File 'lib/rtf/node.rb', line 1111

def bottom_crop
  @bottom_crop
end

#heightObject (readonly)

Attribute accessor.



1111
1112
1113
# File 'lib/rtf/node.rb', line 1111

def height
  @height
end

#left_cropObject

Attribute accessor.



1111
1112
1113
# File 'lib/rtf/node.rb', line 1111

def left_crop
  @left_crop
end

#right_cropObject

Attribute accessor.



1111
1112
1113
# File 'lib/rtf/node.rb', line 1111

def right_crop
  @right_crop
end

#top_cropObject

Attribute accessor.



1111
1112
1113
# File 'lib/rtf/node.rb', line 1111

def top_crop
  @top_crop
end

#widthObject (readonly)

Attribute accessor.



1111
1112
1113
# File 'lib/rtf/node.rb', line 1111

def width
  @width
end

#x_scalingObject

Attribute accessor.



1111
1112
1113
# File 'lib/rtf/node.rb', line 1111

def x_scaling
  @x_scaling
end

#y_scalingObject

Attribute accessor.



1111
1112
1113
# File 'lib/rtf/node.rb', line 1111

def y_scaling
  @y_scaling
end

Instance Method Details

#to_rtfObject

This method generates the RTF for an ImageNode object.



1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
# File 'lib/rtf/node.rb', line 1199

def to_rtf
   text  = StringIO.new
   count = 0

   #text << '{\pard{\*\shppict{\pict'
   text << '{\*\shppict{\pict'
   text << "\\picscalex#{@x_scaling}" if @x_scaling != nil
   text << "\\picscaley#{@y_scaling}" if @y_scaling != nil
   text << "\\piccropl#{@left_crop}" if @left_crop != nil
   text << "\\piccropr#{@right_crop}" if @right_crop != nil
   text << "\\piccropt#{@top_crop}" if @top_crop != nil
   text << "\\piccropb#{@bottom_crop}" if @bottom_crop != nil
   text << "\\picw#{@width}\\pich#{@height}\\bliptag#{@id}"
   text << "\\#{@type.id2name}\n"
   @source.each_byte {|byte| @read << byte} if @source.eof? == false
   @read.each do |byte|
      text << ("%02x" % byte)
      count += 1
      if count == 40
         text << "\n"
         count = 0
      end
   end
   #text << "\n}}\\par}"
   text << "\n}}"

   text.string
end