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