Module: PSD::Renderer::CairoHelpers

Included in:
VectorShape
Defined in:
lib/psd/renderer/cairo_helpers.rb

Overview

Instance Method Summary collapse

Instance Method Details

#cairo_image_surface(w, h, bg = nil) {|cr| ... } ⇒ Object

Yields:

  • (cr)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/psd/renderer/cairo_helpers.rb', line 6

def cairo_image_surface(w, h, bg=nil)
  surface = Cairo::ImageSurface.new(w, h)
  cr = Cairo::Context.new(surface)

  if bg
    cr.set_source_rgba(*bg)
    cr.paint
  end

  yield cr

  surface.finish

  data = cr.target.data.to_s[0, 4 * w * h]

  # Cairo data is stored as BGRA, ugh.
  data = data.unpack("N*").map do |color|
    color = ChunkyPNG::Color.to_truecolor_alpha_bytes(color)
    ChunkyPNG::Color.rgba(color[2], color[1], color[0], color[3])
  end

  ChunkyPNG::Canvas.new(w, h, data)
end

#cairo_path(cr, *pairs) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/psd/renderer/cairo_helpers.rb', line 30

def cairo_path(cr, *pairs)
  first = true
  pairs.each do |cmd|
    if cmd == :c
      cr.close_path
      first = true
    elsif first
      cr.move_to(*cmd)
      first = false
    else
      cr.curve_to(*cmd)
    end 
  end
end