ソフトウェア/tchart.rb の履歴(No.9)
更新テキストファイルのソースを与えてタイミング図を作成するスクリプトです†
熊谷正朗さんの "Timing chart formatter by kumagai"
http://www.mech.tohoku-gakuin.ac.jp/rde/contents/library/tchart/indexframe.html
を参考にして、svg を出力するように ruby で作りました。
svg の使い方はこちらのサイトを参考にさせていただきました。
http://www.h2.dion.ne.jp/~defghi/svgMemo/svgMemo.htm
かなり走り書き的なところがありますが、ご容赦下さい。
もしかして、javascript で作った方が良かったんじゃないだろうか・・・†
javascript で実装すればクライアント側の処理だけで図を描画できますし、 最近はそのまま画像として保存することもできるそうで、
http://www.pori2.net/html5/Canvas/150.html
ずっと良かったのかもしれません???
確かに・・・ずっとよさそうです
ソフトウェア/タイミングチャート清書サービス
javascript 版がいろいろ便利すぎたため、
この ruby 版は細かいバグを残したまま現在更新されておりません
javascript 版をご利用ください。
使用例†
次のようなテキストから、
# AXI4 fundamental protocol clock _~_~_~_~_~_~_~_~_~_~_ data =?====X=DATA========X=?==== valid _____~~~~~~~~~~______ ready _____________[~~]______
このようなタイミング図を SVG として出力します。
&tchart(
# AXI4 fundamental protocol clock _~_~_~_~_~_~_~_~_~_~_ data =?====X=DATA========X=?==== valid ~~~~~~~~~~_ ready ________[~~]_ );
ソースファイル†
いやほんと、走り書きですみません。
LANGUAGE:ruby(linenumber) #!/usr/bin/ruby #### tchart.rb by osamu@big.jp # # "Timing chart formatter by kumagai" # http://www.mech.tohoku-gakuin.ac.jp/rde/contents/library/tchart/indexframe.html # # を参考に、svg を出力するようにしたものです # # かなり走り書き的なところがありますが、ご容赦下さい。 # ##################################################################### # default config conf = { scale: 1.0, margin: 10, w_caption: 40, w_hold: 10, w_transient: 2, h_line: 10, h_space: 10, signal_style: 'stroke-linecap="round" stroke-width="0.6" stroke="black" fill="none"', grid_style: 'stroke-linecap="round" stroke-width="0.6" stroke="red" fill="none"', highlight_style:'stroke="none" fill="#ff8"', notcare_style: 'fill="#ccc"', rotate: 0, caption_font: 'fill="black" font-family="Helvetica"', signal_font: 'fill="black" font-family="Helvetica"' } ##################################################################### class Line class SubLine def initialize(x,y) @points = [[x,y]] end def accepts?(x,y) @points.last == [x,y] end def add(x,y) @points << [x,y] end def path result = '' last = nil @points.each do |p| if last if last==p next elsif last[0]==p[0] result += 'V%g' % p[1] elsif last[1]==p[1] result += 'H%g' % p[0] else result += "L%g,%g" % p end else result = "M%g,%g" % p end last = p end result end end def initialize(style) @lines = [] @style = style end def draw(x1, y1, x2, y2) unless line = @lines.find {|line| line.accepts?(x1, y1) } line = SubLine.new(x1, y1) @lines << line end line.add(x2, y2) end def svg path = @lines.map{|line| line.path }.join('') %Q[<path #{@style} d="#{path}" />] end end class Timeline # : - ~ _ = @@transitions = [ ' ', # : ' - 1 4 14', # - ' 2 ~ / ~/', # ~ ' 3 ` _ _`', # _ ' 23`~/_= ', # = ' 23`~/_=/', # / ' 23`~/_=`', # \ ' 23`~/_X ', # X ' 23`~/_=X', # * ] @@transition_lines = { ' ' => [], '~' => [[1,1]], '_' => [[0,0]], '=' => [[1,1],[0,0]], 'X' => [[1,0],[0,1]], '`' => [[1,0]], '/' => [[0,1]], '1' => [[1,0.5]], '2' => [[0.5,1]], '3' => [[0.5,0]], '4' => [[0,0.5]], '-' => [[0.5,0.5]], } # : - ~ _ = @@state_lines = [[],[0.5],[1],[0],[0,1]] # @@codes = ':-~_=/\\X*' @@grids = [] def self.grids @@grids end @@highlight = [] def self.highlight @@highlight end def initialize(conf, y) @line = Line.new(conf[:signal_style]) @current = 0 @conf = conf @x = conf[:w_caption] @y = y @crosses = [] @strings = [] end def y(s) @y + (1-s) * @conf[:h_line] end def y0 @y + @conf[:h_line] end def y1 @y end def yz @y + @conf[:h_line]/2.0 end def x @x end def xh @x + @conf[:w_transient]/2.0 end def xt @x + @conf[:w_transient] end def xr @x + @conf[:w_transient] + @conf[:w_hold] end def draw_transition_sub(c) crosses = '' @@transition_lines[c].each do |line| @line.draw(x, y(line[0]), xt, y(line[1])) crosses += c if line[0] != line[1] end crosses end def draw_transition(s) crosses = '' @@transitions[s][2*@current,2].each_char do |c| crosses += draw_transition_sub(c) end crosses end def draw_state(s) @@state_lines[s].each do |line| @line.draw(xt, y(line), xr, y(line)) end end def add(c) s = @@codes.index(c) crosses = draw_transition(s) s = 4 if s > 4 draw_state(s) @crosses << [x, crosses] if crosses!='' if (@current == 0 and s != 0) or (@current != 0 and s == 0) @crosses << [x, '|'] end @current = s @x = xr end def add_string(s) @strings << [@crosses.count, s] end def parse(line) while line != '' if line.sub!(/^\s+/, '') elsif line.sub!(/^\|/, '') @@grids << xh elsif line.sub!(/^\[/, '') if @@highlight.last==nil or @@highlight.last.is_a?(Array) @@highlight << xh end elsif line.sub!(/^\]/, '') if @@highlight.last.is_a?(Numeric) @@highlight[-1] = [@@highlight.last, xh] end elsif line.sub!(/^([:\-~_=\/\\X*])/, '') add($1) elsif line.sub!(/"(([^"]|"")+)"/, '') add_string($1.strip) elsif line.sub!(/([^:\-~_=\/\\X*]+)/, '') add_string($1.strip) end end end def string2svg @crosses << [x,'|'] @strings.map do |string| x1 = @crosses[string[0]-1][0] x1t = x1 + @conf[:w_transient] x1h = x1 + @conf[:w_transient]/2.0 x1r = x1t + @conf[:w_hold] x2 = @crosses[string[0] ][0] x2t = x2 + @conf[:w_transient] x2h = x2 + @conf[:w_transient]/2.0 x2r = x2t + @conf[:w_hold] # sanitized = string[1].gsub(/</, '<').gsub(/>/, '>').gsub(/"/, '"') svg = %Q(<text x="#{(x1h + x2h)/2.0}" y="#{y0-1.5}" text-anchor="middle" font-size="#{@conf[:h_line]}" #{@conf[:signal_font]}>#{sanitized}</text>) if string[1] == '?' line = "M#{x1t},#{y1}H#{x2}" case @crosses[string[0]][1] when '|' ; when 'XX' line += "L#{x2h},#{yz}" when '/' line += "H#{x2t}" when '`' line += "L#{x2t},#{y0}" when '23' line += "H#{x2t}L#{x2},#{yz}L#{x2t},#{y0}" when '14' line += "L#{x2t},#{yz}" when '1' line += "L#{x2t},#{yz}V#{y0}" when '2' line += "H#{x2t}L#{x2},#{yz}" when '3' line += "V#{yz}L#{x2t},#{y0}" when '4' line += "H#{x2t}V#{yz}" end line += "L#{x2},#{y0}H#{x1t}" case @crosses[string[0]-1][1] when '|' ; when 'XX' line += "L#{x1h},#{yz}" when '/' line += "H#{x1}" when '`' line += "L#{x1},#{y1}" when '23' line += "L#{x1},#{yz}" when '14' line += "H#{x1}L#{x1t},#{yz}L#{x1},#{y1}" when '1' line += "V#{yz}L#{x1},#{y1}" when '2' line += "H#{x1}V#{yz}" when '3' line += "L#{x1},#{yz}V{y1}" when '4' line += "H#{x1}L#{xt},#{yz}" end line += "Z" svg = %Q(\n<path stroke="none" d="#{line}" #{@conf[:notcare_style]}/>) + svg end svg end.join("\n") end def svg string2svg + @line.svg end end def caption2svg(conf, y, caption) sanitized = caption.gsub(/</, '<').gsub(/>/, '>').gsub(/"/, '"') %Q(<text x="#{conf[:w_caption]-5}" y="#{y+conf[:h_line]-1.5}" text-anchor="end" font-size="#{conf[:h_line]}" #{conf[:caption_font]}>#{sanitized}</text>) end ##################################################################### svg = [] y = -1 x_max = 0 lines = readlines lines.shift while lines[0] =~ /^\s*$/ lines.pop while lines.last =~ /^\s*$/ lines.each do |line| next if line =~ /^\#/ if line =~ /^@/ #### configuration if line !~ /^@([^\s]+)[\s]+([^\s].*)$/ STDERR.puts "Illegal Line: #{line}" next end if conf[$1.to_sym].is_a?(Numeric) conf[$1.to_sym] = $2.to_f else conf[$1.to_sym] = $2 end next end if line =~ /^%/ #### free string if line !~ /^%([\d\.-]+)[\s]+([\d\.-]+)[\s]+([^\s].*)$/ STDERR.puts "Illegal Line: #{line}" next end svg << %Q(<text x="#{$1}" y="#{$2}" text-anchor="middle" font-size="#{conf[:h_line]}" #{conf[:signal_font]}>#{$3}</text>) next end if y < 0 y = 0 else y += conf[:h_space] end line.sub!(/\s*$/, '') next if line == '' if line !~ /^([^\s]+)[\s]+([^\s].*)$/ STDERR.puts "Illegal Line: #{line}" next end caption_s = $1 timeline_s = $2 svg << caption2svg(conf, y, caption_s) timeline = Timeline.new(conf, y) timeline.parse(timeline_s) x_max = timeline.xr if x_max < timeline.xr svg << timeline.svg y += conf[:h_line] end m = conf[:margin] w = "%g" % ((x_max + 2*m) * conf[:scale]) h = "%g" % ((y + 2*m) * conf[:scale]) l = "%g" % (-m) t = "%g" % (-m) r = "%g" % (x_max+2*m) b = "%g" % (y +2*m) Timeline.grids.each do |g| x = "%g" % g svg << %Q(<path d="M#{x},#{'%g' % (-m/2)}V#{'%g' % (y+m/2)}" #{conf[:grid_style]} />) end Timeline.highlight.each do |h| if h.is_a?(Array) x1 = "%g" % h[0] x2 = "%g" % h[1] svg.unshift %Q(<path d="M#{x1},#{'%g' % (-m/2)}V#{'%g' % (y+m/2)}H#{x2}V#{'%g' % (-m/2)}Z" #{conf[:highlight_style]} />) end end print <<SVG <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="#{w}pt" height="#{h}pt" viewBox="#{l} #{t} #{r} #{b}" version="1.1"> <![CDATA[ #{lines.join("\n").gsub(/\]\]\>/, ']]>')} ]]> <g> #{svg.join("\n")} </g> </svg> SVG
ちょっと疑問†
ハイインピーダンスからの遷移の書き方はこれで良いんだろうか?
他と傾きが違ってしまうけれど・・・
@h_line 30 @h_skip 20 @w_transient 8 @w_caption 100 test1 ___|~~~|___|==|X=|X= test2 ---~~~---==--==
&tchart( @h_line 30 @h_skip 20 @w_transient 8 @w_caption 100 test1 |~~~||==|X=|X= test2 ---~~~---==--== );