update doc.
[chise/ruby.git] / src / chise / kageserver.rb
1 #!/usr/bin/env ruby
2 # KageServer - access and fetch Kage glyph data files
3 # $Id: kageserver.rb,v 1.1 2003-11-10 08:11:47 eto Exp $
4 # Copyright (C) 2002-2003 Kouichirou Eto, All rights reserved.
5 # This is free software with ABSOLUTELY NO WARRANTY.
6 # You can redistribute it and/or modify it under the terms of the GNU GPL2.
7
8 require 'singleton'
9 require 'net/http'
10 require 'uri'
11 $LOAD_PATH << '../../src'
12 require 'chise'
13 require 'chise/network' #漢字間接続のネットワークを計算する
14 #require 'chise/kanjilist'
15 include CHISE
16
17 class KageServer #======================================================================
18   include Singleton
19   TYPES = "skeleton mincho gothic".split
20   SKELETON = 0
21   MINCHO = 1
22   GOTHIC = 2
23   HOME_DIR = "d:/work/chise/"
24   CACHE_DIR = HOME_DIR + "cache_kage"
25   URL  = "http://192.168.2.60:5100/"
26   def initialize(url=URL)
27     @url = url
28     @glyphs = []
29     @use_cache = true #デフォルト: cacheに存在する場合はcacheから引き出す。
30     @offline = false #テスト用
31     @offline = true #テスト用
32     Dir.mkdir(CACHE_DIR) unless FileTest.directory?(CACHE_DIR)
33   end
34   attr_accessor :url, :use_cache, :offline
35   def filename(num, type=SKELETON) sprintf("u%04x.%s", num, TYPES[type]) end
36   def cache_file(num, type=SKELETON) CACHE_DIR+"/"+filename(num, type)+".svg" end
37
38   def list_cache
39     ar = []
40     Dir.chdir(CACHE_DIR)
41     Dir.glob("*.svg").each {|file|
42       if file =~ /^u([0-9a-fA-F]+).skeleton.svg$/
43         code = $1.hex
44         ar << code
45       end
46     }
47     ar
48   end
49
50   def get(num, type=SKELETON)
51     return open(cache_file(num, type)).read if FileTest.exist?(cache_file(num, type))
52     svg = get_http(num, type)
53     return svg if svg
54     return nil
55   end
56   def get_http(num, type=SKELETON)
57     return nil if @offline
58     uri = URI.parse(URL + filename(num, type))
59     p ['uri', uri.to_s]
60     Net::HTTP.version_1_1   # declear to use 1.1 features.
61     Net::HTTP.start( uri.host, uri.port ) {|http|
62       response, body = http.get('/'+filename(num, type)+".svg")
63       if body
64         if error?(body)
65 #         p ['error', uri.to_s]
66           return nil
67         else
68           store_cache(num, type, body)
69           return body
70         end
71       end
72     }
73     return nil
74   end
75   def store_cache(num, type, svg)
76     #p ["store", num]
77     open(cache_file(num, type), "w") {|f|
78       f.print svg
79     }
80   end
81   def error?(svg)
82     (svg =~ /<!-- error -->/)
83   end
84   def read_list
85     h = {}
86     open("kage-list.txt"){|f|
87       while line = f.gets
88         if line =~ /u([0-9a-f]+)\.skeleton/
89           code = $1
90           num = code.hex
91           error = false
92           error = true if line =~ /error/
93           h[num] = error
94         end
95       end
96     }
97     return h
98   end
99   def get_all
100     #error_h = read_list
101     STDOUT.binmode
102     @kn = KanjiNetwork.new
103     @kl = KanjiList.instance
104     #list = @kl.awase(0)
105     #list = @kl.awase(1)
106     #list = @kl.joyo
107     #list = open("../../jis.txt").read
108     list = @kl.jisx0208()
109     @kn.make_network(list)
110     nodes, edges = @kn.nodes_and_edges
111     ar = []
112     nodes.each {|ch|
113       char = ch.char
114       num = char.to_i
115       next if 0xffff < num
116       next if num == 0x3561
117       next if num == 0x4fdb
118       next if num == 0x58d1
119       next if num == 0x891d
120       next if num == 0x8902
121       next if num < 0x8900
122       ar << num
123     }
124     get_ar(ar)
125   end
126   def get_ar(ar)
127     ar.each {|num| #intの数列
128       char = Character.get(num)
129       ch = char.to_s
130       er = char.to_er
131       #TYPES.each_with_index {|type, index|
132       #(0..2).each {|index|
133       (0..0).each {|index|
134         result = get(num, index) #cacheに保存するべしと。
135         next if result
136         err = "error"
137         print "#{er}    #{ch}   #{err}\n"
138       }
139     }
140   end
141   def test_kanji
142     char = "&CDP-8BA5;".de_er
143     #p char.inspect_all
144     #str = (char.to_s+"真")
145     str = (char.to_s+"直")
146     p str.find
147   end
148 end
149
150 if $0 == __FILE__ #======================================================================
151   ks = KageServer.instance
152   #print ks.get(0x4e03)
153   ks.get_all
154 end
155
156 #----------------------------------------------------------------------end.