Class: Prawn::SVG::GradientRenderer

Inherits:
Object
  • Object
show all
Includes:
PDFMatrix
Defined in:
lib/prawn/svg/gradient_renderer.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PDFMatrix

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

Constructor Details

#initialize(prawn, draw_type, from:, to:, stops:, matrix: nil, r1: nil, r2: nil, wrap: :pad, bounding_box: nil) ⇒ GradientRenderer

Returns a new instance of GradientRenderer.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/prawn/svg/gradient_renderer.rb', line 7

def initialize(prawn, draw_type, from:, to:, stops:, matrix: nil, r1: nil, r2: nil, wrap: :pad, bounding_box: nil)
  @prawn = prawn
  @draw_type = draw_type
  @from = from
  @to = to
  @bounding_box = bounding_box

  if r1
    @shading_type = 3
    @coordinates = [*from, r1, *to, r2]
  else
    @shading_type = 2
    @coordinates = [*from, *to]
  end

  @stop_offsets, @color_stops, @opacity_stops = process_stop_arguments(stops)
  @gradient_matrix = matrix ? load_matrix(matrix) : Matrix.identity(3)
  @wrap = wrap
end

Class Method Details

.next_keyObject



46
47
48
# File 'lib/prawn/svg/gradient_renderer.rb', line 46

def self.next_key
  @mutex.synchronize { @counter += 1 }
end

Instance Method Details

#drawObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/prawn/svg/gradient_renderer.rb', line 27

def draw
  key = self.class.next_key

  # If we need transparency, add an ExtGState to the page and enable it.
  if opacity_stops
    prawn.page.ext_gstates["PSVG-ExtGState-#{key}"] = create_transparency_graphics_state
    prawn.renderer.add_content("/PSVG-ExtGState-#{key} gs")
  end

  # Add pattern to the PDF page resources dictionary.
  prawn.page.resources[:Pattern] ||= {}
  prawn.page.resources[:Pattern]["PSVG-Pattern-#{key}"] = create_gradient_pattern

  # Finally set the pattern with the drawing operator for fill/stroke.
  prawn.send(:set_color_space, draw_type, :Pattern)
  draw_operator = draw_type == :fill ? 'scn' : 'SCN'
  prawn.renderer.add_content("/PSVG-Pattern-#{key} #{draw_operator}")
end