Class: Pdfrb::Content::Shading::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/content/shading/base.rb

Overview

Base shading builder. Subclasses set shading_type and shading_fields. Creates a shading dict and registers it in the page's /Resources /Shading sub-dict.

Direct Known Subclasses

Axial, Radial

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color_space: :DeviceGray) ⇒ Base

Returns a new instance of Base.



18
19
20
# File 'lib/pdfrb/content/shading/base.rb', line 18

def initialize(color_space: :DeviceGray)
  @color_space = color_space
end

Instance Attribute Details

#color_spaceObject (readonly)

Returns the value of attribute color_space.



10
11
12
# File 'lib/pdfrb/content/shading/base.rb', line 10

def color_space
  @color_space
end

Class Method Details

.shading_typeObject

Raises:

  • (NotImplementedError)


13
14
15
# File 'lib/pdfrb/content/shading/base.rb', line 13

def shading_type
  raise NotImplementedError
end

Instance Method Details

#register_on(document, page) ⇒ Symbol

Register this shading on a page and return the resource name.

Parameters:

Returns:

  • (Symbol)

    resource name (e.g. :Sh1)



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pdfrb/content/shading/base.rb', line 32

def register_on(document, page)
  shading_obj = document.add(to_dict, type: Pdfrb::Model::Cos::Dictionary)
  ref = Pdfrb::Model::Reference.new(shading_obj.oid, shading_obj.gen)
  resources = ensure_resources(document, page)
  shading_dict = resources.value[:Shading]
  if shading_dict.nil?
    shading_dict = {}
    resources.value[:Shading] = shading_dict
  end
  name = next_shading_name(shading_dict)
  shading_dict[name] = ref
  name
end

#to_dictObject

Build the shading dictionary value (a Hash).



23
24
25
26
# File 'lib/pdfrb/content/shading/base.rb', line 23

def to_dict
  { ShadingType: self.class.shading_type,
    ColorSpace: @color_space }.merge!(shading_fields)
end