text2svg 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/text2svg/cli.rb +26 -12
- data/lib/text2svg/cli_test.rb +41 -25
- data/lib/text2svg/option.rb +25 -0
- data/lib/text2svg/typography.rb +28 -22
- data/lib/text2svg/version.rb +1 -1
- data/text2svg.gemspec +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 669e306f0e466d680462a3fe6277e7fee87394fd
|
4
|
+
data.tar.gz: 17c4b5490802f0a842cb37584cf8c3bb35e8be20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53ddc5d148dab731052f69a1ea2ccd45f5e097e7b345f9f6c7e0f1ac760787d833dd54e4edf7d03cd00b583a99a0f50fee16425685dd5842ac1b2080d5083cc2
|
7
|
+
data.tar.gz: 6d5778f049cb11601586bf8f67d8e88d8a3716ab11d0306309be302fcab21bc0d789c8ed61f0cea5e07de80bd8ab8410a18b397cbebe3279e5376f2cad14df08
|
data/lib/text2svg/cli.rb
CHANGED
@@ -2,10 +2,19 @@ require 'text2svg/typography'
|
|
2
2
|
|
3
3
|
module Text2svg
|
4
4
|
module CLI
|
5
|
-
Option = Struct.new(:font, :text_align, :fill, :encoding)
|
6
|
-
|
7
5
|
def start
|
8
|
-
o =
|
6
|
+
o = Option.new(
|
7
|
+
nil,
|
8
|
+
:left,
|
9
|
+
:black,
|
10
|
+
:none,
|
11
|
+
1,
|
12
|
+
Encoding::UTF_8,
|
13
|
+
:none,
|
14
|
+
1,
|
15
|
+
false,
|
16
|
+
false,
|
17
|
+
)
|
9
18
|
OptionParser.new.tap { |opt|
|
10
19
|
opt.on('-f', '--font FONT', 'font file path (require)') do |arg|
|
11
20
|
o.font = arg
|
@@ -19,19 +28,24 @@ module Text2svg
|
|
19
28
|
opt.on('--encoding ENCODING', 'input text encoding (default utf-8)') do |arg|
|
20
29
|
o.encoding = Encoding.find(arg)
|
21
30
|
end
|
31
|
+
opt.on('--stroke COLOR', 'stroke color setting (default none)') do |arg|
|
32
|
+
o.stroke = arg
|
33
|
+
end
|
34
|
+
opt.on('--stroke-width NUM', 'stroke-width value (default 1)') do |arg|
|
35
|
+
o.stroke_width = arg
|
36
|
+
end
|
37
|
+
opt.on('--bold', "embolden outline (default false)") do |arg|
|
38
|
+
o.bold = arg
|
39
|
+
end
|
40
|
+
opt.on('--italic', "oblique outline (default false)") do |arg|
|
41
|
+
o.italic = arg
|
42
|
+
end
|
22
43
|
}.parse!(ARGV)
|
23
44
|
unless o.font
|
24
|
-
raise ArgumentError,
|
45
|
+
raise ArgumentError, 'require `--font` cli option. see --help'
|
25
46
|
end
|
26
47
|
text = ARGV[0] || $stdin.read
|
27
|
-
puts Text2svg::Typography.build(
|
28
|
-
text,
|
29
|
-
font: o.font,
|
30
|
-
text_align: o.text_align,
|
31
|
-
stroke: :none,
|
32
|
-
fill: o.fill,
|
33
|
-
encoding: o.encoding,
|
34
|
-
)
|
48
|
+
puts Text2svg::Typography.build(text, o).to_s
|
35
49
|
end
|
36
50
|
module_function :start
|
37
51
|
end
|
data/lib/text2svg/cli_test.rb
CHANGED
@@ -1,49 +1,65 @@
|
|
1
1
|
require 'text2svg/cli'
|
2
2
|
|
3
3
|
module Text2svgCLITest
|
4
|
+
def check(t)
|
5
|
+
out = capture do
|
6
|
+
Text2svg::CLI.start
|
7
|
+
end
|
8
|
+
unless String === out
|
9
|
+
t.error 'return value was break'
|
10
|
+
end
|
11
|
+
unless 0 < out.length
|
12
|
+
t.error 'unsupported font?'
|
13
|
+
end
|
14
|
+
if out.strip[0, 4] != '<svg'
|
15
|
+
t.error 'output format was break'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
4
19
|
def test_start(t)
|
5
20
|
%w(left right center).each do |text_align|
|
6
21
|
ARGV[0] = "Hello,\nWorld!"
|
7
22
|
ARGV[1] = '--font=/Library/Fonts/Times New Roman.ttf'
|
8
23
|
ARGV[2] = "--text-align=#{text_align}"
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
unless String === out
|
13
|
-
t.error 'return value was break'
|
14
|
-
end
|
15
|
-
unless 0 < out.length
|
16
|
-
t.error 'unsupported font?'
|
17
|
-
end
|
18
|
-
if out.strip[0,4] != "<svg"
|
19
|
-
t.error 'output format was break'
|
20
|
-
end
|
24
|
+
|
25
|
+
check(t)
|
21
26
|
end
|
22
27
|
end
|
23
28
|
|
24
29
|
def test_encoding(t)
|
25
30
|
[
|
26
31
|
['utf-8', "\xEF\x82\x9B".force_encoding(Encoding::ASCII_8BIT)],
|
27
|
-
['utf-32', "\x00\x00\xFE\xFF\x00\x00\xF0\x9B".force_encoding(Encoding::ASCII_8BIT)]
|
32
|
+
['utf-32', "\x00\x00\xFE\xFF\x00\x00\xF0\x9B".force_encoding(Encoding::ASCII_8BIT)],
|
28
33
|
].each do |(encoding, text)|
|
29
34
|
ARGV[0] = text
|
30
35
|
ARGV[1] = '--font=~/Library/Fonts/fontawesome-webfont.ttf'
|
31
36
|
ARGV[2] = "--encoding=#{encoding}"
|
32
|
-
|
33
|
-
Text2svg::CLI.start
|
34
|
-
end
|
35
|
-
unless String === out
|
36
|
-
t.error 'return value was break'
|
37
|
-
end
|
38
|
-
unless 0 < out.length
|
39
|
-
t.error 'unsupported font?'
|
40
|
-
end
|
41
|
-
if out.strip[0,4] != "<svg"
|
42
|
-
t.error 'output format was break'
|
43
|
-
end
|
37
|
+
check(t)
|
44
38
|
end
|
45
39
|
end
|
46
40
|
|
41
|
+
def test_bold(t)
|
42
|
+
ARGV[0] = 'Hello'
|
43
|
+
ARGV[1] = '--font=/Library/Fonts/Times New Roman.ttf'
|
44
|
+
ARGV[2] = '--bold'
|
45
|
+
check(t)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_italic(t)
|
49
|
+
ARGV[0] = 'Hello'
|
50
|
+
ARGV[1] = '--font=/Library/Fonts/Times New Roman.ttf'
|
51
|
+
ARGV[2] = '--italic'
|
52
|
+
check(t)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_stroke(t)
|
56
|
+
ARGV[0] = 'Hello'
|
57
|
+
ARGV[1] = '--font=/Library/Fonts/Times New Roman.ttf'
|
58
|
+
ARGV[2] = '--stroke=red'
|
59
|
+
ARGV[3] = '--stroke-width=100'
|
60
|
+
check(t)
|
61
|
+
end
|
62
|
+
|
47
63
|
def capture
|
48
64
|
out = StringIO.new
|
49
65
|
orig = $stdout
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Text2svg
|
2
|
+
class Option < Struct.new(
|
3
|
+
:font,
|
4
|
+
:text_align,
|
5
|
+
:fill,
|
6
|
+
:stroke,
|
7
|
+
:stroke_width,
|
8
|
+
:encoding,
|
9
|
+
:stroke,
|
10
|
+
:stroke_width,
|
11
|
+
:bold,
|
12
|
+
:italic,
|
13
|
+
)
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def from_hash(h)
|
17
|
+
o = new
|
18
|
+
h.to_h.each do |k, v|
|
19
|
+
o[k.to_sym] = v
|
20
|
+
end
|
21
|
+
o
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/text2svg/typography.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'freetype'
|
2
2
|
require 'text2svg/outline2d'
|
3
|
+
require 'text2svg/option'
|
3
4
|
|
4
5
|
module Text2svg
|
5
6
|
class Typography
|
@@ -10,30 +11,25 @@ module Text2svg
|
|
10
11
|
INTER_CHAR_SPACE_DIV = 50r
|
11
12
|
|
12
13
|
class << self
|
13
|
-
def build(text,
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
fill: fill,
|
20
|
-
text_align: text_align,
|
21
|
-
encoding: encoding,
|
22
|
-
)
|
23
|
-
svg = %(<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 #{w} #{h}">\n)
|
14
|
+
def build(text, option)
|
15
|
+
if Hash === option
|
16
|
+
option = Option.from_hash(option)
|
17
|
+
end
|
18
|
+
content = path(text, option)
|
19
|
+
svg = %(<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 #{content.width} #{content.height}">\n)
|
24
20
|
svg << "<title>#{text}</title>\n"
|
25
|
-
svg <<
|
21
|
+
svg << content.data
|
26
22
|
svg << "</svg>\n"
|
27
|
-
svg
|
23
|
+
Content.new(svg, content.width, content.height)
|
28
24
|
end
|
29
25
|
|
30
|
-
def path(text,
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
text.force_encoding(encoding).encode!(Encoding::UTF_8)
|
26
|
+
def path(text, option)
|
27
|
+
if Hash === option
|
28
|
+
option = Option.from_hash(option)
|
29
|
+
end
|
30
|
+
text.force_encoding(option.encoding).encode!(Encoding::UTF_8)
|
35
31
|
|
36
|
-
FreeType::API::Font.open(File.expand_path(font)) do |f|
|
32
|
+
FreeType::API::Font.open(File.expand_path(option.font)) do |f|
|
37
33
|
f.set_char_size(0, 0, 3000, 3000)
|
38
34
|
|
39
35
|
lines = []
|
@@ -62,6 +58,10 @@ module Text2svg
|
|
62
58
|
if glyph.outline.tags.length == 0
|
63
59
|
glyph = f.notdef
|
64
60
|
end
|
61
|
+
|
62
|
+
glyph.bold if option.bold
|
63
|
+
glyph.italic if option.italic
|
64
|
+
|
65
65
|
kern = f.kerning_unfitted(before_char, char).x
|
66
66
|
|
67
67
|
width, is_draw = if IDEOGRAPHIC_SPACE.match char
|
@@ -97,7 +97,7 @@ module Text2svg
|
|
97
97
|
y += line_height
|
98
98
|
before_char = nil
|
99
99
|
|
100
|
-
case text_align.to_sym
|
100
|
+
case option.text_align.to_sym
|
101
101
|
when :center
|
102
102
|
x += (max_width - line_width) / 2r
|
103
103
|
when :right
|
@@ -114,7 +114,7 @@ module Text2svg
|
|
114
114
|
x += f.kerning_unfitted(before_char, cs.char).x.to_i
|
115
115
|
output << %! <g transform="translate(#{x.to_i},0)">\n!
|
116
116
|
if cs.draw?
|
117
|
-
output << %( <path stroke="#{stroke}" stroke-width="#{stroke_width}" fill="#{fill}" d="#{cs.d}"/>\n)
|
117
|
+
output << %( <path stroke="#{option.stroke}" stroke-width="#{option.stroke_width}" fill="#{option.fill}" d="#{cs.d}"/>\n)
|
118
118
|
end
|
119
119
|
x += cs.width
|
120
120
|
x += inter_char_space if cs != line.last
|
@@ -124,7 +124,7 @@ module Text2svg
|
|
124
124
|
output << "</g>\n".freeze
|
125
125
|
end
|
126
126
|
|
127
|
-
|
127
|
+
Content.new(output, max_width.to_i, (y + line_height / 4).to_i)
|
128
128
|
end
|
129
129
|
end
|
130
130
|
end
|
@@ -135,4 +135,10 @@ module Text2svg
|
|
135
135
|
is_draw
|
136
136
|
end
|
137
137
|
end
|
138
|
+
|
139
|
+
Content = Struct.new(:data, :width, :height) do
|
140
|
+
def to_s
|
141
|
+
data
|
142
|
+
end
|
143
|
+
end
|
138
144
|
end
|
data/lib/text2svg/version.rb
CHANGED
data/text2svg.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
|
-
spec.add_runtime_dependency 'freetype'
|
22
|
+
spec.add_runtime_dependency 'freetype', '>= 0.0.3'
|
23
23
|
spec.add_development_dependency 'bundler', '~> 1.11'
|
24
24
|
spec.add_development_dependency 'rake', '~> 10.0'
|
25
25
|
spec.add_development_dependency 'rgot'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: text2svg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ksss
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: freetype
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.0.3
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.0.3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -87,6 +87,7 @@ files:
|
|
87
87
|
- lib/text2svg.rb
|
88
88
|
- lib/text2svg/cli.rb
|
89
89
|
- lib/text2svg/cli_test.rb
|
90
|
+
- lib/text2svg/option.rb
|
90
91
|
- lib/text2svg/outline2d.rb
|
91
92
|
- lib/text2svg/typography.rb
|
92
93
|
- lib/text2svg/version.rb
|