Class: Postsvg::Translation::Handlers::PathHandler

Inherits:
Object
  • Object
show all
Extended by:
Shared
Defined in:
lib/postsvg/translation/handlers/path_handler.rb

Overview

SVG <path d="...">. Walks the PathData command list and emits equivalent PS path construction operators. Full SVG path grammar support:

  • M/m moveto (relative repeats as l)
  • L/l lineto
  • H/h vertical line
  • V/v horizontal line
  • C/c cubic bezier
  • S/s smooth cubic (reflects previous control point)
  • Q/q quadratic bezier (converted to cubic)
  • T/t smooth quadratic (converted to cubic)
  • A/a arc (converted to PS arc via center-point parametrization)
  • Z/z closepath

Defined Under Namespace

Classes: SmoothState

Class Method Summary collapse

Methods included from Shared

emit_fill, emit_paint, emit_paint_setup, emit_stroke, emit_transform, expand_bbox

Class Method Details

.adapt_curve_args(args, opcode, state) ⇒ Object

---- helpers ----



231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 231

def self.adapt_curve_args(args, opcode, state)
  return args if opcode.upcase == opcode

  # Relative: dxN/dyN become xN = current + dxN
  x1 = state.current_x + args[0]
  y1 = state.current_y + args[1]
  x2 = state.current_x + args[2]
  y2 = state.current_y + args[3]
  x3 = state.current_x + args[4]
  y3 = state.current_y + args[5]
  [x1, y1, x2, y2, x3, y3]
end

.adapt_quadratic_args(args, opcode, state) ⇒ Object



244
245
246
247
248
249
250
251
252
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 244

def self.adapt_quadratic_args(args, opcode, state)
  return args if opcode.upcase == opcode

  qx = state.current_x + args[0]
  qy = state.current_y + args[1]
  x3 = state.current_x + args[2]
  y3 = state.current_y + args[3]
  [qx, qy, x3, y3]
end

.call(element, context) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 27

def self.call(element, context)
  context.emitter.emit(Model::Operators::GraphicsState::Gsave.new)
  emit_transform(element, context)
  context.emitter.emit(Model::Operators::Path::Newpath.new)

  state = PathState.new
  smooth = SmoothState.new(control_x: nil, control_y: nil, smoothable: false)
  element.commands.each do |cmd|
    dispatch_command(cmd, state, smooth, context)
  end
  emit_paint(element, context)
  context.emitter.emit(Model::Operators::GraphicsState::Grestore.new)
  expand_bbox(context, *state.bbox_points) if state.bbox_points.any?
end

.dispatch_command(cmd, state, smooth, context) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 42

def self.dispatch_command(cmd, state, smooth, context)
  case cmd.opcode
  when "M", "m"
    handle_moveto(cmd, state, smooth, context)
  when "L", "l"
    handle_lineto(cmd, state, context)
  when "H", "h"
    handle_hv(cmd, state, context, horizontal: cmd.opcode.upcase == "H")
  when "V", "v"
    handle_hv(cmd, state, context, horizontal: cmd.opcode.upcase == "V")
  when "C", "c"
    handle_cubic(cmd, state, smooth, context)
  when "S", "s"
    handle_smooth_cubic(cmd, state, smooth, context)
  when "Q", "q"
    handle_quadratic(cmd, state, smooth, context)
  when "T", "t"
    handle_smooth_quadratic(cmd, state, smooth, context)
  when "A", "a"
    handle_arc(cmd, state, context)
  when "Z", "z"
    context.emitter.emit(Model::Operators::Path::Closepath.new)
    state.close!
  end
end

.handle_arc(cmd, state, context) ⇒ Object

Convert SVG elliptical arc to PS arc(s). SVG arc parametrizes by endpoint + flags; PS arc is center-based. Use the W3C-endorsed endpoint-to-center conversion.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 199

