Class: Prawn::Svg::Calculators::DocumentSizing

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/svg/calculators/document_sizing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bounds, attributes = nil) ⇒ DocumentSizing

Returns a new instance of DocumentSizing


10
11
12
13
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 10

def initialize(bounds, attributes = nil)
  @bounds = bounds
  set_from_attributes(attributes) if attributes
end

Instance Attribute Details

#document_height=(value) ⇒ Object (writeonly)

Sets the attribute document_height

Parameters:

  • value

    the value to set the attribute document_height to.


3
4
5
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 3

def document_height=(value)
  @document_height = value
end

#document_width=(value) ⇒ Object (writeonly)

Sets the attribute document_width

Parameters:

  • value

    the value to set the attribute document_width to.


3
4
5
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 3

def document_width=(value)
  @document_width = value
end

#output_heightObject (readonly)

Returns the value of attribute output_height


8
9
10
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 8

def output_height
  @output_height
end

#output_widthObject (readonly)

Returns the value of attribute output_width


8
9
10
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 8

def output_width
  @output_width
end

#preserve_aspect_ratio=(value) ⇒ Object (writeonly)

Sets the attribute preserve_aspect_ratio

Parameters:

  • value

    the value to set the attribute preserve_aspect_ratio to.


4
5
6
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 4

def preserve_aspect_ratio=(value)
  @preserve_aspect_ratio = value
end

#requested_height=(value) ⇒ Object (writeonly)

Sets the attribute requested_height

Parameters:

  • value

    the value to set the attribute requested_height to.


5
6
7
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 5

def requested_height=(value)
  @requested_height = value
end

#requested_width=(value) ⇒ Object (writeonly)

Sets the attribute requested_width

Parameters:

  • value

    the value to set the attribute requested_width to.


5
6
7
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 5

def requested_width=(value)
  @requested_width = value
end

#view_box=(value) ⇒ Object (writeonly)

Sets the attribute view_box

Parameters:

  • value

    the value to set the attribute view_box to.


4
5
6
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 4

def view_box=(value)
  @view_box = value
end

#viewport_heightObject (readonly)

Returns the value of attribute viewport_height


8
9
10
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 8

def viewport_height
  @viewport_height
end

#viewport_widthObject (readonly)

Returns the value of attribute viewport_width


8
9
10
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 8

def viewport_width
  @viewport_width
end

#x_offsetObject (readonly)

Returns the value of attribute x_offset


7
8
9
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 7

def x_offset
  @x_offset
end

#x_scaleObject (readonly)

Returns the value of attribute x_scale


7
8
9
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 7

def x_scale
  @x_scale
end

#y_offsetObject (readonly)

Returns the value of attribute y_offset


7
8
9
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 7

def y_offset
  @y_offset
end

#y_scaleObject (readonly)

Returns the value of attribute y_scale


7
8
9
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 7

def y_scale
  @y_scale
end

Instance Method Details

#calculateObject


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
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 22

def calculate
  @x_offset = @y_offset = 0
  @x_scale = @y_scale = 1

  width = Prawn::Svg::Calculators::Pixels.to_pixels(@document_width, @bounds[0])
  height = Prawn::Svg::Calculators::Pixels.to_pixels(@document_height, @bounds[1])

  default_aspect_ratio = "x#{width ? "Mid" : "Min"}Y#{height ? "Mid" : "Min"} meet"

  width ||= @bounds[0]
  height ||= @bounds[1]

  if @view_box
    values = @view_box.strip.split(/\s+/)
    @x_offset, @y_offset, @viewport_width, @viewport_height = values.map {|value| value.to_f}
    @x_offset = -@x_offset

    @preserve_aspect_ratio ||= default_aspect_ratio
    aspect = Prawn::Svg::Calculators::AspectRatio.new(@preserve_aspect_ratio, [width, height], [@viewport_width, @viewport_height])
    @x_scale = aspect.width / @viewport_width
    @y_scale = aspect.height / @viewport_height
    @x_offset -= aspect.x
    @y_offset -= aspect.y
  end

  @viewport_width ||= width
  @viewport_height ||= height

  if @requested_width
    scale = @requested_width / width
    width = @requested_width
    height *= scale
    @x_scale *= scale
    @y_scale *= scale

  elsif @requested_height
    scale = @requested_height / height
    height = @requested_height
    width *= scale
    @x_scale *= scale
    @y_scale *= scale
  end

  @output_width = width
  @output_height = height

  self
end

#set_from_attributes(attributes) ⇒ Object


15
16
17
18
19
20
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 15

def set_from_attributes(attributes)
  @document_width = attributes['width']
  @document_height = attributes['height']
  @view_box = attributes['viewBox']
  @preserve_aspect_ratio = attributes['preserveAspectRatio']
end