Class: Podoff::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/podoff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ Document

Returns a new instance of Document.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/podoff.rb', line 43

def initialize(s)

  fail ArgumentError.new('not a PDF file') \
    unless s.match(/\A%PDF-\d+\.\d+\n/)

  @header = []
  #
  @objs = {}
  cur = nil
  #
  @footer = nil

  s.split("\n").each do |l|

    if @footer
      @footer << l
    elsif m = /^(\d+ \d+) obj\b/.match(l)
      cur = (@objs[m[1]] = Obj.new(self, m[1]))
      cur << l
    elsif m = /^xref\b/.match(l)
      @footer = []
      @footer << l
    elsif cur
      cur << l
    else
      @header << l
    end
  end
end

Instance Attribute Details

Returns the value of attribute footer.



41
42
43
# File 'lib/podoff.rb', line 41

def footer
  @footer
end

#headerObject (readonly)

Returns the value of attribute header.



39
40
41
# File 'lib/podoff.rb', line 39

def header
  @header
end

#objsObject (readonly)

Returns the value of attribute objs.



40
41
42
# File 'lib/podoff.rb', line 40

def objs
  @objs
end

Instance Method Details

#dupObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/podoff.rb', line 81

def dup

  d0 = self

  d = d0.class.allocate

  d.instance_eval do
    @header = d0.header.dup
    @footer = d0.footer.dup
    @objs = d0.objs.values.inject({}) { |h, v| h[v.ref] = v.dup(d); h }
  end

  d
end

#fontsObject



73
# File 'lib/podoff.rb', line 73

def fonts; @objs.values.select(&:is_font?); end

#page(i) ⇒ Object



76
77
78
79
# File 'lib/podoff.rb', line 76

def page(i)

  i < 1 ? nil : @objs.values.find { |o| o.page_number == i }
end

#pagesObject



74
# File 'lib/podoff.rb', line 74

def pages; @objs.values.select(&:is_page?); end

#write(path) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/podoff.rb', line 96

def write(path)

  File.open(path, 'wb') do |f|

    @header.each { |l| f.print(l); f.print("\n") }

    @objs.values.each do |o|
      o.lines.each { |l| f.print(l); f.print("\n") }
    end

    @footer.each { |l| f.print(l); f.print("\n") }
  end
end