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.


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

def initialize image_blob
  self.gimage = GMagick::Image.new image_blob

  @bits = self.gimage.depth
  @width = self.gimage.width
  @height = self.gimage.height
end

Instance Attribute Details

#bitsObject (readonly)

Returns the value of attribute bits


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

def bits
  @bits
end

#channelsObject (readonly)

Returns the value of attribute channels


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

def channels
  @channels
end

#gimageObject

Returns the value of attribute gimage


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

def gimage
  @gimage
end

#heightObject (readonly)

Returns the value of attribute height


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

def height
  @height
end

#scaled_heightObject

Returns the value of attribute scaled_height


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

def scaled_height
  @scaled_height
end

#scaled_widthObject

Returns the value of attribute scaled_width


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

def scaled_width
  @scaled_width
end

#widthObject (readonly)

Returns the value of attribute width


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

def width
  @width
end

Class Method Details

.can_render?(image_blob) ⇒ Boolean

Returns:

  • (Boolean)

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

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

Instance Method Details

#build_pdf_object(document) ⇒ Object


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
# File 'lib/prawn-gmagick.rb', line 22

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

  alpha_mask = self.gimage.alpha_unpack
  if alpha_mask.unpack("C*").uniq.length > 1
    smask_obj = document.ref!(
            :Type             => :XObject,
            :Subtype          => :Image,
            :Height           => gimage.height,
            :Width            => gimage.width,
            :BitsPerComponent => gimage.depth,
            :ColorSpace       => :DeviceGray,
            :Decode           => [0, 1]
    )
    smask_obj.stream << alpha_mask
    obj.data[:SMask] = smask_obj
  end

  obj
end