Class: Podoff::Obj

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc, ref) ⇒ Obj

Returns a new instance of Obj.



117
118
119
120
121
122
# File 'lib/podoff.rb', line 117

def initialize(doc, ref)

  @document = doc
  @ref = ref
  @lines = []
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



113
114
115
# File 'lib/podoff.rb', line 113

def document
  @document
end

#linesObject (readonly)

Returns the value of attribute lines.



115
116
117
# File 'lib/podoff.rb', line 115

def lines
  @lines
end

#refObject (readonly)

Returns the value of attribute ref.



114
115
116
# File 'lib/podoff.rb', line 114

def ref
  @ref
end

Instance Method Details

#<<(l) ⇒ Object



124
125
126
127
# File 'lib/podoff.rb', line 124

def <<(l)

  @lines << l
end

#contentsObject



193
194
195
196
197
# File 'lib/podoff.rb', line 193

def contents

  r = lookup('Contents')
  r ? r[0..-2].strip : nil
end

#crop_boxObject



237
238
239
240
241
242
# File 'lib/podoff.rb', line 237

def crop_box

  r = lookup('CropBox') || lookup('MediaBox')

  r ? r.strip[1..-2].split(' ').collect(&:strip).collect(&:to_f) : nil
end

#crop_dimsObject



244
245
246
247
248
249
# File 'lib/podoff.rb', line 244

def crop_dims

  x, y, w, h = crop_box

  x ? [ w - x, h - y ] : nil
end

#dup(new_doc) ⇒ Object



216
217
218
219
220
221
222
223
# File 'lib/podoff.rb', line 216

def dup(new_doc)

  o0 = self
  o = o0.class.new(new_doc, @ref)
  o.instance_eval { @lines = o0.lines.dup }

  o
end

#find(opts = {}, &block) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/podoff.rb', line 225

def find(opts={}, &block)

  return self if block.call(self)

  [ *kids, contents ].compact.each do |k|
    o = @document.objs[k]
    return o if o && block.call(o)
  end

  nil
end

#font_namesObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/podoff.rb', line 199

def font_names

  @lines.inject(nil) do |names, l|

    if names
      return names if l == '>>'
      if m = l.match(/\/([^ ]+) /); names << m[1]; end
    elsif l.match(/\/Font\s*$/)
      names = []
    end

    names
  end

  []
end

#index(o, start = 0) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/podoff.rb', line 140

def index(o, start=0)

  @lines[start..-1].each_with_index do |l, i|

    if o.is_a?(String)
      return start + i if l == o
    else
      return start + i if l.match(o)
    end
  end

  nil
end

#is_font?Boolean

Returns:

  • (Boolean)


171
172
173
174
# File 'lib/podoff.rb', line 171

def is_font?

  type() == 'Font'
end

#is_page?Boolean

Returns:

  • (Boolean)


166
167
168
169
# File 'lib/podoff.rb', line 166

def is_page?

  page_number != nil
end

#kidsObject



185
186
187
188
189
190
191
# File 'lib/podoff.rb', line 185

def kids

  # /Kids [1 0 R 16 0 R 33 0 R]

  r = lookup('Kids')
  (r || '').split(/[\[\]R]/).collect(&:strip).reject(&:empty?)
end

#lookup(k) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/podoff.rb', line 129

def lookup(k)

  @lines.each do |l|

    m = l.match(/^\/#{k} (.*)$/)
    return m[1] if m
  end

  nil
end

#page_numberObject



160
161
162
163
164
# File 'lib/podoff.rb', line 160

def page_number

  r = lookup('pdftk_PageNum')
  r ? r.to_i : nil
end

#parentObject



176
177
178
179
180
181
182
183
# File 'lib/podoff.rb', line 176

def parent

  # /Parent 2 0 R

  r = lookup('Parent')

  r ? r[0..-2].strip : nil
end

#prepend_text(x, y, text, opts = {}) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/podoff.rb', line 251

def prepend_text(x, y, text, opts={})

  o = find { |o| o.index('BT') }
  fail ArgumentError.new('found no BT in the tree') unless o

  font = opts[:font] || o.font_names.first || 'TT0'
  size = opts[:size] || 10
  comm = opts[:comment]

  i = o.index('BT')
  bt = []
  bt << 'BT'
  bt << "#{x} #{y} Td"
  bt << "/#{font} #{size} Tf"
  bt << "(#{text})Tj"
  bt << 'ET'
  bt << " % #{comm}" if comm
  bt = bt.join(' ')

  o.lines.insert(i, bt)

  o
end

#typeObject



154
155
156
157
158
# File 'lib/podoff.rb', line 154

def type

  t = lookup('Type')
  t ? t[1..-1] : nil
end