% !TeX program = LuaTeX
% Copyright (C) 2019-2022 Roberto Giacomelli

\newbox\mybox
\nopagenumbers

Code 128 encoder test.

Test 1: one simple barcode with default parameters:

\directlua{
barracuda = require "barracuda"
}

\directlua{
local barcode = barracuda:barcode()
barcode:set_param{debug_bbox="symb"}
local c128, err = barcode:new_encoder("code128")
assert(not err, err)

local symbo, err = c128:from_string("5465468132546812")
assert(not err, err)

local canvas = barracuda:new_canvas()
symbo:draw(canvas)

local drv = barracuda:get_driver()
drv:ga_to_hbox(canvas, "mybox")
}
\vrule A\vrule\box\mybox \vrule A\vrule

Test 2: two symbols, different anchor. The first symbol (more longer than the
second one) takes the anchor parameters from the super class Barcode, while
the second one takes anchors from itself:

\directlua{
local barcode = barracuda:barcode()
local ok, err = barcode:set_param{ax = 1, ay = 1} % super class set up
assert(ok, err)
assert(barcode.ax == 1, "barcode ax is "..tostring(barcode.ax))

local c128, err = barcode:enc_by_name("code128")
assert(c128.ax == 1, "c128 ax is "..tostring(c128.ax))

local s1, err = c128:from_string("12345678901234567890"); assert(s1, err)
local s2, err = c128:from_string("0987654321", {ax = 0, ay = 0}); assert(s2, err)

local s1_ax = s1:get_param("ax")
assert(s1.ax == 1, "1 ax is "..tostring(s1.ax))
local s2_ax = s2:get_param("ax")
assert(s2.ax == 0, "0 ax is "..tostring(s2.ax))

local canvas = barracuda:new_canvas()
s1:draw(canvas)
s2:draw(canvas)

local drv = barracuda:get_driver()
drv:ga_to_hbox(canvas, "mybox")
}\box\mybox

Test 3: play with the barcodes height (ydim parameter). The vertical anchor ay
will take to super class Barcode, previously set to 1.0. So, the symbols will
be aligned to the top:

\directlua{
local c128, err = barracuda:barcode()
                           :enc_by_name "code128";
assert(c128, err)

local s, err = c128:from_string("123456"); assert(s, err)
local canvas = barracuda:new_canvas()
s:draw(canvas)

local ok, err = s:set_param("ydim", tex.sp "30mm")
s:draw(canvas, tex.sp "20mm")

local ok, err = s:set_param("ydim", tex.sp "60mm")
s:draw(canvas, tex.sp "40mm")

local drv = barracuda:get_driver()
drv:ga_to_hbox(canvas, "mybox")
}\box\mybox

Test 4: set up vertical anchor globally to 0.0, locally to the encoder to 0.5
and locally to 1.0:

\directlua{
local barcode = barracuda:barcode()
% set ay globally to 0.0
local ok, err = barcode:set_param("ay", 0); assert(ok, err)

local c128, err = barcode:enc_by_name "code128"; assert(c128, err)
local s, err = c128:from_string("ABCDEFGHI"); assert(s, err)

local canvas = barracuda:new_canvas()
local _, err = s:draw(canvas); assert(not err, err)

% set ay to 0.5 for the encoder
local ok, err = c128:set_param("ay", 0.5); assert(ok, err)
local _, err = s:draw(canvas, tex.sp "30mm"); assert(not err, err)
% set locally to the symbol ay to 1.0
local ok, err = s:set_param("ay", 1)
local _, err = s:draw(canvas, tex.sp "60mm"); assert(not err, err)

local drv = barracuda:get_driver()
drv:ga_to_hbox(canvas, "mybox")
}\box\mybox

Test 5: uint() costructor and save a file with PDF literal code:

\directlua{
local barcode = barracuda:barcode()
local c128, err = barcode:enc_by_name("code128");
assert(c128, err)
local s, e = c128:from_uint(1234567890); assert(s, e)
local canvas = barracuda:new_canvas()
local _, errc = s:draw(canvas); assert(not errc, errc)
local drv = barracuda:get_driver()
drv:ga_to_hbox(canvas, "mybox")
drv:save("native", canvas, "02-05-pdfliteral")
}\box\mybox

Test 6: encode ABC123:

\directlua{
local barcode = barracuda:barcode()
local c128, err = barcode:enc_by_name "code128"; assert(c128, err)
local s, err = c128:from_string("ABC123"); assert(s, err)

local canvas = barracuda:new_canvas()
local _, err = s:draw(canvas); assert(not err, err)

local drv = barracuda:get_driver()
drv:ga_to_hbox(canvas, "mybox")
}\box\mybox

Test 7: encode ABC123999:

\directlua{
local barcode = barracuda:barcode()
local c128, err = barcode:enc_by_name "code128"; assert(c128, err)
local s, err = c128:from_string("ABC123999"); assert(s, err)

local canvas = barracuda:new_canvas()
local _, err = s:draw(canvas); assert(not err, err)

local drv = barracuda:get_driver()
drv:ga_to_hbox(canvas, "mybox")
}\box\mybox

Test 8: encode ABC123456789:

\directlua{
local barcode = barracuda:barcode()
local c128, err = barcode:enc_by_name "code128"; assert(c128, err)
local s, err = c128:from_string("ABC123456789"); assert(s, err)

local canvas = barracuda:new_canvas()
local _, err = s:draw(canvas); assert(not err, err)

local drv = barracuda:get_driver()
drv:ga_to_hbox(canvas, "mybox")
}\box\mybox

Test 9: encode A1B2C3D999:

\directlua{
local barcode = barracuda:barcode()
local c128, err = barcode:enc_by_name "code128"; assert(c128, err)
local s, err = c128:from_string("A1B2C3D999"); assert(s, err)

local canvas = barracuda:new_canvas()
local _, err = s:draw(canvas); assert(not err, err)

local drv = barracuda:get_driver()
drv:ga_to_hbox(canvas, "mybox")
}\box\mybox

Test 10: encode 123abcedfg:

\directlua{
local barcode = barracuda:barcode()
local c128, err = barcode:enc_by_name "code128"; assert(c128, err)
local s, err = c128:from_string("123abcedfg"); assert(s, err)

local canvas = barracuda:new_canvas()
local _, err = s:draw(canvas); assert(not err, err)

local drv = barracuda:get_driver()
drv:ga_to_hbox(canvas, "mybox")
}\box\mybox

Test 11: encode 999Aaaa:

\directlua{
local barcode = barracuda:barcode()
barcode:set_param{debug_bbox="none"}
local c128, err = barcode:enc_by_name "code128"; assert(c128, err)
local s, err = c128:from_string("999Aaaa"); assert(s, err)

local canvas = barracuda:new_canvas()
local _, err = s:draw(canvas); assert(not err, err)

local drv = barracuda:get_driver()
drv:ga_to_hbox(canvas, "mybox")
}\box\mybox

That's all folks!
\bye

.
