Class: Podoff::Document
- Inherits:
-
Object
- Object
- Podoff::Document
- Defined in:
- lib/podoff.rb
Instance Attribute Summary collapse
-
#footer ⇒ Object
readonly
Returns the value of attribute footer.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#objs ⇒ Object
readonly
Returns the value of attribute objs.
Instance Method Summary collapse
- #dup ⇒ Object
- #fonts ⇒ Object
-
#initialize(s) ⇒ Document
constructor
A new instance of Document.
- #page(i) ⇒ Object
- #pages ⇒ Object
- #write(path) ⇒ Object
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 # = nil s.split("\n").each do |l| if << 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) = [] << l elsif cur cur << l else @header << l end end end |
Instance Attribute Details
#footer ⇒ Object (readonly)
Returns the value of attribute footer.
41 42 43 |
# File 'lib/podoff.rb', line 41 def end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
39 40 41 |
# File 'lib/podoff.rb', line 39 def header @header end |
#objs ⇒ Object (readonly)
Returns the value of attribute objs.
40 41 42 |
# File 'lib/podoff.rb', line 40 def objs @objs end |
Instance Method Details
#dup ⇒ Object
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 = d0..dup @objs = d0.objs.values.inject({}) { |h, v| h[v.ref] = v.dup(d); h } end d end |
#fonts ⇒ Object
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 |
#pages ⇒ Object
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 .each { |l| f.print(l); f.print("\n") } end end |