Class: Postsvg::GraphicsState

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

Overview

Manages PostScript graphics state including path, color, transforms, etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraphicsState

Returns a new instance of GraphicsState.



9
10
11
12
13
14
15
16
17
18
# File 'lib/postsvg/graphics_state.rb', line 9

def initialize
  @current_path = []
  @current_x = 0
  @current_y = 0
  @line_width = 1.0
  @fill_color = { r: 0, g: 0, b: 0 }
  @stroke_color = { r: 0, g: 0, b: 0 }
  @state_stack = []
  @transform_matrix = [1, 0, 0, 1, 0, 0] # Identity matrix [a, b, c, d, e, f]
end

Instance Attribute Details

#current_pathObject (readonly)

Returns the value of attribute current_path.



7
8
9
# File 'lib/postsvg/graphics_state.rb', line 7

def current_path
  @current_path
end

#current_xObject

Returns the value of attribute current_x.



6
7
8
# File 'lib/postsvg/graphics_state.rb', line 6

def current_x
  @current_x
end

#current_yObject

Returns the value of attribute current_y.



6
7
8
# File 'lib/postsvg/graphics_state.rb', line 6

def current_y
  @current_y
end

#fill_colorObject (readonly)

Returns the value of attribute fill_color.



7
8
9
# File 'lib/postsvg/graphics_state.rb', line 7

def fill_color
  @fill_color
end

#line_widthObject

Returns the value of attribute line_width.



6
7
8
# File 'lib/postsvg/graphics_state.rb', line 6

def line_width
  @line_width
end

#stroke_colorObject (readonly)

Returns the value of attribute stroke_color.



7
8
9
# File 'lib/postsvg/graphics_state.rb', line 7

def stroke_color
  @stroke_color
end

Instance Method Details

#closepathObject



56
57
58
# File 'lib/postsvg/graphics_state.rb', line 56

def closepath
  @current_path << { type: :closepath }
end

#curveto(x1, y1, x2, y2, x3, y3) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/postsvg/graphics_state.rb', line 40

def curveto(x1, y1, x2, y2, x3, y3)
  coords1 = transform_point(x1, y1)
  coords2 = transform_point(x2, y2)
  coords3 = transform_point(x3, y3)

  @current_path << {
    type: :curveto,
    x1: coords1[:x], y1: coords1[:y],
    x2: coords2[:x], y2: coords2[:y],
    x3: coords3[:x], y3: coords3[:y]
  }

  @current_x = coords3[:x]
  @current_y = coords3[:y]
end

#fill_color_hexObject



139
140
141
# File 'lib/postsvg/graphics_state.rb', line 139

def fill_color_hex
  rgb_to_hex(fill_color[:r], fill_color[:g], fill_color[:b])
end

#lineto(x, y) ⇒ Object



28
29
30
31
32
33
# File 'lib/postsvg/graphics_state.rb', line 28

def lineto(x, y)
  coords = transform_point(x, y)
  @current_x = coords[:x]
  @current_y = coords[:y]
  @current_path << { type: :lineto, x: @current_x, y: @current_y }
end

#moveto(x, y) ⇒ Object

Path construction operations



21
22
23
24
25
26
# File 'lib/postsvg/graphics_state.rb', line 21

def moveto(x, y)
  coords = transform_point(x, y)
  @current_x = coords[:x]
  @current_y = coords[:y]
  @current_path << { type: :moveto, x: @current_x, y: @current_y }
end

#newpathObject



60
61
62
# File 'lib/postsvg/graphics_state.rb', line 60

def newpath
  @current_path = []
end

#restoreObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/postsvg/graphics_state.rb', line 88

def restore
  return if @state_stack.empty?

  state = @state_stack.pop
  @current_path = state[:path]
  @current_x = state[:x]
  @current_y = state[:y]
  @line_width = state[:line_width]
  @fill_color = state[:fill_color]
  @stroke_color = state[:stroke_color]
  @transform_matrix = state[:transform_matrix]
end

#rgb_to_hex(r, g, b) ⇒ Object

Helper methods



135
136
137
# File 'lib/postsvg/graphics_state.rb', line 135

def rgb_to_hex(r, g, b)
  format("#%02x%02x%02x", (r * 255).to_i, (g * 255).to_i, (b * 255).to_i)
end

#rlineto(dx, dy) ⇒ Object



35
36
37
38
# File 'lib/postsvg/graphics_state.rb', line 35

def rlineto(dx, dy)
  # Relative line to
  lineto(@current_x + dx, @current_y + dy)
end

#rotate(angle) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/postsvg/graphics_state.rb', line 116

def rotate(angle)
  # Convert angle from degrees to radians
  rad = angle * Math::PI / 180.0
  cos_a = Math.cos(rad)
  sin_a = Math.sin(rad)

  # Create rotation matrix and multiply with current matrix
  a, b, c, d, e, f = @transform_matrix
  @transform_matrix = [
    (a * cos_a) + (c * sin_a),
    (b * cos_a) + (d * sin_a),
    (-a * sin_a) + (c * cos_a),
    (-b * sin_a) + (d * cos_a),
    e,
    f,
  ]
end

#saveObject

Graphics state stack



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/postsvg/graphics_state.rb', line 76

def save
  @state_stack.push({
                      path: @current_path.dup,
                      x: @current_x,
                      y: @current_y,
                      line_width: @line_width,
                      fill_color: @fill_color.dup,
                      stroke_color: @stroke_color.dup,
                      transform_matrix: @transform_matrix.dup,
                    })
end

#scale(sx, sy) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/postsvg/graphics_state.rb', line 108

def scale(sx, sy)
  # Modify transformation matrix for scaling
  @transform_matrix[0] *= sx
  @transform_matrix[1] *= sx
  @transform_matrix[2] *= sy
  @transform_matrix[3] *= sy
end

#set_gray(gray) ⇒ Object



71
72
73
# File 'lib/postsvg/graphics_state.rb', line 71

def set_gray(gray)
  set_rgb_color(gray, gray, gray)
end

#set_rgb_color(r, g, b) ⇒ Object

Color operations



65
66
67
68
69
# File 'lib/postsvg/graphics_state.rb', line 65

def set_rgb_color(r, g, b)
  color = { r: r, g: g, b: b }
  @fill_color = color
  @stroke_color = color
end

#stroke_color_hexObject



143
144
145
# File 'lib/postsvg/graphics_state.rb', line 143

def stroke_color_hex
  rgb_to_hex(stroke_color[:r], stroke_color[:g], stroke_color[:b])
end

#translate(tx, ty) ⇒ Object

Transformation operations



102
103
104
105
106
# File 'lib/postsvg/graphics_state.rb', line 102

def translate(tx, ty)
  # Modify transformation matrix for translation
  @transform_matrix[4] += tx
  @transform_matrix[5] += ty
end