liquid_code 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/liquid_code.rb +116 -0
  3. metadata +87 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a272ced6e3f395ae26846debe69806ebe697e95a
4
+ data.tar.gz: 5543878c1b9559a014b08c1da6b2e21f336829b9
5
+ SHA512:
6
+ metadata.gz: 64c9b02a04137c366eea6ff44af06150ad27542e43b296acc5a1af61bd3cc4c895a29aab97ccecc61f7dbe921fa9b817643e999efdcf94504b9f022eb7a1fd54
7
+ data.tar.gz: 35130571f07a99d91728c5dfb4c84565a5594886113024cec6b9c32d87f52759b423245cbdff62b2a7a23889a1119887437141ce4b6b3ff1d193081f782eec51
@@ -0,0 +1,116 @@
1
+ require 'RMagick'
2
+ require 'rqrcode'
3
+ require 'active_support/all'
4
+
5
+ class LiquidCode
6
+ def initialize(qr)
7
+ unless qr.is_a?(RQRCode::QRCode)
8
+ # convert input's string represantation to qr_code
9
+ qr = RQRCode::QRCode.new( qr.to_s, :size => LiquidCode.minimum_qr_size_from_string(qr.to_s), :level => :h )
10
+ end
11
+
12
+ @qr = qr
13
+
14
+ end
15
+ def as_png(bgcolor='white', fgcolor='black', fgcolorwhite=true, block_size=16)
16
+ image_size=((@qr.modules.size+1) * block_size)
17
+ canvas_image = Magick::ImageList.new
18
+ canvas_image.new_image(image_size, image_size) do
19
+ self.background_color = bgcolor
20
+ self.format = "png"
21
+ end
22
+
23
+ canvas = Magick::Draw.new
24
+
25
+ draw_to_canvas(bgcolor, fgcolor, fgcolorwhite, canvas, 0, 0, block_size)
26
+
27
+ canvas.draw(canvas_image)
28
+ canvas_image.to_blob
29
+ end
30
+
31
+ def draw_to_canvas(bgcolor, fgcolor, fgcolorwhite, canvas, ox, oy, bs)
32
+
33
+ image_size=@qr.modules.size * bs
34
+ canvas.stroke(bgcolor)
35
+ canvas.fill(bgcolor)
36
+ canvas.rectangle(ox, oy, ox + image_size, oy + image_size)
37
+ canvas.stroke(fgcolor)
38
+ canvas.fill(fgcolor)
39
+ canvas.fill_opacity(1)
40
+ canvas.stroke_width(1)
41
+
42
+ # half block size
43
+ hbs = bs/2
44
+
45
+ is_dark = lambda { |x, y|
46
+ begin
47
+ return ((@qr.is_dark(y, x) and fgcolorwhite) or ((not @qr.is_dark(y, x)) and (not fgcolorwhite)))
48
+ rescue IndexError, RQRCode::QRCodeRunTimeError
49
+ return (not fgcolorwhite)
50
+ end
51
+ }
52
+
53
+ @qr.modules.each_index do |x|
54
+ @qr.modules.each_index do |y|
55
+ if is_dark.call(x, y)
56
+ canvas.roundrectangle(ox+(x * bs), oy+(y * bs), ox+((x+1) * bs), oy+((y+1) * bs), hbs, hbs)
57
+ if (is_dark.call(x, y+1))
58
+ canvas.rectangle(ox+(x * bs), oy+(y * bs)+hbs, ox+((x+1) * bs), oy+((y+1) * bs)+hbs)
59
+ end
60
+ if (is_dark.call(x+1, y))
61
+ canvas.rectangle(ox+(x * bs)+hbs, oy+(y * bs), ox+((x+1) * bs)+hbs, oy+((y+1) * bs))
62
+ end
63
+
64
+ end
65
+ end
66
+ end
67
+
68
+ @qr.modules.each_index do |x|
69
+ @qr.modules.each_index do |y|
70
+ if (not is_dark.call(x, y))
71
+
72
+ # I am white
73
+
74
+ # upper right corner should be round
75
+ if (is_dark.call(x+1, y) and is_dark.call(x, y-1) and is_dark.call(x+1, y-1))
76
+ canvas.rectangle(ox+(x * bs)+hbs, oy+(y * bs), ox+((x+1) * bs), oy+((y) * bs)+hbs)
77
+ end
78
+
79
+ # lower right corner should be round
80
+ if (is_dark.call(x+1, y) and is_dark.call(x+1, y+1) and is_dark.call(x, y+1))
81
+ canvas.rectangle(ox+(x * bs)+hbs, oy+(y * bs)+hbs, ox+((x+1) * bs), oy+((y+1) * bs))
82
+ end
83
+
84
+ # lower left corner should be round
85
+ if (is_dark.call(x-1, y) and is_dark.call(x-1, y+1) and is_dark.call(x, y+1))
86
+ canvas.rectangle(ox+(x * bs), oy+(y * bs)+hbs, ox+(x * bs)+hbs, oy+((y+1) * bs))
87
+ end
88
+
89
+ # upper left corner should be round
90
+ if (is_dark.call(x-1, y-1) and is_dark.call(x-1, y) and is_dark.call(x, y-1))
91
+ canvas.rectangle(ox+(x * bs), oy+(y * bs), ox+(x * bs)+hbs, oy+(y * bs)+hbs)
92
+ end
93
+
94
+ canvas.fill(bgcolor)
95
+ canvas.stroke(bgcolor)
96
+ canvas.roundrectangle(ox+(x * bs)+1, oy+(y * bs)+1, ox+((x+1) * bs)-1, oy+((y+1) * bs)-1, hbs, hbs)
97
+ canvas.stroke(fgcolor)
98
+ canvas.fill(fgcolor)
99
+
100
+ end
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ def LiquidCode.minimum_qr_size_from_string(str)
107
+ sizes = [7, 14, 24, 34, 44, 58, 64, 84, 98, 119, 137, 155, 177, 194]
108
+ sizes.each_index do |i|
109
+ if str.length <= sizes[i]
110
+ return i+1
111
+ end
112
+ end
113
+ return -1
114
+ end
115
+
116
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: liquid_code
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Juraj Bednar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rmagick
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.15'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rqrcode
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.10'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ description: This library uses ImageMagick and RQRCode to create QR codes with rounded
56
+ corners
57
+ email: juraj@bednar.sk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/liquid_code.rb
63
+ homepage: https://github.com/jooray/liquid-code
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.6.3
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Generate QR codes with rounded corners
87
+ test_files: []