text2svg 0.3.4 → 0.3.5
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/.travis.yml +13 -0
- data/README.md +2 -0
- data/data/Lobster.otf +0 -0
- data/data/sporq.ttf +0 -0
- data/lib/text2svg/cli_test.rb +4 -4
- data/lib/text2svg/typography.rb +42 -19
- data/lib/text2svg/typography_test.rb +28 -27
- data/lib/text2svg/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5dc265c2022da3597a26d0b43dd53540ff28000d
|
4
|
+
data.tar.gz: 666f2d5988a3cb2be258b695edb1ee2ed7152dfd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2ebd92f86fb3b0b69d2abe5c529a5b66e08581790533ab75d72270bc2bdd3b0a17fcfb567ac5b99cc71f886bb828e014a75c25fcf45bdaf656d4f8db83079fd
|
7
|
+
data.tar.gz: 39329909625340a14ca97d2f0d9df5775dd21dfec2380f5510dbe617b33e4b6d655fc5da49c138ca1e2ff3ef38cbfc9f156ae5a69774a8e1b233c64f7f30fbc5
|
data/.travis.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
language: ruby
|
2
|
+
before_install:
|
3
|
+
- sudo apt-get install -qq libfreetype6-dev
|
4
|
+
rvm:
|
5
|
+
- 2.1.7 # ffi is not support to 2.1.8
|
6
|
+
- 2.2.4
|
7
|
+
- 2.3.0
|
8
|
+
matrix:
|
9
|
+
allow_failures:
|
10
|
+
- rvm: ruby-head
|
11
|
+
notifications:
|
12
|
+
email: false
|
13
|
+
before_install: gem install bundler -v 1.11.2
|
data/README.md
CHANGED
data/data/Lobster.otf
ADDED
Binary file
|
data/data/sporq.ttf
ADDED
Binary file
|
data/lib/text2svg/cli_test.rb
CHANGED
@@ -19,7 +19,7 @@ module Text2svgCLITest
|
|
19
19
|
def test_start(t)
|
20
20
|
%w(left right center).each do |text_align|
|
21
21
|
ARGV[0] = "Hello,\nWorld!"
|
22
|
-
ARGV[1] = '--font
|
22
|
+
ARGV[1] = '--font=data/sporq.ttf'
|
23
23
|
ARGV[2] = "--text-align=#{text_align}"
|
24
24
|
|
25
25
|
check(t)
|
@@ -32,7 +32,7 @@ module Text2svgCLITest
|
|
32
32
|
['utf-32', "\x00\x00\xFE\xFF\x00\x00\xF0\x9B".force_encoding(Encoding::ASCII_8BIT)],
|
33
33
|
].each do |(encoding, text)|
|
34
34
|
ARGV[0] = text
|
35
|
-
ARGV[1] = '--font
|
35
|
+
ARGV[1] = '--font=data/sporq.ttf'
|
36
36
|
ARGV[2] = "--encoding=#{encoding}"
|
37
37
|
check(t)
|
38
38
|
end
|
@@ -40,14 +40,14 @@ module Text2svgCLITest
|
|
40
40
|
|
41
41
|
def test_bold(t)
|
42
42
|
ARGV[0] = 'Hello'
|
43
|
-
ARGV[1] = '--font
|
43
|
+
ARGV[1] = '--font=data/sporq.ttf'
|
44
44
|
ARGV[2] = '--bold'
|
45
45
|
check(t)
|
46
46
|
end
|
47
47
|
|
48
48
|
def test_italic(t)
|
49
49
|
ARGV[0] = 'Hello'
|
50
|
-
ARGV[1] = '--font
|
50
|
+
ARGV[1] = '--font=data/sporq.ttf'
|
51
51
|
ARGV[2] = '--italic'
|
52
52
|
check(t)
|
53
53
|
end
|
data/lib/text2svg/typography.rb
CHANGED
@@ -47,13 +47,14 @@ module Text2svg
|
|
47
47
|
lines = []
|
48
48
|
line = []
|
49
49
|
lines << line
|
50
|
-
|
50
|
+
min_hori_bearing_x_by_line = [0]
|
51
51
|
|
52
52
|
space_width = f.glyph(' '.freeze).char_width
|
53
53
|
text.each_char.with_index do |char, index|
|
54
54
|
if NEW_LINE.match char
|
55
|
+
min_hori_bearing_x_by_line[-1] = min_hori_bearing_x(line)
|
56
|
+
min_hori_bearing_x_by_line << 0
|
55
57
|
line = []
|
56
|
-
first_hori_bearing_x << 0
|
57
58
|
lines << line
|
58
59
|
next
|
59
60
|
end
|
@@ -74,33 +75,42 @@ module Text2svg
|
|
74
75
|
glyph.bold if option.bold
|
75
76
|
glyph.italic if option.italic
|
76
77
|
|
77
|
-
|
78
|
-
|
78
|
+
metrics = FreeType::C::FT_Glyph_Metrics.new
|
79
|
+
is_draw = if IDEOGRAPHIC_SPACE.match char
|
80
|
+
FreeType::C::FT_Glyph_Metrics.members.each do |m|
|
81
|
+
metrics[m] = space_width * 2r
|
82
|
+
end
|
83
|
+
false
|
79
84
|
elsif WHITESPACE.match char
|
80
|
-
|
85
|
+
FreeType::C::FT_Glyph_Metrics.members.each do |m|
|
86
|
+
metrics[m] = space_width
|
87
|
+
end
|
88
|
+
false
|
81
89
|
else
|
82
|
-
|
83
|
-
|
90
|
+
FreeType::C::FT_Glyph_Metrics.members.each do |m|
|
91
|
+
metrics[m] = glyph.metrics[m]
|
84
92
|
end
|
85
|
-
|
93
|
+
true
|
86
94
|
end
|
87
|
-
line << CharSet.new(char,
|
95
|
+
line << CharSet.new(char, metrics, is_draw, glyph.outline.svg_path_data)
|
88
96
|
end
|
89
97
|
|
98
|
+
min_hori_bearing_x_by_line[-1] = min_hori_bearing_x(line)
|
90
99
|
inter_char_space = space_width / INTER_CHAR_SPACE_DIV
|
100
|
+
min_hori_bearing_x_all = min_hori_bearing_x_by_line.min
|
91
101
|
|
92
|
-
width_by_line = lines.
|
102
|
+
width_by_line = lines.map do |line|
|
93
103
|
before_char = nil
|
94
104
|
if line.empty?.!
|
95
105
|
line.map { |cs|
|
96
|
-
|
97
|
-
|
106
|
+
width = if cs.equal?(line.last)
|
107
|
+
cs.metrics[:width] + cs.metrics[:horiBearingX]
|
98
108
|
else
|
99
|
-
cs.
|
109
|
+
cs.metrics[:horiAdvance]
|
100
110
|
end
|
101
|
-
w =
|
111
|
+
w = width + f.kerning_unfitted(before_char, cs.char).x
|
102
112
|
w.tap { before_char = cs.char }
|
103
|
-
}.inject(:+) + (line.length - 1) * inter_char_space -
|
113
|
+
}.inject(:+) + (line.length - 1) * inter_char_space - min_hori_bearing_x_all
|
104
114
|
else
|
105
115
|
0
|
106
116
|
end
|
@@ -113,7 +123,7 @@ module Text2svg
|
|
113
123
|
|
114
124
|
output << %(<g #{option.attribute}>\n) if option.attribute
|
115
125
|
|
116
|
-
lines.zip(width_by_line
|
126
|
+
lines.zip(width_by_line).each do |(line, line_width)|
|
117
127
|
x = 0r
|
118
128
|
y += line_height
|
119
129
|
before_char = nil
|
@@ -131,13 +141,13 @@ module Text2svg
|
|
131
141
|
|
132
142
|
output << %!<g transform="matrix(1,0,0,1,0,#{y.to_i})">\n!
|
133
143
|
|
134
|
-
x -=
|
144
|
+
x -= min_hori_bearing_x_all
|
135
145
|
line.each do |cs|
|
136
146
|
x += f.kerning_unfitted(before_char, cs.char).x.to_i
|
137
147
|
if cs.draw?
|
138
148
|
output << %! <path transform="matrix(1,0,0,1,#{x.to_i},0)" d="#{cs.outline2d.join(' '.freeze)}"/>\n!
|
139
149
|
end
|
140
|
-
x += cs.
|
150
|
+
x += cs.metrics[:horiAdvance]
|
141
151
|
x += inter_char_space if cs != line.last
|
142
152
|
before_char = cs.char
|
143
153
|
end
|
@@ -150,10 +160,23 @@ module Text2svg
|
|
150
160
|
Content.new(output, (max_width + option_width).to_i, (y + line_height / 4).to_i, notdef_indexes)
|
151
161
|
end
|
152
162
|
end
|
163
|
+
|
164
|
+
private
|
165
|
+
|
166
|
+
def min_hori_bearing_x(line)
|
167
|
+
return 0 if line.empty?
|
168
|
+
point = 0
|
169
|
+
bearings = line.map do |cs|
|
170
|
+
(cs.metrics[:horiBearingX] + point).tap {
|
171
|
+
point += cs.metrics[:horiAdvance]
|
172
|
+
}
|
173
|
+
end
|
174
|
+
[bearings.min, 0].min
|
175
|
+
end
|
153
176
|
end
|
154
177
|
end
|
155
178
|
|
156
|
-
CharSet = Struct.new(:char, :
|
179
|
+
CharSet = Struct.new(:char, :metrics, :is_draw, :outline2d) do
|
157
180
|
def draw?
|
158
181
|
is_draw
|
159
182
|
end
|
@@ -14,34 +14,35 @@ module Text2svgTypographyTest
|
|
14
14
|
t.error('error conditions was changed')
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
opt.italic = italic
|
31
|
-
['ABC', "\n", "\n\nA", "", "A\nB\n\n", "A\n\n\nC", "<", ">", "&", "=", "@", "%", "#", "("].each do |text|
|
32
|
-
begin
|
33
|
-
c = Text2svg::Typography.build(text, opt)
|
34
|
-
rescue => e
|
35
|
-
t.log("raise error #{e.class}: #{e.message} with text=\"#{text}\",opt=#{opt}")
|
36
|
-
raise
|
37
|
-
end
|
17
|
+
Dir["data/*"].grep(/otf|ttf/).each do |font|
|
18
|
+
[nil, :left, :right, :center].each do |text_align|
|
19
|
+
[Encoding::UTF_8, Encoding::ASCII_8BIT].each do |encoding|
|
20
|
+
[nil, 'fill="red"'].each do |attribute|
|
21
|
+
[false, true].each do |bold|
|
22
|
+
[false, true].each do |italic|
|
23
|
+
opt = Text2svg::Option.new
|
24
|
+
opt.font = font
|
25
|
+
opt.text_align = text_align
|
26
|
+
opt.encoding = encoding
|
27
|
+
opt.attribute = attribute
|
28
|
+
opt.bold = bold
|
29
|
+
opt.italic = italic
|
38
30
|
|
39
|
-
|
40
|
-
|
41
|
-
|
31
|
+
['ABC', "\n", "\n\nA", "", "A\nB\n\n", "A\n\n\nC", "<", ">", "&", "=", "@", "%", "#", "("].each do |text|
|
32
|
+
begin
|
33
|
+
c = Text2svg::Typography.build(text, opt)
|
34
|
+
rescue => e
|
35
|
+
t.log("raise error #{e.class}: #{e.message} with text=\"#{text}\",opt=#{opt}")
|
36
|
+
raise
|
37
|
+
end
|
38
|
+
|
39
|
+
unless Text2svg::Content === c
|
40
|
+
t.error('return value was break')
|
41
|
+
end
|
42
42
|
|
43
|
-
|
44
|
-
|
43
|
+
unless c.data.encoding == Encoding::UTF_8
|
44
|
+
t.error('encoding was changed')
|
45
|
+
end
|
45
46
|
end
|
46
47
|
end
|
47
48
|
end
|
@@ -60,7 +61,7 @@ module Text2svgTypographyTest
|
|
60
61
|
|
61
62
|
def benchmark_build(b)
|
62
63
|
str = [*'!'..'z'].join
|
63
|
-
opt = Text2svg::Option.new('/
|
64
|
+
opt = Text2svg::Option.new('data/sporq.ttf')
|
64
65
|
b.reset_timer
|
65
66
|
i = 0
|
66
67
|
while i < b.n
|
data/lib/text2svg/version.rb
CHANGED
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.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ksss
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: freetype
|
@@ -75,6 +75,7 @@ extensions: []
|
|
75
75
|
extra_rdoc_files: []
|
76
76
|
files:
|
77
77
|
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
78
79
|
- CODE_OF_CONDUCT.md
|
79
80
|
- Gemfile
|
80
81
|
- LICENSE.txt
|
@@ -82,7 +83,9 @@ files:
|
|
82
83
|
- Rakefile
|
83
84
|
- bin/console
|
84
85
|
- bin/setup
|
86
|
+
- data/Lobster.otf
|
85
87
|
- data/sample.jpg
|
88
|
+
- data/sporq.ttf
|
86
89
|
- exe/text2svg
|
87
90
|
- lib/text2svg.rb
|
88
91
|
- lib/text2svg/cli.rb
|