From: eto Date: Fri, 14 Mar 2003 08:38:05 +0000 (+0000) Subject: add map_character X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=afc63b7b6582288bc7d91356040b69e34fb1fcbb;p=chise%2Fruby.git add map_character split stroke.rb to codeviewer.rb --- diff --git a/src/chise.rb b/src/chise.rb index 084a24e..d81a9d5 100755 --- a/src/chise.rb +++ b/src/chise.rb @@ -6,8 +6,9 @@ require 'uconv' require 'singleton' $KCODE = 'u' #今のところこれ以外では動かない。String.splitが影響大。inspectも影響。 +$debug = false #これはテスト用 $debug = true #これはテスト用 -#$stdout.binmode if $debug +$stdout.binmode if $debug $stdout.sync = true if $debug class String #====================================================================== @@ -27,6 +28,23 @@ class String #================================================================== }.join('') end + def map_char(block = Proc.new) + return unless block_given? + return self.to_a.map {|ch| (block.call(ch)).to_s }.join("") + end + def map_char!(block = Proc.new) + return unless block_given? + self.replace(self.map_char {|ch| block.call(ch)}) + end + def map_character(block = Proc.new) + return unless block_given? + return self.to_a.map {|ch| (block.call(ch.char)).to_s }.join("") + end + def map_character!(block = Proc.new) + return unless block_given? + self.replace(self.map_char {|ch| block.call(ch.char)}) + end + def method_missing(mid, *args) if char_length == 1 #省略形が有効なのは、一文字の時だけ char.method_missing(mid, *args) @@ -50,14 +68,6 @@ class String #================================================================== end def de_er() return self.dup.de_er!; end - def map_char(block = Proc.new) - return unless block_given? - return self.to_a.map {|ch| (block.call(ch)).to_s }.join("") - end - def map_char!(block = Proc.new) - return unless block_given? - self.replace(self.map_char {|ch| block.call(ch)}) - end def inspect_all() map_char {|ch| ch.char.inspect_all } end def inspect_x() map_char {|ch| ch.char.inspect_x } end diff --git a/src/codeviewer.rb b/src/codeviewer.rb new file mode 100755 index 0000000..53ff3dd --- /dev/null +++ b/src/codeviewer.rb @@ -0,0 +1,169 @@ +#!/usr/bin/env ruby +# StrokeFont library by eto 2003-0314 + +require 'stroke' + +module StrokeFont + class CodeViewer #====================================================================== + WIDTH, HEIGHT = 256, 256 + SCALE = 2 + def initialize(cx=0, cy=0) + @cx, @cy = cx, cy + @s = SCALE + @x1, @y1 = @cx-@s*WIDTH/2, @cy-@s*HEIGHT/2 + @x2, @y2 = @cx+@s*WIDTH/2, @cy+@s*HEIGHT/2 + @px, @py = @cx, @cy #とりあえず中心が開始点 + @pw, @ph, @pr = 30, 30, 10 + @dragging = false + @onkeydown = false + @code = 0 + calc_code + end + attr_reader :code + def draw + colorHSV(0, 0, 100, 10) #まずは下敷きになる枠を書きます。 + rect(@x1, @y1, @x2, @y2) + lineWidth(1) + colorHSV(0, 0, 100, 50) + b = 8; s = @s*WIDTH/b + (0..b).each {|n| + line(@x1, @y1+n*s, @x2, @y1+n*s) + line(@x1+n*s, @y1, @x1+n*s, @y2) + } + colorHSV(0, 100, 100, 100) # 次にポインターを書きます + circle(@px, @py, @pr) + line(@px-@pw/2, @py, @px+@pw/2, @py) + line(@px, @py-@ph/2, @px, @py+@ph/2) + end + def onMouse(x, y) + if @onkeydown + x, y = @px, @py + end + if @dragging || @onkeydown + @onkeydown = false + @px, @py = x, y #p [x, y] + @px = @x1 if @px < @x1 + @py = @y1 if @py < @y1 + @px = @x2-1 if @x2-1 < @px + @py = @y2-1 if @y2-1 < @py + return calc_code + else + return false + end + end + def calc_code() + x = ((@px - @x1)/@s).to_i + y = HEIGHT-1 - ((@py - @y1)/@s).to_i + code = x + y*WIDTH + if @code != code + @code = code + return true #changed + #p [x, y, code] + printf("%02x %02x %04x\n", x, y, @code) + else + return false + end + end + def show_list(list) + colorHSV(0, 100, 100, 100) + list.each {|code| + x, y = code_to_xy(code) + rect(x, y, x+2, y-2) + } + end + def code_to_xy(code) + cx = code % WIDTH + cy = HEIGHT - (code / WIDTH) #intになる? + x = cx * SCALE + @x1 + y = cy * SCALE + @y1 + return x, y + end + def length(x, y) Math.sqrt(x*x + y*y) end + def onMouseDown(x, y) + if length(@px-x, @py-y) < @pr + @dragging = true + end + end + def onMouseUp(x, y) @dragging = false end + def onKeyDown(key) + @onkeydown = true + case key + when 273 + @py += @s + when 274 + @py -= @s + when 276 + @px -= @s + when 275 + @px += @s + end + end + end +end + +if $0 == __FILE__ #====================================================================== + $LOAD_PATH << '../ruby/src' + require 'chise' + include CHISE + require 'stroke' + include StrokeFont + + def setup + useSmooth() + window(-300,-300,300,300) + background 0,0,20 + useFramerate(30) + @cs = CodeViewer.new + @csf1 = CSFFont.new() #普通の文字 + @csf2 = CSFFont.new(CSF_KOUKOTSU_FILE) #甲骨文字 + @key = 1 + @kage = KageFont.new() + @changed = nil + end + + def display + @changed = @cs.onMouse(mouseX, mouseY) #変化があったか? + @cs.draw + @cs.show_list(@kage.cache_list) + code = @cs.code + + push + scale 0.2 + translate -500,-500 + lineWidth(2) + draw_kage(code) + draw_csf(code) + pop + end + + def draw_kage(code) + char = Character.get(code) + return if char.nil? + @kage.init(code) if @changed + @kage.print(code) if @changed + @kage.draw(code) + end + + def draw_csf(ucs) + char = Character.get(ucs) + return if char.nil? + j = char.japanese_jisx0208 + return if j.nil? + code = j + csf = @key == 1 ? @csf1 : @csf2 + csf.init(code) if @changed + csf.print(code) if @changed + csf.draw(code) + end + + def onMouseDown(x, y) @cs.onMouseDown(x, y)end + def onMouseUp(x, y) @cs.onMouseUp(x, y)end + def onKeyDown(key) + @key = key + @cs.onKeyDown(key) + end + mainloop +end + +#----------------------------------------------------------------------end. + diff --git a/src/kage.rb b/src/kage.rb index 2c4baf1..c1b9d55 100755 --- a/src/kage.rb +++ b/src/kage.rb @@ -3,6 +3,7 @@ require 'sgl' require 'kageserver' +require 'singleton' #こんな感じのフォーマットになっている。 # @@ -66,7 +67,8 @@ module StrokeFont @cache_list = @server.list_cache @rend = nil @rend = StrokesRenderer.new - @rend.hsv = [13, 100, 100] + #@rend.hsv = [13, 100, 100] + @rend.hsv = [0, 0, 100] end attr_reader :cache_list def get(code) diff --git a/src/kageserver.rb b/src/kageserver.rb index 079346d..69dc095 100755 --- a/src/kageserver.rb +++ b/src/kageserver.rb @@ -22,7 +22,7 @@ class KageServer #============================================================== @glyphs = [] @use_cache = true #デフォルト: cacheに存在する場合はcacheから引き出す。 @offline = false #テスト用 - @offline = true #テスト用 + #@offline = true #テスト用 Dir.mkdir(CACHE_DIR) unless FileTest.directory?(CACHE_DIR) end attr_accessor :url, :use_cache @@ -56,7 +56,7 @@ class KageServer #============================================================== response, body = http.get('/'+filename(num, type)+".svg") if body if error?(body) - p ['error', uri.to_s] +# p ['error', uri.to_s] return nil else store_cache(num, type, body) @@ -67,6 +67,7 @@ class KageServer #============================================================== return nil end def store_cache(num, type, svg) + #p ["store", num] open(cache_file(num, type), "w") {|f| f.print svg } @@ -96,14 +97,21 @@ class KageServer #============================================================== @kl = KanjiList.instance #list = @kl.awase(0) #list = @kl.awase(1) - list = @kl.joyo + #list = @kl.joyo + list = open("../../jis.txt").read @kn.make_network(list) nodes, edges = @kn.nodes_and_edges ar = [] nodes.each {|ch| char = ch.char num = char.to_i - #next if 0xffff < num + next if 0xffff < num + next if num == 0x3561 + next if num == 0x4fdb + next if num == 0x58d1 + next if num == 0x891d + next if num == 0x8902 + next if num < 0x8900 ar << num } get_ar(ar) diff --git a/src/network.rb b/src/network.rb index 882f53b..6fa4eed 100755 --- a/src/network.rb +++ b/src/network.rb @@ -1,6 +1,7 @@ #!/usr/bin/env ruby # calc KanjiNetwork by eto 2003-0305 +require 'chise' require 'kanjilist' require 'defkanji' require 'graphviz' diff --git a/src/stroke.rb b/src/stroke.rb index b6f379b..0d1491b 100755 --- a/src/stroke.rb +++ b/src/stroke.rb @@ -20,8 +20,10 @@ module StrokeFont init end def draw + return if @strokes.nil? @strokes.strokes.each_with_index {|stroke, index| - draw_delay(stroke, index) + #draw_delay(stroke, index) + draw_alpha(stroke, 100) } end def draw_alpha(stroke, time) @@ -85,167 +87,6 @@ module StrokeFont @px, @py = x2, y2 end end - - class CodeSelector #====================================================================== - WIDTH, HEIGHT = 256, 256 - SCALE = 2 - def initialize(cx=0, cy=0) - @cx, @cy = cx, cy - @s = SCALE - @x1, @y1 = @cx-@s*WIDTH/2, @cy-@s*HEIGHT/2 - @x2, @y2 = @cx+@s*WIDTH/2, @cy+@s*HEIGHT/2 - @px, @py = @cx, @cy #とりあえず中心が開始点 - @pw, @ph, @pr = 30, 30, 10 - @dragging = false - @onkeydown = false - @code = 0 - calc_code - end - attr_reader :code - def draw - colorHSV(0, 0, 100, 10) #まずは下敷きになる枠を書きます。 - rect(@x1, @y1, @x2, @y2) - lineWidth(1) - colorHSV(0, 0, 100, 50) - b = 8; s = @s*WIDTH/b - (0..b).each {|n| - line(@x1, @y1+n*s, @x2, @y1+n*s) - line(@x1+n*s, @y1, @x1+n*s, @y2) - } - colorHSV(0, 100, 100, 100) # 次にポインターを書きます - circle(@px, @py, @pr) - line(@px-@pw/2, @py, @px+@pw/2, @py) - line(@px, @py-@ph/2, @px, @py+@ph/2) - end - def onMouse(x, y) - if @onkeydown - x, y = @px, @py - end - if @dragging || @onkeydown - @onkeydown = false - @px, @py = x, y #p [x, y] - @px = @x1 if @px < @x1 - @py = @y1 if @py < @y1 - @px = @x2-1 if @x2-1 < @px - @py = @y2-1 if @y2-1 < @py - return calc_code - else - return false - end - end - def calc_code() - x = ((@px - @x1)/@s).to_i - y = HEIGHT-1 - ((@py - @y1)/@s).to_i - code = x + y*WIDTH - if @code != code - @code = code - return true #changed - #p [x, y, code] - printf("%02x %02x %04x\n", x, y, @code) - else - return false - end - end - def show_list(list) - colorHSV(0, 100, 100, 100) - list.each {|code| - x, y = code_to_xy(code) - rect(x, y, x+2, y-2) - } - end - def code_to_xy(code) - cx = code % WIDTH - cy = HEIGHT - (code / WIDTH) #intになる? - x = cx * SCALE + @x1 - y = cy * SCALE + @y1 - return x, y - end - def length(x, y) Math.sqrt(x*x + y*y) end - def onMouseDown(x, y) - if length(@px-x, @py-y) < @pr - @dragging = true - end - end - def onMouseUp(x, y) @dragging = false end - def onKeyDown(key) - @onkeydown = true - case key - when 273 - @py += @s - when 274 - @py -= @s - when 276 - @px -= @s - when 275 - @px += @s - end - end - end - -end - -if $0 == __FILE__ #====================================================================== - $LOAD_PATH << '../ruby/src' - require 'chise' - include CHISE - require 'stroke' - include StrokeFont - - def setup - useSmooth() - window(-300,-300,300,300) - background 0,0,20 - useFramerate(30) - @cs = CodeSelector.new - @csf1 = CSFFont.new() #普通の文字 - @csf2 = CSFFont.new(CSF_KOUKOTSU_FILE) #甲骨文字 - @key = 1 - @kage = KageFont.new() - @changed = nil - end - - def display - @changed = @cs.onMouse(mouseX, mouseY) #変化があったか? - @cs.draw - @cs.show_list(@kage.cache_list) - code = @cs.code - - push - scale 0.2 - translate -500,-500 - lineWidth(2) - draw_kage(code) - draw_csf(code) - pop - end - - def draw_kage(code) - char = Character.get(code) - return if char.nil? - @kage.init(code) if @changed - @kage.print(code) if @changed - @kage.draw(code) - end - - def draw_csf(ucs) - char = Character.get(ucs) - return if char.nil? - j = char.japanese_jisx0208 - return if j.nil? - code = j - csf = @key == 1 ? @csf1 : @csf2 - csf.init(code) if @changed - csf.print(code) if @changed - csf.draw(code) - end - - def onMouseDown(x, y) @cs.onMouseDown(x, y)end - def onMouseUp(x, y) @cs.onMouseUp(x, y)end - def onKeyDown(key) - @key = key - @cs.onKeyDown(key) - end - mainloop end #----------------------------------------------------------------------end. diff --git a/t/tc_char.rb b/t/tc_char.rb index ca0d7d8..f9127e2 100755 --- a/t/tc_char.rb +++ b/t/tc_char.rb @@ -83,12 +83,12 @@ class TC_Character < Test::Unit::TestCase char1 = cf.get("字") char2 = cf.get("字") assert_equal(char1, char2, "factory") #==である - assert_same(char1, char2, "factory") #かつ同じinstanceであることが保証される + assert_same(char1, char2, "factory") #かつ同じinstanceであることが保証される char1 = Character.get("字") #Character.newの代りにCharacter.getを使うとCharacterFactoryを使ったのと同じ効果がある。 char2 = Character.get("字") - assert_equal(char1, char2) #==ではある - assert_same(char1, char2) #equal?かというと違う + assert_equal(char1, char2) #==である + assert_same(char1, char2) #かつ同じinstanceであることが保証される end def p_er(er) p er.de_er.char.inspect_all diff --git a/t/tc_str.rb b/t/tc_str.rb index 009b354..bd0c1b7 100755 --- a/t/tc_str.rb +++ b/t/tc_str.rb @@ -23,9 +23,9 @@ class TC_String < Test::Unit::TestCase end def test_attributes assert_equal(23383, "字".ucs) - #assert_equal(23383, "字字".ucs) + #assert_equal(23383, "字字".ucs) #エラーが発生する assert_equal(25991, "文".ucs) - #assert_equal(25991, @str.ucs) + #assert_equal(25991, @str.ucs) #エラーが発生する end def test_er @char = @str.char_at(1)