Class: Postsvg::GraphicsStack

Inherits:
Object
  • Object
show all
Defined in:
lib/postsvg/graphics_stack.rb

Overview

LIFO stack of immutable GraphicsContext snapshots. gsave pushes a duplicate of the current top; grestore pops. Popping an empty stack is a no-op (mirrors PLRM ยง7.2.2).

Instance Method Summary collapse

Constructor Details

#initialize(initial: GraphicsContext.new) ⇒ GraphicsStack

Returns a new instance of GraphicsStack.



8
9
10
# File 'lib/postsvg/graphics_stack.rb', line 8

def initialize(initial: GraphicsContext.new)
  @stack = [initial]
end

Instance Method Details

#currentObject



12
13
14
# File 'lib/postsvg/graphics_stack.rb', line 12

def current
  @stack.last
end

#depthObject



30
31
32
# File 'lib/postsvg/graphics_stack.rb', line 30

def depth
  @stack.size
end

#popObject Also known as: grestore



22
23
24
25
26
27
# File 'lib/postsvg/graphics_stack.rb', line 22

def pop
  return current if @stack.size == 1

  @stack.pop
  current
end

#pushObject Also known as: gsave



16
17
18
19
# File 'lib/postsvg/graphics_stack.rb', line 16

def push
  @stack.push(current)
  self
end

#replace(new_state) ⇒ Object



34
35
36
37
# File 'lib/postsvg/graphics_stack.rb', line 34

def replace(new_state)
  @stack[-1] = new_state
  new_state
end

#update(**overrides) ⇒ Object



39
40
41
# File 'lib/postsvg/graphics_stack.rb', line 39

def update(**overrides)
  replace(current.with(**overrides))
end