Class: Sevgi::Graphics::Paper

Inherits:
Data
  • Object
show all
Includes:
Comparable
Defined in:
lib/sevgi/graphics/auxiliary/paper.rb

Overview

Paper size and unit profile.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width:, height:, unit: "mm", name: :custom, **options) ⇒ void

Creates a paper profile. Dimensions must be finite real numbers greater than zero.

Parameters:

  • width (Numeric)

    paper width

  • height (Numeric)

    paper height

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

    SVG unit

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

    profile name

  • options (Hash)

    unsupported extra options

Raises:

  • (Sevgi::ArgumentError)

    when dimensions, unit, name, or options are invalid



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

def initialize(width:, height:, unit: "mm", name: :custom, **options)
  self.class.send(:options!, options)
  super(
    width: self.class.send(:dimension!, :width, width),
    height: self.class.send(:dimension!, :height, height),
    unit: self.class.send(:normalize!, :unit, unit),
    name: self.class.send(:normalize!, :name, name)
  )
end

Instance Attribute Details

#heightFloat (readonly)

Returns paper height.

Returns:

  • (Float)

    paper height



6
7
8
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 6

def height
  @height
end

#nameSymbol (readonly)

Returns profile name.

Returns:

  • (Symbol)

    profile name



6
7
8
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 6

def name
  @name
end

#unitSymbol (readonly)

Returns SVG unit.

Returns:

  • (Symbol)

    SVG unit



6
7
8
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 6

def unit
  @unit
end

#widthFloat (readonly)

Returns paper width.

Returns:

  • (Float)

    paper width



6
7
8
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 6

def width
  @width
end

Class Method Details

.defaultSevgi::Graphics::Paper

Returns the default paper profile.



162
163
164
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 162

def self.default
  @mutex.synchronize { @profiles.fetch(:default) }
end

.define(name, overwrite: true, **spec) ⇒ Sevgi::Graphics::Paper

Defines a named paper profile after complete validation. Registration is process-global and thread-atomic. Existing profiles are replaced by default; with overwrite: false, identical definitions return the canonical profile and conflicting definitions raise. Names that are not Ruby call syntax remain accessible through public_send.

Parameters:

  • name (Symbol, String)

    profile name

  • overwrite (Boolean) (defaults to: true)

    true to replace an existing profile

  • spec (Hash)

    paper dimensions and unit

Options Hash (**spec):

  • :width (Numeric)

    paper width

  • :height (Numeric)

    paper height

  • :unit (Symbol, String)

    SVG unit

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when the name, dimensions, unit, overwrite flag, or options are reserved or invalid, or a non-bang definition conflicts with the registered profile



87
88
89
90
91
92
93
94
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 87

def self.define(name, overwrite: true, **spec)
  name = normalize!(:name, name)
  ArgumentError.("Paper name is reserved: #{name}") if reserved?(name)
  overwrite = overwrite!(overwrite)
  profile = new(name:, **spec)

  register(name, profile, overwrite:)
end

.exist?(name) ⇒ Boolean

Reports whether a normalizable named paper profile exists. Invalid converters return false.

Parameters:

  • name (Object)

    profile name

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 69

def self.exist?(name)
  name = normalize(name)
  name ? @mutex.synchronize { @profiles.key?(name) } : false
end

Instance Method Details

#<=>(other) ⇒ Integer?

Compares papers by width, height, unit, then name.

Parameters:

Returns:

  • (Integer, nil)

    comparison result, or nil for a non-Paper operand



39
40
41
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 39

def <=>(other)
  deconstruct <=> other.deconstruct if other.is_a?(self.class)
end

#eql?(other) ⇒ Boolean Also known as: ==

Reports strict paper equality.

Parameters:

  • other (Object)

    object to compare

Returns:

  • (Boolean)


46
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 46

def eql?(other) = self.class == other.class && deconstruct == other.deconstruct

#hashInteger

Returns a hash compatible with strict equality.

Returns:

  • (Integer)


50
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 50

def hash = [self.class, *deconstruct].hash

#longestFloat

Returns the longer side.

Returns:

  • (Float)


54
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 54

def longest = [width, height].max

#shortestFloat

Returns the shorter side.

Returns:

  • (Float)


58
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 58

def shortest = [width, height].min