Class: Gmagick

Inherits:
Prawn::Images::Image
  • Object
show all
Defined in:
lib/prawn/gmagick.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_blob) ⇒ Gmagick

Returns a new instance of Gmagick.



13
14
15
16
17
18
# File 'lib/prawn/gmagick.rb', line 13

def initialize image_blob
  self.gimage = GMagick::Image.new image_blob
  self.bits = gimage.depth
  self.width = gimage.width
  self.height = gimage.height
end

Instance Attribute Details

#bitsObject

Returns the value of attribute bits.



5
6
7
# File 'lib/prawn/gmagick.rb', line 5

def bits
  @bits
end

#gimageObject

Returns the value of attribute gimage.



7
8
9
# File 'lib/prawn/gmagick.rb', line 7

def gimage
  @gimage
end

#heightObject

Returns the value of attribute height.



5
6
7
# File 'lib/prawn/gmagick.rb', line 5

def height
  @height
end

#scaled_heightObject

Returns the value of attribute scaled_height.



6
7
8
# File 'lib/prawn/gmagick.rb', line 6

def scaled_height
  @scaled_height
end

#scaled_widthObject

Returns the value of attribute scaled_width.



6
7
8
# File 'lib/prawn/gmagick.rb', line 6

def scaled_width
  @scaled_width
end

#widthObject

Returns the value of attribute width.



5
6
7
# File 'lib/prawn/gmagick.rb', line 5

def width
  @width
end

Class Method Details

.can_render?(image_blob) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/prawn/gmagick.rb', line 9

def self.can_render? image_blob
  GMagick::Image.format(image_blob) ? true : false
end

Instance Method Details

#build_pdf_object(document) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/prawn/gmagick.rb', line 20

def build_pdf_object(document)
  obj = document.ref!(
    Type: :XObject,
    Subtype: :Image,
    ColorSpace: gimage.colorspace,
    Height: height,
    Width: width,
    BitsPerComponent: bits
  )

  obj << gimage.unpack
  obj.stream.filters << { FlateDecode: nil }

  alpha_mask = self.gimage.alpha_unpack
  if alpha_mask.unpack("C*").uniq.length > 1
    smask_obj = document.ref!(
            :Type             => :XObject,
            :Subtype          => :Image,
            :Height           => height,
            :Width            => width,
            :BitsPerComponent => bits,
            :ColorSpace       => :DeviceGray,
            :Decode           => [0, 1]
    )
    smask_obj.stream << alpha_mask
    smask_obj.stream.filters << { FlateDecode: nil }

    obj.data[:SMask] = smask_obj
  end

  obj
end