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.



1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
# File 'lib/rtf/node.rb', line 1284

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.



1263
1264
1265
# File 'lib/rtf/node.rb', line 1263

def bottom_crop
  @bottom_crop
end

#heightObject (readonly)

Attribute accessor.



1263
1264
1265
# File 'lib/rtf/node.rb', line 1263

def height
  @height
end

#left_cropObject

Attribute accessor.



1263
1264
1265
# File 'lib/rtf/node.rb', line 1263

def left_crop
  @left_crop
end

#right_cropObject

Attribute accessor.



1263
1264
1265
# File 'lib/rtf/node.rb', line 1263

def right_crop
  @right_crop
end

#top_cropObject

Attribute accessor.



1263
1264
1265
# File 'lib/rtf/node.rb', line 1263

def top_crop
  @top_crop
end

#widthObject (readonly)

Attribute accessor.



1263
1264
1265
# File 'lib/rtf/node.rb', line 1263

def width
  @width
end

#x_scalingObject

Attribute accessor.



1263
1264
1265
# File 'lib/rtf/node.rb', line 1263

def x_scaling
  @x_scaling
end

#y_scalingObject

Attribute accessor.



1263
1264
1265
# File 'lib/rtf/node.rb', line 1263

def y_scaling
  @y_scaling
end

Instance Method Details

#to_rtfObject

This method generates the RTF for an ImageNode object.



1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
# File 'lib/rtf/node.rb', line 1351

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