Class: PDFBeads::PageDataProvider

Inherits:
Array
  • Object
show all
Defined in:
lib/pdfbeads/pdfpage.rb

Overview

Represents a set of page images accompanies with auxiliary files needed to build a PDF document.

Defined Under Namespace

Classes: PageData

Instance Method Summary collapse

Constructor Details

#initialize(files, args) ⇒ PageDataProvider

Takes a list of file names and a hash containing a set of options.



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/pdfbeads/pdfpage.rb', line 370

def initialize( files,args )
  @pageargs = args

  ext_lossless = [ 'PNG','TIFF?' ]
  ext_jpeg     = [ 'JPE?G' ]
  ext_jpeg2000 = [ 'JP2','JPX' ]

  @exts = Array.new()

  case @pageargs[:bg_format]
  when 'JP2'
    @exts << ext_jpeg2000 << ext_jpeg << ext_lossless
    @pref = Array.new( ext_jpeg2000 )
  when 'JPG'
    @exts << ext_jpeg << ext_jpeg2000 << ext_lossless
    @pref = Array.new( ext_jpeg )
  else
    @exts << ext_lossless << ext_jpeg2000 << ext_jpeg
    @pref = Array.new( ext_lossless )
  end

  # A hack for some Windows versions of RMagick, which throw an error the
  # first time when Magick.formats is accessed
  begin
    retries = 2
    mfmts = Magick.formats
  rescue
    retry if (retries -= 1 ) > 0
  end
  unless mfmts.has_key? 'JP2'
    @exts.delete_if{ |ext| ext_jpeg2000.include? ext }
    @pref = Array.new( ext_jpeg ) if @pref.include? 'JP2'
  end

  for fname in files do
    if /\A([^.]*)\.(TIFF?|PNG)\Z/i.match( fname )
      page = PageData.new( fname,$1,args,@exts,@pref )
      scnt = page.fillStencilArray()
      if scnt > 0
        page.addSupplementaryFiles()
        push( page )
      end
    end
  end
end

Instance Method Details

#jbig2EncodeObject

A wrapper for the jbig2 encoder. The jbig2 utility is called as many times as needed to encode all pages with the given pages-per-dict value.



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/pdfbeads/pdfpage.rb', line 418

def jbig2Encode()
  per_dict = @pageargs[:pages_per_dict]
  force = @pageargs[:force_update]

  has_jbig2 = false
  if /(win|w)32$/i.match( RUBY_PLATFORM )
    schar = ';'
    ext = '.exe'
    sep = '\\'
  else
    schar = ':'
    ext = ''
    sep = '/'
  end
  ENV['PATH'].split( schar ).each do |dir|
    if File.exists?( dir << sep << 'jbig2' << ext )
      has_jbig2 = true
      break
    end
  end

  unless has_jbig2
    $stderr.puts("JBIG2 compression has been requested, but the encoder is not available.")
    $stderr.puts( "  I'll use CCITT Group 4 fax compression instead." )
    return false
  end

  pidx = 0
  needs_update = force
  toConvert = Array.new()
  each_index do |i|
    p = fetch(i)
    pidx += 1
    p.stencils.each do |s|
      toConvert << s[:path]
      s[:jbig2path] = s[:path].sub( /\.(TIFF?|PNG)\Z/i,'.jbig2' )
      s[:jbig2dict] = toConvert[0].sub( /\.(TIFF?|PNG)\Z/i,'.sym' )
      if needs_update == false
        needs_update = true unless File.exists? s[:jbig2path] and File.exists? s[:jbig2dict]
      end
    end

    if pidx == per_dict or i == length - 1
      # The jbig2 encoder processes a bunch of files at once, producing 
      # pages which depend from a shared dictionary. Thus we can skip this
      # stage only if both the dictionary and each of the individual pages
      # are already found on the disk
      if needs_update
        IO.popen("jbig2 -s -p " << toConvert.join(' ') ) do |f|
          out = f.gets
          $stderr.puts out unless out.nil?
        end
        return false if $?.exitstatus > 0

        toConvert.each_index do |j|
          oname = sprintf( "output.%04d",j )
          File.rename( oname,toConvert[j].sub( /\.(TIFF?|PNG)\Z/i,'.jbig2' ) ) if File.exists? oname
        end
        File.rename( 'output.sym',toConvert[0].sub( /\.(TIFF?|PNG)\Z/i,'.sym' ) ) if File.exists? 'output.sym'
      end

      toConvert.clear
      needs_update = force
      pidx = 0
    end
  end
  return true
end