def self.handle_arc(cmd, state, context)
  rx, ry, x_axis_rotation, large_arc_flag, sweep_flag, x, y = cmd.args
  cx, cy, normalized_rx, normalized_ry, psi, theta1, theta2 =
    Postsvg::Translation::ArcConverter.endpoint_to_center(
      x1: state.current_x, y1: state.current_y,
      rx: rx, ry: ry, x_axis_rotation: x_axis_rotation,
      large_arc: large_arc_flag != 0, sweep: sweep_flag != 0,
      x2: x, y2: y,
    )
  # Emit arc with center, radius, angle range. For elliptical
  # arcs (rx != ry or non-zero rotation), emit scale/rotate
  # transforms around the arc.
  if x_axis_rotation.abs > 1e-6 || (rx - ry).abs > 1e-6
    context.emitter.emit(Model::Operators::GraphicsState::Gsave.new)
    context.emitter.emit(Model::Operators::Transformations::Concat.new(matrix: [
      Math.cos(psi), Math.sin(psi), -Math.sin(psi), Math.cos(psi), cx, cy,
    ]))
    context.emitter.emit(Model::Operators::Transformations::Scale.new(sx: normalized_rx, sy: normalized_ry))
    context.emitter.emit(Model::Operators::Path::Arc.new(
      x: 0, y: 0, radius: 1.0, angle1: theta1, angle2: theta2,
    ))
    context.emitter.emit(Model::Operators::GraphicsState::Grestore.new)
  else
    context.emitter.emit(Model::Operators::Path::Arc.new(
      x: cx, y: cy, radius: normalized_rx, angle1: theta1, angle2: theta2,
    ))
  end
  state.move_to(x, y)
end

.handle_cubic(cmd, state, smooth, context) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 114

def self.handle_cubic(cmd, state, smooth, context)
  args = cmd.args
  x1, y1, x2, y2, x3, y3 = adapt_curve_args(args, cmd.opcode, state)
  state.curve_to(x3, y3)
  context.emitter.emit(Model::Operators::Path::Curveto.new(
    x1: x1, y1: y1, x2: x2, y2: y2, x3: x3, y3: y3,
  ))
  smooth.control_x = x2
  smooth.control_y = y2
  smooth.smoothable = true
end

.handle_hv(cmd, state, context, horizontal:) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 93

def self.handle_hv(cmd, state, context, horizontal:)
  v = cmd.args[0]
  if cmd.opcode.upcase == cmd.opcode # absolute
    if horizontal
      new_x = v
      state.line_to(new_x, state.current_y)
      context.emitter.emit(Model::Operators::Path::Lineto.new(x: new_x, y: state.current_y))
    else
      new_y = v
      state.line_to(state.current_x, new_y)
      context.emitter.emit(Model::Operators::Path::Lineto.new(x: state.current_x, y: new_y))
    end
  elsif horizontal
    state.line_to_rel(v, 0)
    context.emitter.emit(Model::Operators::Path::Rlineto.new(dx: v, dy: 0))
  else
    state.line_to_rel(0, v)
    context.emitter.emit(Model::Operators::Path::Rlineto.new(dx: 0, dy: v))
  end
end

.handle_lineto(cmd, state, context) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 82

def self.handle_lineto(cmd, state, context)
  x, y = cmd.args
  if cmd.opcode == "L"
    state.line_to(x, y)
    context.emitter.emit(Model::Operators::Path::Lineto.new(x: x, y: y))
  else
    state.line_to_rel(x, y)
    context.emitter.emit(Model::Operators::Path::Rlineto.new(dx: x, dy: y))
  end
end

.handle_moveto(cmd, state, smooth, context) ⇒ Object

---- per-command handlers ----



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 70

def self.handle_moveto(cmd, state, smooth, context)
  x, y = cmd.args
  if cmd.opcode == "M"
    state.move_to(x, y)
    context.emitter.emit(Model::Operators::Path::Moveto.new(x: x, y: y))
  else
    state.move_to_rel(x, y)
    context.emitter.emit(Model::Operators::Path::Rmoveto.new(dx: x, dy: y))
  end
  smooth.smoothable = false
end

.handle_quadratic(cmd, state, smooth, context) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 148

