Class: Morandi::ImageCaption

Inherits:
ImageOp
  • Object
show all
Defined in:
lib/morandi/image-ops.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ImageOp

new_from_hash, #priority

Constructor Details

#initializeImageCaption

Returns a new instance of ImageCaption.



110
111
112
# File 'lib/morandi/image-ops.rb', line 110

def initialize()
  super()
end

Instance Attribute Details

#fontObject

Returns the value of attribute font.



109
110
111
# File 'lib/morandi/image-ops.rb', line 109

def font
  @font
end

#positionObject

Returns the value of attribute position.



109
110
111
# File 'lib/morandi/image-ops.rb', line 109

def position
  @position
end

#textObject

Returns the value of attribute text.



109
110
111
# File 'lib/morandi/image-ops.rb', line 109

def text
  @text
end

Instance Method Details

#call(image, pixbuf) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/morandi/image-ops.rb', line 123

def call(image, pixbuf)
  @pixbuf = pixbuf
  surface = Cairo::ImageSurface.new(:rgb24, pixbuf.width, pixbuf.height)
  cr = Cairo::Context.new(surface)

  cr.save do
    cr.set_source_pixbuf(pixbuf)
    #cr.rectangle(0, 0, pixbuf.width, pixbuf.height)
    cr.paint(1.0)
    cr.translate(*self.position)

    layout = cr.create_pango_layout
    layout.set_text(self.text)
    fd = Pango::FontDescription.new(self.font)
    layout.font_description = fd
    layout.set_width((pixbuf.width - self.position[0] - 100)*Pango::SCALE)
    layout.context_changed
    ink, _ = layout.pixel_extents
    cr.set_source_rgba(0, 0, 0, 0.3)
    cr.rectangle(-25, -25, ink.width + 50, ink.height + 50)
    cr.fill
    cr.set_source_rgb(1, 1, 1)
    cr.show_pango_layout(layout)
  end


  final_pb = surface.to_pixbuf
  cr.destroy
  surface.destroy
  return final_pb
end