Sha256: c3fb93ceeeb4f9702ade992cbe3f8fec9e4c5d8812938696b6b94a606b26a796

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module Languages
  class Zpl2
    class BarcodeFactory
      def self.create_barcode(code_type, opts = {})
        if Barcode1D::BarcodeClasses.keys.include? code_type
          Barcode1D.new code_type,opts
        elsif Barcode2D::BarcodeClasses.keys.include? code_type
          Barcode2D.new code_type,opts
        else
          raise ArgumentException.new("Unknown barcode: #{code_type}")
        end
      end
    end

    class Barcode1D
      BarcodeClasses = {
                        code_128: "BC",
                        ean13: "BE"
                       }
      def initialize(code_type, opts = {})
        @code = BarcodeClasses[code_type]
        @font = opts[:font] || Font.new
        @human_readable = opts[:human_readable] || "Y"
        @text = opts[:text] || ""
      end

      def render
        "^#{@code}#{@font.rotation},#{@font.height*2},#{@human_readable},N,N^FD#{@text}^FS"
      end
    end

    class Barcode2D
      BarcodeClasses = {
                        data_matrix: ["X",4,16,16]
                       }

      def initialize(code_type, opts = {})
        @font = opts[:font] || Font.new
        @code, @symbol_height, @columns_encode, @rows_encode = BarcodeClasses[code_type]
        @text = opts[:text] || ""
      end
      
      def render
        "^B#{@code}#{@font.rotation},#{@symbol_height},200,#{@columns_encode},#{@rows_encode}^FD#{@text}^FS"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zebra_printer-0.1.1 lib/languages/zpl2/barcode.rb