Class: Sevgi::Graphics::Canvas

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sevgi/graphics/auxiliary/canvas.rb

Overview

SVG canvas size, margins, viewport, and viewBox.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width:, height:, unit: "mm", name: :custom, margins: []) ⇒ void

Creates a canvas with finite real dimensions greater than zero and margins that leave a positive inner area.

Parameters:

  • width (Numeric)

    canvas width

  • height (Numeric)

    canvas height

  • unit (Symbol, String) (defaults to: "mm")

    SVG unit

  • name (Symbol, String) (defaults to: :custom)

    paper name

  • margins (Array<Numeric>) (defaults to: [])

    margin shorthand values

Raises:

  • (Sevgi::ArgumentError)

    when margins or numeric dimensions are invalid



75
76
77
78
79
80
81
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 75

def initialize(width:, height:, unit: "mm", name: :custom, margins: [])
  @size = Paper[width, height, unit, name]
  @margin = Margin[*margins]

  compute
  freeze
end

Instance Attribute Details

#innerSevgi::Graphics::Paper (readonly)

Returns the inner paper after margins.



65
66
67
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 65

def inner
  @inner
end

#marginSevgi::Graphics::Margin (readonly)

Returns the canvas margins.



61
62
63
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 61

def margin
  @margin
end

#sizeSevgi::Graphics::Paper (readonly)

Returns the paper size object.



57
58
59
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 57

def size
  @size
end

Class Method Details

.call(arg = Undefined, **kwargs) ⇒ Sevgi::Graphics::Canvas

Builds a canvas from a paper profile or explicit size.

Parameters:

  • arg (Sevgi::Graphics::Paper, Symbol, String, Sevgi::Undefined) (defaults to: Undefined)

    paper profile or paper object

  • kwargs (Hash)

    canvas keyword arguments

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when the paper profile is unknown



19
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 19

def self.call(...) = from_paper(...)

.from_paper(arg = Undefined, **kwargs) ⇒ Sevgi::Graphics::Canvas

Builds a canvas from a paper profile or explicit size.

Parameters:

  • arg (Sevgi::Graphics::Paper, Symbol, String, Sevgi::Undefined) (defaults to: Undefined)

    paper profile or paper object

  • kwargs (Hash)

    canvas keyword arguments

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when the paper profile is unknown



26
27
28
29
30
31
32
33
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 26

def self.from_paper(arg = Undefined, **kwargs)
  case arg
  when Undefined
    new(**kwargs)
  else
    new(**paper(arg).to_h, **kwargs)
  end
end

Instance Method Details

#attributes(origin = Undefined) ⇒ Hash{Symbol => String}

Returns SVG root viewport attributes.

Parameters:

  • origin (Numeric, Array<Numeric>, nil, Sevgi::Undefined) (defaults to: Undefined)

    viewBox origin

Returns:

  • (Hash{Symbol => String})

    SVG viewport and viewBox attributes

Raises:

  • (Sevgi::ArgumentError)

    when origin is invalid



88
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 88

def attributes(...) = {**viewport, viewBox: viewbox(...)}

#viewbox(origin = Undefined) ⇒ String

Returns the SVG viewBox string.

Parameters:

  • origin (Numeric, Array<Numeric>, nil, Sevgi::Undefined) (defaults to: Undefined)

    viewBox origin

Returns:

  • (String)

Raises:

  • (Sevgi::ArgumentError)

    when origin is invalid



98
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 98

def viewbox(origin = Undefined) = prettify(*originate(origin), width, height).join(" ")

#viewportHash{Symbol => String}

Returns SVG width and height attributes.

Returns:

  • (Hash{Symbol => String})

    SVG width and height attributes



92
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 92

def viewport = {width: "#{width}#{unit}", height: "#{height}#{unit}"}

#with(**kwargs) ⇒ Sevgi::Graphics::Canvas

Returns a canvas with selected fields replaced.

Parameters:

  • kwargs (Hash)

    replacement options

Options Hash (**kwargs):

  • :width (Numeric)

    replacement canvas width

  • :height (Numeric)

    replacement canvas height

  • :unit (Symbol, String)

    replacement SVG unit

  • :name (Symbol, String)

    replacement paper name

  • :margins (Array<Numeric>)

    replacement margin shorthand values

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when an unknown option is supplied

  • (Sevgi::ArgumentError)

    when a replacement value is invalid



110
111
112
113
114
115
116
117
118
119
# File 'lib/sevgi/graphics/auxiliary/canvas.rb', line 110

def with(**kwargs)
  unknown = kwargs.keys - REPLACEMENTS

  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