Class: Compass::Canvas::Backend::Base
- Inherits:
-
Sass::Script::Literal
- Object
- Sass::Script::Literal
- Compass::Canvas::Backend::Base
- 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
Instance Attribute Summary collapse
-
#file ⇒ String
The external file where the backend will be loaded/saved in a PNG format.
-
#height ⇒ Fixnum
The height of the canvas, in pixels.
-
#width ⇒ Fixnum
The width of the canvas, in pixels.
Instance Method Summary collapse
-
#begin_canvas ⇒ Object
Abstract method.
-
#execute ⇒ Object
Creates an empty canvas and executes all stored actions.
-
#execute_one(action, *args) ⇒ Object
Abstract method.
-
#initialize(*args) ⇒ Base
constructor
Initializes a new instance of a backend class.
-
#load_dependencies ⇒ Object
Abstract method.
-
#property(name) ⇒ Object
Reads a property of the backend.
-
#read_canvas ⇒ Object
Abstract method.
-
#to_blob ⇒ Object
Abstract method.
-
#to_s(options = {}) ⇒ Object
Serializes the canvas as a Sass type.
-
#value ⇒ Object
Returns the canvas as a Base64 encoded Data URI or as a file on disk depending on the configuration.
Constructor Details
#initialize(width, height, *actions) ⇒ Base #initialize(file, width, height, *actions) ⇒ Base #initialize(file) ⇒ Base
Initializes a new instance of a backend class.
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
#file ⇒ String
Returns 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 |
#height ⇒ Fixnum
Returns The height of the canvas, in pixels.
18 19 20 |
# File 'lib/canvas/backend.rb', line 18 def height @height end |
#width ⇒ Fixnum
Returns 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_canvas ⇒ Object
Abstract method.
Initialization code before the canvas is drawn.
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 |
#execute ⇒ Object
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.
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_dependencies ⇒ Object
Abstract method.
Initializes the backend by loading third-party dependencies.
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.
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_canvas ⇒ Object
Abstract method.
Reads a canvas from a file
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_blob ⇒ Object
Abstract method.
Clean up code, must return a String
representation of the canvas in a PNG format.
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( = {}) Sass::Script::String.new(value) end |
#value ⇒ Object
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 |