summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/barracuda/test/test-code128/002-code128-test.tex
blob: eeaf60e48321ca0fdec135e9820635e6b9dae72b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
% !TeX program = LuaTeX
% Copyright (C) 2019 Roberto Giacomelli

\newbox\mybox
\nopagenumbers

Code 128 encoder test.

Test 1: one simple barcode with default parameter:

\directlua{
barracuda = require "barracuda"
}

\directlua{
local barcode = barracuda:get_barcode_class()

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:append_ga(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:get_barcode_class()
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:append_ga(canvas)
s2:append_ga(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:get_barcode_class()
                           :enc_by_name "code128"; assert(c128, err)

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

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

local ok, err = s:set_param("ydim", tex.sp "60mm")
s:append_ga(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:get_barcode_class()
% 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:append_ga(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:append_ga(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:append_ga(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:get_barcode_class()
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:append_ga(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

That's all folk!

\bye

function Driver:save(id_drv, ga, filename, ext) --> ok, err