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.



1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
# File 'lib/rtf/node.rb', line 1115

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.



1094
1095
1096
# File 'lib/rtf/node.rb', line 1094

def bottom_crop
  @bottom_crop
end

#heightObject (readonly)

Attribute accessor.



1094
1095
1096
# File 'lib/rtf/node.rb', line 1094

def height
  @height
end

#left_cropObject

Attribute accessor.



1094
1095
1096
# File 'lib/rtf/node.rb', line 1094

def left_crop
  @left_crop
end

#right_cropObject

Attribute accessor.



1094
1095
1096
# File 'lib/rtf/node.rb', line 1094

def right_crop
  @right_crop
end

#top_cropObject

Attribute accessor.



1094
1095
1096
# File 'lib/rtf/node.rb', line 1094

def top_crop
  @top_crop
end

#widthObject (readonly)

Attribute accessor.



1094
1095
1096
# File 'lib/rtf/node.rb', line 1094

def width
  @width
end

#x_scalingObject

Attribute accessor.



1094
1095
1096
# File 'lib/rtf/node.rb', line 1094

def x_scaling
  @x_scaling
end

#y_scalingObject

Attribute accessor.



1094
1095
1096
# File 'lib/rtf/node.rb', line 1094

def y_scaling
  @y_scaling
end

Instance Method Details

#to_rtfObject

This method generates the RTF for an ImageNode object.



1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
# File 'lib/rtf/node.rb', line 1181

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