Class: DPTM6::PDF::DCLImage

Inherits:
PageObject show all
Defined in:
lib/dptm6/pdf.rb

Constant Summary collapse

REGEXP_PARSE =

array

Regexp.compile('(?:(\[[^\[\]]*+\])\s++)?+'           <<   # array
'(?:(-?+\d++(?:\.\d++)?+)\s++)?+' * 6 <<   # numbers
'(?:(/\w++)\s++)?+'                   <<   # set ExtGState parameters ("'/a0 'gs ")
'(\w++)\s++'                          <<   # operator (required)
'((?:/\w++\s++\w++\s++){2})?+'         )

Instance Attribute Summary

Attributes inherited from Object

#data, #num, #pdf

Instance Method Summary collapse

Methods inherited from PageObject

#to_string

Methods inherited from Object

#replace_pagenum, #to_string

Constructor Details

#initialize(pdf, num) ⇒ DCLImage

Returns a new instance of DCLImage.



287
288
289
# File 'lib/dptm6/pdf.rb', line 287

def initialize(pdf, num)
  super(pdf, pdf.pages[num])
end

Instance Method Details

#move_to(pdf, num) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/dptm6/pdf.rb', line 291

def move_to(pdf, num)
  # OBJECT / NUMBER
  # psimage   +1
  #   length  +2
  # resource  +0
  # page      +3+nx
  # xobj_i    +3+i
  #   length  +3+nx+1+i
  m = pdf.xref.size + 1
  nx = @obj_resource.x_objects.size
  @obj_content.           move_to(pdf, m + 1)
  @obj_content.obj_length.move_to(pdf, m + 2)
  @obj_resource.          move_to(pdf, m    )
  super(pdf, m + 3 + nx)
  @obj_resource.x_objects.each_with_index do |(key,obj),i|
    obj.           move_to(pdf, m + 3      + i)
    obj.obj_length.move_to(pdf, m + 4 + nx + i)
  end
  self
end

#parseObject

draw XObject (“… cm ‘/a0 gs /x5 Do ’”)



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/dptm6/pdf.rb', line 328

def parse
  stream_new = "q\n"

  # previous values
  concat = nil
  color  = { :rg => nil, :RG => nil }
  flag_x = false

  buf_node = []
  path = Path.new
  str_check = @obj_content.stream.gsub(REGEXP_PARSE) do |a|
    md = $~
    op = md[-2].to_sym
    case op
    when :m, :l   # moveto, lineto
      buf_node << Path::Node.new(md[2], md[3], op)
    when :h, :B   # close, fill and stroke
      if flag_x   # frame of XObject
        flag_x = false
        Path.new(buf_node).output_stroke(stream_new, true)
        path = Path.new
      else
        path.add(buf_node)
      end
      buf_node = []
    when :S   # stroke
      path.optimize.output_fill(stream_new)
      path = Path.new
      Path.new(buf_node).output_stroke(stream_new)
      buf_node = []
    when :rg, :RG   # set RGB for stroking / non-stroking
      rgbstr = md[2..4].join(' ')
      if (rgbstr == color[op])
      else
        path.optimize.output_fill(stream_new)
        path = Path.new
        color[op] = rgbstr
        stream_new << a
      end
    when :cm   # concat matrix
      if md[-1]   # insert XObject
        flag_x = true
        stream_new << "q\n"
        stream_new << "0 1 1 0 -842 0 cm\n" if concat   # cancel concat matrix for paths
        stream_new << a << "Q\n"
      else
        stream_new << "q " << (concat = a) unless concat
      end
    when :q, :Q   # gsave, grestore
    else   # others : copy
      stream_new << a
    end
    ''   # delete parsed string
  end
  STDERR.puts("WARNING: Some script could not be parsed -- #{str_check.inspect}") if (str_check != '')

  stream_new << "Q\n"
  @obj_content.stream = stream_new
  self
end

#set_deflevel(level) ⇒ Object



312
313
314
# File 'lib/dptm6/pdf.rb', line 312

def set_deflevel(level)
  @obj_content.set_deflevel(level)
end

#writeObject



316
317
318
319
320
321
# File 'lib/dptm6/pdf.rb', line 316

def write
  @obj_content. write
  @obj_resource.write
  super
  @obj_resource.x_objects.each_value(&:write)
end