Class: PDFBeads::PageDataProvider::PageData

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

Overview

Allows to collect data needed for building an individual page of a PDF document and gives access to those data.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, basename, args, exts, pref) ⇒ PageData

Returns a new instance of PageData.



43
44
45
46
47
48
49
50
51
52
# File 'lib/pdfbeads/pdfpage.rb', line 43

def initialize( path,basename,args,exts,pref )
  @name = path
  @basename = basename
  @s_type = 'b'
  @stencils = Array.new()
  @pageargs = args
  @exts = exts
  @pref = pref
  @bg_layer = @fg_layer = nil
end

Instance Attribute Details

#basenameObject (readonly)

Returns the value of attribute basename.



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

def basename
  @basename
end

#bg_layerObject

Returns the value of attribute bg_layer.



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

def bg_layer
  @bg_layer
end

#fg_layerObject

Returns the value of attribute fg_layer.



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

def fg_layer
  @fg_layer
end

#heightObject

Returns the value of attribute height.



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

def height
  @height
end

#hocr_pathObject (readonly)

Returns the value of attribute hocr_path.



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

def hocr_path
  @hocr_path
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#s_typeObject (readonly)

Returns the value of attribute s_type.



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

def s_type
  @s_type
end

#stencilsObject (readonly)

Returns the value of attribute stencils.



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

def stencils
  @stencils
end

#widthObject

Returns the value of attribute width.



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

def width
  @width
end

#x_resObject

Returns the value of attribute x_res.



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

def x_res
  @x_res
end

#y_resObject

Returns the value of attribute y_res.



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

def y_res
  @y_res
end

Class Method Details

.fixResolution(img) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/pdfbeads/pdfpage.rb', line 147

def self.fixResolution( img )
  xres = img.x_resolution; yres = img.y_resolution
  if img.units == PixelsPerCentimeterResolution
    img.units = PixelsPerInchResolution
    xres = (xres * 2.54).round
    yres = (yres * 2.54).round
  end
  return [ xres, yres ]
end

Instance Method Details

#addSupplementaryFilesObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/pdfbeads/pdfpage.rb', line 103

def addSupplementaryFiles()
  force = @pageargs[:force_update]
  exts_pattern = @exts.join( '|' )
  pref_pattern = @pref.join( '|' )

  if @bg_layer.nil?
    bgpath = Dir.entries('.').detect do |f|
      /\A#{@basename}.(bg|sep).(#{pref_pattern})\Z/i.match(f)
    end
    bgpath = Dir.entries('.').detect do |f|
      /\A#{@basename}.(bg|sep).(#{exts_pattern})\Z/i.match(f)
    end if bgpath.nil?
    @bg_layer = bgpath unless bgpath.nil?

    # If updating auxiliary files is requested and the base image is
    # either monochrome or indexed with just a few colors (i. e. doesn't
    # contain any elements which should be encoded to the background layer),
    # then the *.color.* image (if present) takes priority over any existing
    # *.bg.* and *.fg.* images. So we should regenerate them.
    if bgpath.nil? or ( force and not @s_type.eql? 'c' )
      colorpath = Dir.entries('.').detect do |f|
        /\A#{@basename}.color.(#{exts_pattern})\Z/i.match(f)
      end
      unless colorpath.nil?
        fnames = Array.new()
        separateColor( colorpath )
      end
    end
  end

  if @fg_layer.nil? and @stencils.length == 1
    fgpath = Dir.entries('.').detect do |f|
      /\A#{@basename}.(fg).(#{exts_pattern})\Z/i.match(f)
    end
    @fg_layer = fgpath unless fgpath.nil?
  end

  if $has_hpricot
    @hocr_path = Dir.entries('.').detect do |f|
      /\A#{@basename}.(HOCR|HTML?)/i.match(f)
    end
  end
end

#fillStencilArrayObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pdfbeads/pdfpage.rb', line 54

def fillStencilArray()
  ret   = 0
  force = @pageargs[:force_update]
  fres  = @pageargs[:st_resolution]
  treshold = @pageargs[:threshold]

  map = Hash[
    :path => @name,
    :rgb  => [0.0, 0.0, 0.0],
  ]

  insp = ImageInspector.new( @name )
  return 0 if insp.width.nil?

  @width = insp.width; @height = insp.height
  unless fres > 0
    @x_res = insp.x_dpi; @y_res = insp.y_dpi
  else
    @x_res = @y_res = fres
  end

  if insp.depth == 1
    @stencils << map
    ret = 1

  else
    img = ImageList.new( @name )
    # ImageMagick incorrectly identifies indexed PNG images as DirectClass.
    # It also assigns a strange color value to fully opaque areas. So
    # we have to use an independent approach to recognize indexed images.
    unless insp.palette.nil?
      img.class_type = PseudoClass
      ret = processIndexed( img,@pageargs[:maxcolors],force )
    end
    ret = processMixed( img,treshold,force,map ) if ret == 0
    img.destroy!

    # Make sure there are no more RMagick objects
    GC.start
  end

  $stderr.puts( "Prepared data for processing #{@name}\n" )
  if insp.nextImage
    $stderr.puts( "Warning: #{@name} contains multiple images, but only the first one") 
    $stderr.puts( "\tis going to be used\n" )
  end
  ret
end