Class: Sumi::Graphics::Paper
- Inherits:
-
Data
- Object
- Data
- Sumi::Graphics::Paper
- Includes:
- Comparable
- Defined in:
- graphics/lib/sumi/graphics/auxiliary/paper.rb,
graphics/lib/sumi/graphics/auxiliary/paper.rb
Overview
Paper size and unit profile.
Instance Attribute Summary collapse
-
#height ⇒ Float
readonly
Paper height.
-
#name ⇒ Symbol
readonly
Profile name.
-
#unit ⇒ Symbol
readonly
SVG unit.
-
#width ⇒ Float
readonly
Paper width.
Class Method Summary collapse
-
.[](width, height, unit = "mm", name = :custom) ⇒ Sumi::Graphics::Paper
Creates a paper profile from dimensions and optional metadata.
-
.default ⇒ Sumi::Graphics::Paper
Returns the default paper profile.
-
.define(name, overwrite: false, **spec) ⇒ Sumi::Graphics::Paper
Defines a named paper profile after complete validation.
-
.exist?(name) ⇒ Boolean
Reports whether a normalizable named paper profile exists.
-
.fetch(name) ⇒ Sumi::Graphics::Paper
Returns a registered paper profile by name.
-
.keys ⇒ Array<Symbol>
Returns registered profile names.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Compares papers by width, height, unit, then name.
-
#aspect_ratio ⇒ Float
Returns the width-to-height ratio.
-
#eql?(other) ⇒ Boolean
(also: #==)
Reports strict paper equality.
-
#hash ⇒ Integer
Returns a hash compatible with strict equality.
-
#initialize(width:, height:, unit: "mm", name: :custom, **options) ⇒ void
constructor
Creates a paper profile.
-
#landscape ⇒ Sumi::Graphics::Paper
Returns a landscape orientation of this paper.
-
#landscape? ⇒ Boolean
Reports whether width exceeds height.
-
#longest ⇒ Float
Returns the longer side.
-
#portrait ⇒ Sumi::Graphics::Paper
Returns a portrait orientation of this paper.
-
#portrait? ⇒ Boolean
Reports whether height exceeds width.
-
#rotate ⇒ Sumi::Graphics::Paper
Returns a paper with width and height swapped.
-
#shortest ⇒ Float
Returns the shorter side.
-
#square? ⇒ Boolean
Reports whether width equals height.
Constructor Details
#initialize(width:, height:, unit: "mm", name: :custom, **options) ⇒ void
Creates a paper profile. Dimensions must be finite real numbers greater than zero.
39 40 41 42 43 44 45 46 47 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 39 def initialize(width:, height:, unit: "mm", name: :custom, **) self.class.send(: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
#height ⇒ Float (readonly)
Returns paper height.
19 20 21 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 19 def height @height end |
#name ⇒ Symbol (readonly)
Returns profile name.
19 20 21 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 19 def name @name end |
#unit ⇒ Symbol (readonly)
Returns SVG unit.
19 20 21 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 19 def unit @unit end |
#width ⇒ Float (readonly)
Returns paper width.
19 20 21 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 19 def width @width end |
Class Method Details
.[](width, height, unit = "mm", name = :custom) ⇒ Sumi::Graphics::Paper
Creates a paper profile from dimensions and optional metadata.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 19 Paper = Data.define(:width, :height, :unit, :name) do include Comparable # @!attribute [r] width # @return [Float] paper width # @!attribute [r] height # @return [Float] paper height # @!attribute [r] unit # @return [Symbol] SVG unit # @!attribute [r] name # @return [Symbol] profile name # Creates a paper profile. Dimensions must be finite real numbers greater than zero. # @param width [Numeric] paper width # @param height [Numeric] paper height # @param unit [Symbol, String] SVG unit # @param name [Symbol, String] profile name # @param options [Hash] unsupported extra options # @return [void] # @raise [Sumi::ArgumentError] when dimensions, unit, name, or options are invalid def initialize(width:, height:, unit: "mm", name: :custom, **) self.class.send(: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 # Compares papers by width, height, unit, then name. # @param other [Sumi::Graphics::Paper] paper to compare # @return [Integer, nil] comparison result, or nil for a non-Paper operand def <=>(other) deconstruct <=> other.deconstruct if other.is_a?(self.class) end # Returns the width-to-height ratio. # @return [Float] def aspect_ratio = width.fdiv(height) # Reports strict paper equality. # @param other [Object] object to compare # @return [Boolean] def eql?(other) = self.class == other.class && deconstruct == other.deconstruct # Returns a hash compatible with strict equality. # @return [Integer] def hash = [self.class, *deconstruct].hash # Returns a landscape orientation of this paper. # Returns self when already landscape, or swaps width and height. # @return [Sumi::Graphics::Paper] def landscape = landscape? ? self : rotate # Reports whether width exceeds height. # @return [Boolean] def landscape? = width > height # Returns the longer side. # @return [Float] def longest = [width, height].max # Returns a portrait orientation of this paper. # Returns self when already portrait, or swaps width and height. # @return [Sumi::Graphics::Paper] def portrait = portrait? ? self : rotate # Reports whether height exceeds width. # @return [Boolean] def portrait? = height > width # Returns a paper with width and height swapped. # @return [Sumi::Graphics::Paper] def rotate = with(width: height, height: width) # Returns the shorter side. # @return [Float] def shortest = [width, height].min # Reports whether width equals height. # @return [Boolean] def square? = width == height alias_method :==, :eql? @profiles = {} @accessors = {} @mutex = ::Mutex.new # Reports whether a normalizable named paper profile exists. Invalid converters return false. # @param name [Object] profile name # @return [Boolean] def self.exist?(name) name = normalize(name) name ? @mutex.synchronize { @profiles.key?(name) } : false end # Returns a registered paper profile by name. # @param name [Symbol, String] profile name, including names that are not Ruby identifiers # @return [Sumi::Graphics::Paper] registered profile # @raise [Sumi::ArgumentError] when name is invalid or no profile is registered # @example Look up a non-identifier profile name # Sumi::Graphics::Paper.define("business-card", width: 90, height: 50) # Sumi::Graphics::Paper.fetch("business-card") def self.fetch(name) name = normalize!(:name, name) @mutex.synchronize { @profiles.fetch(name) { ArgumentError.("Unknown paper profile: #{name}") } } end # Returns registered profile names. # @return [Array<Symbol>] frozen name snapshot def self.keys = @mutex.synchronize { @profiles.keys.freeze } # Defines a named paper profile after complete validation. Registration is process-global and thread-atomic. # Identical definitions return the canonical profile and conflicting definitions raise unless replacement is # explicitly requested. Names that are not Ruby call syntax remain accessible through {.fetch}. # @param name [Symbol, String] profile name # @param overwrite [Boolean] true to replace an existing profile # @param spec [Hash] paper dimensions and unit # @option spec [Numeric] :width paper width # @option spec [Numeric] :height paper height # @option spec [Symbol, String] :unit SVG unit # @return [Sumi::Graphics::Paper] # @raise [Sumi::ArgumentError] when the name, dimensions, unit, overwrite flag, or options are reserved or invalid, # or a non-bang definition conflicts with the registered profile # @example Define or reuse a matching profile # Sumi::Graphics::Paper.define(:card, width: 90, height: 50) # @example Replace a profile explicitly # Sumi::Graphics::Paper.define(:card, width: 100, height: 60, overwrite: true) def self.define(name, overwrite: false, **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 class << self private def install(name) return if @accessors.key?(name) define_singleton_method(name) { @mutex.synchronize { @profiles.fetch(name) } } @accessors[name] = true end def register(name, profile, overwrite:) @mutex.synchronize do if !overwrite && (current = @profiles[name]) ArgumentError.("Paper already defined differently: #{name}") unless current == profile next current end install(name) @profiles[name] = profile end end def reserved?(name) = @reserved.include?(name) def dimension!(field, value) Scalar.finite(value, context: "paper", field:, positive: true) end def () return if .empty? ArgumentError.("Unknown paper options: #{.keys.join(", ")}") end def overwrite!(value) return value if [true, false].include?(value) ArgumentError.("Paper overwrite must be true or false") end def normalize(value) normalized = value.to_sym if value.respond_to?(:to_sym) normalized if normalized.is_a?(::Symbol) rescue ::StandardError nil end def normalize!(field, value) normalize(value) || ArgumentError.("Invalid paper #{field}") end end @reserved = (methods | private_methods).to_set.freeze { a0: [841, 1189, "mm"], a1: [594, 841, "mm"], a2: [420, 594, "mm"], a3: [297, 420, "mm"], a4: [210, 297, "mm"], a5: [148, 210, "mm"], a6: [105, 148, "mm"], a7: [74, 105, "mm"], a8: [52, 74, "mm"], a9: [37, 52, "mm"], a10: [26, 37, "mm"], b0: [1000, 1414, "mm"], b1: [707, 1000, "mm"], b2: [500, 707, "mm"], b3: [353, 500, "mm"], b4: [250, 353, "mm"], b5: [176, 250, "mm"], b6: [125, 176, "mm"], b7: [88, 125, "mm"], b8: [62, 88, "mm"], b9: [44, 62, "mm"], b10: [31, 44, "mm"], c0: [917, 1297, "mm"], c1: [648, 917, "mm"], c2: [458, 648, "mm"], c3: [324, 458, "mm"], c4: [229, 324, "mm"], c5: [162, 229, "mm"], c6: [114, 162, "mm"], c7: [81, 114, "mm"], c8: [57, 81, "mm"], c9: [40, 57, "mm"], c10: [28, 40, "mm"], business: [85, 55, "mm"], large: [130, 210, "mm"], passport: [88, 125, "mm"], pocket: [90, 140, "mm"], travelers: [110, 210, "mm"], us: [216, 279, "mm"], xlarge: [190, 250, "mm"], icon16: [16, 16, "px"], icon32: [32, 32, "px"], icon64: [64, 64, "px"], icon128: [128, 128, "px"], icon256: [256, 256, "px"], icon512: [512, 512, "px"] }.each { |name, (width, height, unit)| define(name, width:, height:, unit:) } # Returns the default paper profile. # @return [Sumi::Graphics::Paper] def self.default @mutex.synchronize { @profiles.fetch(:default) } end @accessors[:default] = true @profiles[:default] = @profiles.fetch(:a4) end |
.default ⇒ Sumi::Graphics::Paper
Returns the default paper profile.
268 269 270 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 268 def self.default @mutex.synchronize { @profiles.fetch(:default) } end |
.define(name, overwrite: false, **spec) ⇒ Sumi::Graphics::Paper
Defines a named paper profile after complete validation. Registration is process-global and thread-atomic. Identical definitions return the canonical profile and conflicting definitions raise unless replacement is explicitly requested. Names that are not Ruby call syntax remain accessible through fetch.
149 150 151 152 153 154 155 156 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 149 def self.define(name, overwrite: false, **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.
112 113 114 115 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 112 def self.exist?(name) name = normalize(name) name ? @mutex.synchronize { @profiles.key?(name) } : false end |
.fetch(name) ⇒ Sumi::Graphics::Paper
Returns a registered paper profile by name.
124 125 126 127 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 124 def self.fetch(name) name = normalize!(:name, name) @mutex.synchronize { @profiles.fetch(name) { ArgumentError.("Unknown paper profile: #{name}") } } end |
.keys ⇒ Array<Symbol>
Returns registered profile names.
131 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 131 def self.keys = @mutex.synchronize { @profiles.keys.freeze } |
Instance Method Details
#<=>(other) ⇒ Integer?
Compares papers by width, height, unit, then name.
52 53 54 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 52 def <=>(other) deconstruct <=> other.deconstruct if other.is_a?(self.class) end |
#aspect_ratio ⇒ Float
Returns the width-to-height ratio.
58 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 58 def aspect_ratio = width.fdiv(height) |
#eql?(other) ⇒ Boolean Also known as: ==
Reports strict paper equality.
63 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 63 def eql?(other) = self.class == other.class && deconstruct == other.deconstruct |
#hash ⇒ Integer
Returns a hash compatible with strict equality.
67 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 67 def hash = [self.class, *deconstruct].hash |
#landscape ⇒ Sumi::Graphics::Paper
Returns a landscape orientation of this paper. Returns self when already landscape, or swaps width and height.
72 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 72 def landscape = landscape? ? self : rotate |
#landscape? ⇒ Boolean
Reports whether width exceeds height.
76 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 76 def landscape? = width > height |
#longest ⇒ Float
Returns the longer side.
80 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 80 def longest = [width, height].max |
#portrait ⇒ Sumi::Graphics::Paper
Returns a portrait orientation of this paper. Returns self when already portrait, or swaps width and height.
85 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 85 def portrait = portrait? ? self : rotate |
#portrait? ⇒ Boolean
Reports whether height exceeds width.
89 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 89 def portrait? = height > width |
#rotate ⇒ Sumi::Graphics::Paper
Returns a paper with width and height swapped.
93 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 93 def rotate = with(width: height, height: width) |
#shortest ⇒ Float
Returns the shorter side.
97 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 97 def shortest = [width, height].min |
#square? ⇒ Boolean
Reports whether width equals height.
101 |
# File 'graphics/lib/sumi/graphics/auxiliary/paper.rb', line 101 def square? = width == height |