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