Class: Sevgi::Graphics::Canvas
- Inherits:
-
Object
- Object
- Sevgi::Graphics::Canvas
- Extended by:
- Forwardable
- Defined in:
- lib/sevgi/graphics/auxiliary/canvas.rb
Overview
Immutable SVG canvas size, margins, viewport, and viewBox.
size is the outer paper. inner subtracts the margins but remains a
size value, not a positioned rectangle. By default the viewBox origin is
the negative left/top margin, so drawing coordinates (0, 0) begin at the
inner area's top-left while the SVG viewport still includes the margins.
Pass nil to #viewbox or #attributes for a literal zero origin.
Instance Attribute Summary collapse
-
#inner ⇒ Sevgi::Graphics::Paper
readonly
Returns the inner paper after margins.
-
#margin ⇒ Sevgi::Graphics::Margin
readonly
Returns the canvas margins.
-
#size ⇒ Sevgi::Graphics::Paper
readonly
Returns the paper size object.
Class Method Summary collapse
- .call(paper = Undefined, **fields) ⇒ Object
-
.from_paper(paper, **overrides) ⇒ Sevgi::Graphics::Canvas
Builds a canvas from a paper profile with optional field overrides.
Instance Method Summary collapse
-
#attributes(origin = Undefined) ⇒ Hash{Symbol => String}
Returns SVG root viewport attributes.
-
#eql?(other) ⇒ Boolean
(also: #==)
Reports structural canvas equality by size and margins.
-
#hash ⇒ Integer
Returns a hash compatible with structural equality.
-
#initialize(width:, height:, unit: "mm", name: :custom, margins: []) ⇒ void
constructor
Creates a canvas with finite real dimensions greater than zero and margins that leave a positive inner area.
-
#viewbox(origin = Undefined) ⇒ String
Returns the SVG viewBox string.
-
#viewport ⇒ Hash{Symbol => String}
Returns SVG width and height attributes.
-
#with(**kwargs) ⇒ Sevgi::Graphics::Canvas
Returns a canvas with selected fields replaced.
Constructor Details
#initialize(width:, height:, unit: "mm", name: :custom, margins: []) ⇒ void
Returns a new instance of Canvas.
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 100 def initialize(**fields) unknown = fields.keys - FIELDS ArgumentError.("Unknown canvas option: #{unknown.first}") unless unknown.empty? ArgumentError.("Canvas width and height are required") unless REQUIRED.all? { fields.key?(it) } fields = DEFAULTS.merge(fields) @size = Paper[*fields.values_at(:width, :height, :unit, :name)] @margin = Margin[*fields[:margins]] compute freeze end |
Instance Attribute Details
#inner ⇒ Sevgi::Graphics::Paper (readonly)
Returns the inner paper after margins.
89 90 91 |
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 89 def inner @inner end |
#margin ⇒ Sevgi::Graphics::Margin (readonly)
Returns the canvas margins.
85 86 87 |
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 85 def margin @margin end |
#size ⇒ Sevgi::Graphics::Paper (readonly)
Returns the paper size object.
81 82 83 |
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 81 def size @size end |
Class Method Details
.call(paper, **overrides) ⇒ Sevgi::Graphics::Canvas .call(width:, height:, unit: "mm", name: :custom, margins: []) ⇒ Sevgi::Graphics::Canvas
43 44 45 |
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 43 def self.call(paper = Undefined, **fields) paper.equal?(Undefined) ? new(**fields) : from_paper(paper, **fields) end |
.from_paper(paper, **overrides) ⇒ Sevgi::Graphics::Canvas
55 56 57 58 59 |
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 55 def self.from_paper(paper = Undefined, **overrides) ArgumentError.("Paper is required") if paper.equal?(Undefined) new(**profile(paper).to_h, **overrides) end |
Instance Method Details
#attributes(origin = Undefined) ⇒ Hash{Symbol => String}
124 |
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 124 def attributes(...) = {**, viewBox: viewbox(...)} |
#eql?(other) ⇒ Boolean Also known as: ==
Reports structural canvas equality by size and margins.
132 |
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 132 def eql?(other) = self.class == other.class && size.eql?(other.size) && margin.eql?(other.margin) |
#hash ⇒ Integer
Returns a hash compatible with structural equality.
136 |
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 136 def hash = [self.class, size, margin].hash |
#viewbox(origin = Undefined) ⇒ String
Returns the SVG viewBox string. Omission uses the negative left and top margins; nil uses zero for both coordinates.
149 |
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 149 def viewbox(origin = Undefined) = prettify(*originate(origin), width, height).join(" ") |
#viewport ⇒ Hash{Symbol => String}
Returns SVG width and height attributes.
142 |
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 142 def = {width: "#{width}#{unit}", height: "#{height}#{unit}"} |
#with(**kwargs) ⇒ Sevgi::Graphics::Canvas
Returns a canvas with selected fields replaced. Unspecified fields, including margins, retain their current values.
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 167 def with(**kwargs) unknown = kwargs.keys - FIELDS ArgumentError.("Unknown canvas option: #{unknown.first}") unless unknown.empty? margins = kwargs.fetch(:margins, margin.to_a) replacements = kwargs.dup.tap { it.delete(:margins) } self.class.new(**size.to_h, **replacements, margins:) end |