Class: Pdfrb::Content::TilingPattern

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

Overview

Tiling patterns (ISO 32000-2 ยง8.7.3). A tiling pattern is a repeating cell that fills an area. Type 1 (constant spacing) and Type 2 (no distortion) are supported.

The pattern cell is drawn via a block using the Canvas API. Registered in page Resources under /Pattern.

Constant Summary collapse

PATTERN_TYPE =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paint_type: 1, tiling_type: 1, bbox: [0, 0, 16, 16], x_step: 16, y_step: 16) ⇒ TilingPattern

Returns a new instance of TilingPattern.

Parameters:

  • paint_type (Integer) (defaults to: 1)

    1 = colored, 2 = uncolored.

  • tiling_type (Integer) (defaults to: 1)

    1 = constant, 2 = no distortion, 3 = constant spacing + no distortion.

  • bbox (Array) (defaults to: [0, 0, 16, 16])

    bounding box of the pattern cell.

  • x_step (Numeric) (defaults to: 16)

    horizontal repeat distance.

  • y_step (Numeric) (defaults to: 16)

    vertical repeat distance.



21
22
23
24
25
26
27
28
# File 'lib/pdfrb/content/tiling_pattern.rb', line 21

def initialize(paint_type: 1, tiling_type: 1, bbox: [0, 0, 16, 16],
               x_step: 16, y_step: 16)
  @paint_type = paint_type
  @tiling_type = tiling_type
  @bbox = bbox
  @x_step = x_step
  @y_step = y_step
end

Instance Attribute Details

#bboxObject (readonly)

Returns the value of attribute bbox.



14
15
16
# File 'lib/pdfrb/content/tiling_pattern.rb', line 14

def bbox
  @bbox
end

#paint_typeObject (readonly)

Returns the value of attribute paint_type.



14
15
16
# File 'lib/pdfrb/content/tiling_pattern.rb', line 14

def paint_type
  @paint_type
end

#tiling_typeObject (readonly)

Returns the value of attribute tiling_type.



14
15
16
# File 'lib/pdfrb/content/tiling_pattern.rb', line 14

def tiling_type
  @tiling_type
end

#x_stepObject (readonly)

Returns the value of attribute x_step.



14
15
16
# File 'lib/pdfrb/content/tiling_pattern.rb', line 14

def x_step
  @x_step
end

#y_stepObject (readonly)

Returns the value of attribute y_step.



14
15
16
# File 'lib/pdfrb/content/tiling_pattern.rb', line 14

def y_step
  @y_step
end

Instance Method Details

#register_on(document, page) {|canvas| ... } ⇒ Symbol

Build the pattern stream and register it in the page's Resources.

Parameters:

Yields:

  • (canvas)

    block that draws the pattern cell.

Returns:

  • (Symbol)

    the resource name (e.g. :P1)



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pdfrb/content/tiling_pattern.rb', line 35

def register_on(document, page)
  pattern_stream = document.add(
    {
      Type: :Pattern,
      PatternType: PATTERN_TYPE,
      PaintType: @paint_type,
      TilingType: @tiling_type,
      BBox: @bbox,
      XStep: @x_step,
      YStep: @y_step,
    },
    type: Pdfrb::Model::Cos::Stream
  )

  if block_given?
    canvas = Pdfrb::Content::Canvas.new(pattern_stream, document: document)
    yield canvas
  end

  ref = Pdfrb::Model::Reference.new(pattern_stream.oid, pattern_stream.gen)
  register_in_resources(document, page, ref)
end