rqrcode_to_svg 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/rqrcode_to_svg.rb +91 -0
- data/lib/rqrcode_to_svg/version.rb +3 -0
- data/rqrcode_to_svg.gemspec +26 -0
- metadata +81 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require "rqrcode_to_svg/version"
|
2
|
+
|
3
|
+
module RQRCode
|
4
|
+
class QRCode
|
5
|
+
|
6
|
+
def to_svg( *args )
|
7
|
+
|
8
|
+
# Options.
|
9
|
+
|
10
|
+
# The :border option sets the border around the QR code.
|
11
|
+
# true adds a default border, with a size of four QR Code-points.
|
12
|
+
# false adds no border. This is the default.
|
13
|
+
# Or, enter a number (as a measure of QR Code-points) to set your own custom border size.
|
14
|
+
|
15
|
+
# The :px option determines the size of each QR Code-point in pixels. The default is 4 pixels.
|
16
|
+
# Throughout the conversion, we'll multiply each QR Code-point by this :px value.
|
17
|
+
|
18
|
+
# The :file option sets the path to which the SVG file will be written.
|
19
|
+
# If set to false (default), the SVG XML will be returned as a string.
|
20
|
+
|
21
|
+
# The :namespace option is a boolean that adds the "svg" XML namespace prefix. Default is false.
|
22
|
+
|
23
|
+
# The :indent option is used to pretty print the XML. Defaults to false.
|
24
|
+
# false gives a tag soup with no line breaks.
|
25
|
+
# true adds line breaks and indents each child element by two spaces.
|
26
|
+
# A String object adds line breaks and will be used as the literal indentation.
|
27
|
+
# A number adds line breaks and will be interpreted as the number of spaces to indent.
|
28
|
+
|
29
|
+
options = args.extract_options!
|
30
|
+
border = options[:border] || false
|
31
|
+
px_multiplier = options[:px] || 4
|
32
|
+
file = options[:file] || false
|
33
|
+
namespace = options[:namespace] || false
|
34
|
+
indent = options[:indent] || false
|
35
|
+
|
36
|
+
# The following case evaluates the :border option. Goal: to set the length of the border.
|
37
|
+
border_length = 0
|
38
|
+
case border
|
39
|
+
when TrueClass
|
40
|
+
border_length = 4 * px_multiplier
|
41
|
+
when String, Numeric
|
42
|
+
border_length = border.to_i * px_multiplier
|
43
|
+
end
|
44
|
+
|
45
|
+
# The following case evaluates the :indent option. Goal: to pretty print the XML if desired.
|
46
|
+
indentation = String.new
|
47
|
+
case indent
|
48
|
+
when TrueClass
|
49
|
+
indentation = " " * 2
|
50
|
+
when String
|
51
|
+
indentation = indent
|
52
|
+
when Numeric
|
53
|
+
indentation = " " * indent
|
54
|
+
end
|
55
|
+
|
56
|
+
# qr_side_length calculates the side length of the QR code *without the border*.
|
57
|
+
qr_side_length = px_multiplier * @module_count
|
58
|
+
|
59
|
+
# total_side_length calculates the side length of the QR code with the border included.
|
60
|
+
total_side_length = qr_side_length + (border_length * 2)
|
61
|
+
|
62
|
+
# Find all of the "dark" points within the QR code.
|
63
|
+
# For each dark point, add a <rect/> SVG XML element (as a String) to the svg_squares array.
|
64
|
+
svg_squares = Array.new
|
65
|
+
@modules.each_index do |y|
|
66
|
+
@modules.each_index do |x|
|
67
|
+
if dark?(y,x)
|
68
|
+
svg_squares.push("#{indentation}<#{"svg:" if namespace}rect x='#{(border_length + (px_multiplier * x))}px' y='#{(border_length + (px_multiplier * y))}px' width='#{px_multiplier}px' height='#{px_multiplier}px' style='fill:rgb(0,0,0)'/>#{"\n" if indent}")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Constuct the SVG XML.
|
74
|
+
svg = "<#{"svg:" if namespace}svg xmlns#{":svg" if namespace}='http://www.w3.org/2000/svg' version='1.1' baseProfile='full' width='#{total_side_length}px' height='#{total_side_length}px'>#{"\n" if indent}#{svg_squares.join}</#{"svg:" if namespace}svg>"
|
75
|
+
|
76
|
+
# Checks the file option.
|
77
|
+
# If a path is given, write the SVG to the file and return the path.
|
78
|
+
# If a path is not given, return the SVG as a string.
|
79
|
+
if file
|
80
|
+
File.open(file, 'w') do |file|
|
81
|
+
file.write(("<?xml version='1.0' encoding='UTF-8'?>\n" + svg))
|
82
|
+
end
|
83
|
+
return file
|
84
|
+
else
|
85
|
+
return svg
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rqrcode_to_svg/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rqrcode_to_svg"
|
7
|
+
s.version = RqrcodeToSvg::VERSION
|
8
|
+
s.authors = ["Jon Jensen"]
|
9
|
+
s.email = ["jdjensen.public@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/verisimilitudinous/rqrcode_to_svg"
|
11
|
+
s.summary = %q{Creates an SVG graphic of a QR Code.}
|
12
|
+
s.description = %q{Creates an SVG graphic of a QR Code. Adds a to_svg method to the RQRCode::QRCode class provided by the 'rqrcode' gem.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "rqrcode_to_svg"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
s.add_development_dependency "rqrcode"
|
25
|
+
s.add_runtime_dependency "rqrcode"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rqrcode_to_svg
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jon Jensen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-11-21 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rqrcode
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rqrcode
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
description: Creates an SVG graphic of a QR Code. Adds a to_svg method to the RQRCode::QRCode class provided by the 'rqrcode' gem.
|
38
|
+
email:
|
39
|
+
- jdjensen.public@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- Rakefile
|
50
|
+
- lib/rqrcode_to_svg.rb
|
51
|
+
- lib/rqrcode_to_svg/version.rb
|
52
|
+
- rqrcode_to_svg.gemspec
|
53
|
+
homepage: http://github.com/verisimilitudinous/rqrcode_to_svg
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project: rqrcode_to_svg
|
76
|
+
rubygems_version: 1.8.11
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Creates an SVG graphic of a QR Code.
|
80
|
+
test_files: []
|
81
|
+
|