text2svg 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0d4f42bf934a3299f66266c33f5bd7b93ae462c9
4
+ data.tar.gz: 6edb0be855125adbfca06e23f6caa95739768600
5
+ SHA512:
6
+ metadata.gz: e08fc22e1184c5cb354db66980500df389b3e9c20c054babbeff06d2493ec6b2a1fb87dcf03efbdb0b7a207e8536892fa72a3be6744807ad8cbe18727fc34628
7
+ data.tar.gz: 5666d80f8424d2c93d53b4cd4833880e6d8a9e55dcd56b63ee929fab788e4986d16a100c150f52c5bfd8bc11118a34ff8178132b67183ffc8080b40ec1746532
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at co000ri@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in text2svg.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 ksss
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Text2svg
2
+
3
+ Build svg path data from font file
4
+
5
+ Using by freetype API
6
+
7
+ ```
8
+ $ text2svg "Hello, World\!" --font="/Library/Fonts/Times New Roman.ttf" > test.svg && open test.svg -a /Applications/Google\ Chrome.app
9
+ ```
10
+
11
+ ![img](https://raw.githubusercontent.com/ksss/text2svg/master/data/sample.jpg)
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'text2svg'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install text2svg
28
+
29
+ ## Development
30
+
31
+ 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.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/text2svg. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
38
+
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ task :test do
3
+ sh 'rgot -v'
4
+ end
5
+ task default: [:test]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'text2svg'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/data/sample.jpg ADDED
Binary file
data/exe/text2svg ADDED
@@ -0,0 +1,6 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'text2svg/cli'
5
+
6
+ Text2svg::CLI.start
data/lib/text2svg.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'text2svg/typography'
2
+ require 'text2svg/outline2d'
3
+ require 'text2svg/version'
@@ -0,0 +1,19 @@
1
+ require 'text2svg/typography'
2
+
3
+ module Text2svg
4
+ module CLI
5
+ Option = Struct.new(:font)
6
+
7
+ def start
8
+ o = CLI::Option.new
9
+ OptionParser.new.tap { |opt|
10
+ opt.on('-f', '--font [FONT]', 'font file path (require)') do |arg|
11
+ o.font = arg
12
+ end
13
+ }.parse!(ARGV)
14
+ text = ARGV[0] || $stdin.read
15
+ puts Text2svg::Typography.build(text, font: o.font)
16
+ end
17
+ module_function :start
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ require 'text2svg/cli'
2
+
3
+ module Text2svgCLITest
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
9
+ end
10
+ unless String === out
11
+ t.error 'return value was break'
12
+ end
13
+ unless 0 < out.length
14
+ t.error 'unsupported font?'
15
+ end
16
+ end
17
+
18
+ def capture
19
+ out = StringIO.new
20
+ orig = $stdout
21
+ $stdout = out
22
+ yield
23
+ out.string.dup
24
+ ensure
25
+ $stdout = orig
26
+ end
27
+ end
@@ -0,0 +1,79 @@
1
+ require 'freetype'
2
+
3
+ module Text2svg
4
+ class Outline2d
5
+ def initialize(outline)
6
+ @outline = outline
7
+ end
8
+
9
+ def to_d
10
+ end_ptd_of_counts = @outline.contours
11
+ contours = []
12
+ contour = []
13
+ @outline.points.each.with_index do |point, index|
14
+ contour << point
15
+ if index == end_ptd_of_counts.first
16
+ end_ptd_of_counts.shift
17
+ contours << contour
18
+ contour = []
19
+ end
20
+ end
21
+
22
+ path = []
23
+ contours.each do |contour|
24
+ first_pt = contour.first
25
+ last_pt = contour.last
26
+ curve_pt = nil
27
+ start = 0
28
+ if first_pt.on_curve?
29
+ curve_pt = nil
30
+ start = 1
31
+ else
32
+ if last_pt.on_curve?
33
+ first_pt = last_pt
34
+ else
35
+ first_pt = FreeType::API::Point.new(0, (first_pt.x + last_pt.x) / 2, (first_pt.y + last_pt.y) / 2)
36
+ end
37
+ curve_pt = first_pt
38
+ end
39
+ path << ['M', first_pt.x, -first_pt.y]
40
+
41
+ prev_pt = nil
42
+ (start...contour.length).each do |j|
43
+ pt = contour[j]
44
+ prev_pt = if j == 0
45
+ first_pt
46
+ else
47
+ contour[j - 1]
48
+ end
49
+
50
+ if prev_pt.on_curve? && pt.on_curve?
51
+ path << ['L', pt.x, -pt.y]
52
+ elsif prev_pt.on_curve? && !pt.on_curve?
53
+ curve_pt = pt
54
+ elsif !prev_pt.on_curve? && !pt.on_curve?
55
+ path << ['Q', prev_pt.x, -prev_pt.y, (prev_pt.x + pt.x) / 2, -((prev_pt.y + pt.y) / 2)]
56
+ curve_pt = pt
57
+ elsif !prev_pt.on_curve? && pt.on_curve?
58
+ path << ['Q', curve_pt.x, -curve_pt.y, pt.x, -pt.y]
59
+ curve_pt = nil
60
+ else
61
+ raise
62
+ end
63
+ end
64
+
65
+ next unless first_pt != last_pt
66
+ if curve_pt
67
+ path << ['Q', curve_pt.x, -curve_pt.y, first_pt.x, -first_pt.y]
68
+ else
69
+ path << ['L', first_pt.x, -first_pt.y]
70
+ end
71
+ end
72
+ path << ['z'] if 0 < path.length
73
+
74
+ path.map { |(command, *args)|
75
+ "#{command}#{args.join(' ')}"
76
+ }.join('')
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,121 @@
1
+ require 'freetype'
2
+ require 'text2svg/outline2d'
3
+
4
+ module Text2svg
5
+ class Typography
6
+ WHITESPACE = /[[:space:]]/
7
+ IDEOGRAPHIC_SPACE = /[\u{3000}]/
8
+ NEW_LINE = /[\n]/
9
+ NOTDEF_GLYPH_ID = 0
10
+ INTER_CHAR_SPACE_DIV = 50r
11
+
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>)
16
+ end
17
+
18
+ def path(text, font:, stroke: :black, stroke_width: 1, fill: :black, text_align: :left)
19
+ FreeType::API::Font.open(font) do |f|
20
+ f.set_char_size(0, 0, 3000, 3000)
21
+
22
+ lines = []
23
+ line = []
24
+ lines << line
25
+
26
+ before_char = nil
27
+ space_width = f.glyph(' '.freeze).char_width
28
+ text.each_char do |char|
29
+ if NEW_LINE.match char
30
+ line = []
31
+ lines << line
32
+ before_char = nil
33
+ next
34
+ end
35
+
36
+ # glyph登録されていない文字
37
+ glyph_id = f.char_index(char)
38
+ glyph = if glyph_id == 0
39
+ f.notdef
40
+ else
41
+ f.glyph(char)
42
+ end
43
+
44
+ # glyphが登録されているが字形が登録されていない文字
45
+ if glyph.outline.tags.length == 0
46
+ glyph = f.notdef
47
+ end
48
+ kern = f.kerning_unfitted(before_char, char).x
49
+
50
+ width, is_draw = if IDEOGRAPHIC_SPACE.match char
51
+ [space_width * 2r, false]
52
+ elsif WHITESPACE.match char
53
+ [space_width, false]
54
+ else
55
+ [glyph.char_width, true]
56
+ end
57
+ before_char = char
58
+ line << CharSet.new(char, width, is_draw, Outline2d.new(glyph.outline).to_d)
59
+ end
60
+
61
+ inter_char_space = space_width / INTER_CHAR_SPACE_DIV
62
+ width_by_line = lines.map do |line|
63
+ before_char = nil
64
+ if 0 < line.length
65
+ line.map { |cs|
66
+ w = cs.width + f.kerning_unfitted(before_char, cs.char).x
67
+ w.tap { before_char = cs.char }
68
+ }.inject(:+) + (line.length - 1) * inter_char_space
69
+ else
70
+ 0
71
+ end
72
+ end
73
+ max_width = width_by_line.max
74
+
75
+ y = 0r
76
+ output = ''
77
+ line_height = f.line_height
78
+ lines.zip(width_by_line).each do |(line, line_width)|
79
+ x = 0r
80
+ y += line_height
81
+ before_char = nil
82
+
83
+ case text_align.to_sym
84
+ when :center
85
+ x += (max_width - line_width) / 2r
86
+ when :right
87
+ x += max_width - line_width
88
+ when :left
89
+ # nothing
90
+ else
91
+ warn 'text_align must be left,right or center'
92
+ end
93
+
94
+ output << %!<g transform="translate(0,#{y.to_i})">\n!
95
+
96
+ line.each do |cs|
97
+ x += f.kerning_unfitted(before_char, cs.char).x.to_i
98
+ output << %! <g transform="translate(#{x.to_i},0)">\n!
99
+ if cs.draw?
100
+ output << %( <path stroke="#{stroke}" stroke-width="#{stroke_width}" fill="#{fill}" d="#{cs.d}"/>\n)
101
+ end
102
+ x += cs.width
103
+ x += inter_char_space if cs != line.last
104
+ output << " </g>\n".freeze
105
+ before_char = cs.char
106
+ end
107
+ output << "</g>\n".freeze
108
+ end
109
+
110
+ [output, max_width.to_i, (y + line_height / 4).to_i]
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ CharSet = Struct.new(:char, :width, :is_draw, :d) do
117
+ def draw?
118
+ is_draw
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,3 @@
1
+ module Text2svg
2
+ VERSION = '0.0.1'
3
+ end
data/text2svg.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'text2svg/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'text2svg'
8
+ spec.version = Text2svg::VERSION
9
+ spec.authors = ['ksss']
10
+ spec.email = ['co000ri@gmail.com']
11
+
12
+ spec.summary = 'Build svg path data from font file'
13
+ spec.description = 'Build svg path data from font file'
14
+ spec.homepage = 'https://github.com/ksss/text2svg'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_runtime_dependency 'freetype'
23
+ spec.add_development_dependency 'bundler', '~> 1.11'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rgot'
26
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: text2svg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ksss
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: freetype
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rgot
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Build svg path data from font file
70
+ email:
71
+ - co000ri@gmail.com
72
+ executables:
73
+ - text2svg
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - data/sample.jpg
86
+ - exe/text2svg
87
+ - lib/text2svg.rb
88
+ - lib/text2svg/cli.rb
89
+ - lib/text2svg/cli_test.rb
90
+ - lib/text2svg/outline2d.rb
91
+ - lib/text2svg/typography.rb
92
+ - lib/text2svg/version.rb
93
+ - text2svg.gemspec
94
+ homepage: https://github.com/ksss/text2svg
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.5.0
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Build svg path data from font file
118
+ test_files: []