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, #numbered?, #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.



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

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:



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

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

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

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

    real
end

Instance Method Details

#to_s(eol: $/) ⇒ Object Also known as: to_obfuscated_str

:nodoc:



176
177
178
# File 'lib/origami/numeric.rb', line 176

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