Class: Geometry

Inherits:
Object
  • Object
show all
Defined in:
lib/geometry.rb

Overview

This Geometry class was yanked from RMagick. However, it lets ImageMagick handle the actual change_geometry. Use #new_dimensions_for to get new dimensons Used so I can use spiffy RMagick geometry strings with ImageScience

Constant Summary collapse

FLAGS =

! and @ are removed until support for them is added

['', '%', '<', '>']
RFLAGS =
{ '%' => :percent,
'!' => :aspect,
'<' => :>,
'>' => :<,
'@' => :area }
RE =

Construct an object from a geometry string

/\A(\d*)(?:x(\d+))?([-+]\d+)?([-+]\d+)?([%!<>@]?)\Z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width = nil, height = nil, x = nil, y = nil, flag = nil) ⇒ Geometry

Returns a new instance of Geometry.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/geometry.rb', line 15

def initialize(width=nil, height=nil, x=nil, y=nil, flag=nil)
  # Support floating-point width and height arguments so Geometry
  # objects can be used to specify Image#density= arguments.
  raise ArgumentError, "width must be >= 0: #{width}"   if width < 0
  raise ArgumentError, "height must be >= 0: #{height}" if height < 0
  @width  = width.to_f
  @height = height.to_f
  @x      = x.to_i
  @y      = y.to_i
  @flag   = flag
end

Instance Attribute Details

#flagObject

Returns the value of attribute flag.



13
14
15
# File 'lib/geometry.rb', line 13

def flag
  @flag
end

#heightObject

Returns the value of attribute height.



13
14
15
# File 'lib/geometry.rb', line 13

def height
  @height
end

#widthObject

Returns the value of attribute width.



13
14
15
# File 'lib/geometry.rb', line 13

def width
  @width
end

#xObject

Returns the value of attribute x.



13
14
15
# File 'lib/geometry.rb', line 13

def x
  @x
end

#yObject

Returns the value of attribute y.



13
14
15
# File 'lib/geometry.rb', line 13

def y
  @y
end

Class Method Details

.from_s(str) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
# File 'lib/geometry.rb', line 30

def self.from_s(str)
  raise(ArgumentError, "no geometry string specified") unless str

  if m = RE.match(str)
    new(m[1].to_i, m[2].to_i, m[3].to_i, m[4].to_i, RFLAGS[m[5]])
  else
    raise ArgumentError, "invalid geometry format"
  end
end

Instance Method Details

#new_dimensions_for(orig_width, orig_height) ⇒ Object

attempts to get new dimensions for the current geometry string given these old dimensions. This doesn’t implement the aspect flag (!) or the area flag (@). PDI



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
# File 'lib/geometry.rb', line 52

def new_dimensions_for(orig_width, orig_height)
  new_width  = orig_width
  new_height = orig_height

  case @flag
    when :percent
      scale_x = @width.zero?  ? 100 : @width
      scale_y = @height.zero? ? @width : @height
      new_width    = scale_x.to_f * (orig_width.to_f  / 100.0)
      new_height   = scale_y.to_f * (orig_height.to_f / 100.0)
    when :<, :>, nil
      scale_factor =
        if new_width.zero? || new_height.zero?
          1.0
        else
          if @width.nonzero? && @height.nonzero?
            [@width.to_f / new_width.to_f, @height.to_f / new_height.to_f].min
          else
            @width.nonzero? ? (@width.to_f / new_width.to_f) : (@height.to_f / new_height.to_f)
          end
        end
      new_width  = scale_factor * new_width.to_f
      new_height = scale_factor * new_height.to_f
      new_width  = orig_width  if @flag && orig_width.send(@flag,  new_width)
      new_height = orig_height if @flag && orig_height.send(@flag, new_height)
  end

  [new_width, new_height].collect! { |v| v.round }
end

#to_sObject

Convert object to a geometry string



41
42
43
44
45
46
47
48
# File 'lib/geometry.rb', line 41

def to_s
  str = ''
  str << "%g" % @width if @width > 0
  str << 'x' if (@width > 0 || @height > 0)
  str << "%g" % @height if @height > 0
  str << "%+d%+d" % [@x, @y] if (@x != 0 || @y != 0)
  str << FLAGS[@flag.to_i]
end