move file to chise/
[chise/ruby.git] / src / csf.rb
1 #!/usr/bin/env ruby
2 #
3 # CSF module by eto 2002-1115
4 # Copyright (C) 2002-2003 Kouichirou Eto
5 #     All rights reserved.
6 #     This is free software with ABSOLUTELY NO WARRANTY.
7 #
8 # You can redistribute it and/or modify it under the terms of 
9 # the GNU General Public License version 2.
10 #
11
12 require 'sgl'
13 require 'kconv'
14 require 'uconv'
15
16 module StrokeFont
17   CSF_FONT_DIR = 'd:/work/chise/csf/'
18   CSF_DEFAULT_FILE  = 'KST32B.CSF1'
19   CSF_KOUKOTSU_FILE = 'KST32ZX.CSF1'
20
21   class CSFStrokeMaker #======================================================================
22     DEST_WIDTH = 1000 #\91å\82«\82³\82ð\82¨\82¨\82æ\82»1000x1000\82É\90³\8bK\89»\82·\82é\81B
23     ORG_WIDTH = 32 #\8c³\82Ì\83T\83C\83Y\82Í\81A\89¡30\81~\8fc32
24     def initialize
25       @x, @y, @nx, @ny = 0, 0, 0, 0
26       @strokes = Strokes.new
27     end
28     attr_reader :strokes
29     def move_to_x(x) @x = x; @nx = x; end
30     def draw_to_x(x) @nx = x; drawline; @x = @nx; @y = @ny; end
31     def next_x_to(x) @nx = x; end
32     def move_to_y(y) @y = y; @ny = y; end
33     def draw_to_y(y) @ny = y; drawline; @x = @nx; @y = @ny; end
34     def drawline()
35       @strokes.add_line(t(@x), t(@y), t(@nx), t(@ny))
36     end
37     def t(a) a*DEST_WIDTH/ORG_WIDTH; end
38   end
39
40   class CSFParser #======================================================================
41     def self.parse(str) #Strokes\82ð\95Ô\82·
42       return Strokes.new if str == nil
43       sm = CSFStrokeMaker.new
44       (0...str.length).each {|i|
45         n = str[i]
46         if 0x21 <= n && n <= 0x26
47           sm.move_to_x(n - 0x21)
48         elsif 0x28 <= n && n <= 0x3f
49           sm.move_to_x(n - 0x28 + 6)
50         elsif 0x40 <= n && n <= 0x5b
51           sm.draw_to_x(n - 0x40)
52         elsif 0x5e <= n && n <= 0x5f
53           sm.draw_to_x(n - 0x5e + 28)
54         elsif 0x60 <= n && n <= 0x7d
55           sm.next_x_to(n - 0x60)
56         elsif 0x7e == n
57           sm.move_to_y(n - 0x7e)
58         elsif 0xa1 <= n && n <= 0xbf
59           sm.move_to_y(n - 0xa1 + 1)
60         elsif 0xc0 <= n && n <= 0xdf
61           sm.draw_to_y(n - 0xc0)
62         end
63       }
64       return sm.strokes
65     end
66   end
67
68   class CSFGlyph #======================================================================
69     def initialize(code, stroke)
70       @code = code
71       @stroke_str = stroke
72       @strokes = nil
73     end
74     attr_reader :strokes
75     def parse()
76       return if @strokes
77       @strokes = CSFParser.parse(@stroke_str)
78     end
79     def init
80       parse if @strokes.nil?
81     end
82   end
83
84   class CSFFont #======================================================================
85     def initialize(file=CSF_DEFAULT_FILE)
86       @file = CSF_FONT_DIR + file
87       @glyphs = []
88       read_file
89       @rend = nil
90       @rend = StrokesRenderer.new
91       @rend.hsv = [50, 100, 100]
92     end
93     def read_file()
94       open(@file) {|f|
95         while(line = f.gets)
96           next if line =~ /^\*/
97           c, s = line.split
98           code = c.hex #JIS\82Ì\92l\82ª\90\94\92l\82Å\82Í\82¢\82é
99           @glyphs[code] = CSFGlyph.new(code, s)
100         end
101       }
102     end
103     def init(code)
104       glyph = @glyphs[code]
105       return if glyph == nil
106       glyph.init()
107       glyph.parse
108       @rend.set_strokes(glyph.strokes)
109     end
110     def draw(code) #\88ø\90\94\82É\82ÍJIS\82ð\90\94\92l\89»\82µ\82½\82à\82Ì\82ª\82Í\82¢\82é
111       glyph = @glyphs[code]
112       return if glyph == nil
113       @rend.draw
114     end
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     def ucs_to_jis(ucs)
121       char = Character.get(ucs)
122       j = char.japanese_jisx0208
123       return j
124     end
125   end
126
127 end
128
129 #----------------------------------------------------------------------\8fI\97¹