Class: Pdfrb::Document::Shadings

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/document/shadings.rb

Overview

Gradient shading patterns facade. Creates Type 2 (axial) and Type 3 (radial) shading dictionaries and registers them in page /Resources /Shading.

For 2-stop gradients, uses a Type 2 ExponentialInterpolation function (/FunctionType 2). For multi-stop, chains Type 3 StitchingFunction.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Shadings

Returns a new instance of Shadings.



15
16
17
18
19
# File 'lib/pdfrb/document/shadings.rb', line 15

def initialize(document)
  @document = document
  @registry = {}
  @next_id = 1
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



13
14
15
# File 'lib/pdfrb/document/shadings.rb', line 13

def document
  @document
end

#registryObject (readonly)

Returns the value of attribute registry.



13
14
15
# File 'lib/pdfrb/document/shadings.rb', line 13

def registry
  @registry
end

Instance Method Details

#[](name) ⇒ Object



74
75
76
# File 'lib/pdfrb/document/shadings.rb', line 74

def [](name)
  @registry[name]
end

#add_axial(from:, to:, stops:, extend: [true, true]) ⇒ Symbol

Add an axial (linear) gradient.

Parameters:

  • from (Array(Numeric, Numeric))

    start point [x, y].

  • to (Array(Numeric, Numeric))

    end point [x, y].

  • stops (Array<Array>)

    gradient stops: [[offset, [color_family, *values]], ...]

  • extend (Array<Boolean, Boolean>) (defaults to: [true, true])

    extend before/after.

Returns:

  • (Symbol)

    shading resource name (e.g., :Sh1).



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pdfrb/document/shadings.rb', line 28

def add_axial(from:, to:, stops:, extend: [true, true])
  color_space = stops_to_color_values(stops.first || [0, [:gray, 0]])[0]
  name = next_name

  function = build_gradient_function(stops, color_space)

  shading = document.add(
    {
      ShadingType: 2,
      ColorSpace: color_space,
      Coords: Pdfrb::Model::PdfArray.new([*from, *to]),
      Function: function,
      Extend: Pdfrb::Model::PdfArray.new(extend),
    },
    type: Pdfrb::Model::Cos::Dictionary
  )
  register(name, shading)
  name
end

#add_radial(from:, to:, stops:, extend: [true, true]) ⇒ Symbol

Add a radial gradient.

Parameters:

  • from (Array(Numeric, Numeric, Numeric))

    [cx, cy, radius].

  • to (Array(Numeric, Numeric, Numeric))

    [cx, cy, radius].

  • stops (Array<Array>)

    same format as axial.

  • extend (Array<Boolean, Boolean>) (defaults to: [true, true])

    extend before/after.

Returns:

  • (Symbol)

    shading resource name.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pdfrb/document/shadings.rb', line 54

def add_radial(from:, to:, stops:, extend: [true, true])
  color_space = stops_to_color_values(stops.first || [0, [:gray, 0]])[0]
  name = next_name

  function = build_gradient_function(stops, color_space)

  shading = document.add(
    {
      ShadingType: 3,
      ColorSpace: color_space,
      Coords: Pdfrb::Model::PdfArray.new([*from, *to]),
      Function: function,
      Extend: Pdfrb::Model::PdfArray.new(extend),
    },
    type: Pdfrb::Model::Cos::Dictionary
  )
  register(name, shading)
  name
end

#each(&block) ⇒ Object



78
79
80
81
82
83
# File 'lib/pdfrb/document/shadings.rb', line 78

def each(&block)
  return enum_for(:each) unless block

  @registry.each(&block)
  self
end