Class: Prawn::SVG::Elements::Gradient

Inherits:
Base
  • Object
show all
Defined in:
lib/prawn/svg/elements/gradient.rb

Constant Summary collapse

TAG_NAME_TO_TYPE =
{
  'linearGradient' => :linear,
  'radialGradient' => :radial
}.freeze

Constants inherited from Base

Base::COMMA_WSP_REGEXP, Base::MissingAttributesError, Base::PAINT_TYPES, Base::SVG_NAMESPACE, Base::SkipElementError, Base::SkipElementQuietly

Constants included from Attributes::Stroke

Attributes::Stroke::CAP_STYLE_TRANSLATIONS, Attributes::Stroke::JOIN_STYLE_TRANSLATIONS

Instance Attribute Summary collapse

Attributes inherited from Base

#attributes, #base_calls, #calls, #document, #parent_calls, #properties, #source, #state

Instance Method Summary collapse

Methods inherited from Base

#initialize, #name, #parse_and_apply, #process

Methods included from TransformParser

#parse_transform_attribute

Methods included from PDFMatrix

#load_matrix, #matrix_for_pdf, #rotation_matrix, #scale_matrix, #translation_matrix

Methods included from Attributes::Space

#parse_xml_space_attribute

Methods included from Attributes::Stroke

#parse_stroke_attributes_and_call

Methods included from Attributes::ClipPath

#parse_clip_path_attribute_and_call

Methods included from Attributes::Opacity

#parse_opacity_attributes_and_call

Methods included from Attributes::Transform

#parse_transform_attribute_and_call

Constructor Details

This class inherits a constructor from Prawn::SVG::Elements::Base

Instance Attribute Details

#cxObject (readonly)

Returns the value of attribute cx.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def cx
  @cx
end

#cyObject (readonly)

Returns the value of attribute cy.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def cy
  @cy
end

#frObject (readonly)

Returns the value of attribute fr.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def fr
  @fr
end

#fxObject (readonly)

Returns the value of attribute fx.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def fx
  @fx
end

#fyObject (readonly)

Returns the value of attribute fy.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def fy
  @fy
end

#parent_gradientObject (readonly)

Returns the value of attribute parent_gradient.



2
3
4
# File 'lib/prawn/svg/elements/gradient.rb', line 2

def parent_gradient
  @parent_gradient
end

#rObject (readonly)

Returns the value of attribute r.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def r
  @r
end

#stopsObject (readonly)

Returns the value of attribute stops.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def stops
  @stops
end

#transform_matrixObject (readonly)

Returns the value of attribute transform_matrix.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def transform_matrix
  @transform_matrix
end

#unitsObject (readonly)

Returns the value of attribute units.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def units
  @units
end

#wrapObject (readonly)

Returns the value of attribute wrap.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def wrap
  @wrap
end

#x1Object (readonly)

Returns the value of attribute x1.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def x1
  @x1
end

#x2Object (readonly)

Returns the value of attribute x2.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def x2
  @x2
end

#y1Object (readonly)

Returns the value of attribute y1.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def y1
  @y1
end

#y2Object (readonly)

Returns the value of attribute y2.



3
4
5
# File 'lib/prawn/svg/elements/gradient.rb', line 3

def y2
  @y2
end

Instance Method Details

#derive_attribute(name) ⇒ Object



57
58
59
# File 'lib/prawn/svg/elements/gradient.rb', line 57

def derive_attribute(name)
  attributes[name] || parent_gradient&.derive_attribute(name)
end

#gradient_arguments(element) ⇒ Object



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
# File 'lib/prawn/svg/elements/gradient.rb', line 28

def gradient_arguments(element)
  bbox = element.bounding_box

  stroke_width = element.stroke_width
  bbox_with_stroke = bbox&.zip([-stroke_width, stroke_width, stroke_width, -stroke_width])&.map(&:sum)

  if type == :radial
    {
      from:         [fx, fy],
      r1:           fr,
      to:           [cx, cy],
      r2:           r,
      stops:        stops,
      matrix:       matrix_for_bounding_box(bbox),
      wrap:         wrap,
      bounding_box: bbox_with_stroke
    }
  else
    {
      from:         [x1, y1],
      to:           [x2, y2],
      stops:        stops,
      matrix:       matrix_for_bounding_box(bbox),
      wrap:         wrap,
      bounding_box: bbox_with_stroke
    }
  end
end

#parseObject

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/prawn/svg/elements/gradient.rb', line 10

def parse
  # A gradient tag without an ID is inaccessible and can never be used
  raise SkipElementQuietly if attributes['id'].nil?

  @parent_gradient = document.gradients[href_attribute[1..]] if href_attribute && href_attribute[0] == '#'
  @transform_matrix = Matrix.identity(3)
  @wrap = :pad

  assert_compatible_prawn_version
  load_gradient_configuration
  load_coordinates
  load_stops

  document.gradients[attributes['id']] = self

  raise SkipElementQuietly # we don't want anything pushed onto the call stack
end