draught 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f97b0d14ba36e7199c9f941d1ce79e0e47b94b88
4
+ data.tar.gz: cfcb571fea88b2057ce38a2980bcd7a1fd542071
5
+ SHA512:
6
+ metadata.gz: '05487261c930a1b093c7b2fd0c524c0cb05ab1a7724d43bb7fa0d2f4bb614c6c7d28fdbbafa12ed1bdd732df5e7f841e624f07065307cb96b4b23001dd06a540'
7
+ data.tar.gz: 753a9862b9593e3bda6e9449f4a9b253c53d605d73763eecfc6d70f69ccaefc79fd205c909e599c2a2884597215616c9b3318fda3045979d06de65bdadcc8c47
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.pdf
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+ # simplecov reports
15
+ coverage
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
4
+ --order rand
@@ -0,0 +1 @@
1
+ 2.4
@@ -0,0 +1,19 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ env:
6
+ global:
7
+ - GIT_COMMITTED_AT=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then git log -1 --pretty=format:%ct; else git log -1 --skip 1 --pretty=format:%ct; fi)
8
+ before_script:
9
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
10
+ - chmod +x ./cc-test-reporter
11
+ script:
12
+ - bundle exec rake
13
+ # Preferably you will run test-reporter on branch update events. But
14
+ # if you setup travis to build PR updates only, you don't need to run
15
+ # the line below
16
+ - if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT; fi
17
+ # In the case where travis is setup to build PR updates only,
18
+ # uncomment the line below
19
+ # - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at matt@reprocessed.org. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Matt Patterson
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.
@@ -0,0 +1,62 @@
1
+ # Draught - a Geometry library for creating vector graphics
2
+
3
+ > draught, v.
4
+ >
5
+ > 2. To make a plan or sketch of; esp. to draw a preliminary plan of (something to be constructed); to design. (Sometimes draft.)
6
+ >
7
+ > [Oxford English Dictionary](http://www.oed.com/view/Entry/57521?result=2&rskey=9oHsDI)
8
+
9
+ Draught is a library for creating vector graphics as PDFs. It's grown out of generating shapes that can be laser cut, and that's still its focus.
10
+
11
+ TODO: Explain the fundamental concepts
12
+
13
+ ## Installation
14
+
15
+ To use it as a library add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'draught'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install draught
28
+
29
+ ## Usage
30
+
31
+ Example docs are bad and I will fix them.
32
+
33
+ Also having to require everything individually.
34
+
35
+ Create a simple straight line and add it to a sheet, then generate a PDF with it:
36
+
37
+ ```ruby
38
+ require 'draught/line'
39
+ require 'draught/bounding_box'
40
+ require 'draught/sheet'
41
+ require 'draught/renderer'
42
+
43
+ line = Draught::Line.horizontal(100).translate(Draught::Vector.new(100,100))
44
+ box = Draught::BoundingBox.new(line)
45
+ sheet = Draught::Sheet.new(width: 300, height: 200, containers: [box])
46
+
47
+ renderer = Draught::Renderer.render_to_file(sheet, './example.pdf')
48
+ ```
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ 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).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fidothe/draught. 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.
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "draught"
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(__FILE__)
@@ -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
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'draught/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "draught"
8
+ spec.version = Draught::VERSION
9
+ spec.authors = ["Matt Patterson"]
10
+ spec.email = ["matt@reprocessed.org"]
11
+
12
+ spec.summary = %q{A library for creating 2D vector graphics as PDF.}
13
+ spec.homepage = "https://github.com/fidothe/draught"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "prawn", "~> 2.1"
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency 'simplecov'
28
+ end
@@ -0,0 +1,4 @@
1
+ require "draught/version"
2
+
3
+ module Draught
4
+ end
@@ -0,0 +1,161 @@
1
+ require_relative './point'
2
+ require_relative './vector'
3
+ require_relative './cubic_bezier'
4
+ require_relative './curve'
5
+ require_relative './path'
6
+ require_relative './transformations'
7
+
8
+ module Draught
9
+ class ArcBuilder
10
+ # Largest arc segment representable by a single Cubic Bézier is 90º
11
+ # There's enough jitter in floating point maths that we round the result
12
+ # to 12 d.p. which means we avoid requiring two segments to represent an
13
+ # arc of 90.000000000000000001º
14
+ LARGEST_SEGMENT_RADIANS = (Math::PI / 2.0).round(12)
15
+ CIRCLE_RADIANS = Math::PI * 2
16
+
17
+ attr_reader :radius, :starting_angle, :radians
18
+
19
+ def self.deg_to_rad(degrees)
20
+ degrees * (Math::PI / 180)
21
+ end
22
+
23
+ def self.radians(args = {})
24
+ new_args = args.merge(radians: args[:angle])
25
+ new(new_args)
26
+ end
27
+
28
+ def self.degrees(args = {})
29
+ new_args = {radius: args[:radius]}
30
+ new_args[:radians] = deg_to_rad(args.fetch(:angle))
31
+ new_args[:starting_angle] = deg_to_rad(args.fetch(:starting_angle, 0))
32
+ new(new_args)
33
+ end
34
+
35
+ def initialize(args = {})
36
+ @radius = args.fetch(:radius)
37
+ @starting_angle = args.fetch(:starting_angle, 0)
38
+ @radians = args.fetch(:radians)
39
+ end
40
+
41
+ def path
42
+ @path ||= Path.new([start_point, curve])
43
+ end
44
+
45
+ def start_point
46
+ @start_point ||= untranslated_start_point.transform(transformer)
47
+ end
48
+
49
+ def curve
50
+ @curve ||= Curve.new({
51
+ point: segments.last.end_point,
52
+ cubic_beziers: segments.map { |s| s.cubic_bezier }
53
+ }).transform(transformer)
54
+ end
55
+
56
+ private
57
+
58
+ def untranslated_start_point
59
+ segments.first.first_point
60
+ end
61
+
62
+ def segments
63
+ @segments ||= begin
64
+ remaining_angle = positive_radians
65
+ start = starting_angle
66
+ segments = []
67
+ while remaining_angle > LARGEST_SEGMENT_RADIANS
68
+ remaining_angle = remaining_angle - LARGEST_SEGMENT_RADIANS
69
+ segments << SegmentBuilder.new(LARGEST_SEGMENT_RADIANS, start, radius)
70
+ start = positive_radians + starting_angle - remaining_angle
71
+ end
72
+ segments << SegmentBuilder.new(remaining_angle, start, radius)
73
+ end
74
+ end
75
+
76
+ def positive_radians
77
+ radians.abs
78
+ end
79
+
80
+ def transformer
81
+ @transformer ||= begin
82
+ transformations = [translation_transformer]
83
+ transformations << Transformations.x_axis_reflect if negative?
84
+ Transformations::Composer.compose(*transformations)
85
+ end
86
+ end
87
+
88
+ def translation_transformer
89
+ @translation_transformer ||= Vector.translation_to_zero(untranslated_start_point).to_transform
90
+ end
91
+
92
+ def negative?
93
+ !positive?
94
+ end
95
+
96
+ def positive?
97
+ radians >= 0
98
+ end
99
+
100
+ # @api: private
101
+ # Based on learnings from http://www.tinaja.com/glib/bezcirc2.pdf,
102
+ # via http://www.whizkidtech.redprince.net/bezier/circle/
103
+ class SegmentBuilder
104
+ attr_reader :sweep, :start, :radius
105
+
106
+ def initialize(sweep, start, radius)
107
+ @sweep, @start, @radius = sweep, start, radius
108
+ end
109
+
110
+ def first_point
111
+ @first_point ||= Point.new(x0, -y0).transform(transform)
112
+ end
113
+
114
+ def end_point
115
+ @end_point ||= Point.new(x0, y0).transform(transform)
116
+ end
117
+
118
+ def cubic_bezier
119
+ @cubic_bezier ||= CubicBezier.new({
120
+ end_point: end_point, control_point_1: control_point_1, control_point_2: control_point_2
121
+ })
122
+ end
123
+
124
+ private
125
+
126
+ def control_point_1
127
+ Point.new(x1, -y1).transform(transform)
128
+ end
129
+
130
+ def control_point_2
131
+ Point.new(x1, y1).transform(transform)
132
+ end
133
+
134
+ def transform
135
+ @transform ||= Transformations::Composer.compose(
136
+ Transformations.rotate(start + sweep_offset), Transformations.scale(radius)
137
+ )
138
+ end
139
+
140
+ def sweep_offset
141
+ @sweep_offset ||= sweep / 2.0
142
+ end
143
+
144
+ def x0
145
+ Math.cos(sweep_offset)
146
+ end
147
+
148
+ def y0
149
+ Math.sin(sweep_offset)
150
+ end
151
+
152
+ def x1
153
+ (4 - x0)/3.0
154
+ end
155
+
156
+ def y1
157
+ ((1 - x0) * (3 - x0)) / (3 * y0)
158
+ end
159
+ end
160
+ end
161
+ end