Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Saturday 24 April 2010

Faux barcode using ruby-processing Context Free DSL

Here I create a faux bar code (approximates to a 2 from 5 version), the output is random, apart from the start and end bars and the number text. Groups of 5 bars are created, which will on average will contain two wide and 3 narrow bars. The text is drawn using native ruby-processing, the bars are produced using the context free DSL.


#########################
# bar_code.rb (2 of 5)
# demonstrates the vbar
# custom terminal
#########################
load_library 'context_free'

def setup_the_barcode
  @barcode = ContextFree.define do
    ############ Begin defining custom terminal, a proportional vertical bar
    class << self    
      define_method(:vbar) do |some_options|
        size, options = *self.get_shape_values(some_options)
        w = some_options[:w]|| 0.1    # default vbar width is 0.1
        ratio = w * size
        rot = options[:rotation]
        rect_mode(CENTER)
        rotate(rot) if rot
        rect(-0.5 * ratio, -0.4 * size, 0.5 * ratio, 0.6 * size)
        rotate(-rot) if rot
      end
    end
    ########### End definition of custom terminal 'vbar'
    rule :strip do
      2.times do
        end_bar :x => 0.09
      end
      4.times do
        five :x => 0.04
      end
      2.times do
        end_bar :x => 0.09
      end
    end
    
    rule :bar, 1 do             # wide
      vbar :size => 0.8, :w => 0.08, :brightness => -1
    end
  
    rule :bar, 1 do             # wide
      vbar :size => 0.8, :w => 0.06, :brightness => -1
    end
  
    rule :bar, 1.6 do             # narrow
      vbar :size => 0.8, :w => 0.02, :brightness => -1
    end
  
    rule :bar, 1.6 do             # narrow
      vbar :size => 0.8, :w => 0.03, :brightness => -1
    end
  
    rule :end_bar, 1.6 do         # narrow extra long
      vbar :size => 1, :w => 0.03, :brightness => -1
    end

    rule :five do
      5.times do
      bar :x => 0.06
    end
    end    
    end
  end

def setup
  size 350, 200
  text_font(create_font("Dialog.plain", 24), 24)
  background 255, 255, 0
  draw_text
  smooth
  setup_the_barcode
  draw_it
end

def draw_it
  @barcode.render :strip, :start_x => 0, :start_y => height,
               :size => height
end

def draw_text
  code = "23467"
  fill 0
  text code, 40, 80
end




No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2