def self.handle_quadratic(cmd, state, smooth, context)
  args = cmd.args
  qx, qy, x3, y3 = adapt_quadratic_args(args, cmd.opcode, state)
  # Convert quadratic to cubic: c1 = p0 + 2/3*(q-p0),
  # c2 = p3 + 2/3*(q-p3).
  p0x, p0y = state.current_x, state.current_y
  c1x = p0x + (2.0 / 3.0) * (qx - p0x)
  c1y = p0y + (2.0 / 3.0) * (qy - p0y)
  c2x = x3 + (2.0 / 3.0) * (qx - x3)
  c2y = y3 + (2.0 / 3.0) * (qy - y3)
  state.curve_to(x3, y3)
  context.emitter.emit(Model::Operators::Path::Curveto.new(
    x1: c1x, y1: c1y, x2: c2x, y2: c2y, x3: x3, y3: y3,
  ))
  smooth.control_x = qx
  smooth.control_y = qy
  smooth.smoothable = true
end

.handle_smooth_cubic(cmd, state, smooth, context) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 126

def self.handle_smooth_cubic(cmd, state, smooth, context)
  x2, y2, x3, y3 = cmd.args
  if cmd.opcode == "S"
    x1, y1 = reflect_control(smooth, state)
    state.curve_to(x3, y3)
    context.emitter.emit(Model::Operators::Path::Curveto.new(
      x1: x1, y1: y1, x2: x2, y2: y2, x3: x3, y3: y3,
    ))
  else
    dx1, dy1 = reflect_control_rel(smooth, state)
    state.curve_to_rel(dx2: x2, dy2: y2, dx3: x3, dy3: y3, dx1: dx1, dy1: dy1)
    context.emitter.emit(Model::Operators::Path::Rcurveto.new(
      dx1: dx1, dy1: dy1, dx2: x2, dy2: y2, dx3: x3, dy3: y3,
    ))
    x2 = state.current_x - x2
    y2 = state.current_y - y2
  end
  smooth.control_x = state.current_x + (x2 - state.current_x)
  smooth.control_y = state.current_y + (y2 - state.current_y)
  smooth.smoothable = true
end

.handle_smooth_quadratic(cmd, state, smooth, context) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 167

def self.handle_smooth_quadratic(cmd, state, smooth, context)
  x3, y3 = cmd.args
  qx, qy =
    if smooth.smoothable
      [reflect_x(smooth.control_x, state), reflect_y(smooth.control_y, state)]
    else
      [state.current_x, state.current_y]
    end
  p0x, p0y = state.current_x, state.current_y
  c1x = p0x + (2.0 / 3.0) * (qx - p0x)
  c1y = p0y + (2.0 / 3.0) * (qy - p0y)
  c2x = x3 + (2.0 / 3.0) * (qx - x3)
  c2y = y3 + (2.0 / 3.0) * (qy - y3)
  if cmd.opcode == "T"
    state.curve_to(x3, y3)
  else
    state.curve_to_rel(x3 - p0x, y3 - p0y) # treat as rel target
    # Re-anchor to absolute target for emission below.
    state.current_x = p0x + x3
    state.current_y = p0y + y3
  end
  context.emitter.emit(Model::Operators::Path::Curveto.new(
    x1: c1x, y1: c1y, x2: c2x, y2: c2y, x3: state.current_x, y3: state.current_y,
  ))
  smooth.control_x = qx
  smooth.control_y = qy
  smooth.smoothable = true
end

.reflect_control(smooth, state) ⇒ Object



254
255
256
257
258
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 254

def self.reflect_control(smooth, state)
  return [state.current_x, state.current_y] unless smooth.smoothable

  [reflect_x(smooth.control_x, state), reflect_y(smooth.control_y, state)]
end

.reflect_control_rel(smooth, state) ⇒ Object



260
261
262
263
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 260

def self.reflect_control_rel(smooth, state)
  abs_x, abs_y = reflect_control(smooth, state)
  [abs_x - state.current_x, abs_y - state.current_y]
end

.reflect_x(control_x, state) ⇒ Object



265
266
267
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 265

def self.reflect_x(control_x, state)
  2 * state.current_x - control_x
end

.reflect_y(control_y, state) ⇒ Object



269
270
271
# File 'lib/postsvg/translation/handlers/path_handler.rb', line 269

def self.reflect_y(control_y, state)
  2 * state.current_y - control_y
end