Class: Sumi::Graphics::Canvas
- Inherits:
-
Object
- Object
- Sumi::Graphics::Canvas
- Extended by:
- Forwardable
- Defined in:
- graphics/lib/sumi/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 ⇒ Sumi::Graphics::Paper
readonly
Returns the inner paper after margins.
-
#margin ⇒ Sumi::Graphics::Margin
readonly
Returns the canvas margins.
-
#size ⇒ Sumi::Graphics::Paper
readonly
Returns the paper size object.
Class Method Summary collapse
- .call(paper = Undefined, **fields) ⇒ Object
-
.from_paper(paper, **overrides) ⇒ Sumi::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.
-
#landscape ⇒ Sumi::Graphics::Canvas
Returns a landscape canvas, rotating dimensions and margins when width does not exceed height.
-
#portrait ⇒ Sumi::Graphics::Canvas
Returns a portrait canvas, rotating dimensions and margins when height does not exceed width.
-
#rotate ⇒ Sumi::Graphics::Canvas
Returns a canvas with width and height swapped and margins rotated 90 degrees clockwise.
-
#to_h ⇒ Hash{Symbol => Object}
Returns canvas fields as a Hash compatible with Canvas.call.
-
#viewbox(origin = Undefined) ⇒ String
Returns the SVG viewBox string.
-
#viewport ⇒ Hash{Symbol => String}
Returns SVG width and height attributes.
-
#with(**kwargs) ⇒ Sumi::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 'graphics/lib/sumi/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 ⇒ Sumi::Graphics::Paper (readonly)
Returns the inner paper after margins.
89 90 91 |
# File 'graphics/lib/sumi/graphics/auxiliary/canvas.rb', line 89 def inner @inner end |
#margin ⇒ Sumi::Graphics::Margin (readonly)
Returns the canvas margins.
85 86 87 |
# File 'graphics/lib/sumi/graphics/auxiliary/canvas.rb', line 85 def margin @margin end |
#size ⇒ Sumi::Graphics::Paper (readonly)
Returns the paper size object.
81 82 83 |
# File 'graphics/lib/sumi/graphics/auxiliary/canvas.rb', line 81 def size @size end |
Class Method Details
.call(paper, **overrides) ⇒ Sumi::Graphics::Canvas .call(width:, height:, unit: "mm", name: :custom, margins: []) ⇒ Sumi::Graphics::Canvas
43 44 45 |
# File 'graphics/lib/sumi/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) ⇒ Sumi::Graphics::Canvas
55 56 57 58 59 |
# File 'graphics/lib/sumi/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 'graphics/lib/sumi/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 'graphics/lib/sumi/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 'graphics/lib/sumi/graphics/auxiliary/canvas.rb', line 136 def hash = [self.class, size, margin].hash |
#landscape ⇒ Sumi::Graphics::Canvas
Returns a landscape canvas, rotating dimensions and margins when width does not exceed height.
142 |
# File 'graphics/lib/sumi/graphics/auxiliary/canvas.rb', line 142 def landscape = size.landscape? ? self : rotate |
#portrait ⇒ Sumi::Graphics::Canvas
Returns a portrait canvas, rotating dimensions and margins when height does not exceed width.
146 |
# File 'graphics/lib/sumi/graphics/auxiliary/canvas.rb', line 146 def portrait = size.portrait? ? self : rotate |
#rotate ⇒ Sumi::Graphics::Canvas
Returns a canvas with width and height swapped and margins rotated 90 degrees clockwise.
Margins rotate from [top, right, bottom, left] to [left, top, right, bottom] so vertical and horizontal
margin sums swap alongside width and height.
152 |
# File 'graphics/lib/sumi/graphics/auxiliary/canvas.rb', line 152 def rotate = self.class.new(**size.rotate.to_h, margins: [margin.left, margin.top, margin.right, margin.bottom]) |
#to_h ⇒ Hash{Symbol => Object}
Returns canvas fields as a Hash compatible with call.
156 |
# File 'graphics/lib/sumi/graphics/auxiliary/canvas.rb', line 156 def to_h = {**size.to_h, margins: margin.to_a} |
#viewbox(origin = Undefined) ⇒ String
Returns the SVG viewBox string. Omission uses the negative left and top margins. Nil uses zero for both coordinates.
167 |
# File 'graphics/lib/sumi/graphics/auxiliary/canvas.rb', line 167 def viewbox(origin = Undefined) = prettify(*originate(origin), width, height).join(" ") |
#viewport ⇒ Hash{Symbol => String}
Returns SVG width and height attributes.
160 |
# File 'graphics/lib/sumi/graphics/auxiliary/canvas.rb', line 160 def = {width: "#{width}#{unit}", height: "#{height}#{unit}"} |
#with(**kwargs) ⇒ Sumi::Graphics::Canvas
Returns a canvas with selected fields replaced. Unspecified fields, including margins, retain their current values.
185 186 187 188 189 190 191 192 193 194 |
# File 'graphics/lib/sumi/graphics/auxiliary/canvas.rb', line 185 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 |