text2svg 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 669e306f0e466d680462a3fe6277e7fee87394fd
4
- data.tar.gz: 17c4b5490802f0a842cb37584cf8c3bb35e8be20
3
+ metadata.gz: 9bda63aa25ac6727ef210af1f40414c3b7d120b7
4
+ data.tar.gz: 19469eeca0f2b0308345d6f60b7b94fdd77b9d64
5
5
  SHA512:
6
- metadata.gz: 53ddc5d148dab731052f69a1ea2ccd45f5e097e7b345f9f6c7e0f1ac760787d833dd54e4edf7d03cd00b583a99a0f50fee16425685dd5842ac1b2080d5083cc2
7
- data.tar.gz: 6d5778f049cb11601586bf8f67d8e88d8a3716ab11d0306309be302fcab21bc0d789c8ed61f0cea5e07de80bd8ab8410a18b397cbebe3279e5376f2cad14df08
6
+ metadata.gz: 06a6b8ad39765e73045d46fccc6880b337b7f61d0416a7d257a87d50181add2a2529b3567f6539aec822c5b82200ee73497bc8af8a103485d8350e5594bc65e3
7
+ data.tar.gz: b93fdce03dfee133362ce6e0fdeda849f7f631f4a01d340a807f1ea1592e348608e96cf6d335ed044fa08bb93a4a8c6e058bd59fa26d4b5ed88247fa78453532
data/README.md CHANGED
@@ -10,6 +10,19 @@ $ text2svg "Hello, World\!" --font="/Library/Fonts/Times New Roman.ttf" > test.s
10
10
 
11
11
  ![img](https://raw.githubusercontent.com/ksss/text2svg/master/data/sample.jpg)
12
12
 
13
+ ## Option
14
+
15
+ ```shell
16
+ $ text2svg --help
17
+ Usage: text2svg [options]
18
+ -f, --font FONT font file path (require)
19
+ --text-align ALIGN text align left,right or center (default left)
20
+ --encoding ENCODING input text encoding (default utf-8)
21
+ --bold embolden outline (default false)
22
+ --italic oblique outline (default false)
23
+ --option STRING decorate options (default nil)(e.g. fill="red" stroke-width="100")
24
+ ```
25
+
13
26
  ## Installation
14
27
 
15
28
  Add this line to your application's Gemfile:
data/exe/text2svg CHANGED
@@ -1,6 +1,5 @@
1
1
  #! /usr/bin/env ruby
2
2
 
3
- require 'optparse'
4
3
  require 'text2svg/cli'
5
4
 
6
5
  Text2svg::CLI.start
data/lib/text2svg.rb CHANGED
@@ -1,3 +1,4 @@
1
- require 'text2svg/typography'
1
+ require 'text2svg/option'
2
2
  require 'text2svg/outline2d'
3
+ require 'text2svg/typography'
3
4
  require 'text2svg/version'
data/lib/text2svg/cli.rb CHANGED
@@ -1,19 +1,17 @@
1
+ require 'optparse'
1
2
  require 'text2svg/typography'
3
+ require 'text2svg/option'
2
4
 
3
5
  module Text2svg
4
6
  module CLI
5
7
  def start
6
8
  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,
9
+ nil, # font
10
+ :left, # text_align
11
+ Encoding::UTF_8, # encoding
12
+ false, # bold
13
+ false, # italic
14
+ nil, # attribute
17
15
  )
18
16
  OptionParser.new.tap { |opt|
19
17
  opt.on('-f', '--font FONT', 'font file path (require)') do |arg|
@@ -22,24 +20,18 @@ module Text2svg
22
20
  opt.on('--text-align ALIGN', 'text align left,right or center (default left)', %i(left right center)) do |arg|
23
21
  o.text_align = arg
24
22
  end
25
- opt.on('--fill FILL', 'text fill color (default black)') do |arg|
26
- o.fill = arg
27
- end
28
23
  opt.on('--encoding ENCODING', 'input text encoding (default utf-8)') do |arg|
29
24
  o.encoding = Encoding.find(arg)
30
25
  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|
26
+ opt.on('--bold', 'embolden outline (default false)') do |arg|
38
27
  o.bold = arg
39
28
  end
40
- opt.on('--italic', "oblique outline (default false)") do |arg|
29
+ opt.on('--italic', 'oblique outline (default false)') do |arg|
41
30
  o.italic = arg
42
31
  end
32
+ opt.on('--attribute STRING', 'decorate options (default nil)(e.g. fill="red" stroke-width="100")') do |arg|
33
+ o.attribute = arg
34
+ end
43
35
  }.parse!(ARGV)
44
36
  unless o.font
45
37
  raise ArgumentError, 'require `--font` cli option. see --help'
@@ -52,14 +52,6 @@ module Text2svgCLITest
52
52
  check(t)
53
53
  end
54
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
-
63
55
  def capture
64
56
  out = StringIO.new
65
57
  orig = $stdout
