i
[chise/ruby.git] / chise / csf.rb
1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2
3 require "sgl"
4 #require "kconv"
5 require "uconv"
6 require "chise/config"
7
8 module StrokeFont
9   CSF_FONT_DIR = Config.instance.csf_dir+"/"
10   CSF_DEFAULT_FILE  = "KST32B.CSF1"
11   CSF_KOUKOTSU_FILE = "KST32ZX.CSF1"
12
13   class CSFStrokeMaker
14     DEST_WIDTH = 1000 #\91å\82«\82³\82ð\82¨\82¨\82æ\82»1000x1000\82É\90³\8bK\89»\82·\82é\81B
15     ORG_WIDTH = 32 #\8c³\82Ì\83T\83C\83Y\82Í\81A\89¡30\81~\8fc32
16     def initialize
17       @x, @y, @nx, @ny = 0, 0, 0, 0
18       @strokes = Strokes.new
19     end
20     attr_reader :strokes
21     def move_to_x(x) @x = x; @nx = x; end
22     def draw_to_x(x) @nx = x; drawline; @x = @nx; @y = @ny; end
23     def next_x_to(x) @nx = x; end
24     def move_to_y(y) @y = y; @ny = y; end
25     def draw_to_y(y) @ny = y; drawline; @x = @nx; @y = @ny; end
26     def drawline()
27       @strokes.add_line(t(@x), t(@y), t(@nx), t(@ny))
28     end
29     def t(a) a*DEST_WIDTH/ORG_WIDTH; end
30   end
31
32   class CSFParser
33     def self.parse(str) #Strokes\82ð\95Ô\82·
34       return Strokes.new if str == nil
35       sm = CSFStrokeMaker.new
36       (0...str.length).each {|i|
37         n = str[i]
38         if 0x21 <= n && n <= 0x26
39           sm.move_to_x(n - 0x21)
40         elsif 0x28 <= n && n <= 0x3f
41           sm.move_to_x(n - 0x28 + 6)
42         elsif 0x40 <= n && n <= 0x5b
43           sm.draw_to_x(n - 0x40)
44         elsif 0x5e <= n && n <= 0x5f
45           sm.draw_to_x(n - 0x5e + 28)
46         elsif 0x60 <= n && n <= 0x7d
47           sm.next_x_to(n - 0x60)
48         elsif 0x7e == n
49           sm.move_to_y(n - 0x7e)
50         elsif 0xa1 <= n && n <= 0xbf
51           sm.move_to_y(n - 0xa1 + 1)
52         elsif 0xc0 <= n && n <= 0xdf
53           sm.draw_to_y(n - 0xc0)
54         end
55       }
56       return sm.strokes
57     end
58   end
59
60   class CSFGlyph
61     def initialize(code, stroke)
62       @code = code
63       @stroke_str = stroke
64       @strokes = nil
65     end
66     attr_reader :strokes
67     def parse()
68       return if @strokes
69       @strokes = CSFParser.parse(@stroke_str)
70     end
71     def init
72       parse if @strokes.nil?
73     end
74   end
75
76   class CSFFont
77     def initialize(file=CSF_DEFAULT_FILE)
78       @file = CSF_FONT_DIR + file
79       @glyphs = []
80       read_file
81       @rend = nil
82       @rend = StrokesRenderer.new
83       @rend.hsv = [50, 100, 100]
84     end
85     def read_file()
86       open(@file) {|f|
87         while(line = f.gets)
88           next if line =~ /^\*/
89           c, s = line.split
90           code = c.hex #JIS\82Ì\92l\82ª\90\94\92l\82Å\82Í\82¢\82é
91           @glyphs[code] = CSFGlyph.new(code, s)
92         end
93       }
94     end
95     def init(code)
96       glyph = @glyphs[code]
97       return if glyph == nil
98       glyph.init()
99       glyph.parse
100       @rend.set_strokes(glyph.strokes)
101     end
102     def draw(code) #\88ø\90\94\82É\82ÍJIS\82ð\90\94\92l\89»\82µ\82½\82à\82Ì\82ª\82Í\82¢\82é
103       glyph = @glyphs[code]
104       return if glyph == nil
105       @rend.draw
106     end
107     def print(code)
108       jis = JISX0208.new
109       char = jis.get_char(code)
110       printf("[%s][%04x]\n", char.nil? ? "nil" : char.map_sjis, code)
111     end
112     def ucs_to_jis(ucs)
113       char = Character.get(ucs)
114       j = char.japanese_jisx0208
115       return j
116     end
117   end
118
119 end