sketch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .irbrc
6
+
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sketch.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'test-unit'
8
+ gem 'mocha'
9
+ end
10
+
@@ -0,0 +1,12 @@
1
+ Introducing Sketch
2
+ ====================
3
+
4
+
5
+ Examples
6
+ -------
7
+
8
+ _basic syntax_
9
+
10
+ class Smile < Sketch::Base
11
+
12
+
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require 'rake'
7
+ require 'rake/testtask'
8
+
9
+ desc "Run unit tests"
10
+ Rake::TestTask.new("test") { |t|
11
+ t.libs << 'test'
12
+ t.pattern = 'test/*.rb'
13
+ t.verbose = true
14
+ t.warning = true
15
+ }
16
+
17
+ desc 'Generate HTML readme file'
18
+ task :readme do
19
+ `markdown README.markdown > README.html`
20
+ end
21
+
22
+ desc 'clean temporary files, rdoc, and gem package'
23
+ task :clean => [:clobber_package, :clobber_rdoc] do
24
+ temp_filenames = File.join('**', '*.*~')
25
+ temp_files = Dir.glob(temp_filenames)
26
+
27
+ File.delete(*temp_files)
28
+ end
29
+
@@ -0,0 +1,4 @@
1
+ 1. Add all SVG elements
2
+ 2. Add all attributes to the canvas
3
+ 3. Add Element#to_raphael http://raphaeljs.com/
4
+
@@ -0,0 +1,24 @@
1
+ class Smile < Sketch
2
+
3
+ # def draw( canvas )
4
+ # circle.new(:id => 'face', :cx => 200, :cy => 200, :r => 195, :fill => "yellow")
5
+ # ellipse(:id => 'left-eye', :center => [120, 150], :radius => [p18, 33], :fill => 'black')
6
+ # ellipse(:id => 'right-eye', :center => [280, 150], :radius => [18, 33], :fill => 'black')
7
+ # path(:id => 'mount', :stoke_width => '10', :stroke => 'black', :stroke_linecap => 'round') do |instructions|
8
+ # instructions.move_to 120, 280
9
+ # instructions.q 200, 330 280,280
10
+ # end
11
+ # end
12
+
13
+ def draw( canvas )
14
+ Circle.new(:id => 'face', :cx => 200, :cy => 200, :r => 195, :fill => "yellow").draw(canvas)
15
+ Sketch::Ellipse.new(:id => 'left-eye', :cx => 120, :cy => 150, :rx => 18, :ry => 33, :fill => 'black').draw(canvas)
16
+ Sketch::Ellipse.new(:id => 'right-eye', :cx => 280, :cy => 150, :rx => 18, :ry => 33, :fill => 'black').draw(canvas)
17
+ Sketch::Path.new(:id => 'mouth', :stroke_width => 10, :stroke => 'black', :fill => 'none', :stroke_linecap => 'round', :d => ['M120,280 Q200,330 280,280']).draw(canvas)
18
+ end
19
+
20
+ end
21
+
22
+
23
+
24
+
@@ -0,0 +1,49 @@
1
+ class Sketch < Valuable
2
+
3
+ has_value :id
4
+ has_value :klass
5
+ has_value :height
6
+ has_value :width
7
+ has_value :baseProfile, :alias => :base_profile
8
+ has_value :doctype
9
+
10
+ def draw(canvas)
11
+ raise NotImplementedError
12
+ end
13
+
14
+ def to_svg( )
15
+ svg_builder do |canvas|
16
+ self.draw( canvas )
17
+ end.to_xml
18
+ end
19
+
20
+ def to_html
21
+ svg_builder do |canvas|
22
+ self.draw( canvas )
23
+ end.doc.root.to_s
24
+ end
25
+
26
+ def svg_attributes
27
+ out = {}
28
+ out[:height] = self.height if self.height
29
+ out[:width] = self.width if self.width
30
+ out[:doctype] = self.doctype if self.doctype
31
+ out
32
+ end
33
+
34
+ def svg_builder
35
+ Canvas.new( svg_attributes ).builder {|canvas| yield canvas}
36
+ end
37
+
38
+ end
39
+
40
+ require 'sketch/base'
41
+ require 'sketch/element'
42
+ require 'sketch/canvas'
43
+
44
+ require 'sketch/circle'
45
+ require 'sketch/ellipse'
46
+ require 'sketch/rect'
47
+ require 'sketch/rectangle'
48
+ require 'sketch/path'
49
+
@@ -0,0 +1,15 @@
1
+ module Sketch::Base
2
+ def svg_attributes
3
+ out = {}
4
+
5
+ #svg is heavy in dashes. Ruby symbols can't handle them.
6
+ attributes.each do |name, value|
7
+ out[name.to_s.gsub('_', '-')] = value
8
+ end
9
+
10
+ out[:class] = out.delete(:klass) if out.has_key?(:klass)
11
+
12
+ out
13
+ end
14
+ end
15
+
@@ -0,0 +1,43 @@
1
+ class Sketch::Canvas < Valuable
2
+ include Sketch::Base
3
+
4
+ has_value :version, :default => '1.1'
5
+ has_value :height
6
+ has_value :width
7
+ has_value :doctype, :default => false
8
+
9
+ def namespace_bindings
10
+ {
11
+ 'xmlns' => 'http://www.w3.org/2000/svg',
12
+ 'xmlns:ev' => 'http://www.w3.org/2001/xml-events',
13
+ 'xmlns:xlink' => 'http://www.w3.org/1999/xlink'
14
+ }
15
+ end
16
+
17
+ def builder
18
+ Nokogiri::XML::Builder.new do |document|
19
+ render_doctype( document )
20
+ document.svg( svg_attributes ) {|svg| yield svg if block_given?}
21
+ end
22
+ end
23
+
24
+ def default_doctype
25
+ ['svg', "-//W3C//DTD SVG 1.0//EN", "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"]
26
+ end
27
+
28
+ def render_doctype( document )
29
+ self.doctype &&= self.default_doctype
30
+
31
+ if( self.doctype )
32
+ document.doc.create_internal_subset(*self.doctype)
33
+ end
34
+ end
35
+
36
+ def svg_attributes
37
+ atts = super
38
+ atts.delete('doctype')
39
+ atts.merge(namespace_bindings)
40
+ end
41
+
42
+ end
43
+
@@ -0,0 +1,14 @@
1
+ class Sketch::Circle < Sketch::Element
2
+
3
+ has_value :cx
4
+ has_value :cy
5
+ has_value :r, :alias => :radius
6
+ has_value :fill
7
+ has_value :stroke
8
+ has_value :stroke_width
9
+
10
+ def draw(canvas)
11
+ canvas.circle(:cx => self.cx, :cy => self.cy, :r => self.radius, :fill => self.fill, :stroke => self.stroke, 'stroke_width' => self.stroke_width)
12
+ end
13
+ end
14
+
@@ -0,0 +1,16 @@
1
+ class Sketch::Element < Valuable
2
+ include Sketch::Base
3
+
4
+ has_value :id
5
+ has_value :klass
6
+ has_value :style
7
+
8
+ def draw(canvas)
9
+ canvas.send( self.svg_node, svg_attributes )
10
+ end
11
+
12
+ def svg_node
13
+ self.class.name.split('::').last.downcase
14
+ end
15
+ end
16
+
@@ -0,0 +1,13 @@
1
+ class Sketch::Ellipse < Sketch::Element
2
+
3
+ has_value :cx
4
+ has_value :cy
5
+ has_value :rx
6
+ has_value :ry
7
+ has_value :radius
8
+ has_value :fill
9
+ has_value :stroke
10
+ has_value :stroke_width
11
+
12
+ end
13
+
@@ -0,0 +1,17 @@
1
+ class Sketch::Path < Sketch::Element
2
+
3
+ has_value :fill
4
+ has_value :stroke
5
+ has_value :stroke_width
6
+ has_value :stroke_linecap
7
+ has_value :d, :alias => 'commands', :default => []
8
+
9
+ def draw(canvas)
10
+ canvas.path(svg_attributes)
11
+ end
12
+
13
+ def svg_attributes
14
+ super.merge(:d => self.commands.join(' '))
15
+ end
16
+ end
17
+
@@ -0,0 +1,14 @@
1
+ class Sketch::Rect < Sketch::Element
2
+
3
+ has_value :x
4
+ has_value :y
5
+ has_value :width
6
+ has_value :height
7
+ has_value :rx
8
+ has_value :ry
9
+ has_value :fill
10
+ has_value :stroke
11
+ has_value :stroke_width
12
+
13
+ end
14
+
@@ -0,0 +1 @@
1
+ Sketch::Rectangle = Sketch::Rect
@@ -0,0 +1,25 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ version = File.read(File.expand_path("../sketch.version",__FILE__)).strip
3
+
4
+ spec = Gem::Specification.new do |s|
5
+ s.name = 'sketch'
6
+ s.version = version
7
+ s.summary = "build SVG images"
8
+ s.description = "build SVG images"
9
+ s.license = 'MIT'
10
+
11
+ s.require_path = 'lib'
12
+
13
+ s.add_dependency('valuable', '> 0.8')
14
+ s.add_dependency('nokogiri', '>= 1.4')
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.require_paths = ["lib"]
19
+
20
+
21
+ s.authors = ["Johnathon Wright"]
22
+ s.email = "jw@mustmodify.com"
23
+ s.homepage = "http://sketch.mustmodify.com/"
24
+ end
25
+
@@ -0,0 +1 @@
1
+ 0.0.1
Binary file
@@ -0,0 +1,16 @@
1
+ require 'test/helper.rb'
2
+
3
+ class Squigly < Sketch::Element
4
+ has_value :reflective_coating
5
+ end
6
+
7
+ class SketchTest < Test::Unit::TestCase
8
+
9
+ def test_that_node_is_based_on_name
10
+ canvas = stub
11
+ canvas.expects(:squigly)
12
+ Squigly.new.draw(canvas)
13
+ end
14
+
15
+ end
16
+
@@ -0,0 +1,16 @@
1
+ require 'helper'
2
+
3
+ class CanvasTest < Test::Unit::TestCase
4
+
5
+ def test_that_width_is_configurable
6
+ art = Sketch::Canvas.new(:width => 350).builder.to_xml
7
+ assert_equal 1, Nokogiri::XML(art).css('svg[width="350"]').size
8
+ end
9
+
10
+ def test_that_height_is_configurable
11
+ art = Sketch::Canvas.new(:height => 450).builder.to_xml
12
+ assert_equal 1, Nokogiri::XML(art).css('svg[height="450"]').size
13
+ end
14
+
15
+ end
16
+
@@ -0,0 +1,26 @@
1
+ require 'helper'
2
+
3
+ class Minimalist < Sketch
4
+ def draw( canvas )
5
+ end
6
+ end
7
+
8
+ class DoctypeTest < Test::Unit::TestCase
9
+
10
+ def test_that_doctype_is_off_by_default
11
+ assert_equal false, Sketch::Canvas.new.doctype
12
+ end
13
+
14
+ def test_that_it_does_not_include_doctype_by_default
15
+ # https://jwatt.org/svg/authoring/#doctype-declaration
16
+ artwork = Minimalist.new.to_svg
17
+ assert_no_match(/doctype/i, artwork)
18
+ end
19
+
20
+ def test_that_it_includes_doctype_when_configured
21
+ artwork = Minimalist.new(:doctype => true).to_svg
22
+ assert_match(/doctype/i, artwork)
23
+ end
24
+
25
+ end
26
+
@@ -0,0 +1,15 @@
1
+ #prerequisites
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+ require 'mocha'
5
+ require 'nokogiri'
6
+ require 'valuable'
7
+
8
+ $:.push File.expand_path("./../../lib", __FILE__)
9
+ $:.push File.expand_path("./../../lib/sketch", __FILE__)
10
+
11
+ require 'sketch'
12
+ require 'sketch/canvas'
13
+ require 'sketch/base'
14
+ require 'sketch/element'
15
+
@@ -0,0 +1,23 @@
1
+ require 'helper'
2
+
3
+ class Minimalist < Sketch
4
+ def draw( canvas )
5
+ end
6
+ end
7
+
8
+ class DoctypeTest < Test::Unit::TestCase
9
+
10
+ def test_that_it_adds_namespace_binds_by_default
11
+ # https://jwatt.org/svg/authoring/#doctype-declaration
12
+ artwork = Minimalist.new.to_svg
13
+ svgns = %r(xmlns="http://www.w3.org/2000/svg")
14
+ xmlns = %r(xmlns:xlink="http://www.w3.org/1999/xlink")
15
+ xlinkns = %r(xmlns:ev="http://www.w3.org/2001/xml-events")
16
+
17
+ assert_match svgns, artwork
18
+ assert_match xmlns, artwork
19
+ assert_match xlinkns, artwork
20
+ end
21
+
22
+ end
23
+
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sketch
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Johnathon Wright
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-19 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: valuable
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">"
27
+ - !ruby/object:Gem::Version
28
+ hash: 27
29
+ segments:
30
+ - 0
31
+ - 8
32
+ version: "0.8"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: nokogiri
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 1
46
+ - 4
47
+ version: "1.4"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: build SVG images
51
+ email: jw@mustmodify.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - Gemfile
61
+ - README.markdown
62
+ - Rakefile
63
+ - TODO.txt
64
+ - examples/smile.rb
65
+ - lib/sketch.rb
66
+ - lib/sketch/base.rb
67
+ - lib/sketch/canvas.rb
68
+ - lib/sketch/circle.rb
69
+ - lib/sketch/element.rb
70
+ - lib/sketch/ellipse.rb
71
+ - lib/sketch/path.rb
72
+ - lib/sketch/rect.rb
73
+ - lib/sketch/rectangle.rb
74
+ - sketch.gemspec
75
+ - sketch.version
76
+ - test/.canvas_test.rb.swp
77
+ - test/base_test.rb
78
+ - test/canvas_test.rb
79
+ - test/doctype_test.rb
80
+ - test/helper.rb
81
+ - test/svg_namespace_test.rb
82
+ homepage: http://sketch.mustmodify.com/
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ requirements: []
109
+
110
+ rubyforge_project:
111
+ rubygems_version: 1.7.2
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: build SVG images
115
+ test_files: []
116
+