Class: Postsvg::Translation::BoundingBox

Inherits:
Object
  • Object
show all
Defined in:
lib/postsvg/translation/bounding_box.rb

Overview

Accumulates the SVG content's geometric bounds as the SVG tree is walked. Used to write the %%BoundingBox DSC comment in the serialized output.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min_x:, min_y:, max_x:, max_y:) ⇒ BoundingBox

Returns a new instance of BoundingBox.



11
12
13
14
15
16
17
# File 'lib/postsvg/translation/bounding_box.rb', line 11

def initialize(min_x:, min_y:, max_x:, max_y:)
  @min_x = min_x
  @min_y = min_y
  @max_x = max_x
  @max_y = max_y
  freeze
end

Instance Attribute Details

#max_xObject (readonly)

Returns the value of attribute max_x.



9
10
11
# File 'lib/postsvg/translation/bounding_box.rb', line 9

def max_x
  @max_x
end

#max_yObject (readonly)

Returns the value of attribute max_y.



9
10
11
# File 'lib/postsvg/translation/bounding_box.rb', line 9

def max_y
  @max_y
end

#min_xObject (readonly)

Returns the value of attribute min_x.



9
10
11
# File 'lib/postsvg/translation/bounding_box.rb', line 9

def min_x
  @min_x
end

#min_yObject (readonly)

Returns the value of attribute min_y.



9
10
11
# File 'lib/postsvg/translation/bounding_box.rb', line 9

def min_y
  @min_y
end

Class Method Details

.emptyObject



19
20
21
# File 'lib/postsvg/translation/bounding_box.rb', line 19

def self.empty
  new(min_x: nil, min_y: nil, max_x: nil, max_y: nil)
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/postsvg/translation/bounding_box.rb', line 23

def empty?
  @min_x.nil?
end

#expand(x_range, y_range) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/postsvg/translation/bounding_box.rb', line 27

def expand(x_range, y_range)
  return self if x_range.nil? || y_range.nil?

  new_min_x = [(empty? ? x_range.begin : @min_x), x_range.begin].compact.min
  new_max_x = [(empty? ? x_range.end : @max_x), x_range.end].compact.max
  new_min_y = [(empty? ? y_range.begin : @min_y), y_range.begin].compact.min
  new_max_y = [(empty? ? y_range.end : @max_y), y_range.end].compact.max
  BoundingBox.new(min_x: new_min_x, min_y: new_min_y, max_x: new_max_x, max_y: new_max_y)
end

#heightObject



45
46
47
# File 'lib/postsvg/translation/bounding_box.rb', line 45

def height
  empty? ? 0 : @max_y - @min_y
end

#to_aObject



37
38
39
# File 'lib/postsvg/translation/bounding_box.rb', line 37

def to_a
  empty? ? [] : [@min_x, @min_y, @max_x, @max_y]
end

#to_dsc_commentObject



49
50
51
52
53
54
55
56
# File 'lib/postsvg/translation/bounding_box.rb', line 49

def to_dsc_comment
  return nil if empty?

  "%s %s %s %s" % [
    FormatNumber.call(@min_x), FormatNumber.call(@min_y),
    FormatNumber.call(@max_x), FormatNumber.call(@max_y),
  ]
end

#widthObject



41
42
43
# File 'lib/postsvg/translation/bounding_box.rb', line 41

def width
  empty? ? 0 : @max_x - @min_x
end