Class | Barby::SvgOutputter |
In: |
lib/barby/outputter/svg_outputter.rb
|
Parent: | Outputter |
Renders the barcode to a simple SVG image using pure ruby
Registers the to_svg, bars_to_path, and bars_to_rects method
Bars can be rendered as a stroked path or as filled rectangles. Path generally yields smaller files, but this doesn‘t render cleanly in Firefox 3 for odd xdims. My guess is that the renderer tries to put half a pixel on one side of the path and half on the other, leading to fuzzy dithering instead of sharp, clean b&w.
Therefore, default behavior is to use a path for even xdims, and rectangles for odd. This can be overridden by calling with explicit :use => ‘rects’ or :use => ‘path’ options.
bmargin | [W] | |
height | [W] | |
lmargin | [W] | |
margin | [W] | |
rmargin | [W] | |
title | [W] | |
tmargin | [W] | |
xdim | [W] | |
xmargin | [W] | |
ydim | [W] | |
ymargin | [W] |
# File lib/barby/outputter/svg_outputter.rb, line 85 85: def bars_to_path(opts={}) 86: with_options opts do 87: %Q|<path stroke="black" stroke-width="#{xdim}" d="#{bars_to_path_data(opts)}" />| 88: end 89: end
# File lib/barby/outputter/svg_outputter.rb, line 92 92: def bars_to_path_data(opts={}) 93: path_data = '' 94: with_options opts do 95: x, y = lmargin+(xdim/2), tmargin 96: 97: if barcode.two_dimensional? 98: booleans.each do |line| 99: line.each do |bar| 100: if bar 101: path_data << "M#{x} #{y}V #{y+ydim}" 102: end 103: x += xdim 104: end 105: y += ydim 106: x = lmargin+(xdim/2) 107: end 108: 109: else 110: booleans.each do |bar| 111: if bar 112: path_data << "M#{x} #{y}V#{y+height}" 113: end 114: x += xdim 115: end 116: 117: end 118: end # with_options 119: 120: path_data 121: end
# File lib/barby/outputter/svg_outputter.rb, line 51 51: def bars_to_rects(opts={}) 52: rects = '' 53: with_options opts do 54: x, y = lmargin, tmargin 55: 56: if barcode.two_dimensional? 57: boolean_groups.each do |line| 58: line.each do |bar, amount| 59: bar_width = xdim * amount 60: if bar 61: rects << %Q|<rect x="#{x}" y="#{y}" width="#{bar_width}px" height="#{ydim}px" />\n| 62: end 63: x += bar_width 64: end 65: y += ydim 66: x = lmargin 67: end 68: 69: else 70: boolean_groups.each do |bar, amount| 71: bar_width = xdim * amount 72: if bar 73: rects << %Q|<rect x="#{x}" y="#{y}" width="#{bar_width}px" height="#{height}px" />\n| 74: end 75: x += bar_width 76: end 77: 78: end 79: end # with_options 80: 81: rects 82: end
# File lib/barby/outputter/svg_outputter.rb, line 165 165: def bmargin 166: @bmargin || _ymargin 167: end
# File lib/barby/outputter/svg_outputter.rb, line 141 141: def full_height 142: height + tmargin + bmargin 143: end
# File lib/barby/outputter/svg_outputter.rb, line 137 137: def full_width 138: width + lmargin + rmargin 139: end
# File lib/barby/outputter/svg_outputter.rb, line 133 133: def height 134: barcode.two_dimensional? ? (ydim * encoding.length) : (@height || 100) 135: end
# File lib/barby/outputter/svg_outputter.rb, line 184 184: def length 185: barcode.two_dimensional? ? encoding.first.length : encoding.length 186: end
# File lib/barby/outputter/svg_outputter.rb, line 153 153: def lmargin 154: @lmargin || _xmargin 155: end
# File lib/barby/outputter/svg_outputter.rb, line 179 179: def margin 180: return nil if @ymargin || @xmargin || @tmargin || @bmargin || @lmargin || @rmargin 181: _margin 182: end
# File lib/barby/outputter/svg_outputter.rb, line 157 157: def rmargin 158: @rmargin || _xmargin 159: end
# File lib/barby/outputter/svg_outputter.rb, line 193 193: def svg_height(opts={}) 194: opts[:rot] ? full_width : full_height 195: end
# File lib/barby/outputter/svg_outputter.rb, line 189 189: def svg_width(opts={}) 190: opts[:rot] ? full_height : full_width 191: end
# File lib/barby/outputter/svg_outputter.rb, line 124 124: def title 125: @title || barcode.to_s 126: end
# File lib/barby/outputter/svg_outputter.rb, line 161 161: def tmargin 162: @tmargin || _ymargin 163: end
# File lib/barby/outputter/svg_outputter.rb, line 25 25: def to_svg(opts={}) 26: with_options opts do 27: case opts[:use] 28: when 'rects' then bars = bars_to_rects 29: when 'path' then bars = bars_to_path 30: else 31: xdim_odd = (xdim % 2 == 1) 32: bars = xdim_odd ? bars_to_rects : bars_to_path 33: end 34: 35: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"\#{svg_width(opts)}px\" height=\"\#{svg_height(opts)}px\" viewBox=\"0 0 \#{svg_width(opts)} \#{svg_height(opts)}\" version=\"1.1\">\n<title>\#{escape title}</title>\n<g id=\"canvas\" \#{transform(opts)}>\n<rect x=\"0\" y=\"0\" width=\"\#{full_width}px\" height=\"\#{full_height}px\" fill=\"white\" />\n<g id=\"barcode\" fill=\"black\">\n\#{bars}\n</g></g>\n</svg>\n" 36: end 37: end
# File lib/barby/outputter/svg_outputter.rb, line 198 198: def transform(opts={}) 199: opts[:rot] ? %Q|transform="rotate(-90) translate(-#{full_width}, 0)"| : nil 200: end
# File lib/barby/outputter/svg_outputter.rb, line 169 169: def xmargin 170: return nil if @lmargin || @rmargin 171: _margin 172: end
# File lib/barby/outputter/svg_outputter.rb, line 174 174: def ymargin 175: return nil if @tmargin || @bmargin 176: _margin 177: end
# File lib/barby/outputter/svg_outputter.rb, line 205 205: def _xmargin 206: @xmargin || _margin 207: end
# File lib/barby/outputter/svg_outputter.rb, line 209 209: def _ymargin 210: @ymargin || _margin 211: end