split chise.rb to db.rb and ids.rb
[chise/ruby.git] / src / kage.rb
1 #!/usr/bin/env ruby
2 # KageFont library by eto 2003-0311
3
4 require 'sgl'
5 require 'kageserver'
6 require 'singleton'
7
8 #こんな感じのフォーマットになっている。
9 #<?xml version="1.0"?>
10 #<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
11 #<svg viewBox="0 0 1000 1000">
12 #<path d="M 50,540 950,255 " style="fill: none; stroke: black; stroke-width: 10; stroke-linecap: round;"/>
13 #<path d="M 330,50 330,900 M 330,900 Q 330,950 380,950 M 380,950 840,950 M 840,950 Q 890,950 915,850 " style="fill: none; stroke: black; stroke-width: 10; stroke-linecap: round;"/>
14 #</svg>
15
16 module StrokeFont
17   class KageParser #======================================================================
18     def self.parse(svg)
19       @strokes = Strokes.new
20       lines = svg.split(/\n/)
21       lines.each {|line|
22         next unless line =~ /^<path/
23         if line =~ /d=\"([a-zA-Z0-9, ]+)\"/
24           KageParser.parse_path($1)
25         end
26       }
27       return @strokes
28     end
29     def self.parse_path(str)
30       #M 50,540 950,255 
31       #M 330,50 330,900 M 330,900 Q 330,950 380,950 M 380,950 840,950 M 840,950 Q 890,950 915,850 
32       px, py = -1, -1
33       str.split.each {|par|
34         next if par.length == 1 #一文字はとばします。本来はちゃんとベジェを展開する。
35         if par =~ /[,0-9]+/
36           sx, sy = par.split(/,/)
37           x, y = sx.to_i, (1000 - sy.to_i)
38           if px != -1
39             @strokes.add_line(px, py, x, y)
40           end
41           px, py = x, y
42         end
43       }
44     end
45   end
46
47   class KageGlyph #====================================================================== てなかんじ
48     def initialize(code, svg)
49       @code = code
50       @svg = svg
51       @strokes = nil
52     end
53     attr_reader :strokes
54     def parse
55       return if @strokes
56       @strokes = KageParser.parse(@svg)
57     end
58     def init
59       parse if @strokes.nil?
60     end
61   end
62
63   class KageFont #======================================================================
64     def initialize
65       @server = KageServer.instance
66       @glyphs = []
67       @cache_list = @server.list_cache
68       @rend = nil
69       @rend = StrokesRenderer.new
70       #@rend.hsv = [13, 100, 100]
71       @rend.hsv = [0, 0, 100]
72     end
73     attr_reader :cache_list, :server
74     def get(code)
75       return @glyphs[code] if @glyphs[code]
76       svg = @server.get(code)
77       return nil if svg.nil?
78       @glyphs[code] = KageGlyph.new(code, svg)
79     end
80     def init(code)
81       glyph = get(code)
82       return if glyph.nil?
83       glyph.init
84       glyph.parse
85       @rend.set_strokes(glyph.strokes)
86     end
87     def draw(code)
88       glyph = get(code)
89       return if glyph.nil?
90       @rend.draw
91     end
92     def print(code)
93       char = Character.new(@code)
94       printf("[%s][%04x]\n", char.nil? ? "nil" : char.map_sjis, code)
95     end
96   end
97
98 end
99
100 #----------------------------------------------------------------------end.