Class: Postsvg::GraphicsContext

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

Overview

Immutable graphics state snapshot. Mutations return new instances via with(...). The GraphicsStack pushes/pops these.

Fields mirror PLRM ยง7.2 graphics state operators. Optional fields are nil meaning "not set in this state"; the renderer falls back to defaults at emit time.

Constant Summary collapse

DEFAULTS =
{
  ctm: Matrix.new,
  fill_color: Color::BLACK,
  stroke_color: Color::BLACK,
  stroke_width: 1.0,
  line_cap: :butt,
  line_join: :miter,
  miter_limit: 10.0,
  dash: nil,
  font_name: "Helvetica",
  font_size: 12.0,
  clip_paths: [],
  last_text_position: nil,
  fill_rule: :nonzero,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**fields) ⇒ GraphicsContext

Returns a new instance of GraphicsContext.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/postsvg/graphics_context.rb', line 32

def initialize(**fields)
  merged = DEFAULTS.merge(fields)
  @ctm = merged[:ctm]
  @fill_color = merged[:fill_color]
  @stroke_color = merged[:stroke_color]
  @stroke_width = merged[:stroke_width]
  @line_cap = merged[:line_cap]
  @line_join = merged[:line_join]
  @miter_limit = merged[:miter_limit]
  @dash = merged[:dash]
  @font_name = merged[:font_name]
  @font_size = merged[:font_size]
  @clip_paths = merged[:clip_paths].freeze
  @last_text_position = merged[:last_text_position]
  @fill_rule = merged[:fill_rule]
  freeze
end

Instance Attribute Details

#clip_pathsObject (readonly)

Returns the value of attribute clip_paths.



11
12
13
# File 'lib/postsvg/graphics_context.rb', line 11

def clip_paths
  @clip_paths
end

#ctmObject (readonly)

Returns the value of attribute ctm.



11
12
13
# File 'lib/postsvg/graphics_context.rb', line 11

def ctm
  @ctm
end

#dashObject (readonly)

Returns the value of attribute dash.



11
12
13
# File 'lib/postsvg/graphics_context.rb', line 11

def dash
  @dash
end

#fill_colorObject (readonly)

Returns the value of attribute fill_color.



11
12
13
# File 'lib/postsvg/graphics_context.rb', line 11

def fill_color
  @fill_color
end

#fill_ruleObject (readonly)

Returns the value of attribute fill_rule.



11
12
13
# File 'lib/postsvg/graphics_context.rb', line 11

def fill_rule
  @fill_rule
end

#font_nameObject (readonly)

Returns the value of attribute font_name.



11
12
13
# File 'lib/postsvg/graphics_context.rb', line 11

def font_name
  @font_name
end

#font_sizeObject (readonly)

Returns the value of attribute font_size.



11
12
13
# File 'lib/postsvg/graphics_context.rb', line 11

def font_size
  @font_size
end

#last_text_positionObject (readonly)

Returns the value of attribute last_text_position.



11
12
13
# File 'lib/postsvg/graphics_context.rb', line 11

def last_text_position
  @last_text_position
end

#line_capObject (readonly)

Returns the value of attribute line_cap.



11
12
13
# File 'lib/postsvg/graphics_context.rb', line 11

def line_cap
  @line_cap
end

#line_joinObject (readonly)

Returns the value of attribute line_join.



11
12
13
# File 'lib/postsvg/graphics_context.rb', line 11

def line_join
  @line_join
end

#miter_limitObject (readonly)

Returns the value of attribute miter_limit.



11
12
13
# File 'lib/postsvg/graphics_context.rb', line 11

def miter_limit
  @miter_limit
end

#stroke_colorObject (readonly)

Returns the value of attribute stroke_color.



11
12
13
# File 'lib/postsvg/graphics_context.rb', line 11

def stroke_color
  @stroke_color
end

#stroke_widthObject (readonly)

Returns the value of attribute stroke_width.



11
12
13
# File 'lib/postsvg/graphics_context.rb', line 11

def stroke_width
  @stroke_width
end

Instance Method Details

#clipped?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/postsvg/graphics_context.rb', line 76

def clipped?
  !@clip_paths.empty?
end

#identity_ctm?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/postsvg/graphics_context.rb', line 72

def identity_ctm?
  @ctm.identity?
end

#push_clip_path(path_d) ⇒ Object



68
69
70
# File 'lib/postsvg/graphics_context.rb', line 68

def push_clip_path(path_d)
  with(clip_paths: @clip_paths + [path_d])
end

#with(**overrides) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/postsvg/graphics_context.rb', line 50

def with(**overrides)
  GraphicsContext.new(
    ctm: overrides.fetch(:ctm, @ctm),
    fill_color: overrides.fetch(:fill_color, @fill_color),
    stroke_color: overrides.fetch(:stroke_color, @stroke_color),
    stroke_width: overrides.fetch(:stroke_width, @stroke_width),
    line_cap: overrides.fetch(:line_cap, @line_cap),
    line_join: overrides.fetch(:line_join, @line_join),
    miter_limit: overrides.fetch(:miter_limit, @miter_limit),
    dash: overrides.fetch(:dash, @dash),
    font_name: overrides.fetch(:font_name, @font_name),
    font_size: overrides.fetch(:font_size, @font_size),
    clip_paths: overrides.fetch(:clip_paths, @clip_paths),
    last_text_position: overrides.fetch(:last_text_position, @last_text_position),
    fill_rule: overrides.fetch(:fill_rule, @fill_rule),
  )
end