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