text2svg 0.0.1 → 0.0.2

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: 0d4f42bf934a3299f66266c33f5bd7b93ae462c9
4
- data.tar.gz: 6edb0be855125adbfca06e23f6caa95739768600
3
+ metadata.gz: 94254b4b10b68d5e825b24f2a4144c69da09e2c5
4
+ data.tar.gz: 9861a858f2d279f0f1c10a66a74b2a0b33bb5843
5
5
  SHA512:
6
- metadata.gz: e08fc22e1184c5cb354db66980500df389b3e9c20c054babbeff06d2493ec6b2a1fb87dcf03efbdb0b7a207e8536892fa72a3be6744807ad8cbe18727fc34628
7
- data.tar.gz: 5666d80f8424d2c93d53b4cd4833880e6d8a9e55dcd56b63ee929fab788e4986d16a100c150f52c5bfd8bc11118a34ff8178132b67183ffc8080b40ec1746532
6
+ metadata.gz: b45729144575ef72fb38d6a1af983e0e2154f99ca0b98a46679946fe25ec936424d5c518165e2dc8ee16568bfc4d4e623eb486c83c4b2cfdeec122f534305c5b
7
+ data.tar.gz: adadaa9dd72fdd0dc23187ebd9a0086e68ab37ec19495a97988322d0c4752ca83e593202104789d35e8010c70868704f93a607125e05a86d0de13c2cec85c41b
data/README.md CHANGED
@@ -26,6 +26,12 @@ Or install it yourself as:
26
26
 
27
27
  $ gem install text2svg
28
28
 
29
+ ## Require
30
+
31
+ - libfreetype
32
+
33
+ see also http://www.freetype.org/
34
+
29
35
  ## Development
30
36
 
31
37
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/text2svg/cli.rb CHANGED
@@ -2,17 +2,36 @@ require 'text2svg/typography'
2
2
 
3
3
  module Text2svg
4
4
  module CLI
5
- Option = Struct.new(:font)
5
+ Option = Struct.new(:font, :text_align, :fill, :encoding)
6
6
 
7
7
  def start
8
- o = CLI::Option.new
8
+ o = CLI::Option.new(nil, :left, :black, Encoding::UTF_8)
9
9
  OptionParser.new.tap { |opt|
10
- opt.on('-f', '--font [FONT]', 'font file path (require)') do |arg|
10
+ opt.on('-f', '--font FONT', 'font file path (require)') do |arg|
11
11
  o.font = arg
12
12
  end
13
+ opt.on('--text-align ALIGN', 'text align left,right or center (default left)', %i(left right center)) do |arg|
14
+ o.text_align = arg
15
+ end
16
+ opt.on('--fill FILL', 'text fill color (default black)') do |arg|
17
+ o.fill = arg
18
+ end
19
+ opt.on('--encoding ENCODING', 'input text encoding (default utf-8)') do |arg|
20
+ o.encoding = Encoding.find(arg)
21
+ end
13
22
  }.parse!(ARGV)
23
+ unless o.font
24
+ raise ArgumentError, "require `--font` cli option. see --help"
25
+ end
14
26
  text = ARGV[0] || $stdin.read
15
- puts Text2svg::Typography.build(text, font: o.font)
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
+ )
16
35
  end
17
36
  module_function :start
18
37
  end
@@ -2,16 +2,45 @@ require 'text2svg/cli'
2
2
 
3
3
  module Text2svgCLITest
4
4
  def test_start(t)
5
- ARGV[0] = 'abc'
6
- ARGV[1] = '--font=/Library/Fonts/Times New Roman.ttf'
7
- out = capture do
8
- Text2svg::CLI.start
5
+ %w(left right center).each do |text_align|
6
+ ARGV[0] = "Hello,\nWorld!"
7
+ ARGV[1] = '--font=/Library/Fonts/Times New Roman.ttf'
8
+ ARGV[2] = "--text-align=#{text_align}"
9
+ out = capture do
10
+ Text2svg::CLI.start
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
9
21
  end
10
- unless String === out
11
- t.error 'return value was break'
12
- end
13
- unless 0 < out.length
14
- t.error 'unsupported font?'
22
+ end
23
+
24
+ def test_encoding(t)
25
+ [
26
+ ['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)]
28
+ ].each do |(encoding, text)|
29
+ ARGV[0] = text
30
+ ARGV[1] = '--font=~/Library/Fonts/fontawesome-webfont.ttf'
31
+ ARGV[2] = "--encoding=#{encoding}"
32
+ out = capture do
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
15
44
  end
16
45
  end
17
46
 
@@ -5,18 +5,35 @@ module Text2svg
5
5
  class Typography
6
6
  WHITESPACE = /[[:space:]]/
7
7
  IDEOGRAPHIC_SPACE = /[\u{3000}]/
8
- NEW_LINE = /[\n]/
8
+ NEW_LINE = /[\u{000A}]/
9
9
  NOTDEF_GLYPH_ID = 0
10
10
  INTER_CHAR_SPACE_DIV = 50r
11
11
 
12
12
  class << self
13
- def build(text, font:, stroke: :black, stroke_width: 1, fill: :black, text_align: :left)
14
- g, w, h = path(text, font: font, stroke: stroke, stroke_width: stroke_width, fill: fill, text_align: text_align)
15
- %(<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 #{w} #{h}">\n#{g}</svg>)
13
+ def build(text, font:, stroke: :none, stroke_width: 1, fill: :black, text_align: :left, encoding: Encoding::UTF_8)
14
+ g, w, h = path(
15
+ text,
16
+ font: font,
17
+ stroke: stroke,
18
+ stroke_width: stroke_width,
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)
24
+ svg << "<title>#{text}</title>\n"
25
+ svg << g
26
+ svg << "</svg>\n"
27
+ svg
16
28
  end
17
29
 
18
- def path(text, font:, stroke: :black, stroke_width: 1, fill: :black, text_align: :left)
19
- FreeType::API::Font.open(font) do |f|
30
+ def path(text, font:, stroke: :none, stroke_width: 1, fill: :black, text_align: :left, encoding: Encoding::UTF_8)
31
+ stroke ||= :none
32
+ fill ||= :black
33
+ text_align ||= :left
34
+ text.force_encoding(encoding).encode!(Encoding::UTF_8)
35
+
36
+ FreeType::API::Font.open(File.expand_path(font)) do |f|
20
37
  f.set_char_size(0, 0, 3000, 3000)
21
38
 
22
39
  lines = []
@@ -1,3 +1,3 @@
1
1
  module Text2svg
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
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.1
4
+ version: 0.0.2
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-17 00:00:00.000000000 Z
11
+ date: 2015-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: freetype