require "chise/config"
require "chise/iconv"
require "chise/uconv"
-require "chise/util"
require "chise/string"
require "chise/character"
module CHISE
def windows?()
rp = RUBY_PLATFORM
- rp =~ /cygwin/ || rp =~ /mswin32/ || rp =~ /mingw32/
+ rp.index("cygwin") || rp.index("mswin32") || rp.index("mingw32")
end
module_function :windows?
class Config
include Singleton
+
def initialize
@base_dir = File.expand_path(File.dirname(__FILE__)+"/../..")
-# @base_dir = "c:/chise"
-# qp @base_dir
+ @csf_dir = @base_dir+"/csf"
if CHISE.windows?()
@db_dir = @base_dir+"/char-db" # /sysmtem-char-id/ucs
@ids_dir = "/home/eto/chise/ids"
end
- @csf_dir = @base_dir+"/csf"
instance_variables.each {|name|
self.class.class_eval { attr_reader name.delete("@") }
}
require "sgl"
#require "kconv"
-require "uconv"
+#require "uconv"
+require "chise/iconv"
require "chise/config"
module StrokeFont
@strokes = nil
end
attr_reader :strokes
+
def parse()
return if @strokes
@strokes = CSFParser.parse(@stroke_str)
end
+
def init
parse if @strokes.nil?
end
@rend = StrokesRenderer.new
@rend.hsv = [50, 100, 100]
end
+
def read_file()
open(@file) {|f|
while(line = f.gets)
end
}
end
+
def init(code)
glyph = @glyphs[code]
return if glyph == nil
glyph.parse
@rend.set_strokes(glyph.strokes)
end
+
def draw(code) #\88ø\90\94\82É\82ÍJIS\82ð\90\94\92l\89»\82µ\82½\82à\82Ì\82ª\82Í\82¢\82é
glyph = @glyphs[code]
return if glyph == nil
@rend.draw
end
+
def print(code)
jis = JISX0208.new
char = jis.get_char(code)
printf("[%s][%04x]\n", char.nil? ? "nil" : char.map_sjis, code)
end
+
def ucs_to_jis(ucs)
char = Character.get(ucs)
j = char.japanese_jisx0208
return j
end
- end
+ end
end
require "singleton"
require "bdb"
require "chise/config"
-require "chise/util"
require "chise/rbchise"
module CHISE
#p "休".decompose.to_er
eval_print_loop
end
+
def show_ids(str)
str.de_er.to_a.sort.uniq.each {|ch|
char = ch.char
print "#{char} #{ids.map_ucs}\n" #できるだけucsに正規化するべし。
}
end
+
def eval_print_loop
while true
line = STDIN.gets
define_ids_represent
define_ids_link
end
+
def define_ids_element
IDS_ELEMENT.each_char {|ch|
char = ch.char
char.ids_element = ch
}
end
+
def define_ids_meaning
IDS_MEANING.each {|line|
next if line.nil? || line == "" || line =~ /^#/
print "#{ch} #{ids}\n"
}
end
+
def define_ids_represent
IDS_REPRESENT.each {|line|
next if line.nil? || line == "" || line =~ /^#/
print "#{char.map_ucs} #{rep}\n"
}
end
+
def define_ids_link
IDS_LINK.each {|line|
next if line.nil? || line == "" || line =~ /^#/
def initialize()
@attr = Hash.new
end
+
def method_missing(mid, *args) #\8eQ\8dl:ostruct.rb
mname = mid.id2name
return @attr[mname] if args.length == 0
@attr[mname.chop] = args[0] if mname =~ /=$/ #\91ã\93ü
end
+
def to_s
str = " #{mainstr()} "
str << "[" + @attr.map {|k, v| %Q|#{k}="#{v}"| }.join(" ") + "]" if 0 < @attr.length
@nodes = []
@edges = []
end
+
def mainstr() @name.to_s; end
+
def add_node(node)
return unless node.is_a? DotNode
@nodes << node
end
+
def add_edge(edge)
return unless edge.is_a? DotEdge
@edges << edge
end
+
def to_s()
str = "digraph G {\n"
# str << %Q| size="6, 6"\n|
@name = name
super()
end
+
def mainstr() @name.to_s; end
end
@from, @to = from, to
super()
end
+
def mainstr() "#{@from.to_s} -> #{@to.to_s}"; end
end
class GraphvizCLI < Graphviz
# COMMAND_DIR = "c:\Program Files\ATT\Graphviz\bin"
COMMAND_DIR = "" #PATH\82ª\82Æ\82¨\82Á\82Ä\82È\82¢\82Æ\82¾\82ß\82Ý\82½\82¢\81B
- NAMES = "dot twopi neato".split
+# NAMES = "dot twopi neato".split
+ NAMES = %w(dot twopi neato)
+
def generate(debug=false)
ar = []
#ar << COMMAND_DIR + NAMES[@type] + ".exe"
class GraphvizOLE < Graphviz
#NAMES = "DOT TWOPI NEATO".split
+
def generate(debug=false)
require "win32ole"
names = "DOT TWOPI NEATO".split
@ole = WIN32OLE.new("Wingraphviz." + names[@type])
@ole.codepage = cp if @codepage #neato.codepage = 65001 #codepage: 65001 Unicode UTF-8
@instr = open(@in).read
+
if @target =~ /svg/i
result = @ole.toSVG(@instr)
open(@out, "w"){|out| out.print result}
result.save(@out)
end
end
+
def nu_to_png(filename=nil)
return "" unless @dot.is_a?(String)
png = @ole.toPNG(@dot)
png.save(filename) unless filename.nil?
return png
end
+
#p neato.validate(str)
#ps = neato.toPS(str)
#open("test.ps", "w"){|out| out.print ps }
def u32to_i
return 0 if length == 0
s = self
-# return (s[3] << 24 | s[2] << 16 | s[1] << 8 | s[0])
+# return (s[3] << 24 | s[2] << 16 | s[1] << 8 | s[0])
return (s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3])
end
end
IDC_FLL = IDC_LL
IDC_O = IDC_OV
- class IDS_TEXT_DB < DB #======================================================================
+ class IDS_TEXT_DB < DB
include Singleton
+
IDS_LIST = "
IDS-UCS-Basic.txt
#IDS-UCS-Compat-Supplement.txt
IDS-Daikanwa-ho.txt
IDS-CBETA.txt
".split
+
def initialize()
super
@ids_list = IDS_LIST
end
}
end
+
def dump_text_all
each_file {|file|
dir = File.dirname(file) + "/../ids-new/"
}
}
end
+
def make_ids_error
each_file {|file|
dir = File.dirname(file) + "/../ids-error"
class IDS_DB < DB # BDB化したIDS DBを扱う
include Singleton
+
def initialize
@dbs = CharDB.instance
end
+
def make_ids_db
db = IDS_TEXT_DB.instance
db.each_file {|file|
@dbs.dump_db("ids-error") #テキスト化する
@dbs.dump_db("ids") #テキスト化する
end
+
def make_ids_reverse
h = Hash.new
@dbs.each("ids") {|k, v|
cdb.open_db("ids") #これが無いと、dump_dbされません。
cdb.dump_db("ids")
end
+
def char_sort(composed)
return composed if composed.char_length == 1
ar = composed.to_a
end
return ar2.join("")
end
+
def dump_ids_duplicated
open("ids-duplicated.txt", "w"){|out|
#out.binmode
}
}
end
+
def make_ids_aggregated
@dbs.each("ids") {|k, v|
char = k.char
}
@dbs.dump_db("ids-aggregated")
end
+
def dump_ids_aggregated
open("ids-aggregated.txt", "w"){|out|
#out.binmode
}
}
end
+
def make_ids_parts
@dbs.each("ids") {|k, v|
char = k.char
}
@dbs.dump_db("ids-parts")
end
+
def make_ids_contained
h = Hash.new
@dbs.each("ids-parts") {|k, v|
}
@dbs.dump_db("ids-contained")
end
+
def make_ids_decomposed
@dbs.each("ids") {|k, v|
char = k.char
}
@dbs.dump_db("ids-decomposed")
end
+
end
- class Node < Array #==================================木構造の中の一つの枝
+ class Node < Array # 木構造の中の一つの枝
def initialize(nodeleaf=nil, nodenum=nil)
super()
@nodeleaf = nodeleaf
end
end
attr_reader :nodenum
+
alias original_add <<
private :original_add
+
def <<(obj)
original_add(obj)
@nodenum -= 1 if @nodenum
end
+
def nodes
ar = []
ar << self.to_s
}
return ar
end
+
end
- class Tree #==================================================木構造を扱う
+ class Tree # 木構造を扱う
def initialize()
@root = Node.new()
@stack = [@root]
@leafnum = 0
@depth = 1 #stackの深さが最大になったところの値、木構造が無いときは1となる
end
+
def depth() @depth - 1 end
+
def add_node(nodeleaf=nil, nodenum=nil) #枝を追加
new_node = Node.new(nodeleaf, nodenum)
@stack.last << new_node
end
self
end
+
def end_node() #この枝は終り
@stack.pop
self
end
+
def add_leaf(a) #葉を追加
@stack.last << a
end_check()
self
end
+
def end_check()
n = @stack.last.nodenum
if n && n == 0
end_check() #再帰
end
end
+
def check_integrity
n = @stack.last.nodenum
return nil if @root.length == 0 #no tree is good tree
return "extra leaves" if @root.length != 1
return nil
end
+
def nodes
r = @root.nodes
r.shift
r
end
+
def sub_nodes
r = nodes
r.shift
r
end
+
def to_s() @root.to_s end
+
def inspect() @root.inspect end
end
super()
parse()
end
+
def parse()
@str.each_char {|ch|
char = Character.new(ch)
end
}
end
+
def is_ids?(obj)
return true if "+*".include?(obj.to_s) #テスト用ですかね
return true if obj.is_ids?
return false
end
+
def ids_operator_argc(obj)
return obj.ids_operator_argc if 0 < obj.ids_operator_argc
return 2 #テスト用ってことで
end
+
def check_integrity
r = super
return r if r #不完全がすでにわかっているならreturn
end
end
- class IDS #=========================================IDSそのものを扱うclass
+ class IDS # IDSそのものを扱うclass
def initialize(str) #IDS文字列をうけとる。
@str = str
end
+
def parse
end
+
def parse_x #柔軟型のParse. IDSキャラクターが前にきてなくてもよい。などなど。
end
end
@count = 0
@proc = proc
end
+
def count
@count += 1
if @max <= @count
@proc.call
end
end
- end
+ end
end
require "sgl"
require "singleton"
-$LOAD_PATH << "../../lib" if $0 == __FILE__
require "chise/kageserver"
#こんな感じのフォーマットになっている。
#</svg>
module StrokeFont
- class QuadraticPath #========================動的な分割に対応できるようにする
+ class QuadraticPath # 動的な分割に対応できるようにする
DEFAULT_DIVIDE = 4
+
def initialize(p1, p2, p3)
@p1, @p2, @p3 = p1, p2, p3
@num = DEFAULT_DIVIDE
end
+
def divide_adaptic #適応的分割数をする。
end
+
def divide()
divide_num(@num)
end
+
def divide_num(num) #分割数を指定できる
#p [num]
x1, y1 = @p1
curve << [x,y]
}
#p curve
- return curve
+ curve
end
+
end
class PathResolver
#M 50,540 950,255
#M 330,50 330,900 M 330,900 Q 330,950 380,950 M 380,950 840,950 M 840,950 Q 890,950 915,850
+
def initialize
reset
end
+
def reset
@lines = []
@px, @py = -1, -1
end
+
def parse(str)
reset
cmd = []
exec_cmd(cmd) if 0 < cmd.length #前のコマンドが残っていたら実行
@lines
end
+
def exec_cmd(cmd)
c = cmd.shift #先頭をとる
case c
quadratic([@px, @py], cmd.shift, cmd.shift)
end
end
+
def moveto(x, y)
@lines << [@px, @py, x, y] if @px != -1
@px, @py = x, y
end
+
def quadratic(p1, p2, p3)
#p ["quadratic", p1, p2, p3]
qp = QuadraticPath.new(p1, p2, p3)
moveto(x, y)
}
end
+
end
class KageParser
end
end
- class KageGlyph #================================================== てなかんじ
+ class KageGlyph # てなかんじ
def initialize(code, svg)
@code = code
@svg = svg
@strokes = nil
end
attr_reader :strokes
+
def parse
return if @strokes
@strokes = KageParser.parse(@svg)
end
+
def init
parse if @strokes.nil?
end
@rend.hsv = [6, 100, 100] #オレンジ
end
attr_reader :cache_list, :server
+
def get(code)
return @glyphs[code] if @glyphs[code]
svg = @server.get(code)
return nil if svg.nil?
@glyphs[code] = KageGlyph.new(code, svg)
end
+
def init(code)
glyph = get(code)
return if glyph.nil?
glyph.parse
@rend.set_strokes(glyph.strokes)
end
+
def draw(code)
glyph = get(code)
return if glyph.nil?
@rend.draw
end
+
def print(code)
char = Character.new(@code)
printf("[%s][%04x]\n", char.nil? ? "nil" : char.map_sjis, code)
end
- end
+ end
end
require "singleton"
require "net/http"
require "uri"
-$LOAD_PATH << "../../lib"
require "chise"
require "chise/network" #漢字間接続のネットワークを計算する
#require "chise/kanjilist"
-include CHISE
+#include CHISE
class KageServer
include Singleton
- TYPES = "skeleton mincho gothic".split
+
+ TYPES = %w(skeleton mincho gothic)
SKELETON = 0
MINCHO = 1
GOTHIC = 2
URL = "http://192.168.2.60:5100/"
+
def initialize(url=URL)
@url = url
@glyphs = []
Dir.mkdir(@cache_dir) unless FileTest.directory?(@cache_dir)
end
attr_accessor :url, :use_cache, :offline
- def filename(num, type=SKELETON) sprintf("u%04x.%s", num, TYPES[type]) end
- def cache_file(num, type=SKELETON) @cache_dir+"/"+filename(num, type)+".svg" end
+
+ def filename(num, type=SKELETON)
+ sprintf("u%04x.%s", num, TYPES[type])
+ end
+
+ def cache_file(num, type=SKELETON)
+ @cache_dir+"/"+filename(num, type)+".svg"
+ end
def list_cache
ar = []
end
def get(num, type=SKELETON)
- return open(cache_file(num, type)).read if FileTest.exist?(cache_file(num, type))
+ if FileTest.exist?(cache_file(num, type))
+ return open(cache_file(num, type)) {|f| f.read }
+ end
svg = get_http(num, type)
- return svg if svg
- return nil
+ svg
end
+
def get_http(num, type=SKELETON)
return nil if @offline
+
uri = URI.parse(URL + filename(num, type))
p ["uri", uri.to_s]
+
Net::HTTP.version_1_1 # declear to use 1.1 features.
Net::HTTP.start( uri.host, uri.port ) {|http|
response, body = http.get("/"+filename(num, type)+".svg")
end
end
}
- return nil
+ nil
end
+
def store_cache(num, type, svg)
#p ["store", num]
open(cache_file(num, type), "w") {|f|
f.print svg
}
end
+
def error?(svg)
(svg =~ /<!-- error -->/)
end
+
def read_list
h = {}
open("kage-list.txt"){|f|
}
return h
end
+
def get_all
#error_h = read_list
STDOUT.binmode
}
get_ar(ar)
end
+
def get_ar(ar)
ar.each {|num| #intの数列
char = Character.get(num)
}
}
end
+
def test_kanji
char = "&CDP-8BA5;".de_er
#p char.inspect_all
$LOAD_PATH << "../../lib" if $0 == __FILE__
require "chise"
-include CHISE
require "chise/network"
module CHISE
- class MakeGraph #==================================実務的な処理を引き受ける。
+ class MakeGraph # 実務的な処理を引き受ける。
#GRAPHVIZ_CMD = 0 # CLI
GRAPHVIZ_CMD = 1 # OLE
GRAPHVIZ_TYPE = Graphviz::TWOPI
# Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
-$LOAD_PATH << "../../lib" if $0 == __FILE__
-require "chise"
-include CHISE
+require "chise/char"
require "chise/kanjilist"
require "chise/defkanji"
require "chise/graphviz"
module CHISE
- class KanjiNetwork #=========================漢字間のネットワークを計算する。
+ class KanjiNetwork # 漢字間のネットワークを計算する
def initialize()
reset()
@formatter = GraphvizFormatter.new(self)
# @formatter = XSpringiesFormatter.new(self)
end
attr_reader :h, :list
+
def reset
@h = Hash.new
@done = Hash.new
@list = []
end
+
def make_network(list) #@h, @listに結果を入れていく。
list.each_char {|ch|
make_network_one(ch)
def out(filename)
open(filename, "w"){|out| out.print to_s }
end
+
def to_s() @formatter.to_s; end
end
- class GraphvizFormatter #======================================== Graphviz関係
+ class GraphvizFormatter # Graphviz関係
def initialize(network)
@network = network
end
+
def to_s() #Graphvizのフォーマット、dotフォーマットに変換する。
page = DotPage.new
page.size = "5.5, 5.5"
end
end
- class XSpringiesFormatter #=================================================
+ class XSpringiesFormatter
def initialize(network)
@network = network
end
module CHISE
class CharacterParser
- def parse(c) # parse a value and return a number
- return 0 if c.nil? # what? should raise exception?
+ def parse(c) # parse a value and return a number (MCS)
+ raise "c is nil" if c.nil?
if c.kind_of?(Numeric)
c = 0x80000000 + c if c < 0 # negative value
require "test/unit"
require "chise/char"
require "chise/qp"
-#include CHISE
class String
alias su sjistou8
require "common"
class TestBDB < Test::Unit::TestCase
-#class TestBDB
def test_bdb
@config = CHISE::Config.instance
dir = @config.db_dir
assert_match(%r|/chise/char-db\Z|, dir)
file = dir+"/=jis-x0208/system-char-id"
-# db = BDB::Hash.open(file, nil, BDB::CREATE | BDB::EXCL)
db = BDB::Hash.open(file, nil, 0)
-# qp db
end
def test_db
-# @cdb = CHISE::CharDB.instance
+# @cdb = CHISE::CharDB.instance
# @sdb = CHISE::CodesysDB.instance
end
end
end
-#class TC_DB < Test::Unit::TestCase
-class TestDataBase
- def setup
- @cdb = CHISE::CharDB.instance
- @sdb = CHISE::CodesysDB.instance
- end
-
- def test_db
- assert_equal("()+!", CHISE::DB.unix_to_win("<>*?"))
- assert_equal("<>*?", CHISE::DB.win_to_unix("()+!"))
- end
-
- def test_each_db(db)
- assert_instance_of(Array, db.keys)
- end
-
- def test_make_db(db)
- h = {"a" => 1, "b" => 2, "c" => 3}
- db.remove_db("test-db") #\82Ü\82¸\8dÅ\8f\89\82É\8fÁ\82µ\82Ä\82¨\82
- assert_not_nil(db.make_db("test-db", h))
- assert_not_nil(db.open_db("test-db"))
- assert_equal(1, db.get("test-db", "a"))
- assert_equal(2, db.get("test-db", "b"))
- assert_equal(3, db.get("test-db", "c"))
- db.remove_db("test-db") #\8dÅ\8cã\82É\82Ü\82½\8fÁ\82µ\82Ä\82¨\82
- end
-
- def test_dbs
- test_each_db(@cdb)
- test_each_db(@sdb)
-# test_make_db(@cdb)
-# test_make_db(@sdb)
- end
-
- def test_db_put
- char = "\8e\9a".char
- char.put_char_attribute("test-attribute", "test")
- assert_equal("test", char.test_attribute)
- end
-
-end
-
class TestCodesys < Test::Unit::TestCase
-#class TestCodesys
- def setup
-# @db = CHISE::CodesysDB.instance
+ def nusetup
+ @db = CHISE::CodesysDB.instance
end
def test_dummy
assert_equal(8481, ks.first)
assert_equal(29566, ks.last)
end
-
end