Module: Compass::Magick::Plugins

Included in:
Functions
Defined in:
lib/magick/plugins.rb,
lib/plugins/phantom.rb,
lib/plugins/pattern.rb,
lib/plugins/corners.rb

Overview

The Plugins module includes all external Compass Magick functions.

See Also:

Instance Method Summary collapse

Instance Method Details

#magick_corners(radius, top_left = nil, top_right = nil, bottom_right = nil, bottom_left = nil) ⇒ Command

Applies rounded corners around the Canvas.

Parameters:

  • radius (Sass::Script::Number)

    The corner radius.

  • top_left (Sass::Script::Bool) (defaults to: nil)

    Controls the top-left corner radius effect (default true)

  • top_right (Sass::Script::Bool) (defaults to: nil)

    Controls the top-right corner radius effect (default true)

  • bottom_right (Sass::Script::Bool) (defaults to: nil)

    Controls the bottom-right corner radius effect (default true)

  • bottom_left (Sass::Script::Bool) (defaults to: nil)

    Controls the bottom-left corner radius effect (default true)

Returns:

  • (Command)

    A command(-set) which applies the corners on the canvas.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/plugins/corners.rb', line 16

def magick_corners(radius, top_left = nil, top_right = nil, bottom_right = nil, bottom_left = nil)
  Command.new do |canvas|
    Canvas.new(canvas, magick_mask(
      magick_canvas(
        Sass::Script::Number.new(canvas.width),
        Sass::Script::Number.new(canvas.height),
          magick_fill(Sass::Script::Color.new([0, 0, 0])),
          magick_border(Sass::Script::Color.new([255, 255, 255]), radius, nil, top_left, top_right, bottom_right, bottom_left)
      )
    ))
  end
end

#magick_pattern(width, height, values) ⇒ Canvas

Draws a pattern and returns a B/W Canvas ready for masking.

Chris Eppstein’s original tweet:

http://twitter.com/chriseppstein/status/56095925843668992

Examples:

Diagonal stripes:

magick-pattern(3, 3,
  x _ _
  _ x _
  _ _ x
);

Parameters:

  • width (Sass::Script::Number)

    The width of the pattern.

  • height (Sass::Script::Number)

    The height of the pattern.

  • values (Sass::Script::List)

    The pattern itself. This is a list of values (or a multi-line string) where ‘1`, `true`, `yes`, `X`, `*` and `+` mark where an opaque white pixel will be placed. Any other value is ignored and is transparent in the output. The size of the list must match the width/height.

Returns:

  • (Canvas)

    A B/W Canvas with the pattern applied. The resulting image can be applied as a mask.

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/plugins/pattern.rb', line 27

def magick_pattern(width, height, values)
  Compass::Magick::Utils.assert_type   'width',              width,  Sass::Script::Number
  Compass::Magick::Utils.assert_type   'height',             height, Sass::Script::Number
  Compass::Magick::Utils.assert_one_of 'magick-pattern(..)', values, Sass::Script::List, Sass::Script::String
  if values.kind_of?(Sass::Script::String)
    list = values.value.strip().split(/[\r\n]+/).map { |line| line.strip().split(/\s/) }.flatten
  else
    list = values.value
  end
  size = width.value * height.value
  raise PatternException.new("magick-pattern(..) expects #{size} values, got #{list.size} instead: #{list.inspect}") unless size == list.size
  canvas = Canvas.new(width, height)
  opaque = ['1', 'true', 'yes', 'X', '*', '+']
  for y in (0...height.value)
    for x in (0...width.value)
      pixel = list[x + y * height.value].to_s.upcase
      canvas.set_pixel(x, y, ChunkyPNG::Color::WHITE) if opaque.include?(pixel)
    end
  end
  canvas
end

#magick_phantom(width, height, styles) ⇒ Canvas #magick_phantom(width, height, *styles) ⇒ Canvas

Creates a new Canvas with the given width and height and renders all styles using the PhantomJS headless Webkit. The resulting image is cropped by removing all transparent pixels.

The PhantomJS viewport will be set to ‘width` / `height` with padding: max(`width`, `height`). If you apply drop shadows or outlines, the returned image will be larger than the specified size to accommodate the overflowing content.

Overloads:

  • #magick_phantom(width, height, styles) ⇒ Canvas

    Parameters:

    • width (Sass::Script::Number)

      The (desired) width of the canvas.

    • height (Sass::Script::Number)

      The (desired) height of the canvas.

    • styles ({String => Sass::Script::String})

      The CSS styles to render in PhantomJS where the hash key is the CSS property name and the hash value is the CSS property value.

  • #magick_phantom(width, height, *styles) ⇒ Canvas

    Parameters:

    • width (Sass::Script::Number)

      The (desired) width of the canvas.

    • height (Sass::Script::Number)

      The (desired) height of the canvas.

    • styles ([String])

      A list of CSS styles to render in PhantomJS.

Returns:

  • (Canvas)

    A new Canvas instance cropped of all transparent pixels.

See Also:

  • Compass::Magick::Plugins.{Compass{Compass::Magick{Compass::Magick::Configuration}


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/plugins/phantom.rb', line 79

def magick_phantom(width, height, *args)
  Compass::Magick::Utils.assert_type 'width',  width,  Sass::Script::Number
  Compass::Magick::Utils.assert_type 'height', height, Sass::Script::Number
  elements = []
  args.each do |styles|
    Compass::Magick::Utils.assert_one_of 'magick-phantom(..)', styles, Sass::Script::String, Hash
    instructions = []
    if styles.kind_of?(Sass::Script::String)
      instructions.push(styles.value)
    else
      styles.each do |key, value|
        instructions.push("#{key.gsub('_', '-')}: #{value.kind_of?(Sass::Script::String) ? value.value : value.to_s}")
      end
    end
    elements.push(instructions.join('; '))
  end
  basename = "~magick-phantom-#{ rand(36**8).to_s(36) }.png"
  filename = File.join(Compass.configuration.generated_images_path, basename)
  command  = [Compass.configuration.phantom_executable, Compass.configuration.phantom_script]
  command  = command.concat([width.to_s, height.to_s])
  command  = command.concat(elements)
  command  = command.concat([Compass::Magick::Configuration.cygwin_path(filename)])
  begin
    system(command.shelljoin)
    canvas = Canvas.new(Sass::Script::String.new(basename))
    min_top    = canvas.height - 1
    max_right  = 0
    max_bottom = 0
    min_left   = canvas.width - 1
    for y in 0...canvas.height
      for x in 0...canvas.width
        if ! ChunkyPNG::Color.fully_transparent?(canvas.get_pixel(x, y))
          min_top    = y if y < min_top
          max_right  = x if x > max_right
          max_bottom = y if y > max_bottom
          min_left   = x if x < min_left
        end
      end
    end
    if min_left < max_right && min_top < max_bottom
      canvas.crop(min_left, min_top, max_right - min_left + 1, max_bottom - min_top + 1)
    else
      puts '(Compass:Magick) Phantom image rendering failed. Please make sure you have at least one drawing instruction in your styles:'
      puts "$ #{command.shelljoin}"
      canvas
    end
  ensure
    File.unlink(filename) if File.exists?(filename)
  end
end