chords 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -2,7 +2,7 @@ Chords
2
2
  ======
3
3
  A chord generator lib for guitar-like instruments.
4
4
 
5
- Using via gem (hosted at gemcutter.org)
5
+ Using via gem (hosted at rubygems.org)
6
6
  ---------------------------------------
7
7
 
8
8
  $ sudo gem install chords
@@ -49,10 +49,26 @@ module Chords
49
49
  @tuning = Chords::Fretboard::TUNINGS[t.to_sym]
50
50
  end
51
51
 
52
- @opts.on("--pdf", "Output to pdf.") do
52
+ @opts.on("--pdf", "Output to pdf. Requires Prawn") do
53
+ begin
54
+ require 'chords/pdf_formatter'
55
+ rescue LoadError => e
56
+ puts "#{e.message}\n\nMake sure you have 'prawn' gem installed."
57
+ exit(1)
58
+ end
53
59
  @formatter = Chords::PDFFormatter
54
60
  end
55
61
 
62
+ @opts.on("--html", "Output to html. Requires RMagick") do
63
+ begin
64
+ require 'chords/html_formatter'
65
+ rescue LoadError => e
66
+ puts "#{e.message}\n\nMake sure you have 'rmagick' gem installed."
67
+ exit(1)
68
+ end
69
+ @formatter = Chords::HTMLFormatter
70
+ end
71
+
56
72
  @opts.on_tail("-h", "--help", examples) do
57
73
  puts @opts
58
74
  end
@@ -87,6 +87,11 @@ module Chords
87
87
  @positions.hash
88
88
  end
89
89
 
90
+ # fingering id, a unique identifier for fretboard/fingering combo
91
+ def fid
92
+ @fretboard.open_notes.to_s + @positions.to_s
93
+ end
94
+
90
95
  # return an array of relative positions
91
96
  def relative(max_fret_dist)
92
97
  not_open = @positions.select{|p| !p.nil? and p > 0}
@@ -1,7 +1,6 @@
1
1
  require 'chords/chord_factory'
2
2
  require 'chords/fingering'
3
3
  require 'chords/text_formatter'
4
- require 'chords/pdf_formatter'
5
4
 
6
5
  module Chords
7
6
 
@@ -0,0 +1,84 @@
1
+ require 'rvg/rvg' # rmagick's RVG (Ruby Vector Graphics)
2
+ require 'base64'
3
+
4
+ Magick::RVG::dpi = 72
5
+
6
+ module Chords
7
+
8
+ # Formats fingerings as <img/> tags (base64-encoded data URIs)
9
+ class HTMLFormatter
10
+ class NonCache
11
+ def fetch(key); yield end
12
+ end
13
+
14
+ def initialize(fretboard, cache=NonCache.new)
15
+ @fretboard = fretboard
16
+ @cache = cache
17
+ end
18
+
19
+ # TODO: accept a separator element in opts
20
+ def print(title, fingerings, opts={})
21
+ @max_dist = opts[:max_fret_distance] || Fingering::DEFAULT_MAX_FRET_DISTANCE
22
+ html = "<h2>#{title}</h2>\n"
23
+
24
+ fingerings.each do |fingering|
25
+ html += get_element(fingering)
26
+ end
27
+
28
+ File.open('chords.html', 'w') do |file|
29
+ file.write html
30
+ end
31
+
32
+ puts "Wrote chords.html"
33
+ end
34
+
35
+ private
36
+
37
+ def get_element(fingering)
38
+
39
+ @cache.fetch(fingering.fid) do
40
+ rvg = Magick::RVG.new(5.cm, 5.cm).viewbox(0,0,270,250) do |canvas|
41
+ canvas.background_fill = 'white'
42
+ x_div = @fretboard.open_notes.size - 1
43
+
44
+ y_diff = 215 / (@max_dist + 1)
45
+
46
+ (@max_dist+2).times do |n|
47
+ canvas.line(20, n*y_diff+20, 250, n*y_diff+20)
48
+ end
49
+
50
+ @fretboard.open_notes.each_with_index do |note, i|
51
+ canvas.line(i*(230/x_div)+20, 20, i*(230/x_div)+20, 230)
52
+
53
+ unless [0,nil].include?(fingering[i])
54
+ canvas.circle(15, i*(230/x_div)+20,
55
+ fingering.relative(@max_dist)[i]*y_diff - 5)
56
+ end
57
+
58
+ canvas.text(i*(230/x_div)+20, 15) do |txt|
59
+ txt.tspan((fingering[i] || 'x').to_s).styles(
60
+ :text_anchor => 'middle',
61
+ :font_size => 20,
62
+ :font_family => 'helvetica',
63
+ :fill => 'black')
64
+ end
65
+ canvas.text(i*(230/x_div)+20, 249) do |txt|
66
+ txt.tspan(note.title).styles(:text_anchor => 'middle',
67
+ :font_size => 18,
68
+ :font_family => 'helvetica',
69
+ :fill => 'black')
70
+ end
71
+ end
72
+
73
+ end
74
+ img = rvg.draw
75
+ img.format = 'PNG'
76
+ "<img src=\"data:image/png;base64,#{Base64.encode64(img.to_blob)}\" />\n"
77
+ end
78
+ end
79
+
80
+ end
81
+
82
+ end
83
+
84
+ # TODO: change pdf and png formatter to be required only when needed
metadata CHANGED
@@ -1,11 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chords
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 13
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 2
8
- version: "0.2"
8
+ - 3
9
+ version: "0.3"
9
10
  platform: ruby
10
11
  authors:
11
12
  - Antti Hakala
@@ -13,23 +14,10 @@ autorequire:
13
14
  bindir: bin
14
15
  cert_chain: []
15
16
 
16
- date: 2010-03-31 00:00:00 +03:00
17
+ date: 2010-07-22 00:00:00 +03:00
17
18
  default_executable:
18
- dependencies:
19
- - !ruby/object:Gem::Dependency
20
- name: prawn
21
- prerelease: false
22
- requirement: &id001 !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- segments:
27
- - 0
28
- - 8
29
- - 4
30
- version: 0.8.4
31
- type: :runtime
32
- version_requirements: *id001
19
+ dependencies: []
20
+
33
21
  description: "Chords is a chord generator for guitar-like instruments. Handy for special tunings. "
34
22
  email: antti.hakala@gmail.com
35
23
  executables:
@@ -47,6 +35,7 @@ files:
47
35
  - lib/chords/command_line_parser.rb
48
36
  - lib/chords/fingering.rb
49
37
  - lib/chords/fretboard.rb
38
+ - lib/chords/html_formatter.rb
50
39
  - lib/chords/note.rb
51
40
  - lib/chords/pdf_formatter.rb
52
41
  - lib/chords/text_formatter.rb
@@ -61,23 +50,27 @@ rdoc_options: []
61
50
  require_paths:
62
51
  - lib
63
52
  required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
64
54
  requirements:
65
55
  - - ">="
66
56
  - !ruby/object:Gem::Version
57
+ hash: 3
67
58
  segments:
68
59
  - 0
69
60
  version: "0"
70
61
  required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
71
63
  requirements:
72
64
  - - ">="
73
65
  - !ruby/object:Gem::Version
66
+ hash: 3
74
67
  segments:
75
68
  - 0
76
69
  version: "0"
77
70
  requirements: []
78
71
 
79
72
  rubyforge_project:
80
- rubygems_version: 1.3.6
73
+ rubygems_version: 1.3.7
81
74
  signing_key:
82
75
  specification_version: 3
83
76
  summary: Chord generator for guitar-like instruments.