hershey 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/hershey +57 -5
- data/lib/hershey.rb +11 -0
- data/lib/hershey/document.rb +23 -32
- data/lib/hershey/page.rb +49 -5
- data/lib/hershey/version.rb +1 -1
- data/spec/ex.svg +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b24545d2a0e0d007d513ce0503af4b8629054a4
|
4
|
+
data.tar.gz: e5a24d52e8c171e2fa92f4d98ed39cb13378c84d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4226c797de0ce2a83db348fdb6beb88b959750b54c40137b8445540090ad9f8b83e9e9e3221bd2ba6ef4f3fec1f38efbe957f809e08a2c735c2dec247b773cb
|
7
|
+
data.tar.gz: 1098108380a568961ca20afb29a9c0daf080077ce881725aedc4b8c230195a77f41862fae35e614d32539a3c0eb7d09e2d306dab22b052eb56402dd6dc60921b
|
data/bin/hershey
CHANGED
@@ -1,9 +1,61 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
1
|
+
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require 'hershey'
|
4
|
+
require 'optparse'
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
options = {}
|
7
|
+
OptionParser.new do |opts|
|
8
|
+
opts.banner = "Usage: hershey [options]"
|
8
9
|
|
9
|
-
|
10
|
+
opts.on("-w", "--width WIDTH", Integer, "Width of the pages") do |vars|
|
11
|
+
options[:width] = vars
|
12
|
+
end
|
13
|
+
|
14
|
+
opts.on("-h", "--height HEIGHT", Integer, "The height per page (defaults to 8.5x11 ratio)") do |f|
|
15
|
+
options[:height] = f
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
opts.on("-i", "--input INFILE", "The input file (required)") do |f|
|
20
|
+
options[:input] = f
|
21
|
+
end
|
22
|
+
|
23
|
+
opts.on("-o", "--output OUTFILE", "The output file base name (required)") do |f|
|
24
|
+
options[:output] = f
|
25
|
+
end
|
26
|
+
|
27
|
+
options[:max] = 100
|
28
|
+
opts.on("-m", "--max MAX", Integer, "Max pages") do |range|
|
29
|
+
options[:max] = range
|
30
|
+
end
|
31
|
+
|
32
|
+
opts.on("-o", "--output OUTFILE", "The output file base name") do |f|
|
33
|
+
options[:output] = f
|
34
|
+
end
|
35
|
+
|
36
|
+
opts.on("-f", "--font FONT", Hershey::FONTS.keys, "The font to use") do |f|
|
37
|
+
options[:font] = f.downcase
|
38
|
+
end
|
39
|
+
opts.separator("Fonts: #{Hershey::FONTS.keys.map {|k| k.to_s}.join(', ')}")
|
40
|
+
|
41
|
+
opts.on("--help", "Shows this help message") do
|
42
|
+
puts opts
|
43
|
+
exit
|
44
|
+
end
|
45
|
+
end.parse!
|
46
|
+
|
47
|
+
unless options[:width].is_a?(Integer)
|
48
|
+
puts "Missing number of variables for equation"
|
49
|
+
puts "Call with -h or --help for help"
|
50
|
+
exit(1)
|
51
|
+
end
|
52
|
+
|
53
|
+
options[:height] ||= (options[:width] / 8.5 * 11 / 20).ceil * 20
|
54
|
+
|
55
|
+
pages = Hershey.svgs(File.read(options[:input]), options.select{|k,_| [:width,:height,:font].include?(k)})
|
56
|
+
|
57
|
+
pages.each_with_index do |page, i|
|
58
|
+
f = "#{options[:output]}_#{i}.svg"
|
59
|
+
puts "#{f}: #{page.each_char.inject(0) {|m,s| s == 'L' ? m + 1 : m}} line segments."
|
60
|
+
open(f, 'w') {|f| f.write(page)}
|
61
|
+
end
|
data/lib/hershey.rb
CHANGED
@@ -6,9 +6,20 @@ require_relative "hershey/version"
|
|
6
6
|
require_relative "hershey/word"
|
7
7
|
|
8
8
|
module Hershey
|
9
|
+
def self.new(options = {})
|
10
|
+
doc = Document.new(options)
|
11
|
+
end
|
12
|
+
|
9
13
|
def self.svg(text, options = {})
|
10
14
|
doc = Document.new(options)
|
11
15
|
doc.write(text)
|
12
16
|
doc.svg
|
13
17
|
end
|
18
|
+
|
19
|
+
def self.svgs(text, options = {})
|
20
|
+
puts options
|
21
|
+
doc = Document.new(options)
|
22
|
+
doc.write(text)
|
23
|
+
doc.svgs
|
24
|
+
end
|
14
25
|
end
|
data/lib/hershey/document.rb
CHANGED
@@ -4,7 +4,7 @@ module Hershey
|
|
4
4
|
SIDE = 10.freeze
|
5
5
|
HEIGHT_STRING = "jfdsklahfdjksjhaljfdskahj".freeze
|
6
6
|
|
7
|
-
def initialize(width:
|
7
|
+
def initialize(width: 1000, height: 1300, spacing: 3, vertical: 20, font: :futural, stroke: 1)
|
8
8
|
@words = []
|
9
9
|
@pages = []
|
10
10
|
@width = width
|
@@ -14,13 +14,15 @@ module Hershey
|
|
14
14
|
@vertical = vertical
|
15
15
|
@closed = false
|
16
16
|
@font = font
|
17
|
-
@
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
17
|
+
@stroke = stroke
|
18
|
+
end
|
19
|
+
|
20
|
+
def [](page)
|
21
|
+
@pages[page].svg
|
22
|
+
end
|
23
|
+
|
24
|
+
def length
|
25
|
+
@pages.length
|
24
26
|
end
|
25
27
|
|
26
28
|
def <<(text)
|
@@ -45,41 +47,30 @@ module Hershey
|
|
45
47
|
|
46
48
|
def svg
|
47
49
|
write_out unless @closed
|
48
|
-
|
50
|
+
self[0]
|
51
|
+
end
|
52
|
+
|
53
|
+
def svgs
|
54
|
+
write_out unless @closed
|
55
|
+
@pages.map {|page| page.svg}
|
49
56
|
end
|
50
57
|
|
51
58
|
private
|
52
59
|
|
53
60
|
def write_out
|
54
|
-
|
55
|
-
space = Character.new(' ').spacing
|
56
|
-
current_offset = SIDE
|
57
|
-
@svg << %Q{<g transform="translate(#{SIDE},#{@line})">}
|
61
|
+
new_page
|
58
62
|
@words.each do |word|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
end
|
65
|
-
@svg << word.to_path(current_offset)
|
66
|
-
current_offset += word.spacing
|
67
|
-
elsif word == :space
|
68
|
-
current_offset += space
|
69
|
-
elsif word == :break
|
70
|
-
@line += BUFFER * 2
|
71
|
-
current_offset = SIDE
|
72
|
-
@svg << %Q{</g><g transform="translate(#{SIDE},#{@line})">}
|
63
|
+
begin
|
64
|
+
@pages.last.write(word)
|
65
|
+
rescue PageFullError => e
|
66
|
+
new_page
|
67
|
+
retry
|
73
68
|
end
|
74
69
|
end
|
75
|
-
@svg << "</g></svg>"
|
76
|
-
@svg.gsub!(HEIGHT_STRING, (@line + BUFFER).to_s)
|
77
|
-
@closed = true
|
78
70
|
end
|
79
71
|
|
80
72
|
def new_page
|
81
|
-
@pages << Page.new(width: @width, height: @height)
|
82
|
-
@line = BUFFER
|
73
|
+
@pages << Page.new(width: @width, height: @height, stroke: @stroke)
|
83
74
|
end
|
84
75
|
end
|
85
76
|
end
|
data/lib/hershey/page.rb
CHANGED
@@ -1,22 +1,66 @@
|
|
1
1
|
module Hershey
|
2
2
|
class Page
|
3
|
-
|
4
|
-
|
3
|
+
SPACE = Character.new(' ').spacing.freeze
|
4
|
+
BUFFER = 20.freeze
|
5
|
+
SIDE = 10.freeze
|
6
|
+
HEIGHT_STRING = "jfdsklahfdjksjhaljfdskahj".freeze
|
7
|
+
|
8
|
+
def initialize(width: 1000, height: 1300, stroke: 1)
|
5
9
|
@close = false
|
10
|
+
@height = height
|
11
|
+
@width = width
|
12
|
+
@current_offset = SIDE
|
13
|
+
@line = BUFFER
|
14
|
+
@svg = <<-HEADER
|
15
|
+
<?xml version="1.0" standalone="no"?>
|
16
|
+
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
|
17
|
+
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
18
|
+
<svg width="#{@width}" height="#{HEIGHT_STRING}" version="1.1"
|
19
|
+
xmlns="http://www.w3.org/2000/svg" style="stroke-width:#{stroke};">
|
20
|
+
HEADER
|
21
|
+
@svg << %Q{<g transform="translate(#{SIDE},#{@line})">}
|
6
22
|
end
|
7
23
|
|
8
24
|
def <<(word)
|
9
|
-
|
25
|
+
if word.is_a?(Word)
|
26
|
+
if word.spacing + @current_offset > @width
|
27
|
+
new_line
|
28
|
+
end
|
29
|
+
@svg << word.to_path(@current_offset)
|
30
|
+
@current_offset += word.spacing
|
31
|
+
elsif word == :space
|
32
|
+
@current_offset += SPACE
|
33
|
+
elsif word == :break
|
34
|
+
new_line
|
35
|
+
end
|
10
36
|
end
|
11
37
|
alias_method :write, :<<
|
12
38
|
|
13
39
|
def write_out
|
14
40
|
close
|
15
|
-
@
|
41
|
+
@svg
|
42
|
+
end
|
43
|
+
alias_method :svg, :write_out
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def new_line
|
48
|
+
@line += BUFFER * 2
|
49
|
+
if @line > @height
|
50
|
+
@line -= BUFFER
|
51
|
+
raise PageFullError, "The page is full"
|
52
|
+
else
|
53
|
+
@current_offset = SIDE
|
54
|
+
@svg << %Q{</g><g transform="translate(#{SIDE},#{@line})">}
|
55
|
+
end
|
16
56
|
end
|
17
57
|
|
18
58
|
def close
|
19
|
-
|
59
|
+
unless @close
|
60
|
+
@svg << "</g></svg>"
|
61
|
+
@svg.gsub!(HEIGHT_STRING, (@line + BUFFER).to_s)
|
62
|
+
@close = true
|
63
|
+
end
|
20
64
|
end
|
21
65
|
end
|
22
66
|
|
data/lib/hershey/version.rb
CHANGED
data/spec/ex.svg
CHANGED
@@ -2,5 +2,5 @@
|
|
2
2
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
|
3
3
|
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
4
4
|
<svg width="500" height="40" version="1.1"
|
5
|
-
xmlns="http://www.w3.org/2000/svg">
|
5
|
+
xmlns="http://www.w3.org/2000/svg" style="stroke-width:1;">
|
6
6
|
<g transform="translate(10,20)"><g transform="translate(10,0)"><path stroke="black" fill="none" d="M0 -12 L0 9" transform="translate(4,0)"></path><path stroke="black" fill="none" d="M0 -12 L0 5 L1 8 L3 9 L5 9 M-3 -5 L4 -5" transform="translate(13,0)"></path><path stroke="black" fill="none" d="M0 -10 L-1 -11 L0 -12 L1 -11 L1 -9 L0 -7 L-1 -6" transform="translate(25,0)"></path><path stroke="black" fill="none" d="M6 -2 L5 -4 L2 -5 L-1 -5 L-4 -4 L-5 -2 L-4 0 L-2 1 L3 2 L5 3 L6 5 L6 6 L5 8 L2 9 L-1 9 L-4 8 L-5 6" transform="translate(38,0)"></path></g><g transform="translate(73,0)"><path stroke="black" fill="none" d="M6 -5 L6 9 M6 -2 L4 -4 L2 -5 L-1 -5 L-3 -4 L-5 -2 L-6 1 L-6 3 L-5 6 L-3 8 L-1 9 L2 9 L4 8 L6 6" transform="translate(9,0)"></path><path stroke="black" fill="none" d="M0 -12 L0 9" transform="translate(23,0)"></path><path stroke="black" fill="none" d="M0 -12 L0 9" transform="translate(31,0)"></path></g><g transform="translate(124,0)"><path stroke="black" fill="none" d="M-2 -12 L-4 -11 L-6 -9 L-7 -7 L-8 -4 L-8 1 L-7 4 L-6 6 L-4 8 L-2 9 L2 9 L4 8 L6 6 L7 4 L8 1 L8 -4 L7 -7 L6 -9 L4 -11 L2 -12 L-2 -12" transform="translate(11,0)"></path><path stroke="black" fill="none" d="M-7 -12 L-7 9 M7 -12 L-7 2 M-2 -3 L7 9" transform="translate(33,0)"></path><path stroke="black" fill="none" d="M0 -12 L-8 9 M0 -12 L8 9 M-5 2 L5 2" transform="translate(52,0)"></path><path stroke="black" fill="none" d="M-8 -12 L0 -2 L0 9 M8 -12 L0 -2" transform="translate(70,0)"></path><path stroke="black" fill="none" d="M0 -12 L0 2 M0 7 L-1 8 L0 9 L1 8 L0 7" transform="translate(84,0)"></path></g></g></svg>
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hershey
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Winston Durand
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|