end of day commit

This commit is contained in:
2012-08-29 17:42:04 +02:00
parent 89700f36e9
commit 8213bae2c6
57 changed files with 1109 additions and 128 deletions
+31
View File
@@ -0,0 +1,31 @@
require 'action_controller'
require 'rqrcode'
require 'rqrcode-rails3/size_calculator.rb'
require 'rqrcode-rails3/renderers/svg.rb'
module RQRCode
Mime::Type.register "image/svg+xml", :svg unless Mime::Type.lookup_by_extension(:svg)
Mime::Type.register "image/png", :png unless Mime::Type.lookup_by_extension(:png)
extend SizeCalculator
ActionController::Renderers.add :qrcode do |string, options|
format = self.request.format.symbol
size = options[:size] || RQRCode.minimum_qr_size_from_string(string)
level = options[:level] || :h
qrcode = RQRCode::QRCode.new(string, :size => size, :level => level)
svg = RQRCode::Renderers::SVG::render(qrcode, options)
data = \
if format == :png
image = MiniMagick::Image.read(svg) { |i| i.format "svg" }
image.format "png"
png = image.to_blob
else
svg
end
self.response_body = render_to_string(:text => data, :template => nil)
end
end
+109
View File
@@ -0,0 +1,109 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="318.89764"
height="318.89764"
id="svg2991"
version="1.1"
inkscape:version="0.48.2 r9819"
sodipodi:docname="table_qr_image.svg">
<defs
id="defs3">
<filter
id="filter3789"
inkscape:label="Cutout"
x="0"
y="0"
width="1"
height="1"
inkscape:menu="Shadows and Glows"
inkscape:menu-tooltip="Drop shadow under the cut-out of the shape"
color-interpolation-filters="sRGB">
<feGaussianBlur
id="feGaussianBlur3791"
in="SourceAlpha"
stdDeviation="4.2" />
<feOffset
id="feOffset3793"
dy="5"
dx="5"
result="result91" />
<feComposite
id="feComposite3795"
in2="result91"
operator="out"
in="SourceGraphic" />
</filter>
</defs>
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="1.3773478"
inkscape:cx="107.03459"
inkscape:cy="78.51279"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
showgrid="false"
units="mm"
inkscape:window-width="1440"
inkscape:window-height="856"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="0" />
<metadata
id="metadata2995">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title />
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,141.73227)">
<text
xml:space="preserve"
style="font-size:40px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:semi-expanded;text-align:start;line-height:125%;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:start;fill:#ffb213;fill-opacity:1;stroke:none;font-family:BankGothic Md BT;-inkscape-font-specification:BankGothic Md BT Semi-Expanded"
x="80.589668"
y="-90.740822"
id="text4695"
sodipodi:linespacing="125%"><tspan
sodipodi:role="line"
id="tspan4697"
x="80.589668"
y="-90.740822">Qwaiter</tspan></text>
<g
id="g3838"
transform="matrix(0.49177687,0,0,0.49177687,49.419301,-77.511451)">#qrcode
</g>
<text
xml:space="preserve"
style="font-size:18px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:end;line-height:125%;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:end;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans"
x="277.10553"
y="160.40939"
id="text3799"
sodipodi:linespacing="125%"><tspan
sodipodi:role="line"
id="tspan3801"
x="277.10553"
y="160.40939">#table_number</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.5 KiB

+50
View File
@@ -0,0 +1,50 @@
module RQRCode
module Renderers
class SVG
class << self
# Render the SVG from the qrcode string provided from the RQRCode gem
# Options:
# offset - Padding around the QR Code (e.g. 10)
# unit - How many pixels per module (Default: 11)
# fill - Background color (e.g "ffffff" or :white)
# color - Foreground color for the code (e.g. "000000" or :black)
def render(qrcode, options={})
offset = options[:offset].to_i || 0
color = options[:color] || "000"
unit = options[:unit] || 11
# height and width dependent on offset and QR complexity
dimension = (qrcode.module_count*unit) + (2*offset)
xml_tag = %{<?xml version="1.0" standalone="yes"?>}
open_tag = %{<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events" width="#{dimension}" height="#{dimension}">}
close_tag = "</svg>"
result = []
qrcode.modules.each_index do |c|
tmp = []
qrcode.modules.each_index do |r|
y = c*unit + offset
x = r*unit + offset
next unless qrcode.is_dark(c, r)
tmp << %{<rect width="#{unit}" height="#{unit}" x="#{x}" y="#{y}" style="fill:##{color}"/>}
end
result << tmp.join("\n")
end
if options[:fill]
result.unshift %{<rect width="#{dimension}" height="#{dimension}" x="0" y="0" style="fill:##{options[:fill]}"/>}
end
svg = [xml_tag, open_tag, result, close_tag].flatten.join("\n")
svg = File.read(File.expand_path('../../qr_container.svg', __FILE__))
svg.gsub!(/#table_number/, options[:table_number].to_s)
svg.gsub!(/#qrcode/, result.join("\n"))
svg
end
end
end
end
end
+40
View File
@@ -0,0 +1,40 @@
module RQRCode
module SizeCalculator
# size - seems to follow this logic
# # | input | modules
# | size | created
#-------|-------|--------
# 1 | 7 | 21
# 2 | 14 | 25 (+4)
# 3 | 24 | 29 -
# 4 | 34 | 33 -
# 5 | 44 | 37 -
# 6 | 58 | 41 -
# 7 | 64 | 45 -
# 8 | 84 | 49 -
# 9 | 98 | 53 -
# 10 | 119 | 57 -
# 11 | 137 | 61 -
# 12 | 155 | 65 -
# 13 | 177 | 69 -
# 14 | 194 | 73 -
QR_CHAR_SIZE_VS_SIZE = [7, 14, 24, 34, 44, 58, 64, 84, 98, 119, 137, 155, 177, 194]
def minimum_qr_size_from_string(string)
QR_CHAR_SIZE_VS_SIZE.each_with_index do |size, index|
return (index + 1) if string.size < size
end
# If it's particularly big, we'll try and create codes until it accepts
i = QR_CHAR_SIZE_VS_SIZE.size
begin
i += 1
RQRCode::QRCode.new(string, :size => i)
return i
rescue RQRCode::QRCodeRunTimeError
retry
end
end
end
end