履歴: ソフトウェア/tchart.rb

各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 11 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます

日時タイトル文字数追加削除内容差分基準
2016/2/10 13:27:37ソフトウェア/tchart.rb 最新版10,927+1 -1表示差分—
2016/2/10 13:27:37ソフトウェア/tchart.rb 10,933+2 -3表示表示中選択
2016/2/10 4:27:37ソフトウェア/tchart.rb 10,901+18 -515表示差分選択
2016/2/8 12:51:14ソフトウェア/tchart.rb 20,840+3 -4表示差分選択
2016/2/8 12:51:14ソフトウェア/tchart.rb 20,827+4 -4表示差分選択
2016/2/8 12:51:14ソフトウェア/tchart.rb 20,768+19 -14表示差分選択
2016/2/8 12:51:14ソフトウェア/tchart.rb 20,712+256 -31表示差分選択
2016/2/8 5:16:25ソフトウェア/tchart.rb 16,250+61 -14表示差分選択
2016/2/5 18:13:23ソフトウェア/tchart.rb 14,430+5 -1表示差分選択
2016/2/5 18:13:23ソフトウェア/tchart.rb 14,376+673 -0表示差分選択
2016/2/5 16:23:17ソフトウェア/tchart.rb 516— —表示—基準

基準: ソフトウェア/tchart.rb (2016/2/5 16:23:17) → 比較: ソフトウェア/tchart.rb (2016/2/10 13:27:37)

@@ -1,24 +1,479 @@
[[公開メモ]]
+#contents
+
* テキストファイルのソースを与えてタイミング図を作成するスクリプトです [#yf72bb15]
-"Timing chart formatter by kumagai" ~
+熊谷正朗さんの "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 で作った方が良かったんじゃないだろうか・・・ [#m8dba977]
+
+javascript で実装すればクライアント側の処理だけで図を描画できますし、
+最近はそのまま画像として保存することもできるそうで、
+
+http://www.pori2.net/html5/Canvas/150.html
+
+ずっと良かったのかもしれません???
+
+確かに・・・ずっとよさそうです。javascript で書き直した物をこちらで紹介しています。~
+[[ソフトウェア/タイミングチャート清書サービス]]
+
+javascript 版がいろいろ便利すぎたため、~
+COLOR(red){SIZE(30){以下の ruby 版は細かいバグを残したまま現在更新されておりません}}
+
+javascript 版をご利用ください。
+
* 使用例 [#ya5f8236]
+次のようなテキストから、
+
+ # AXI4 fundamental protocol
clock _~_~_~_~_~_~_~_~_~_~_
data =?====X=DATA========X=?====
valid _____~~~~~~~~~~______
ready _____________[~~]______
+このようなタイミング図を SVG として出力します。
+
&tchart(
+# AXI4 fundamental protocol
clock _~_~_~_~_~_~_~_~_~_~_
data =?====X=DATA========X=?====
valid _____~~~~~~~~~~______
ready _____________[~~]______
);
+
+* ソースファイル [#u33d6dea]
+
+いやほんと、走り書きですみません。
+
+ 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(/</, '&lt;').gsub(/>/, '&gt;').gsub(/"/, '&quot;')
+ 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(/</, '&lt;').gsub(/>/, '&gt;').gsub(/"/, '&quot;')
+ %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(/\]\]\>/, ']]&gt;')}
+ ]]>
+ <g>
+ #{svg.join("\n")}
+ </g>
+ </svg>
+ SVG
+
+* ちょっと疑問 [#sfbfc96d]
+
+ハイインピーダンスからの遷移の書き方はこれで良いんだろうか?
+
+他と傾きが違ってしまうけれど・・・
+
+ @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 ---~~~---==--==
+);
+
+* コメント・質問 [#jb4548c8]
+
+#article_kcaptcha