png_font 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt ADDED
@@ -0,0 +1,8 @@
1
+ Manifest.txt
2
+ Rakefile
3
+ README.txt
4
+ lib/png_font.rb
5
+ lib/font/p04b_03.rb
6
+ test/png_fonts_test_case.rb
7
+ test/test_png_fonts.rb
8
+ test/output
data/README.txt ADDED
@@ -0,0 +1,10 @@
1
+ PNG Font
2
+ ========
3
+
4
+ EXPERIMENTAL
5
+
6
+ A very simple pixel font for use with the PNG gem.
7
+
8
+ == Author
9
+
10
+ Geoffrey Grosenbach http://nubyonrails.com
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+ $:.unshift(File.dirname(__FILE__) + "/lib")
4
+ require 'png_font'
5
+
6
+ Hoe.new('PNGFont', PNG::Font::VERSION) do |p|
7
+ p.name = "png_font"
8
+ p.author = "Geoffrey Grosenbach"
9
+ p.description = "Simple fonts for use with the pure Ruby PNG library."
10
+ p.email = 'boss@topfunky.com'
11
+ p.summary = "Simple fonts."
12
+ p.url = "http://nubyonrails.com/pages/png_font"
13
+ p.clean_globs = ['test/output/*.png']
14
+
15
+ p.extra_deps = ['png']
16
+ end
@@ -0,0 +1,189 @@
1
+ ##
2
+ # Shamelessly looted from the wonderful freeware font 04B-03.
3
+ #
4
+ # http://www.dsg4.com/04/extra/bitmap/
5
+
6
+ class PNG
7
+ module Font
8
+ module P04B_03
9
+
10
+ @@glyphs = []
11
+ @@glyphs['A'[0]] = [
12
+ [[0,2],[0,5]], # L side
13
+ [[3,2],[3,5]], # R side
14
+ [[1,1],[2,1]], # T
15
+ [[0,4],[3,4]] # Bar
16
+ ]
17
+ @@glyphs['B'[0]] = [
18
+ [[0,1],[0,5]], # L
19
+ [[0,1],[2,1]], # T
20
+ [[0,3],[2,3]], # M bar
21
+ [[0,5],[2,5]], # B
22
+ [[3,2],[3,2]], # TR of B
23
+ [[3,4],[3,4]], # TB of B
24
+ ]
25
+ @@glyphs['C'[0]] = [
26
+ [[0,2],[0,4]], # L side
27
+ [[1,1],[2,1]], # T
28
+ [[1,5],[2,5]] # B
29
+ ]
30
+ @@glyphs['D'[0]] = [
31
+ [[0,1],[0,5]], # L side
32
+ [[0,1],[2,1]], # Top
33
+ [[3,2],[3,4]], # R
34
+ [[0,5],[2,5]] # Bottom
35
+ ]
36
+ @@glyphs['E'[0]] = [
37
+ [[0,1],[0,5]], # L side
38
+ [[0,1],[2,1]], # Top
39
+ [[0,3],[2,3]], # Middle bar
40
+ [[0,5],[2,5]] # Bottom
41
+ ]
42
+ @@glyphs['F'[0]] = [
43
+ [[0,1],[0,5]], # L side
44
+ [[0,1],[2,1]], # Top
45
+ [[0,3],[2,3]] # Middle bar
46
+ ]
47
+ @@glyphs['G'[0]] = [
48
+ [[0,2],[0,4]], # L side
49
+ [[1,1],[3,1]], # T
50
+ [[1,5],[3,5]], # B
51
+ [[3,3],[3,5]], # R
52
+ [[2,3],[3,3]] # Handle of G
53
+ ]
54
+ @@glyphs['H'[0]] = [
55
+ [[0,1],[0,5]], # L side
56
+ [[3,1],[3,5]], # R side
57
+ [[0,3],[3,3]] # Bar
58
+ ]
59
+ @@glyphs['I'[0]] = [
60
+ [[1,1],[1,5]], # M vert bar
61
+ [[0,1],[2,1]], # Top
62
+ [[0,5],[2,5]] # Bottom
63
+ ]
64
+ @@glyphs['J'[0]] = [
65
+ [[2,1],[3,1]], # T
66
+ [[3,1],[3,4]], # R
67
+ [[0,4],[0,4]], # L
68
+ [[1,5],[2,5]] # B
69
+ ]
70
+ @@glyphs['K'[0]] = [
71
+ [[0,1],[0,5]], # L
72
+ [[2,2],[2,2]], # T Angle HACK
73
+ [[3,1],[3,1]], # T Angle HACK
74
+ [[1,3],[3,5]] # B
75
+ ]
76
+ @@glyphs['L'[0]] = [
77
+ [[0,1],[0,5]], # L vert bar
78
+ [[0,5],[2,5]] # Bottom
79
+ ]
80
+ @@glyphs['M'[0]] = [
81
+ [[0,1],[0,5]], # L vert bar
82
+ [[0,1],[2,3]], # L angle
83
+ [[3,2],[3,2]], # Dot on up angle
84
+ [[4,1],[4,5]] # R vert bar
85
+ ]
86
+ @@glyphs['N'[0]] = [
87
+ [[0,1],[0,5]], # L vert bar
88
+ [[0,1],[2,3]], # L Angle
89
+ [[3,1],[3,5]] # R vert bar
90
+ ]
91
+ @@glyphs['O'[0]] = [
92
+ [[0,2],[0,4]], # L
93
+ [[1,1],[2,1]], # T
94
+ [[3,2],[3,4]], # R
95
+ [[1,5],[2,5]] # B
96
+ ]
97
+ @@glyphs['P'[0]] = [
98
+ [[0,1],[0,5]], # L
99
+ [[1,1],[2,1]], # T
100
+ [[3,2],[3,3]], # R
101
+ [[1,4],[2,4]] # B
102
+ ]
103
+ @@glyphs['Q'[0]] = [
104
+ [[0,2],[0,4]], # L
105
+ [[1,1],[2,1]], # T
106
+ [[3,2],[3,4]], # R
107
+ [[1,5],[2,5]], # B
108
+ [[3,6],[3,6]] # Foot
109
+ ]
110
+ @@glyphs['R'[0]] = [
111
+ [[0,1],[0,5]], # L
112
+ [[1,1],[2,1]], # T
113
+ [[3,2],[3,3]], # R
114
+ [[1,4],[2,4]], # B
115
+ [[3,5],[3,5]] # Foot
116
+ ]
117
+ @@glyphs['S'[0]] = [
118
+ [[1,1],[3,1]], # Top
119
+ [[0,2],[0,2]], # L
120
+ [[1,3],[2,3]], # Middle
121
+ [[3,4],[3,4]], # R
122
+ [[0,5],[2,5]] # Bottom
123
+ ]
124
+ @@glyphs['T'[0]] = [
125
+ [[1,1],[1,5]], # M vert bar
126
+ [[0,1],[2,1]] # Top
127
+ ]
128
+ @@glyphs['U'[0]] = [
129
+ [[0,1],[0,4]], # L
130
+ [[3,1],[3,4]], # R
131
+ [[1,5],[2,5]] # B
132
+ ]
133
+ @@glyphs['V'[0]] = [
134
+ [[0,1],[0,4]], # L
135
+ [[2,1],[2,4]], # R
136
+ [[1,5],[1,5]] # B
137
+ ]
138
+ @@glyphs['W'[0]] = [
139
+ [[0,1],[0,4]], # L vert bar
140
+ [[2,2],[2,4]], # M vert
141
+ [[1,5],[1,5]], # low tick
142
+ [[3,5],[3,5]], # low tick
143
+ [[4,1],[4,4]] # R vert bar
144
+ ]
145
+ @@glyphs['X'[0]] = [
146
+ [[0,1],[0,2]], # L vert bar
147
+ [[0,4],[0,5]], # L vert bar
148
+ [[1,3],[2,3]], # horiz bar
149
+ [[3,1],[3,2]], # R vert bar
150
+ [[3,4],[3,5]] # R vert bar
151
+ ]
152
+ @@glyphs['Y'[0]] = [
153
+ [[0,1],[0,2]], # L
154
+ [[3,1],[3,4]], # R
155
+ [[1,3],[2,3]], # horiz bar
156
+ [[1,5],[2,5]] # B
157
+ ]
158
+ @@glyphs['Z'[0]] = [
159
+ [[0,1],[2,1]], # Top
160
+ [[0,4],[0,4]], # M dot
161
+ [[1,3],[1,3]], # M dot
162
+ [[2,2],[2,2]], # M dot
163
+ [[0,5],[2,5]] # Bottom
164
+ ]
165
+
166
+ @@glyphs['.'[0]] = [
167
+ [[0,5],[0,5]]
168
+ ]
169
+
170
+
171
+ # TODO Numbers and punctuation
172
+
173
+ ##
174
+ # A reference to all the characters in the font.
175
+
176
+ def self.glyphs
177
+ @@glyphs
178
+ end
179
+
180
+ ##
181
+ # Defines the space between the letters.
182
+
183
+ def self.tracking
184
+ 2
185
+ end
186
+
187
+ end
188
+ end
189
+ end
data/lib/png_font.rb ADDED
@@ -0,0 +1,47 @@
1
+ # require 'rubygems'
2
+ require 'png'
3
+
4
+ require 'font/p04b_03'
5
+
6
+ class PNG
7
+ module Font
8
+ VERSION = '0.0.1'
9
+
10
+ def self.character_width(ascii_char, pointsize, font)
11
+ # TODO Take pointsize into account
12
+ rightmost_x = 0
13
+ font.glyphs[ascii_char].each do |point_set|
14
+ rightmost_x = [rightmost_x, point_set[0][0], point_set[1][0]].max
15
+ end
16
+ rightmost_x
17
+ rescue # DEBUG ONLY
18
+ return 0 # nothing...for development only
19
+ end
20
+ end
21
+ end
22
+
23
+ class PNG::Canvas
24
+
25
+ def annotate(x, y, string, pointsize=8, font=PNG::Font::P04B_03, color=PNG::Color::Black)
26
+ x_offset = 0
27
+ string.each_byte do |byte|
28
+ character(x + x_offset, y, byte, pointsize, font, color)
29
+ x_offset += PNG::Font.character_width(byte, pointsize, font) + font.tracking
30
+ end
31
+ end
32
+
33
+ protected
34
+
35
+ ##
36
+ # For internal use only. Use +annotate+ to draw a single character or a string.
37
+
38
+ def character(x, y, ascii_char, pointsize, font, color)
39
+ # TODO Multiply coordinates by pointsize
40
+ font.glyphs[ascii_char].each do |point_set|
41
+ line point_set[0][0] + x, point_set[0][1] + y, point_set[1][0] + x, point_set[1][1] + y, color
42
+ end
43
+ rescue # DEBUG ONLY
44
+ # nothing...for development only
45
+ end
46
+
47
+ end
@@ -0,0 +1,41 @@
1
+ $:.unshift(File.dirname(__FILE__) + "/../lib/")
2
+
3
+ require 'test/unit'
4
+ require 'png_font'
5
+ require 'fileutils'
6
+
7
+ class PNGFontsTestCase < Test::Unit::TestCase
8
+
9
+ def setup
10
+ FileUtils.mkdir_p File.dirname(__FILE__) + "/output"
11
+ end
12
+
13
+ def test_dummy
14
+ assert true
15
+ end
16
+
17
+ protected
18
+
19
+ def write_test_file(canvas, filename)
20
+ png = PNG.new canvas
21
+ png.save(File.dirname(__FILE__) + "/output/#{filename}.png")
22
+ end
23
+
24
+ def setup_canvas(width, height)
25
+ canvas = PNG::Canvas.new width, height
26
+ end
27
+
28
+ ##
29
+ # Utility for making a canvas and writing it to disk.
30
+ #
31
+ # canvas('basic', 200) do |c|
32
+ # c.line 50, 50, 100, 50, PNG::Color::Blue
33
+ # end
34
+ #
35
+ def canvas(filename, width=200, height=width)
36
+ c = setup_canvas(width, height)
37
+ yield c
38
+ write_test_file c, filename
39
+ end
40
+
41
+ end
@@ -0,0 +1,18 @@
1
+ require File.dirname(__FILE__) + "/png_fonts_test_case"
2
+
3
+ class PNGFontsTest < PNGFontsTestCase
4
+
5
+ def test_A
6
+ canvas('A', 150, 50) do |c|
7
+ # c.line 0,20,20,0, PNG::Color::Red
8
+
9
+ c.annotate(1,1,"ABCDEFGHIJKLMNOPQRSTUVWXYZ")
10
+ c.annotate(1,21,"A TINY FONT USING RUBY PNG.")
11
+ # c.annotate(1,11,"abcdefghijklmnopqrstuvwxyz")
12
+
13
+ c.annotate(1,31,"LOWERCASE AND PUNCTUATION")
14
+ c.annotate(70,41,"COMING SOON...")
15
+ end
16
+ end
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: png_font
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2006-10-19 00:00:00 -07:00
8
+ summary: Simple fonts.
9
+ require_paths:
10
+ - lib
11
+ - test
12
+ email: boss@topfunky.com
13
+ homepage: http://nubyonrails.com/pages/png_font
14
+ rubyforge_project: pngfont
15
+ description: Simple fonts for use with the pure Ruby PNG library.
16
+ autorequire:
17
+ default_executable:
18
+ bindir: bin
19
+ has_rdoc: true
20
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
21
+ requirements:
22
+ - - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ signing_key:
28
+ cert_chain:
29
+ post_install_message:
30
+ authors:
31
+ - Geoffrey Grosenbach
32
+ files:
33
+ - Manifest.txt
34
+ - Rakefile
35
+ - README.txt
36
+ - lib/png_font.rb
37
+ - lib/font/p04b_03.rb
38
+ - test/png_fonts_test_case.rb
39
+ - test/test_png_fonts.rb
40
+ - test/output
41
+ test_files: []
42
+
43
+ rdoc_options: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ requirements: []
52
+
53
+ dependencies:
54
+ - !ruby/object:Gem::Dependency
55
+ name: png
56
+ version_requirement:
57
+ version_requirements: !ruby/object:Gem::Version::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.0.0
62
+ version: