Class: Compass::Canvas::Backend::Base

Inherits:
Sass::Script::Literal
  • Object
show all
Defined in:
lib/canvas/backend.rb

Overview

Base abstract backend class.

Each implementation must respond to four methods:

  • load_dependencies - initializes the backend by loading third-party dependencies

  • read_canvas - reads a canvas from a file

  • begin_canvas - initialization code before the canvas is drawn

  • execute_one - executes a single action on the canvas

  • to_blob - clean up code, must return a String representation of the canvas in a PNG format

Direct Known Subclasses

Cairo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, *actions) ⇒ Base #initialize(file, width, height, *actions) ⇒ Base #initialize(file) ⇒ Base

Initializes a new instance of a backend class.

Overloads:

  • #initialize(width, height, *actions) ⇒ Base

    Parameters:

    • width (Fixnum)

      The width of the canvas, in pixels.

    • height (Fixnum)

      The height of the canvas, in pixels.

    • actions (Array<Object>)

      The actions to execute.

  • #initialize(file, width, height, *actions) ⇒ Base

    Parameters:

    • file (String)

      The file where the backend will be saved in a PNG format.

    • width (Fixnum)

      The width of the canvas, in pixels.

    • height (Fixnum)

      The height of the canvas, in pixels.

    • actions (Array<Object>)

      The actions to execute.

  • #initialize(file) ⇒ Base

    Parameters:

    • file (String)

      An external file to read.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/canvas/backend.rb', line 35

def initialize(*args)
  load_dependencies
  if args[0].is_a?(String)
    file = args.shift
    file = Compass::Canvas.absolute_path_to(file) unless args[0].is_a?(Fixnum)
    @file = file
  end
  if args[0].is_a?(Fixnum)
    @width  = args.shift
    @height = args.shift
  end
  @actions  = args
  @executed = false
end

Instance Attribute Details

#fileString

Returns The external file where the backend will be loaded/saved in a PNG format.

Returns:

  • (String)

    The external file where the backend will be loaded/saved in a PNG format.



20
21
22
# File 'lib/canvas/backend.rb', line 20

def file
  @file
end

#heightFixnum

Returns The height of the canvas, in pixels.

Returns:

  • (Fixnum)

    The height of the canvas, in pixels.



18
19
20
# File 'lib/canvas/backend.rb', line 18

def height
  @height
end

#widthFixnum

Returns The width of the canvas, in pixels.

Returns:

  • (Fixnum)

    The width of the canvas, in pixels.



16
17
18
# File 'lib/canvas/backend.rb', line 16

def width
  @width
end

Instance Method Details

#begin_canvasObject

Abstract method.

Initialization code before the canvas is drawn.

Raises:



73
74
75
# File 'lib/canvas/backend.rb', line 73

def begin_canvas
  raise Compass::Canvas::Exception.new("(#{self.class}) Class must implement '#{this_method}'.")
end

#executeObject

Creates an empty canvas and executes all stored actions.



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/canvas/backend.rb', line 111

def execute
  return self if @executed
  if @width && @height
    begin_canvas
    execute_actions
  else
    read_canvas
  end
  @executed = true
  self
end

#execute_one(action, *args) ⇒ Object

Abstract method.

Executes a single action on the canvas.

Raises:



82
83
84
# File 'lib/canvas/backend.rb', line 82

def execute_one(action, *args)
  raise Compass::Canvas::Exception.new("(#{self.class}) Class must implement '#{this_method}'.")
end

#load_dependenciesObject

Abstract method.

Initializes the backend by loading third-party dependencies.

Raises:



55
56
57
# File 'lib/canvas/backend.rb', line 55

def load_dependencies
  raise Compass::Canvas::Exception.new("(#{self.class}) Class must implement '#{this_method}'.")
end

#property(name) ⇒ Object

Reads a property of the backend.

This can be used to provide custom information about a backend, such as width, height, the current point’s position, etc.

Parameters:

  • name (String)

    The property name.

Returns:

  • (Object)

    The property value, or nil if it doesn’t exist.



102
103
104
105
106
107
108
# File 'lib/canvas/backend.rb', line 102

def property(name)
  case name
  when :width;  return @width
  when :height; return @height
  else return nil
  end
end

#read_canvasObject

Abstract method.

Reads a canvas from a file

Raises:



64
65
66
# File 'lib/canvas/backend.rb', line 64

def read_canvas
  raise Compass::Canvas::Exception.new("(#{self.class}) Class must implement '#{this_method}'.")
end

#to_blobObject

Abstract method.

Clean up code, must return a String representation of the canvas in a PNG format.

Raises:



91
92
93
# File 'lib/canvas/backend.rb', line 91

def to_blob
  raise Compass::Canvas::Exception.new("(#{self.class}) Class must implement '#{this_method}'.")
end

#to_s(options = {}) ⇒ Object

Serializes the canvas as a Sass type



140
141
142
# File 'lib/canvas/backend.rb', line 140

def to_s(options = {})
  Sass::Script::String.new(value)
end

#valueObject

Returns the canvas as a Base64 encoded Data URI or as a file on disk depending on the configuration.



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/canvas/backend.rb', line 125

def value
  execute
  if @file
    extension = '.png'
    filename  = @file.chomp(extension) + extension
    path      = File.join(Compass.configuration.images_path, filename)
    FileUtils.mkpath(File.dirname(path))
    File.open(path, 'wb') { |io| io << to_blob }
    filename
  else
    "url('data:image/png;base64,#{ Base64.encode64(to_blob).gsub("\n", '') }')"
  end
end