Class: PSD::Renderer::ClippingMask

Inherits:
Object
  • Object
show all
Defined in:
lib/psd/renderer/clipping_mask.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canvas) ⇒ ClippingMask

Returns a new instance of ClippingMask.



6
7
8
9
10
11
12
13
# File 'lib/psd/renderer/clipping_mask.rb', line 6

def initialize(canvas)
  @canvas = canvas
  @node = @canvas.node
  
  mask_node = @canvas.node.clipping_mask

  @mask = MaskCanvas.new(mask_node)
end

Instance Attribute Details

#canvasObject (readonly)

Returns the value of attribute canvas.



4
5
6
# File 'lib/psd/renderer/clipping_mask.rb', line 4

def canvas
  @canvas
end

#maskObject (readonly)

Returns the value of attribute mask.



4
5
6
# File 'lib/psd/renderer/clipping_mask.rb', line 4

def mask
  @mask
end

Instance Method Details

#apply!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/psd/renderer/clipping_mask.rb', line 15

def apply!
  return unless @node.clipped?

  PSD.logger.debug "Applying clipping mask #{mask.node.name} to #{@node.name}"

  @canvas.height.times do |y|
    @canvas.width.times do |x|
      doc_x = @canvas.left + x
      doc_y = @canvas.top + y

      mask_x = doc_x - @mask.left
      mask_y = doc_y - @mask.top

      if mask_x < 0 || mask_x > mask.width || mask_y < 0 || mask_y > mask.height
        alpha = 0
      else
        pixel = mask.canvas.pixels[mask_y * mask.width + mask_x]
        alpha = pixel.nil? ? 0 : ChunkyPNG::Color.a(pixel)
      end
      
      color = @canvas.get_pixel(x, y)
      @canvas.set_pixel x, y, (color & 0xffffff00) | (ChunkyPNG::Color.a(color) * alpha / 255)
    end
  end
end