diff options
Diffstat (limited to 'macros/luatex/generic/barracuda/src/lib-barcode/brcd-code39.lua')
-rw-r--r-- | macros/luatex/generic/barracuda/src/lib-barcode/brcd-code39.lua | 164 |
1 files changed, 71 insertions, 93 deletions
diff --git a/macros/luatex/generic/barracuda/src/lib-barcode/brcd-code39.lua b/macros/luatex/generic/barracuda/src/lib-barcode/brcd-code39.lua index 6d7f2ccea7..0768912987 100644 --- a/macros/luatex/generic/barracuda/src/lib-barcode/brcd-code39.lua +++ b/macros/luatex/generic/barracuda/src/lib-barcode/brcd-code39.lua @@ -1,31 +1,27 @@ -- Code39 barcode encoder implementation --- Copyright (C) 2020 Roberto Giacomelli +-- Copyright (C) 2019-2022 Roberto Giacomelli -- -- All dimensions must be in scaled point (sp) --- every fields that starts with an undercore sign are intended as private +-- every field that starts with an underscore sign are intended to be private local Code39 = { - _VERSION = "code39 v0.0.5", + _VERSION = "code39 v0.0.6", _NAME = "Code39", _DESCRIPTION = "Code39 barcode encoder", } Code39._symb_def = {-- symbol definition - ["0"] = 112122111, ["1"] = 211112112, ["2"] = 211112211, - ["3"] = 111112212, ["4"] = 211122111, ["5"] = 111122112, - ["6"] = 111122211, ["7"] = 212112111, ["8"] = 112112112, - ["9"] = 112112211, ["A"] = 211211112, ["B"] = 211211211, - ["C"] = 111211212, ["D"] = 211221111, ["E"] = 111221112, - ["F"] = 111221211, ["G"] = 212211111, ["H"] = 112211112, - ["I"] = 112211211, ["J"] = 112221111, ["K"] = 221111112, - ["L"] = 221111211, ["M"] = 121111212, ["N"] = 221121111, - ["O"] = 121121112, ["P"] = 121121211, ["Q"] = 222111111, - ["R"] = 122111112, ["S"] = 122111211, ["T"] = 122121111, - ["U"] = 211111122, ["V"] = 211111221, ["W"] = 111111222, - ["X"] = 211121121, ["Y"] = 111121122, ["Z"] = 111121221, - ["-"] = 212111121, ["."] = 112111122, [" "] = 112111221, - ["$"] = 111212121, ["/"] = 121112121, ["+"] = 121211121, - ["%"] = 121212111, + ["0"] = 112122111, ["1"] = 211112112, ["2"] = 211112211, ["3"] = 111112212, + ["4"] = 211122111, ["5"] = 111122112, ["6"] = 111122211, ["7"] = 212112111, + ["8"] = 112112112, ["9"] = 112112211, ["A"] = 211211112, ["B"] = 211211211, + ["C"] = 111211212, ["D"] = 211221111, ["E"] = 111221112, ["F"] = 111221211, + ["G"] = 212211111, ["H"] = 112211112, ["I"] = 112211211, ["J"] = 112221111, + ["K"] = 221111112, ["L"] = 221111211, ["M"] = 121111212, ["N"] = 221121111, + ["O"] = 121121112, ["P"] = 121121211, ["Q"] = 222111111, ["R"] = 122111112, + ["S"] = 122111211, ["T"] = 122121111, ["U"] = 211111122, ["V"] = 211111221, + ["W"] = 111111222, ["X"] = 211121121, ["Y"] = 111121122, ["Z"] = 111121221, + ["-"] = 212111121, ["."] = 112111122, [" "] = 112111221, ["$"] = 111212121, + ["/"] = 121112121, ["+"] = 121211121, ["%"] = 121212111, } Code39._star_def = 112121121 -- '*' start/stop character @@ -54,7 +50,7 @@ pardef.module = { -- with label or form design. -- The module width (width of narrow element) should be at least 7.5 mils -- or 0.1905mm (a mil is 1/1000 inch). - default = 7.5 * 0.0254 * 186467, -- 7.5 mils (sp) unit misure, + default = 10 * 0.0254 * 186467, -- 7.5 mils (sp) unit misure, unit = "sp", -- scaled point isReserved = true, fncheck = function (self, mod, _) --> boolean, err @@ -230,98 +226,83 @@ pardef.text_star = { -- configuration function function Code39:_config() --> ok, err + local Vbar = self._libgeo.Vbar -- Vbar class + local Archive = self._libgeo.Archive -- Archive class + local c39_vbars = Archive:new() + self._vbar_archive = c39_vbars -- build Vbar object for the start/stop symbol local mod, ratio = self.module, self.ratio local n_star = self._star_def - local Vbar = self._libgeo.Vbar -- Vbar class - self._vbar = {['*'] = Vbar:from_int_revpair(n_star, mod, mod*ratio)} + local star = Vbar:from_int_revpair(n_star, mod, mod*ratio) + c39_vbars:insert(star, "*") return true, nil end --- overriding Barcode method -function Code39:_check_char(c) --> elem, err - if type(c) ~= "string" or #c ~= 1 then - return nil, "[ArgErr] invalid char" - end - local symb_def = self._symb_def - local n = symb_def[c] - if not n then - local fmt = "[ArgErr] '%s' is not a valid Code 39 symbol" - return nil, string.format(fmt, c) +function Code39:_ensure_symbol(c) + local Archive = self._vbar_archive + if not Archive:contains_key(c) then + local Vbar = self._libgeo.Vbar + local mod, ratio = self.module, self.ratio + local symb_def = self._symb_def + local n_def = symb_def[c] + local v = Vbar:from_int_revpair(n_def, mod, mod*ratio) + Archive:insert(v, c) end - return c, nil end -- overriding Barcode method -function Code39:_check_digit(n) --> elem, err - if type(n) ~= "number" then - return nil, "[ArgErr] not a number" - end - if n < 0 or n > 9 then - return nil, "[ArgErr] not a digit" +function Code39:_process_char(c) --> elem_code, elem_text, err + local symb_def = self._symb_def + if not symb_def[c] then + local fmt = "[ArgErr] '%s' is not a valid Code 39 symbol" + return nil, nil, string.format(fmt, c) end - return string.char(n + 48), nil + self:_ensure_symbol(c) + return c, nil, nil end -function Code39:_finalize() --> ok, err - local v = assert(self._code_data, "[InternalErr] '_code_data' field is nil") - local vbar = self._vbar - local g_Vbar = self._libgeo.Vbar - local mod, ratio = self.module, self.ratio - local symb_def = self._symb_def - for _, c in ipairs(v) do - if not vbar[c] then - local n1 = symb_def[c] - vbar[c] = g_Vbar:from_int_revpair(n1, mod, mod*ratio) - end - end - return true, nil +-- overriding Barcode method +function Code39:_process_digit(n) --> elem_code, elem_text, err + local c = string.char(n + 48) + self:_ensure_symbol(c) + return c, nil, nil end -- tx, ty is an optional translator vector -function Code39:append_ga(canvas, tx, ty) --> canvas - local code = self._code_data - local ns = self._code_len -- number of chars inside the symbol - local mod = self.module - local ratio = self.ratio - local interspace = self.interspace - local h = self.height - local xs = mod*(6 + 3*ratio) - local xgap = xs + interspace - local w = xgap*(ns + 1) + xs -- (ns + 2)*xgap - interspace - local ax, ay = self.ax, self.ay - local x0 = (tx or 0) - ax * w - local y0 = (ty or 0) - ay * h - local x1 = x0 + w - local y1 = y0 + h - local xpos = x0 - local err - err = canvas:start_bbox_group() - assert(not err, err) - local vbar = self._vbar - -- start/stop symbol - local term_vbar = vbar['*'] - -- draw start symbol - err = canvas:encode_Vbar(term_vbar, xpos, y0, y1) - assert(not err, err) - for _, c in ipairs(code) do -- draw code symbols - xpos = xpos + xgap - local vb = vbar[c] - err = canvas:encode_Vbar(vb, xpos, y0, y1) - assert(not err, err) +function Code39:_append_ga(canvas, tx, ty) --> x1, y1, x2, y2 -- bbox + local code = self._code_data + local ns = #code -- number of chars inside the symbol + local archive = self._vbar_archive + local q = assert(archive:get("*")) -- form the vbar queue, actually it is just a Vbar + local dx = self.interspace + for _, c in ipairs(code) do + q = q + dx + assert(archive:get(c)) end - -- draw stop symbol - err = canvas:encode_Vbar(term_vbar, xpos + xgap, y0, y1) - assert(not err, err) + q = q + dx + assert(archive:get("*")) -- final stop char + assert(canvas:encode_disable_bbox()) + -- draw the symbol + local ax, ay = self.ax, self.ay + local mod = self.module + local ratio = self.ratio + local xs = mod*(6 + 3*ratio) + local xgap = xs + dx + local h = self.height + local w = xgap*(ns + 1) + xs -- (ns + 2)*xgap - interspace + -- + local x0 = tx - ax * w + local x1 = x0 + w + local y0 = ty - ay * h + local y1 = y0 + h + assert(canvas:encode_vbar_queue(q, x0, y0, y1)) -- bounding box setting local qz = self.quietzone - err = canvas:stop_bbox_group(x0 - qz, y0, x1 + qz, y1) - assert(not err, err) + assert(canvas:encode_set_bbox(x0 - qz, y0, x1 + qz, y1)) -- check height as the minimum of 15% of length -- TODO: message could warn the user -- if 0.15 * w > h then -- message("The height of the barcode is to small") -- end + assert(canvas:encode_enable_bbox()) if self.text_enabled then -- human readable text local chars; if self.text_star then chars = {"*"} @@ -356,8 +337,7 @@ function Code39:append_ga(canvas, tx, ty) --> canvas xaxis = xaxis + xgap end xaxis = xaxis + xs/2 - err = canvas:encode_Text_xspaced(txt, xaxis, xgap, ypos, tay) - assert(not err, err) + assert(canvas:encode_Text_xspaced(txt, xaxis, xgap, ypos, tay)) else local xpos, tax if txt_hpos == "left" then @@ -372,12 +352,10 @@ function Code39:append_ga(canvas, tx, ty) --> canvas else error("[InternalErr] wrong option for text_pos") end - err = canvas:encode_Text(txt, xpos, ypos, tax, tay) - assert(not err, err) + assert(canvas:encode_Text(txt, xpos, ypos, tax, tay)) end end - return canvas + return {x0, y0, x1, y1, qz, nil, qz, nil} end return Code39 ---
\ No newline at end of file |