@@ -2,14 +2,10 @@ module Text2svg
2
2
  class Option < Struct.new(
3
3
  :font,
4
4
  :text_align,
5
- :fill,
6
- :stroke,
7
- :stroke_width,
8
5
  :encoding,
9
- :stroke,
10
- :stroke_width,
11
6
  :bold,
12
7
  :italic,
8
+ :attribute,
13
9
  )
14
10
 
15
11
  class << self
@@ -15,20 +15,25 @@ module Text2svg
15
15
  if Hash === option
16
16
  option = Option.from_hash(option)
17
17
  end
18
+ option.encoding ||= Encoding::UTF_8
18
19
  content = path(text, option)
19
20
  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)
20
21
  svg << "<title>#{text}</title>\n"
21
22
  svg << content.data
22
23
  svg << "</svg>\n"
23
- Content.new(svg, content.width, content.height)
24
+ Content.new(svg, content.width, content.height, content.notdef_indexes)
24
25
  end
25
26
 
26
27
  def path(text, option)
27
28
  if Hash === option
28
29
  option = Option.from_hash(option)
29
30
  end
31
+ return Content.new("", 0, 0) if text.empty?
32
+
33
+ option.encoding ||= Encoding::UTF_8
30
34
  text.force_encoding(option.encoding).encode!(Encoding::UTF_8)
31
35
 
36
+ notdef_indexes = []
32
37
  FreeType::API::Font.open(File.expand_path(option.font)) do |f|
33
38
  f.set_char_size(0, 0, 3000, 3000)
34
39
 
@@ -38,7 +43,7 @@ module Text2svg
38
43
 
39
44
  before_char = nil
40
45
  space_width = f.glyph(' '.freeze).char_width
41
- text.each_char do |char|
46
+ text.each_char.with_index do |char, index|
42
47
  if NEW_LINE.match char
43
48
  line = []
44
49
  lines << line
@@ -46,24 +51,22 @@ module Text2svg
46
51
  next
47
52
  end
48
53
 
49
- # glyph登録されていない文字
50
54
  glyph_id = f.char_index(char)
51
55
  glyph = if glyph_id == 0
56
+ notdef_indexes << index
52
57
  f.notdef
53
58
  else
54
59
  f.glyph(char)
55
60
  end
56
61
 
57
- # glyphが登録されているが字形が登録されていない文字
58
62
  if glyph.outline.tags.length == 0
63
+ notdef_indexes << index
59
64
  glyph = f.notdef
60
65
  end
61
66
 
62
67
  glyph.bold if option.bold
63
68
  glyph.italic if option.italic
64
69
 
65
- kern = f.kerning_unfitted(before_char, char).x
66
-
67
70
  width, is_draw = if IDEOGRAPHIC_SPACE.match char
68
71
  [space_width * 2r, false]
69
72
  elsif WHITESPACE.match char
@@ -92,6 +95,7 @@ module Text2svg
92
95
  y = 0r
93
96
  output = ''
94
97
  line_height = f.line_height
98
+ output << %(<g #{option.attribute}>\n)
95
99
  lines.zip(width_by_line).each do |(line, line_width)|
96
100
  x = 0r
97
101
  y += line_height
@@ -114,7 +118,7 @@ module Text2svg
114
118
  x += f.kerning_unfitted(before_char, cs.char).x.to_i
115
119
  output << %! <g transform="translate(#{x.to_i},0)">\n!
116
120
  if cs.draw?
117
- output << %( <path stroke="#{option.stroke}" stroke-width="#{option.stroke_width}" fill="#{option.fill}" d="#{cs.d}"/>\n)
121
+ output << %( <path d="#{cs.d}"/>\n)
118
122
  end
119
123
  x += cs.width
120
124
  x += inter_char_space if cs != line.last
@@ -123,8 +127,11 @@ module Text2svg
123
127
  end
124
128
  output << "</g>\n".freeze
125
129
  end
130
+ output << "</g>\n".freeze
126
131
 
127
- Content.new(output, max_width.to_i, (y + line_height / 4).to_i)
132
+ option_width = 0
133
+ option_width += space_width / 1.5 if option.italic
134
+ Content.new(output, (max_width + option_width).to_i, (y + line_height / 4).to_i, notdef_indexes)
128
135
  end
129
136
  end
130
137
  end
@@ -136,7 +143,7 @@ module Text2svg
136
143
  end
137
144
  end
138
145
 
139
- Content = Struct.new(:data, :width, :height) do
146
+ Content = Struct.new(:data, :width, :height, :notdef_indexes) do
140
147
  def to_s
141
148
  data
142
149
  end
@@ -1,3 +1,3 @@
1
1
  module Text2svg
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
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.3
4
+ version: 0.1.0
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-22 00:00:00.000000000 Z
11
+ date: 2015-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: freetype
@@ -112,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  version: '0'
113
113
  requirements: []
114
114
  rubyforge_project:
115
- rubygems_version: 2.5.0
115
+ rubygems_version: 2.5.1
116
116
  signing_key:
117
117
  specification_version: 4
118
118
  summary: Build svg path data from font file