Module: Ea::Shapescript::Renderer
- Defined in:
- lib/ea/shapescript/renderer.rb
Overview
Renders a list of parsed Shape primitives to SVG elements as
a single string. Coordinates are emitted as-is — callers
should wrap in a <g> with the appropriate transform if they
want to position the icon.
Class Method Summary collapse
- .escape(text) ⇒ Object
-
.render(shapes, fill: "#FFFFFF", stroke: "#000000") ⇒ String
SVG fragment with one element per shape.
- .render_ellipse(p, fill, stroke) ⇒ Object
- .render_label(p, fill) ⇒ Object
- .render_line(p, stroke) ⇒ Object
- .render_one(shape, fill:, stroke:) ⇒ Object
- .render_path(p, stroke) ⇒ Object
- .render_polygon(p, fill, stroke) ⇒ Object
- .render_rect(p, fill, stroke) ⇒ Object
Class Method Details
.escape(text) ⇒ Object
38 39 40 41 42 |
# File 'lib/ea/shapescript/renderer.rb', line 38 def escape(text) text.to_s.gsub("&", "&") .gsub("<", "<") .gsub(">", ">") end |
.render(shapes, fill: "#FFFFFF", stroke: "#000000") ⇒ String
Returns SVG fragment with one element per shape.
16 17 18 19 20 |
# File 'lib/ea/shapescript/renderer.rb', line 16 def render(shapes, fill: "#FFFFFF", stroke: "#000000") return "" if shapes.nil? || shapes.empty? shapes.map { |s| render_one(s, fill: fill, stroke: stroke) }.join("\n") end |
.render_ellipse(p, fill, stroke) ⇒ Object
49 50 51 52 |
# File 'lib/ea/shapescript/renderer.rb', line 49 def render_ellipse(p, fill, stroke) cx, cy, rx, ry = p %(<ellipse cx="#{cx}" cy="#{cy}" rx="#{rx}" ry="#{ry}" fill="#{fill}" stroke="#{stroke}"/>) end |
.render_label(p, fill) ⇒ Object
33 34 35 36 |
# File 'lib/ea/shapescript/renderer.rb', line 33 def render_label(p, fill) text = p.first.to_s %(<text x="0" y="0" fill="#{fill}">#{escape(text)}</text>) end |
.render_line(p, stroke) ⇒ Object
59 60 61 62 |
# File 'lib/ea/shapescript/renderer.rb', line 59 def render_line(p, stroke) x1, y1, x2, y2 = p %(<line x1="#{x1}" y1="#{y1}" x2="#{x2}" y2="#{y2}" stroke="#{stroke}"/>) end |
.render_one(shape, fill:, stroke:) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ea/shapescript/renderer.rb', line 22 def render_one(shape, fill:, stroke:) case shape.kind when :rectangle then render_rect(shape.params, fill, stroke) when :ellipse then render_ellipse(shape.params, fill, stroke) when :polygon then render_polygon(shape.params, fill, stroke) when :line then render_line(shape.params, stroke) when :path then render_path(shape.params, stroke) when :label then render_label(shape.params, fill) end end |
.render_path(p, stroke) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/ea/shapescript/renderer.rb', line 64 def render_path(p, stroke) return "" if p.empty? x, y = p.shift(2) d = "M #{x} #{y}" p.each_slice(2) { |px, py| d += " L #{px} #{py}" } %(<path d="#{d}" fill="none" stroke="#{stroke}"/>) end |
.render_polygon(p, fill, stroke) ⇒ Object
54 55 56 57 |
# File 'lib/ea/shapescript/renderer.rb', line 54 def render_polygon(p, fill, stroke) points = p.each_slice(2).map { |x, y| "#{x},#{y}" }.join(" ") %(<polygon points="#{points}" fill="#{fill}" stroke="#{stroke}"/>) end |
.render_rect(p, fill, stroke) ⇒ Object
44 45 46 47 |
# File 'lib/ea/shapescript/renderer.rb', line 44 def render_rect(p, fill, stroke) x, y, w, h = p %(<rect x="#{x}" y="#{y}" width="#{w}" height="#{h}" fill="#{fill}" stroke="#{stroke}"/>) end |