Class: Origami::Real

Inherits:
Float
  • Object
show all
Includes:
Number
Defined in:
lib/origami/numeric.rb,
lib/origami/obfuscation.rb

Overview

Class representing a Real number Object. PDF real numbers are arbitrary precision numbers, depending on architectures.

Constant Summary collapse

TOKENS =

:nodoc:

[ "(\\+|-)?([\\d]*\\.[\\d]+|[\\d]+\\.[\\d]*)([eE](\\+|-)?[\\d]+)?" ]
REGEXP_TOKEN =
Regexp.new(TOKENS.first)
@@regexp =
Regexp.new(WHITESPACES + "(?<real>#{TOKENS.first})")

Instance Attribute Summary

Attributes included from Object

#file_offset, #generation, #no, #objstm_offset, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Number

#&, #*, #**, #+, #-, #-@, #/, #<<, #>>, #^, #abs, #|, #~

Methods included from Object

#<=>, #cast_to, #copy, #document, #export, included, #indirect?, #indirect_parent, #logicalize, #logicalize!, #native_type, #post_build, #pre_build, #reference, #set_document, #set_indirect, skip_until_next_obj, #solve, #to_o, #type, typeof, #version_required, #xrefs

Constructor Details

#initialize(f = 0) ⇒ Real

Creates a new Real from a Ruby Float.

f

The new Real value.



150
151
152
153
154
155
156
# File 'lib/origami/numeric.rb', line 150

def initialize(f = 0)
    unless f.is_a?(Float)
        raise TypeError, "Expected type Float, received #{f.class}."
    end

    super(f)
end

Class Method Details

.parse(stream, _parser = nil) ⇒ Object

:nodoc:



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/origami/numeric.rb', line 158

def self.parse(stream, _parser = nil) #:nodoc:
    offset = stream.pos

    if not stream.scan(@@regexp)
        raise InvalidRealObjectError, "Invalid real number format"
    end

    value = stream['real'].to_f
    real = Real.new(value)
    real.file_offset = offset

    real
end

Instance Method Details

#to_sObject Also known as: to_obfuscated_str



174
175
176
# File 'lib/origami/numeric.rb', line 174

def to_s
    sprintf("%f", self).sub(/\.0*$|(\.\d*[^0])0*$/, '\1')
end