update.
[chise/ruby.git] / chise / kageserver.rb
1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2
3 require "singleton"
4 require "net/http"
5 require "uri"
6 require "chise"
7 require "chise/network"
8 require "chise/kanjilist"
9
10 class KageServer
11   include Singleton
12
13   TYPES = %w(skeleton mincho gothic)
14   SKELETON = 0
15   MINCHO = 1
16   GOTHIC = 2
17   URL  = "http://192.168.2.60:5100/"
18
19   def initialize(url=URL)
20     @url = url
21     @glyphs = []
22     @use_cache = true # retriev from cache.  default.
23     # @offline = false
24     @offline = true # for test
25     c = Config.instance
26     @cache_dir = c.base_dir+"/cache_kage"
27     Dir.mkdir(@cache_dir) unless FileTest.directory?(@cache_dir)
28   end
29   attr_accessor :url, :use_cache, :offline
30
31   def filename(num, type=SKELETON)
32     sprintf("u%04x.%s", num, TYPES[type])
33   end
34
35   def cache_file(num, type=SKELETON)
36     @cache_dir+"/"+filename(num, type)+".svg"
37   end
38
39   def list_cache
40     ar = []
41     Dir.chdir(@cache_dir)
42     Dir.glob("*.svg").each {|file|
43       if file =~ /^u([0-9a-fA-F]+).skeleton.svg$/
44         code = $1.hex
45         ar << code
46       end
47     }
48     ar
49   end
50
51   def get(num, type=SKELETON)
52     if FileTest.exist?(cache_file(num, type))
53       return open(cache_file(num, type)) {|f| f.read }
54     end
55     svg = get_http(num, type)
56     svg
57   end
58
59   def get_http(num, type=SKELETON)
60     return nil if @offline
61
62     uri = URI.parse(URL + filename(num, type))
63     p ["uri", uri.to_s]
64
65     Net::HTTP.version_1_1   # declear to use 1.1 features.
66     Net::HTTP.start( uri.host, uri.port ) {|http|
67       response, body = http.get("/"+filename(num, type)+".svg")
68       if body
69         if error?(body)
70 #         p ["error", uri.to_s]
71           return nil
72         else
73           store_cache(num, type, body)
74           return body
75         end
76       end
77     }
78     nil
79   end
80
81   def store_cache(num, type, svg)
82     #p ["store", num]
83     open(cache_file(num, type), "w") {|f|
84       f.print svg
85     }
86   end
87
88   def error?(svg)
89     (svg =~ /<!-- error -->/)
90   end
91
92   def read_list
93     h = {}
94     open("kage-list.txt"){|f|
95       while line = f.gets
96         if line =~ /u([0-9a-f]+)\.skeleton/
97           code = $1
98           num = code.hex
99           error = false
100           error = true if line =~ /error/
101           h[num] = error
102         end
103       end
104     }
105     return h
106   end
107
108   def get_all
109     #error_h = read_list
110     STDOUT.binmode
111     @kn = KanjiNetwork.new
112     @kl = KanjiList.instance
113     #list = @kl.awase(0)
114     #list = @kl.awase(1)
115     #list = @kl.joyo
116     #list = open("../../jis.txt").read
117     list = @kl.jisx0208()
118     @kn.make_network(list)
119     nodes, edges = @kn.nodes_and_edges
120     ar = []
121     nodes.each {|ch|
122       char = ch.char
123       num = char.to_i
124       next if 0xffff < num
125       next if num == 0x3561
126       next if num == 0x4fdb
127       next if num == 0x58d1
128       next if num == 0x891d
129       next if num == 0x8902
130       next if num < 0x8900
131       ar << num
132     }
133     get_ar(ar)
134   end
135
136   def get_ar(ar)
137     ar.each {|num| #intの数列
138       char = Character.get(num)
139       ch = char.to_s
140       er = char.to_er
141       #TYPES.each_with_index {|type, index|
142       #(0..2).each {|index|
143       (0..0).each {|index|
144         result = get(num, index) #cacheに保存するべしと。
145         next if result
146         err = "error"
147         print "#{er}    #{ch}   #{err}\n"
148       }
149     }
150   end
151
152   def test_kanji
153     char = "&CDP-8BA5;".de_er
154     #p char.inspect_all
155     #str = (char.to_s+"真")
156     str = (char.to_s+"直")
157     p str.find
158   end
159 end
160
161 if $0 == __FILE__
162   ks = KageServer.instance
163   #print ks.get(0x4e03)
164   ks.get_all
165 end