Skip to content

Instantly share code, notes, and snippets.

@seki
Last active June 9, 2024 03:58
Show Gist options
  • Save seki/ff829f6102221a14f888e5569657e48e to your computer and use it in GitHub Desktop.
Save seki/ff829f6102221a14f888e5569657e48e to your computer and use it in GitHub Desktop.
text to svg (for debugging)
require 'erb'
class MySVG
include ERB::Util
SRC = <<EOS
<svg xmlns='http://www.w3.org/2000/svg'><%
x = 10
dy = 18
y = dy
font = 16
%><g x='<%= x %>' font-size='<%= font %>' font-family='Helvetica' fill='navy'><%
text.each_line do |line|
line.chomp!
%><text y='<%= y %>' xml:space='preserve'><%=h line %></text><%
y += dy
end
%></g>
</svg>
EOS
ERB.new(SRC).def_method(self,'to_svg(text)')
def to_url(text)
svg = to_svg(text)
'data:image/svg+xml,' + svg.chomp!.gsub!(/[#"]/){|c|ERB::Util.url_encode(c)}
end
end
puts MySVG.new.to_svg(File.read(__FILE__))
# puts MySVG.new.to_url(File.read(__FILE__))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment