From 98a8cf07f9f44a4cac1d296b1dd75e3878530ccc Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Thu, 23 Jun 2022 20:54:26 +0000 Subject: barracuda (23jun22) git-svn-id: svn://tug.org/texlive/trunk@63701 c570f23f-e606-0410-a88d-b1316a301751 --- Master/texmf-dist/scripts/barracuda/barracuda.lua | 30 +- .../scripts/barracuda/lib-barcode/brcd-barcode.lua | 287 +++++++-- .../scripts/barracuda/lib-barcode/brcd-code128.lua | 404 +++++++------ .../scripts/barracuda/lib-barcode/brcd-code39.lua | 164 +++--- .../scripts/barracuda/lib-barcode/brcd-ean.lua | 651 +++++++++++---------- .../scripts/barracuda/lib-barcode/brcd-i2of5.lua | 217 ++++--- .../scripts/barracuda/lib-barcode/brcd-upc.lua | 251 ++++++++ .../scripts/barracuda/lib-driver/brcd-driver.lua | 243 ++++++-- .../barracuda/lib-driver/brcd-drv-pdfliteral.lua | 50 +- .../scripts/barracuda/lib-driver/brcd-drv-svg.lua | 173 +++++- .../scripts/barracuda/lib-geo/brcd-gacanvas.lua | 563 ++++++++++++++---- .../scripts/barracuda/lib-geo/brcd-libgeo.lua | 216 ++++++- 12 files changed, 2288 insertions(+), 961 deletions(-) create mode 100644 Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-upc.lua (limited to 'Master/texmf-dist/scripts') diff --git a/Master/texmf-dist/scripts/barracuda/barracuda.lua b/Master/texmf-dist/scripts/barracuda/barracuda.lua index d8eae352165..c854941d274 100644 --- a/Master/texmf-dist/scripts/barracuda/barracuda.lua +++ b/Master/texmf-dist/scripts/barracuda/barracuda.lua @@ -2,7 +2,7 @@ -- -- Encode a message into a barcode symbol, in Lua or within a LuaTeX source file -- --- Copyright (C) 2020 Roberto Giacomelli +-- Copyright (C) 2019-2022 Roberto Giacomelli -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by @@ -26,26 +26,36 @@ local Barracuda = { _NAME = "barracuda", - _VERSION = "barracuda v0.0.10", - _DATE = "2020-02-04", + _VERSION = "barracuda v0.0.12", + _DATE = "2022-06-23", _DESCRIPTION = "Lua library for barcode printing", _URL = "https://github.com/robitex/barracuda", _LICENSE = "GNU GENERAL PUBLIC LICENSE, Version 2, June 1991", } -- essential sub-module loading -Barracuda._libgeo = require "lib-geo.brcd-libgeo" -- basic vectorial objects +Barracuda._libgeo = require "lib-geo.brcd-libgeo" -- basic vector objects Barracuda._gacanvas = require "lib-geo.brcd-gacanvas" -- ga stream library Barracuda._barcode = require "lib-barcode.brcd-barcode" -- barcode abstract class +Barracuda._lib_driver = require "lib-driver.brcd-driver" -- abstract driver class local Barcode = Barracuda._barcode Barcode._libgeo = Barracuda._libgeo +-- a reference to Driver class for gaCancas class +local canvas = Barracuda._gacanvas +canvas._driver_class = Barracuda._lib_driver + -- return a reference of Barcode abstract class -function Barracuda:barcode() --> Barcode class object +function Barracuda:barcode() --> return self._barcode end +-- return a reference to the libgeo library +function Barracuda:libgeo() --> + return self._libgeo +end + -- encoder costructor (a bridge to Barcode class method) function Barracuda:new_encoder(treename, opt) --> object, err local barcode = self._barcode @@ -55,19 +65,15 @@ end -- where we place the output driver library function Barracuda:get_driver() --> Driver object - if not self._lib_driver then - self._lib_driver = require "lib-driver.brcd-driver" - end return self._lib_driver end -function Barracuda:new_canvas() --> driver +function Barracuda:new_canvas() --> canvas local gacanvas = self._gacanvas return gacanvas:new() end -- high level barcode functions --- only default options -- panic on error -- save barcode as a graphic external file @@ -104,7 +110,7 @@ function Barracuda:save(treename, data, filename, id_drv, opt) assert(ok, err) end local canvas = self:new_canvas() - symb:append_ga(canvas) + symb:draw(canvas) local driver = self:get_driver() id_drv = id_drv or "svg" local ok, err = driver:save(id_drv, canvas, filename) @@ -144,7 +150,7 @@ function Barracuda:hbox(treename, data, box_name, opt) assert(ok, err) end local canvas = self:new_canvas() - symb:append_ga(canvas) + symb:draw(canvas) local driver = self:get_driver() local ok, err = driver:ga_to_hbox(canvas, box_name) assert(ok, err) diff --git a/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-barcode.lua b/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-barcode.lua index b43bc511d72..1208adeb752 100644 --- a/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-barcode.lua +++ b/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-barcode.lua @@ -1,13 +1,8 @@ - -- Barcode Abstract Class --- Copyright (C) 2020 Roberto Giacomelli +-- Copyright (C) 2019-2022 Roberto Giacomelli -- Please see LICENSE.TXT for any legal information about present software -local Barcode = { - _VERSION = "Barcode v0.0.9", - _NAME = "Barcode", - _DESCRIPTION = "Barcode abstract class", -} +local Barcode = {_classname = "Barcode"} Barcode.__index = Barcode -- barcode_type/submodule name @@ -16,6 +11,7 @@ Barcode._available_enc = {-- keys must be lowercase code128 = "lib-barcode.brcd-code128", -- Code128 ean = "lib-barcode.brcd-ean", -- EAN family (ISBN, EAN8, etc) i2of5 = "lib-barcode.brcd-i2of5", -- Interleaved 2 of 5 + upc = "lib-barcode.brcd-upc", -- UPC } Barcode._builder_instances = {} -- encoder builder instances repository Barcode._encoder_instances = {} -- encoder instances repository @@ -24,6 +20,7 @@ Barcode._encoder_instances = {} -- encoder instances repository Barcode._super_par_order = { "ax", "ay", + "debug_bbox", } Barcode._super_par_def = {} local pardef = Barcode._super_par_def @@ -36,7 +33,7 @@ pardef.ax = { default = 0.0, unit = "sp", -- scaled point isReserved = false, - fncheck = function (self, ax, _) --> boolean, err + fncheck = function (_self, ax, _) --> boolean, err if ax >= 0.0 and ax <= 1.0 then return true, nil end return false, "[OutOfRange] 'ax' out of [0, 1] interval" end, @@ -46,13 +43,34 @@ pardef.ay = { default = 0.0, unit = "sp", -- scaled point isReserved = false, - fncheck = function (self, ay, _) --> boolean, err + fncheck = function (_self, ay, _) --> boolean, err if ay >= 0.0 and ay <= 1.0 then return true, nil end return false, "[OutOfRange] 'ay' out of [0, 1] interval" end, } --- Barcode.bbox_to_quietzone -- under assessment +-- debug only purpose +-- enable/disable bounding box drawing of symbols +Barcode.debug_bbox = "none" +pardef.debug_bbox = { + default = "none", + isReserved = false, + enum = { + none = true, -- do nothing + symb = true, -- draw bbox of the symbol + qz = true, -- draw a bbox at quietzone border + qzsymb = true, -- draw quietzone and symbol bboxes + }, + fncheck = function (self, e, _) --> boolean, err + if type(e) ~= "string" then return false, "[TypeError] not a string" end + local keys = self.enum + if keys[e] == true then + return true, nil + else + return false, "[Err] enumeration value '"..e.."' not found" + end + end, +} -- Barcode methods @@ -221,7 +239,7 @@ function Barcode:new_encoder(treename, opt) --> object, err -- argument checking local family, variant, enc_name, err = parse_treename(treename) if err then - return err + return nil, err end local av_enc = self._available_enc local mod_path = av_enc[family] @@ -254,7 +272,7 @@ function Barcode:new_encoder(treename, opt) --> object, err else return nil, "[ArgErr] provided 'opt' is not a table" end - local enc = {} -- the new encoder + local enc = {_classname = "Encoder"} -- the new encoder enc.__index = enc enc._variant = variant setmetatable(enc, { @@ -292,13 +310,14 @@ function Barcode:new_encoder(treename, opt) --> object, err end end if enc._config then -- this must be called after the parameter definition - enc:_config() + local ok, e = enc:_config() + if not ok then return nil, e end end return enc, nil end -- retrive an encoder object already created --- 'trename' is the special identifier of the encoder +-- 'treename' is the special identifier of the encoder function Barcode:enc_by_name(treename) --> , -- argument checking local _family, _variant, _enc_name, err = parse_treename(treename) @@ -316,25 +335,36 @@ end -- base methods common to all the encoders -- for numeric only simbology -function Barcode:_check_char(c) --> elem, err +-- elem_code : encoded char +-- elem_text : human readable char +-- err : error description +function Barcode:_check_char(c, parse_state) --> elem_code, elem_text, err if type(c) ~= "string" or #c ~= 1 then - return nil, "[InternalErr] invalid char" + return nil, nil, "[InternalErr] invalid char" + end + local process_char = self._process_char + if process_char then + return process_char(self, c, parse_state) end local n = string.byte(c) - 48 if n < 0 or n > 9 then - return nil, "[ArgErr] found a not digit char" + return nil, nil, "[ArgErr] found a not digit char" end - return n, nil + return n, nil, nil end -- -function Barcode:_check_digit(n) --> elem, err +function Barcode:_check_digit(n, parse_state) --> elem_code, elem_text, err if type(n) ~= "number" then - return nil, "[ArgErr] not a number" + return nil, nil, "[ArgErr: n] not a number" + end + local process_digit = self._process_digit + if process_digit then + return process_digit(self, n, parse_state) end if n < 0 or n > 9 then - return nil, "[ArgErr] not a digit" + return nil, nil, "[ArgErr: n] not a digit" end - return n, nil + return n, nil, nil end -- not empty string --> Barcode object @@ -345,22 +375,33 @@ function Barcode:from_string(symb, opt) --> object, err if #symb == 0 then return nil, "[ArgErr] 'symb' is an empty string" end - local chars = {} - local len = 0 - local parse_state = {} + local chars_code = {} + local chars_text + local parse_state + if self._init_parse_state then + parse_state = self:_init_parse_state() + else + parse_state = {} + end for c in string.gmatch(symb, ".") do - local elem, err = self:_check_char(c, parse_state) + local elem_code, elem_text, err = self:_check_char(c, parse_state) if err then return nil, err - elseif elem then - chars[#chars+1] = elem - len = len + 1 + else + if elem_code then + chars_code[#chars_code + 1] = elem_code + end + if elem_text then + chars_text = chars_text or {} + chars_text[#chars_text + 1] = elem_text + end end end -- build the barcode object local o = { - _code_data = chars, -- array of chars - _code_len = len, -- symbol lenght + _classname = "BarcodeSymbol", + _code_data = chars_code, -- array of chars + _code_text = chars_text, } setmetatable(o, self) if opt ~= nil then @@ -394,42 +435,58 @@ function Barcode:from_uint(n, opt) --> object, err if opt ~= nil and type(opt) ~= "table" then return nil, "[ArgErr] 'opt' is not a table" end - local digits = {} - local parse_state = {} - local i = 0 + local digits_code = {} + local digits_text + local parse_state + if self._init_parse_state then + parse_state = self:_init_parse_state() + else + parse_state = {} + end if n == 0 then - local elem, err = self:_check_digit(0, parse_state) + local elem_code, elem_text, err = self:_check_digit(0, parse_state) if err then return nil, err end - if elem then - digits[1] = elem - i = 1 + if elem_code then + digits_code[1] = elem_code + end + if elem_text then + digits_text = {elem_text} end else while n > 0 do local d = n % 10 - local elem, err = self:_check_digit(d, parse_state) + local elem_code, elem_text, err = self:_check_digit(d, parse_state) if err then return nil, err end - if elem then - i = i + 1 - digits[i] = elem + if elem_code then + digits_code[#digits_code + 1] = elem_code + end + if elem_text then + digits_text = digits_text or {} + digits_text[#digits_text + 1] = elem_text end n = (n - d) / 10 end - for k = 1, i/2 do -- reverse the array - local d = digits[k] - local h = i - k + 1 - digits[k] = digits[h] - digits[h] = d + local rev_array = function (a) + local len = #a + for k = 1, len/2 do -- reverse the array + local h = len - k + 1 + a[k], a[h] = a[h], a[k] + end + end + rev_array(digits_code) + if digits_text then + rev_array(digits_text) end end -- build the barcode object local o = { - _code_data = digits, -- array of digits - _code_len = i, -- symbol lenght + _classname = "BarcodeSymbol", + _code_data = digits_code, -- array of digits + _code_text = digits_text, -- array of human readable information } setmetatable(o, self) if opt ~= nil then @@ -449,6 +506,26 @@ function Barcode:from_uint(n, opt) --> object, err return o, nil end +-- recursive general Barcode costructor +function Barcode:new(code) --> object, err + local t = type(code) + if t == "string" then + return self:from_string(code) + elseif t == "number" then + return self:from_uint(code) + elseif t == "table" then + local res = {} + for _, c in ipairs(code) do + local b, err = self:new(c) + if err then return nil, err end + res[#res + 1] = b + end + return res, nil + else + return nil, "[ArgErr] unsuitable type '"..t.."' for the input code" + end +end + -- check a parameter set -- this method check also reserved parameter -- argments: {k=v, ...}, "default" | "current" @@ -532,13 +609,13 @@ function Barcode:info() --> table description = self._DESCRIPTION, param = {}, } - local tpar = info.param + local tpar = info.param for _, pdef in self:param_ord_iter() do - local id = pdef.pname + local id = pdef.pname local def = pdef.pdef tpar[#tpar + 1] = { name = id, - descr = nil, -- TODO: + descr = def.descr, value = self[id], isReserved = def.isReserved, unit = def.unit, @@ -547,12 +624,32 @@ function Barcode:info() --> table return info end --- return the code being represented as a string --- or nil if the method is called from Barcode abstract class -function Barcode:get_code() --> string|nil - local code = self._code_data - if code then - return table.concat(code) +-- return internal code representation +function Barcode:get_code() --> array + if self._classname == "BarcodeSymbol" then + local code = self._code_data + local res = {} + for _, c in ipairs(code) do + res[#res + 1] = c + end + return res + else + error("[Err: OOP] 'BarcodeSymbol' only method") + end +end + +-- human readable interpretation hri or nil +function Barcode:get_hri() --> array|nil + if self._classname == "BarcodeSymbol" then + local code = self._code_text + if code == nil then return nil end + local res = {} + for _, c in ipairs(code) do + res[#res + 1] = c + end + return res + else + error("[Err: OOP] 'BarcodeSymbol' only method") end end @@ -562,8 +659,8 @@ function Barcode:get_param(id) --> value, err if type(id) ~= "string" then return nil, "[ArgErr] 'id' must be a string" end - local pardef = self._par_def - if not pardef[id] then + local par_def = self._par_def + if not par_def[id] then return nil, "[Err] Parameter '"..id.."' doesn't exist" end local res = assert(self[id], "[InternalErr] parameter value unreachable") @@ -622,6 +719,76 @@ function Barcode:set_param(arg1, arg2) --> boolean, err return true, nil end +-- canvas is a gaCanvas object +-- tx, ty is an optional point to place symbol local origin on the canvas plane +function Barcode:draw(canvas, tx, ty) --> canvas, err + local nclass = self._classname + if nclass == "Barcode" then + error("[ErrOOP:Barcode] method 'draw' must be called only on a Symbol object") + end + if nclass == "Encoder" then + error("[ErrOOP:Encoder] method 'draw' must be called only on a Symbol object") + end + assert(nclass == "BarcodeSymbol") + if canvas._classname ~= "gaCanvas" then + return nil, "[ErrOOP:canvas] object 'gaCanvas' expected" + end + local ga_fn = assert( + self._append_ga, + "[InternalErr] unimplemented '_append_ga' method" + ) + local bb_info = ga_fn(self, canvas, tx or 0, ty or 0) + local dbg_bbox = self.debug_bbox + if dbg_bbox == "none" then + -- do nothing + else + -- dashed style: phase = 3bp, dash pattern = 6bp 3bp + local bp = canvas.bp + local W = bp/10 -- 0.1bp + local w = W/2 + assert(canvas:encode_linewidth(W)) + assert(canvas:encode_dash_pattern(3*bp, 6*bp, 3*bp)) + local x1, y1, x2, y2 = bb_info[1], bb_info[2], bb_info[3], bb_info[4] + if dbg_bbox == "symb" then + assert(canvas:encode_rect(x1 + w, y1 + w, x2 - w, y2 - w)) + elseif dbg_bbox == "qz" then + local q1, q2, q3, q4 = bb_info[5], bb_info[6], bb_info[7], bb_info[8] + x1 = x1 - (q1 or 0) + w + y1 = y1 - (q2 or 0) + w + x2 = x2 + (q3 or 0) - w + y2 = y2 + (q4 or 0) - w + assert(canvas:encode_rect(x1, y1, x2, y2)) + elseif dbg_bbox == "qzsymb" then + local q1, q2, q3, q4 = bb_info[5], bb_info[6], bb_info[7], bb_info[8] + x1 = x1 + w + y1 = y1 + w + x2 = x2 - w + y2 = y2 - w + if q1 then + assert(canvas:encode_vline(y1, y2, x1)) + x1 = x1 - q1 + end + if q3 then + assert(canvas:encode_vline(y1, y2, x2)) + x2 = x2 + q3 + end + if q2 then + assert(canvas:encode_hline(x1, x2, y1)) + y1 = y1 - q2 + end + if q4 then + assert(canvas:encode_hline(x1, x2, y2)) + y2 = y2 + q4 + end + assert(canvas:encode_rect(x1, y1, x2, y2)) + else + error("[InternalErr:debug_bbox] unrecognized enum value") + end + assert(canvas:encode_reset_pattern()) + end + return canvas +end + return Barcode -- diff --git a/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-code128.lua b/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-code128.lua index 1ee1c983434..294162f6b69 100644 --- a/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-code128.lua +++ b/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-code128.lua @@ -1,11 +1,11 @@ -- Code128 barcode generator module --- Copyright (C) 2020 Roberto Giacomelli +-- Copyright (C) 2019-2022 Roberto Giacomelli -- -- All dimension must be in scaled point (sp) -- every fields that starts with an undercore sign are intended as private local Code128 = { - _VERSION = "code128 v0.0.5", + _VERSION = "code128 v0.0.6", _NAME = "Code128", _DESCRIPTION = "Code128 barcode encoder", } @@ -23,7 +23,7 @@ Code128._int_def_bar = {-- code bar definitions 111242, 121142, 121241, 114212, 124112, 124211, 411212, 421112, 421211, 212141, 214121, 412121, 111143, 111341, 131141, 114113, 114311, 411113, 411311, 113141, 114131, 311141, 411131, 211412, 211214, 211232, - 2331112, -- this is the stop char at index 106 + 2331112, -- the last number is the stop char at index 106 } Code128._codeset = { @@ -96,248 +96,308 @@ function Code128:_config() --> ok, err local mod = self.xdim local sc = self._codeset.stopChar -- build the stop char local n = self._int_def_bar[sc] - local Vbar = self._libgeo.Vbar -- Vbar class - self._vbar = {} - local b = self._vbar - b[sc] = Vbar:from_int(n, mod, true) + local repo = self._libgeo.Archive:new() + self._vbar_archive = repo + local Vbar = self._libgeo.Vbar + repo:insert(Vbar:from_int(n, mod, true), 106) return true, nil end -- utility functions --- the number of consecutive digits from the index 'i' in the code array -local function count_digits_from(arr, i) - local start = i - local dim = #arr - while i <= dim and (arr[i] > 47 and arr[i] < 58) do - i = i + 1 - end - return i - start -end - --- evaluate the check digit of the data representation +-- evaluate the check digit of encoded data local function check_digit(code) local sum = code[1] -- this is the start character for i = 2, #code do sum = sum + code[i]*(i-1) end - return sum % 103 + code[#code + 1] = sum % 103 +end + +local function isdigit(char) --> true/false + assert(char) + return (char > 47) and (char < 58) +end + +local function iscontrol(char) --> true/false + assert(char) + -- [0,31] control chars interval + return char < 32 +end + +local function islower(char) + assert(char) + -- [96, 127] lower case chars + return (char > 95) and (char < 128) end --- return a pair of boolean the first one is true --- if a control char and a lower case char occurs in the data --- and the second one is true if the control char occurs before --- the lower case char -local function ctrl_or_lowercase(pos, data) --> boolean, boolean|nil - local len = #data - local ctrl_occur, lower_occur = false, false - for i = pos, len do - local c = data[i] - if (not ctrl_occur) and (c >= 0 and c < 32) then - -- [0,31] control chars - if lower_occur then - return true, false -- lowercase char < ctrl char +-- count digits +local function digits_group(data, len) --> counting digits + local res = {} + local last = false + for i = len, 1, -1 do + local digit = isdigit(data[i]) + if last then + if digit then + res[i] = res[i+1] + 1 else - ctrl_occur = true + res[i] = 0 + last = false end - elseif (not lower_occur) and (c > 95 and c < 128) then - -- [96, 127] lower case chars - if ctrl_occur then - return true, true -- ctrl char < lowercase char + else + if digit then + res[i] = 1 + last = true else - lower_occur = true + res[i] = 0 end end end - return false -- no such data + return res end --- encode the provided char against a codeset --- in the future this function will consider FN data -local function encode_char(t, codesetAorB, char_code, codesetA) - local code - if codesetAorB == codesetA then -- codesetA - if char_code < 32 then - code = char_code + 64 - elseif char_code > 31 and char_code < 96 then - code = char_code - 32 - else - error("Not implemented or wrong code") - end - else -- codesetB - if char_code > 31 and char_code < 128 then - code = char_code - 32 - else - error("Not implemented or wrong code") +-- find the first char in the codeset that adhere +-- with the function argument 'filter' +local function indexof_char_by(filter, arr, counter) --> index or nil + counter = counter or 1 + local char = arr[counter] + while char do + if filter(char) then + return counter end + counter = counter + 1 + char = arr[counter] end - t[#t + 1] = code end --- encode the message in a sequence of Code128 symbol minimizing its lenght -local function encode128(arr, codeset, switch) --> data, err :TODO: - local res = {} -- the result array (the check character will be appended) - -- find the Start Character A, B, or C - local cur_codeset - local ndigit = count_digits_from(arr, 1) - local len = #arr - --local no_ctrl_lower_char - if (ndigit == 2 and len == 2) or ndigit > 3 then -- start char code C - cur_codeset = codeset.C +-- determine the Start character +local function start_codeset_char(codeset, arr, len) + assert(len>0) + local ctrl = indexof_char_by(iscontrol, arr) + local lowc = indexof_char_by(islower, arr) + local t_digits = digits_group(arr, len) + local first_digits = t_digits[1] + -- case 1 + if (len == 2) and (first_digits == 2) then + return lowc, ctrl, codeset.C, t_digits + end + -- case 2 + if first_digits >= 4 then + return lowc, ctrl, codeset.C, t_digits + end + -- case 3 + local cs = codeset.B + if (ctrl and lowc) and (ctrl < lowc) then + cs = codeset.A + end + -- case 4 + return lowc, ctrl, cs, t_digits +end + +-- codeset A char +local function encode_char_A(res, char) + local code + if char < 32 then + code = char + 64 + elseif char > 31 and char < 96 then + code = char - 32 else - local ok, ctrl_first = ctrl_or_lowercase(1, arr) - if ok and ctrl_first then - cur_codeset = codeset.A - else - cur_codeset = codeset.B - end + error("[InternalErr] Not implemented or wrong code" ) end - res[#res + 1] = cur_codeset - local pos = 1 -- symbol's index to encode - while pos <= len do - if cur_codeset == codeset.C then - if arr[pos] < 48 or arr[pos] > 57 then -- not numeric char - local ok, ctrl_first = ctrl_or_lowercase(pos, arr) - if ok and ctrl_first then - cur_codeset = codeset.A + res[#res + 1] = code +end +-- codeset B char +local function encode_char_B(res, char) + local code + if char > 31 and char < 128 then + code = char - 32 + else + error("[InternalErr] Not implemented or wrong code") + end + res[#res + 1] = code +end + +-- every function encodes a group of chars +-- A = 103, -- Start char for Codeset A +-- B = 104, -- Start char for Codeset B +-- C = 105, -- Start char for Codeset C +local encode_codeset = { + -- A + [103] = function (codeset, res, data, index, t_digits, i_low, _ctrl) + assert(t_digits[index] < 4, "[InternalErr] in codeset A digits must be less than 4") + while data[index] do + local char = data[index] + if i_low and islower(char) then -- ops a lower case char + local next = data[index + 1] + local next_next = data[index + 2] + if next and next_next then + -- case 5a + if iscontrol(char) and islower(next_next) then + res[#res+1] = codeset.shift + encode_char_B(res, char) + index = index + 1 + end else - cur_codeset = codeset.B + -- case 5b + return codeset.B, index end - res[#res + 1] = switch[codeset.C][cur_codeset] else - local imax = pos + 2*math.floor(ndigit/2) - 1 - for idx = pos, imax, 2 do - res[#res + 1] = (arr[idx] - 48)*10 + arr[idx+1] - 48 - end - pos = pos + imax - ndigit = ndigit - imax - if ndigit == 1 then - -- cur_codeset setup - local ok, ctrl_first = ctrl_or_lowercase(pos + 1, arr) - if ok and ctrl_first then - cur_codeset = codeset.A - else - cur_codeset = codeset.B + local digits = t_digits[index] + if digits > 3 then -- go to codeset C + if (digits % 2) == 1 then -- odd number of a group of digits + encode_char_A(res, char) + digits = digits - 1 + index = index + 1 end - res[#res + 1] = switch[codeset.C][cur_codeset] + return codeset.C, index end + encode_char_A(res, char) + index = index + 1 end - else --- current codeset is A or B - if ndigit > 3 then - if ndigit % 2 > 1 then -- odd number of digits - encode_char(res, cur_codeset, arr[pos], codeset.A) - pos = pos + 1 - ndigit = ndigit - 1 - end - res[#res + 1] = switch[cur_codeset][codeset.C] - cur_codeset = codeset.C - elseif (cur_codeset == codeset.B) and - (arr[pos] >= 0 and arr[pos] < 32) then -- ops a control char - local ok, ctrl_first = ctrl_or_lowercase(pos + 1, arr) - if ok and (not ctrl_first) then -- shift to codeset A - res[#res + 1] = codeset.shift - encode_char(res, codeset.A, arr[pos], codeset.A) - pos = pos + 1 - ndigit = count_digits_from(pos, arr) - else -- switch to code set A - res[#res + 1] = switch[cur_codeset][codeset.A] - cur_codeset = codeset.A - end - elseif (cur_codeset == codeset.A) and - (arr[pos] > 95 and arr[pos] < 128) then -- ops a lower case char - local ok, ctrl_first = ctrl_or_lowercase(pos+1, arr) - if ok and ctrl_first then -- shift to codeset B - res[#res + 1] = codeset.shift - encode_char(res, codeset.B, arr[pos], codeset.A) - pos = pos + 1 - ndigit = count_digits_from(arr, pos) - else -- switch to code set B - res[#res + 1] = switch[cur_codeset][codeset.B] - cur_codeset = codeset.B + end + return nil, index + end, + -- B + [104] = function (codeset, res, data, index, t_digits, _low, i_ctrl) + assert(t_digits[index] < 4, "[InternalErr] in codeset B digits must be less than 4") + while data[index] do + local char = data[index] + if i_ctrl and iscontrol(char) then -- ops a control char + local next = data[index + 1] + local next_next = data[index + 2] + if next and next_next then + -- case 4a + if islower(next) and iscontrol(next_next) then + res[#res+1] = codeset.shift + encode_char_A(res, char) + index = index + 1 + end + else + -- case 4b + return codeset.A, index end else - -- insert char - encode_char(res, cur_codeset, arr[pos], codeset.A) - pos = pos + 1 - ndigit = count_digits_from(arr, pos) + local digits = t_digits[index] + if digits > 3 then -- go to codeset C + if (digits % 2) == 1 then -- odd number of a group of digits + encode_char_B(res, char) + digits = digits - 1 + index = index + 1 + end + return codeset.C, index + end + encode_char_B(res, char) + index = index + 1 end end + return nil, index + end, + -- C + [105] = function (codeset, res, data, index, t_digits, i_low, i_ctrl) + local digits = t_digits[index] + assert(digits > 1, "[InternalErr] at least a pair of digit is required") + while digits > 1 do + local d1, d2 = data[index], data[index + 1] + res[#res + 1] = (d1 - 48)*10 + d2 - 48 + digits = digits - 2 + index = index + 2 + end + local res_codeset + if i_ctrl and i_low then + local ctrl = indexof_char_by(iscontrol, data, index) + local low = indexof_char_by(islower, data, index) + if low and (ctrl < low) then + res_codeset = codeset.A + end + else + res_codeset = codeset.B + end + return res_codeset, index + end, +} + +-- encode the message in a sequence of Code128 symbol minimizing the symbol width +local function encode128(arr, codeset, switch) --> data, err + local len = #arr + local i_low, i_ctrl, cur_codeset, t_digits = start_codeset_char(codeset, arr, len) + local res = {cur_codeset} -- the result array (the check character will be appended later) + local switch_codeset + local cur_index = 1 + while cur_index <= len do + if switch_codeset then + res[#res+1] = switch[cur_codeset][switch_codeset] + cur_codeset = switch_codeset + end + local fn = assert(encode_codeset[cur_codeset], "[InternalErr] cur_codeset is "..(cur_codeset or "nil")) + switch_codeset, cur_index = fn(codeset, res, arr, cur_index, t_digits, i_low, i_ctrl) end - res[#res + 1] = check_digit(res) - res[#res + 1] = codeset.stopChar - return res + check_digit(res) + return res, nil end -- Code 128 internal functions used by Barcode costructors -function Code128:_check_char(c) --> elem, err - if type(c) ~= "string" or #c ~= 1 then - return nil, "[InternalErr] invalid char" - end +function Code128:_process_char(c) --> char_code, char_text, err local b = string.byte(c) if b > 127 then local fmt = "[unimplemented] the '%d' is an ASCII extented char" return nil, string.format(fmt, c) end - return b, nil + return b, c, nil end -function Code128:_check_digit(n) --> elem, err - if type(n) ~= "number" then - return nil, "[InternalErr] invalid digit" - end - return n + 48, nil +function Code128:_process_digit(n) --> digit_code, char_text, err + local res = n + 48 + return res, string.char(res), nil end function Code128:_finalize() --> ok, err local chr = assert(self._code_data, "[InternalErr] '_code_data' field is nil") local data, err = encode128(chr, self._codeset, self._switch) if err then return false, err end - -- load dynamically required Vbar objects - local vbar = self._vbar - local oVbar = self._libgeo.Vbar + self._enc_data = data + -- dynamically load the required Vbar objects + local Repo = self._vbar_archive + local Vbar = self._libgeo.Vbar for _, c in ipairs(data) do - if not vbar[c] then + if not Repo:contains_key(c) then local n = self._int_def_bar[c] local mod = self.xdim - vbar[c] = oVbar:from_int(n, mod, true) + assert(Repo:insert(Vbar:from_int(n, mod, true), c)) end end - self._enc_data = data return true, nil end -- Drawing into the provided channel the geometrical barcode data -- tx, ty is the optional translator vector -- the function return the canvas reference to allow call chaining -function Code128:append_ga(canvas, tx, ty) --> canvas - local xdim, h = self.xdim, self.ydim - local sw = 11*xdim -- the width of a symbol +function Code128:_append_ga(canvas, tx, ty) --> bbox local data = self._enc_data - local w = #data * sw + 2 * xdim -- total symbol width + local Repo = self._vbar_archive + local queue = self._libgeo.Vbar_queue:new() + for _, c in ipairs(data) do + queue = queue + Repo:get(c) + end + local stop = self._codeset.stopChar + queue = queue + Repo:get(stop) + local xdim, h = self.xdim, self.ydim + local ns = #data + 1 + local w = (11*ns + 2) * xdim -- total symbol width local ax, ay = self.ax, self.ay - local x0 = (tx or 0) - ax * w - local y0 = (ty or 0) - ay * h + local x0 = tx - ax * w + local y0 = ty - ay * h local x1 = x0 + w local y1 = y0 + h - local xpos = x0 -- drawing the symbol - local err - err = canvas:start_bbox_group() - assert(not err, err) - for _, c in ipairs(data) do - local vb = self._vbar[c] - err = canvas:encode_Vbar(vb, xpos, y0, y1) - assert(not err, err) - xpos = xpos + sw - end + assert(canvas:encode_disable_bbox()) + assert(canvas:encode_vbar_queue(queue, x0, y0, y1)) -- bounding box setting local qz = self.quietzone_factor * xdim -- { xmin, ymin, xmax, ymax } - err = canvas:stop_bbox_group(x0 - qz, y0, x1 + qz, y1) - assert(not err, err) - return canvas + assert(canvas:encode_set_bbox(x0 - qz, y0, x1 + qz, y1)) + assert(canvas:encode_enable_bbox()) + return {x0, y0, x1, y1, qz, nil, qz, nil,} end return Code128 --- \ No newline at end of file diff --git a/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-code39.lua b/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-code39.lua index 6d7f2ccea7f..0768912987e 100644 --- a/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-code39.lua +++ b/Master/texmf-dist/scripts/barracuda/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 diff --git a/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-ean.lua b/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-ean.lua index bddaa36103d..d7f7733f3a2 100644 --- a/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-ean.lua +++ b/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-ean.lua @@ -1,10 +1,10 @@ -- EAN family barcode generator -- --- Copyright (C) 2020 Roberto Giacomelli +-- Copyright (C) 2019-2022 Roberto Giacomelli -- see LICENSE.txt file local EAN = { - _VERSION = "ean v0.0.6", + _VERSION = "ean v0.0.8", _NAME = "ean", _DESCRIPTION = "EAN barcode encoder", } @@ -75,6 +75,7 @@ EAN._par_order = { "text_enabled", "text_ygap_factor", "text_xgap_factor", + "text_guard_enabled", } EAN._par_def = {} local pardef = EAN._par_def @@ -148,7 +149,7 @@ pardef.bars_depth_factor = { end, } --- enable/disable a text label upon the barcode symbol +-- enable/disable human readble text pardef.text_enabled = { -- boolean type default = true, isReserved = false, @@ -187,6 +188,30 @@ pardef.text_xgap_factor = { end, } +pardef.text_guard_enabled = { -- boolean type + default = false, + isReserved = false, + fncheck = function (_self, flag, _) --> boolean, err + if type(flag) == "boolean" then + return true, nil + else + return false, "[TypeErr] not a boolean value for text_guard_enabled" + end + end, +} + +EAN._par_def_8 = {} -- ean8 substitute parameters +local pardef8 = EAN._par_def_8 +pardef8.quietzone_left_factor = pardef.quietzone_right_factor + +EAN._par_def_5 = {} -- ean5 add-on substitute parameters +local pardef5 = EAN._par_def_5 +pardef5.quietzone_left_factor = pardef.quietzone_right_factor + +EAN._par_def_2 = {} -- ean2 add-on substitute parameters +local pardef2 = EAN._par_def_2 +pardef2.quietzone_left_factor = pardef.quietzone_right_factor + -- variant parameters EAN._par_variant_order = { ["13"] = {}, -- EAN13 @@ -337,101 +362,103 @@ par_def_var["issn"].text_issn_ygap_factor = text_ygap_factor -- utility for generic configuration of full symbol, add-on included -- n1 length of main symbol, 8 or 13 -- n2 length of add-on symbol, 2 or 5 -local function config_full(ean, Vbar, mod, n1, n2) +local function config_full(ean, n1, n2) local i1 = tostring(n1) local fn_1 = assert(ean._config_variant[i1]) - fn_1(ean, Vbar, mod) + fn_1(ean) local i2 = tostring(n2) local fn_2 = assert(ean._config_variant[i2]) - fn_2(ean, Vbar, mod) + fn_2(ean) ean._main_len = n1 ean._addon_len = n2 ean._is_last_checksum = true end local config_variant = { - ["13"] = function (ean13, Vbar, mod) + ["13"] = function (ean13) ean13._main_len = 13 ean13._is_last_checksum = true - local start = ean13._start + local Vbar = ean13._libgeo.Vbar -- Vbar class + local Vbar_archive = ean13._libgeo.Archive -- Archive class + local Codeset = Vbar_archive:new() + ean13._13_codeset_vbar = Codeset + local mod = ean13.mod local stop = ean13._stop - ean13._13_start_stop_vbar = Vbar:from_int(start[1], mod, start[2]) - ean13._13_ctrl_center_vbar = Vbar:from_int(stop[1], mod, stop[2]) - ean13._13_codeset_vbar = {} - local tvbar = ean13._13_codeset_vbar + local start = ean13._start + Codeset:insert(Vbar:from_int(start[1], mod, start[2]), "start_stop") + Codeset:insert(Vbar:from_int(stop[1], mod, stop[2]), "central") for i_cs, codetab in ipairs(ean13._symbol) do - tvbar[i_cs] = {} - local tv = tvbar[i_cs] local isbar = ean13._is_first_bar[i_cs] for i = 0, 9 do - tv[i] = Vbar:from_int(codetab[i], mod, isbar) + Codeset:insert(Vbar:from_int(codetab[i], mod, isbar), i_cs, i) end end end, - ["8"] = function (ean8, Vbar, mod) + ["8"] = function (ean8) ean8._main_len = 8 ean8._is_last_checksum = true + local Vbar = ean8._libgeo.Vbar + local Vbar_archive = ean8._libgeo.Archive local start = ean8._start local stop = ean8._stop - ean8._8_start_stop_vbar = Vbar:from_int(start[1], mod, start[2]) - ean8._8_ctrl_center_vbar = Vbar:from_int(stop[1], mod, stop[2]) - ean8._8_codeset_vbar = {} - local tvbar = ean8._8_codeset_vbar + local mod = ean8.mod + local Codeset = Vbar_archive:new() + ean8._8_codeset_vbar = Codeset + Codeset:insert(Vbar:from_int(start[1], mod, start[2]), "start_stop") + Codeset:insert(Vbar:from_int(stop[1], mod, stop[2]), "central") for k = 1, 3, 2 do -- only codeset A and C (k == 1, 3) - tvbar[k] = {} local codetab = ean8._symbol[k] local isbar = ean8._is_first_bar[k] - local tv = tvbar[k] for i = 0, 9 do - tv[i] = Vbar:from_int(codetab[i], mod, isbar) + Codeset:insert(Vbar:from_int(codetab[i], mod, isbar), k, i) end end end, - ["5"] = function (ean5, Vbar, mod) -- add-on EAN5 + ["5"] = function (ean5) -- add-on EAN5 ean5._main_len = 5 ean5._is_last_checksum = false - ean5._5_start_vbar = Vbar:from_int(112, mod, true) - ean5._5_sep_vbar = Vbar:from_int(11, mod, false) - ean5._5_codeset_vbar = {} - local tvbar = ean5._5_codeset_vbar + local mod = ean5.mod + local Vbar = ean5._libgeo.Vbar + local Vbar_archive = ean5._libgeo.Archive + local Codeset = Vbar_archive:new() + Codeset:insert(Vbar:from_int(112, mod, true), "start") + Codeset:insert(Vbar:from_int(11, mod, false), "sep") + ean5._5_codeset_vbar = Codeset local symbols = ean5._symbol for c = 1, 2 do - tvbar[c] = {} - local tcs = tvbar[c] - local sb = symbols[c] for i = 0, 9 do - tcs[i] = Vbar:from_int(sb[i], mod, false) + Codeset:insert(Vbar:from_int(symbols[c][i], mod, false), c, i) end end end, - ["2"] = function (ean2, Vbar, mod) -- add-on EAN2 + ["2"] = function (ean2) -- add-on EAN2 ean2._main_len = 2 ean2._is_last_checksum = false - ean2._2_start_vbar = Vbar:from_int(112, mod, true) - ean2._2_sep_vbar = Vbar:from_int(11, mod, false) - ean2._2_codeset_vbar = {} - local tvbar = ean2._2_codeset_vbar + local mod = ean2.mod + local Vbar = ean2._libgeo.Vbar + local Vbar_archive = ean2._libgeo.Archive + local Codeset = Vbar_archive:new() + ean2._2_codeset_vbar = Codeset + Codeset:insert(Vbar:from_int(112, mod, true), "start") + Codeset:insert(Vbar:from_int(11, mod, false), "sep") local symbols = ean2._symbol for c = 1, 2 do - tvbar[c] = {} - local tcs = tvbar[c] - local sb = symbols[c] for i = 0, 9 do - tcs[i] = Vbar:from_int(sb[i], mod, false) + Codeset:insert(Vbar:from_int(symbols[c][i], mod, false), c, i) end end end, ["13+5"] = function (ean, Vbar, mod) -- EAN13 with EAN5 add-on - config_full(ean, Vbar, mod, 13, 5) + config_full(ean, 13, 5) end, ["13+2"] = function(ean, Vbar, mod) -- EAN13 with EAN2 add-on - config_full(ean, Vbar, mod, 13, 2) + config_full(ean, 13, 2) end, ["8+5"] = function(ean, Vbar, mod) -- EAN8 with EAN5 add-on - config_full(ean, Vbar, mod, 8, 5) + config_full(ean, 8, 5) end, ["8+2"] = function(ean, Vbar, mod) -- EAN8 with EAN2 add-on - config_full(ean, Vbar, mod, 8, 2) + config_full(ean, 8, 2) end, } -- ISBN @@ -491,58 +518,58 @@ local function isbn_checksum(isbn) return sum % 11 end +local function isbn_parse_state() --> parse state + return { + is_space = false, + is_dash = false, + isbn_len = 0, + } +end + -- group char for readibility '-' or ' ' -- char won't be inserted in the top isbn code -local function isbn_check_char(_, c, parse_state) --> elem, err - if type(c) ~= "string" or #c ~= 1 then - return nil, "[InternalErr] invalid char" - end - if parse_state.isbncode == nil then parse_state.isbncode = {} end - local code = parse_state.isbncode - if parse_state.isspace == nil then parse_state.isspace = false end - if parse_state.isdash == nil then parse_state.isdash = false end - if parse_state.isbn_len == nil then parse_state.isbn_len = 0 end +local function isbn_process_char(_, c, parse_state) --> elem_code, elem_text, err local isbn_len = parse_state.isbn_len if c == "-" then if isbn_len == 0 then - return nil, "[ArgErr] an initial dash is not allowed" + return nil, nil, "[ArgErr] an initial dash is not allowed" end - if parse_state.isdash then - return nil, "[ArgErr] two consecutive dash char found" + if parse_state.is_dash then + return nil, nil, "[ArgErr] found two consecutive dash" end - parse_state.isdash = true - return nil, nil + parse_state.is_dash = true + return nil, c, nil elseif c == " " then if isbn_len == 0 then - return nil, "[ArgErr] an initial space is not allowed" + return nil, nil, "[ArgErr] an initial space is not allowed" + end + if parse_state.is_space then + return nil, nil, nil + else + parse_state.is_space = true + return nil, c, nil end - parse_state.isspace = true - return nil, nil elseif c == "X" then -- ISBN-10 checksum for 10 - code[#code + 1] = c isbn_len = isbn_len + 1 parse_state.isbn_len = isbn_len if isbn_len ~= 10 then - return nil, "[ArgErr] found a checksum 'X' in a wrong position" + return nil, nil, "[ArgErr] found a checksum 'X' in a wrong position" end - return 10, nil + return 10, c, nil else -- c is at this point eventually a digit local n = string.byte(c) - 48 if n < 0 or n > 9 then - return nil, "[ArgErr] found a not digit or a not grouping char" + return nil, nil, "[ArgErr] found a not digit or a not group char" end - if parse_state.isdash then -- close a group - code[#code + 1] = "-" - parse_state.isdash = false - parse_state.isspace = false - elseif parse_state.isspace then - code[#code + 1] = " " - parse_state.isspace = false + if parse_state.is_dash then -- close a group + parse_state.is_dash = false + parse_state.is_space = false + elseif parse_state.is_space then + parse_state.is_space = false end - code[#code + 1] = c isbn_len = isbn_len + 1 parse_state.isbn_len = isbn_len - return n, nil + return n, c, nil end end @@ -550,7 +577,8 @@ end -- parsed local function isbn_finalize(enc, parse_state) --> ok, err local var = enc._variant - local code_len = enc._code_len + local code_data = enc._code_data + local code_len = #code_data local isbn_len = parse_state.isbn_len local l1, l2 if var == "isbn" then @@ -559,7 +587,7 @@ local function isbn_finalize(enc, parse_state) --> ok, err elseif isbn_len == 13 then l1 = 13 else - return false, "[ArgErr] unsuitable ISBN code length" + return false, "[ArgErr] wrong ISBN code length of "..isbn_len end assert(l1 == code_len) elseif var == "isbn+5" then @@ -569,7 +597,7 @@ local function isbn_finalize(enc, parse_state) --> ok, err elseif isbn_len == 18 then l1, l2 = 13, 5 else - return false, "[ArgErr] unsuitable ISBN+5 code length" + return false, "[ArgErr] wrong ISBN+5 code length of "..isbn_len end assert(l1 + l2 == code_len) elseif var == "isbn+2" then @@ -579,13 +607,12 @@ local function isbn_finalize(enc, parse_state) --> ok, err elseif isbn_len == 15 then l1, l2 = 13, 2 else - return false, "[ArgErr] unsuitable ISBN+2 code length" + return false, "[ArgErr] wrong ISBN+2 code length of "..isbn_len end assert(l1 + l2 == code_len) else error("[InternalErr] unexpected ISBN variant code") end - local code_data = enc._code_data local isbn_auto = false if l1 == 10 then -- isbn 10 to 13 conversion local ck = isbn_checksum(code_data) @@ -606,7 +633,7 @@ local function isbn_finalize(enc, parse_state) --> ok, err return false, "[ArgErr] unmatched ISBN 13 checksum" end end - local isbncode = parse_state.isbncode + local isbncode = enc._code_text if l2 then -- nils the add-on digits local i = #isbncode while l2 > 0 do @@ -631,7 +658,6 @@ local function isbn_finalize(enc, parse_state) --> ok, err return false, "[ArgErr] too many groups found in the ISBN code" end if g > 0 then isbn_auto = true end - enc._isbncode = isbncode enc._isbntxt_on = isbn_auto return true, nil end @@ -660,84 +686,86 @@ local function to_n(c) --> n, err return n, false end +local function issn_parse_state() + return { + is_dash = false, + is_group_open = false, + is_group_close = false, + ed_var_len = 0, + ed_var_arr = {}, + code_len = 0, + addon_len = 0, + } +end + -- ISSN dddd-dddx[dd] or 13-long array -- spaces is always ignored -local function issn_check_char(enc, c, parse_state) --> elem, err - if (type(c) ~= "string") or (#c ~= 1) then - return nil, "[InternalErr] invalid char" - end - if parse_state.is_dash == nil then parse_state.is_dash = false end - if parse_state.is_group_open == nil then parse_state.is_group_open = false end - if parse_state.is_group_close == nil then parse_state.is_group_close = false end - if parse_state.ed_var_len == nil then parse_state.ed_var_len = 0 end - if parse_state.ed_var_arr == nil then parse_state.ed_var_arr = {} end - if parse_state.code_len == nil then parse_state.code_len = 0 end - if parse_state.addon_len == nil then parse_state.addon_len = 0 end +local function issn_process_char(enc, c, parse_state) --> e_data, e_txt, err local addon_len = enc._addon_len - -- edition variant part + -- `edition variant` input code part if c == " " then - return nil, nil -- ignore all spaces + return nil, nil, nil -- ignore all spaces end if parse_state.is_group_close then if addon_len then if parse_state.addon_len == enc._addon_len then - return nil, "[ArgErr] too many chars in the ISSN input code" + return nil, nil, "[ArgErr] too many chars in the ISSN input code" end local n, e = to_n(c) if e then - return nil, "[ArgErr] non digit char after a edition variant group" + return nil, nil, "[ArgErr] non digit char after an edition variant group" end parse_state.addon_len = parse_state.addon_len + 1 - return n, nil + return n, n, nil else - return nil, "[ArgErr] too many chars in the ISSN input code" + return nil, nil, "[ArgErr] too many chars in the ISSN input code" end end -- code part if c == "-" then if parse_state.is_dash then - return nil, "[ArgErr] two or more dash char in the input code" + return nil, nil, "[ArgErr] two or more dash chars in the input code" end if parse_state.code_len ~= 4 then - return nil, "[ArgErr] incorrect position for a dash sign" + return nil, nil, "[ArgErr] incorrect position for a dash sign" end parse_state.is_dash = true - return nil, nil + return nil, c, nil elseif c == "[" then -- two digits edition variant group opening if parse_state.code_len ~= 8 then - return nil, "[ArgErr] not a 8 digits long code for the ISSN input" + return nil, nil, "[ArgErr] not a 8-digits long code for the ISSN input" end parse_state.is_group_open = true - return nil, nil + return nil, nil, nil elseif c == "]" then -- two digits edition variant closing if not parse_state.is_group_open then - return nil, "[ArgErr] found a ']' without a '['" + return nil, nil, "[ArgErr] found a ']' without an opened '['" end if parse_state.ed_var_len ~= 2 then - return nil, "[ArgErr] edition variant group must be two digits long" + return nil, nil, "[ArgErr] edition variant group must be two digits long" end parse_state.is_group_open = false parse_state.is_group_close = true - return nil, nil + return nil, nil, nil elseif c == "X" then -- 8th ISSN checksum digit if parse_state.code_len ~= 7 then - return nil, "[ArgErr] incorrect position for checksum digit 'X'" + return nil, nil, "[ArgErr] incorrect position for checksum digit 'X'" end parse_state.code_len = 8 - return 10, nil - else -- at this point 'c' can be only a digit + return 10, c, nil + else -- at this point 'c' may be only a digit local n, e = to_n(c) if e then - return nil, "[ArgErr] found a non digit in code part" + return nil, nil, "[ArgErr] found a non digit in code part" end if parse_state.is_group_open then if parse_state.ed_var_len == 2 then - return nil, "[ArgErr] group digits are more than two" + return nil, nil, "[ArgErr] group digits are more than two" end parse_state.ed_var_len = parse_state.ed_var_len + 1 local t = parse_state.ed_var_arr t[#t + 1] = n - return nil, nil + return nil, c, nil end if parse_state.is_dash then if addon_len then @@ -745,29 +773,29 @@ local function issn_check_char(enc, c, parse_state) --> elem, err parse_state.code_len = parse_state.code_len + 1 else if parse_state.addon_len == addon_len then - return nil, "[ArgErr] too many digits for a 8 + "..addon_len.." ISSN input code" + return nil, nil, "[ArgErr] too many digits for a 8 + "..addon_len.." ISSN input code" end parse_state.addon_len = parse_state.addon_len + 1 end else if parse_state.code_len == 8 then - return nil, "[ArgErr] too many digits found for a 8 digits long ISSN input code" + return nil, nil, "[ArgErr] too many digits found for a 8 digits long ISSN input code" end parse_state.code_len = parse_state.code_len + 1 end else if addon_len then if parse_state.code_len == (13 + addon_len) then - return nil, "[ArgErr] too many digits in ISSN input code" + return nil, nil, "[ArgErr] too many digits in ISSN input code" end else if parse_state.code_len == 13 then - return nil, "[ArgErr] too many digits in a 13 digits long ISSN input code" + return nil, nil, "[ArgErr] too many digits in a 13 digits long ISSN input code" end end parse_state.code_len = parse_state.code_len + 1 end - return n, nil + return n, n, nil end end @@ -822,7 +850,7 @@ local function issn_finalize(enc, parse_state) --> ok, err return false, "[ArgErr] unclosed edition variant group in ISSN input code" end local data = enc._code_data - local code_len = enc._code_len + local code_len = #data local addon_len = enc._addon_len local main_len = code_len - (addon_len or 0) if main_len == 8 then @@ -862,12 +890,12 @@ local function basic_finalize(enc) --> ok, err local l1 = enc._main_len local l2 = enc._addon_len local ok_len = l1 + (l2 or 0) - local symb_len = enc._code_len + local data = enc._code_data + local symb_len = #data if symb_len ~= ok_len then return false, "[ArgErr] not a "..ok_len.."-digit long array" end if enc._is_last_checksum then -- is the last digit ok? - local data = enc._code_data local ck = checksum_8_13(data, l1 - 1) if ck ~= data[l1] then return false, "[Err] wrong checksum digit" @@ -881,7 +909,7 @@ end function EAN:_config() --> ok, err local variant = self._variant if not variant then - return false, "[Err] variant is mandatory for EAN family" + return false, "[Err] variant is mandatory for the EAN family" end local plus = variant:find("+") local v1 @@ -894,13 +922,13 @@ function EAN:_config() --> ok, err self._sub_variant_1 = v1 end local fnconfig = self._config_variant[variant] - local VbarClass = self._libgeo.Vbar -- Vbar class - local mod = self.mod - fnconfig(self, VbarClass, mod) + fnconfig(self) if v1 == "isbn" then - self._check_char = isbn_check_char + self._process_char = isbn_process_char + self._init_parse_state = isbn_parse_state elseif v1 == "issn" then - self._check_char = issn_check_char + self._process_char = issn_process_char + self._init_parse_state = issn_parse_state end return true, nil end @@ -987,51 +1015,39 @@ local fn_append_ga_variant = EAN._append_ga_variant -- draw EAN13 symbol fn_append_ga_variant["13"] = function (ean, canvas, tx, ty, ax, ay) - local code = ean._code_data - local mod = ean.mod - local bars_depth = mod * ean.bars_depth_factor - local w, h = 95*mod, ean.height + bars_depth - 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 -- current insertion x-coord - local ys = y0 + bars_depth - local s_width = 7*mod - local code_seq = ean._codeset_seq[code[1]] - -- draw the start symbol - local err - err = canvas:start_bbox_group(); assert(not err, err) - local be = ean._13_start_stop_vbar - err = canvas:encode_Vbar(be, xpos, y0, y1); assert(not err, err) - xpos = xpos + 3*mod - -- draw the first 6 numbers + local Codeset = ean._13_codeset_vbar + local mod = ean.mod + local queue_0 = assert(Codeset:get("start_stop")) + + 42*mod + + assert(Codeset:get("central")) + + 42*mod + + assert(Codeset:get("start_stop")) + local code = ean._code_data + local code_seq = ean._codeset_seq[code[1]] + local queue_1 = ean._libgeo.Vbar_queue:new() for i = 2, 7 do - local codeset = code_seq[i-1] - local n = code[i] - local vbar = ean._13_codeset_vbar[codeset][n] - err = canvas:encode_Vbar(vbar, xpos, ys, y1); assert(not err, err) - xpos = xpos + s_width + queue_1 = queue_1 + assert(Codeset:get(code_seq[i-1], code[i])) end - -- draw the control symbol - local ctrl = ean._13_ctrl_center_vbar - err = canvas:encode_Vbar(ctrl, xpos, y0, y1); assert(not err, err) - xpos = xpos + 5*mod - -- draw the last 6 numbers + queue_1 = queue_1 + 5*mod for i = 8, 13 do - local codeset = code_seq[i-1] - local n = code[i] - local vbar = ean._13_codeset_vbar[codeset][n] - err = canvas:encode_Vbar(vbar, xpos, ys, y1); assert(not err, err) - xpos = xpos + s_width + queue_1 = queue_1 + assert(Codeset:get(code_seq[i-1], code[i])) end - -- draw the stop char - err = canvas:encode_Vbar(be, xpos, y0, y1); assert(not err, err) + local bars_depth = mod * ean.bars_depth_factor + local w, h = 95*mod, ean.height + bars_depth + local x0 = tx - ax * w + local y0 = ty - ay * h + local x1 = x0 + w + local y1 = y0 + h + local ys = y0 + bars_depth + -- draw the symbol + assert(canvas:encode_disable_bbox()) + assert(canvas:encode_vbar_queue(queue_0, x0, y0, y1)) + assert(canvas:encode_vbar_queue(queue_1, x0 + 3*mod, ys, y1)) -- bounding box set up local qzl = ean.quietzone_left_factor * mod local qzr = ean.quietzone_right_factor * mod - err = canvas:stop_bbox_group(x0 - qzl, y0, x1 + qzr, y1) - assert(not err, err) + assert(canvas:encode_set_bbox(x0 - qzl, y0, x1 + qzr, y1)) + assert(canvas:encode_enable_bbox()) if ean.text_enabled then -- human readable text local Text = ean._libgeo.Text local txt_1 = Text:from_digit_array(code, 1, 1) @@ -1039,16 +1055,30 @@ fn_append_ga_variant["13"] = function (ean, canvas, tx, ty, ax, ay) local txt_3 = Text:from_digit_array(code, 8, 13) local y_bl = ys - ean.text_ygap_factor * mod local mx = ean.text_xgap_factor - err = canvas:encode_Text(txt_1, x0 - qzl, y_bl, 0, 1) - assert(not err, err) + assert(canvas:encode_Text(txt_1, x0 - qzl, y_bl, 0, 1)) local x2_1 = x0 + (3+mx)*mod local x2_2 = x0 + (46-mx)*mod - err = canvas:encode_Text_xwidth(txt_2, x2_1, x2_2, y_bl, 1) - assert(not err, err) + assert(canvas:encode_Text_xwidth(txt_2, x2_1, x2_2, y_bl, 1)) local x3_1 = x0 + (49+mx)*mod local x3_2 = x0 + (92-mx)*mod - err = canvas:encode_Text_xwidth(txt_3, x3_1, x3_2, y_bl, 1) - assert(not err, err) + assert(canvas:encode_Text_xwidth(txt_3, x3_1, x3_2, y_bl, 1)) + if ean.text_guard_enabled and (not ean._addon_len) then + local Polyline = ean._libgeo.Polyline + local mm = 186467.98 + local alpha = 36 * math.pi/180 + local pw = 0.30*mm + local dx = pw/(2*math.sin(alpha)) + local dy = (pw/2) * math.cos(alpha) + local px = x1 + qzr - dx + local pb = 1.55*mm + local ph = pb * math.tan(alpha) + local p = Polyline:new() + p:add_point(px - pb, y_bl - 2*ph - dy) + p:add_point(px, y_bl - ph - dy) + p:add_point(px - pb, y_bl - dy) + assert(canvas:encode_linewidth(pw)) + assert(canvas:encode_polyline(p)) + end local istxt = false if ean.text_isbn_enabled then if ean.text_isbn_enabled == "auto" then @@ -1060,7 +1090,7 @@ fn_append_ga_variant["13"] = function (ean, canvas, tx, ty, ax, ay) end end if istxt then - local isbn = assert(ean._isbncode, "[InternalErr] ISBN text not found") + local isbn = assert(ean._code_text, "[InternalErr] ISBN text not found") local descr = {"I", "S", "B", "N", " ",} for _, d in ipairs(isbn) do descr[#descr + 1] = d @@ -1068,13 +1098,12 @@ fn_append_ga_variant["13"] = function (ean, canvas, tx, ty, ax, ay) local isbn_txt = Text:from_chars(descr) local x_isbn = x0 + 47.5 * mod local y_isbn = y1 + ean.text_isbn_ygap_factor * mod - err = canvas:encode_Text(isbn_txt, x_isbn, y_isbn, 0.5, 0) - assert(not err, err) + assert(canvas:encode_Text(isbn_txt, x_isbn, y_isbn, 0.5, 0)) end -- issn text if ean.text_issn_enabled then local hri = {"I", "S", "S", "N", " "} - local txt = assert(ean._code_text, "[IternalErr] _code_text not found") + local txt = assert(ean._code_text, "[InternalErr] _code_text not found") for i = 1, 4 do hri[i + 5] = txt[i] end @@ -1085,59 +1114,46 @@ fn_append_ga_variant["13"] = function (ean, canvas, tx, ty, ax, ay) local issn_txt = Text:from_chars(hri) local x_issn = x0 + 47.5 * mod local y_issn = y1 + ean.text_issn_ygap_factor * mod - err = canvas:encode_Text(issn_txt, x_issn, y_issn, 0.5, 0) - assert(not err, err) + assert(canvas:encode_Text(issn_txt, x_issn, y_issn, 0.5, 0)) end end + return {x0, y0, x1, y1, qzl, nil, qzr, nil,} end -- draw EAN8 symbol fn_append_ga_variant["8"] = function (ean, canvas, tx, ty, ax, ay) - local code = ean._code_data - local mod = ean.mod - local bars_depth = mod * ean.bars_depth_factor - local w, h = 67*mod, ean.height + bars_depth - 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 -- current insertion x-coord - local ys = y0 + bars_depth - local s_width = 7*mod - -- draw the start symbol - local err - err = canvas:start_bbox_group(); assert(not err, err) - local be = ean._8_start_stop_vbar - err = canvas:encode_Vbar(be, xpos, y0, y1); assert(not err, err) - xpos = xpos + 3*mod - -- draw the first 4 numbers - local t_vbar = ean._8_codeset_vbar + local Codeset = ean._8_codeset_vbar + local mod = ean.mod + local q0 = assert(Codeset:get("start_stop")) + 28*mod + + assert(Codeset:get("central")) + 28*mod + + assert(Codeset:get("start_stop")) + local code = ean._code_data local cs14 = ean._codeset14_8 + local q1 = ean._libgeo.Vbar_queue:new() for i = 1, 4 do - local n = code[i] - local vbar = t_vbar[cs14][n] - err = canvas:encode_Vbar(vbar, xpos, ys, y1); assert(not err, err) - xpos = xpos + s_width + q1 = q1 + assert(Codeset:get(cs14, code[i])) end - -- draw the control symbol - local ctrl = ean._8_ctrl_center_vbar - err = canvas:encode_Vbar(ctrl, xpos, y0, y1); assert(not err, err) - xpos = xpos + 5*mod - -- draw the product code + q1 = q1 + 5*mod local cs58 = ean._codeset58_8 for i = 5, 8 do - local n = code[i] - local vbar = t_vbar[cs58][n] - err = canvas:encode_Vbar(vbar, xpos, ys, y1); assert(not err, err) - xpos = xpos + s_width + q1 = q1 + assert(Codeset:get(cs58, code[i])) end - -- draw the stop char - err = canvas:encode_Vbar(be, xpos, y0, y1); assert(not err, err) + local bars_depth = mod * ean.bars_depth_factor + local w, h = 67*mod, ean.height + bars_depth + local x0 = tx - ax * w + local y0 = ty - ay * h + local x1 = x0 + w + local y1 = y0 + h + local ys = y0 + bars_depth + -- draw the symbol + assert(canvas:encode_disable_bbox()) + assert(canvas:encode_vbar_queue(q0, x0, y0, y1)) + assert(canvas:encode_vbar_queue(q1, x0 + 3*mod, ys, y1)) -- bounding box set up local qzl = ean.quietzone_left_factor * mod local qzr = ean.quietzone_right_factor * mod - err = canvas:stop_bbox_group(x0 - qzl, y0, x1 + qzr, y1) - assert(not err, err) + assert(canvas:encode_set_bbox(x0 - qzl, y0, x1 + qzr, y1)) + assert(canvas:encode_enable_bbox()) if ean.text_enabled then -- human readable text local Text = ean._libgeo.Text local t_1 = Text:from_digit_array(code, 1, 4) @@ -1146,18 +1162,40 @@ fn_append_ga_variant["8"] = function (ean, canvas, tx, ty, ax, ay) local mx = ean.text_xgap_factor local x1_1 = x0 + ( 3 + mx)*mod local x1_2 = x0 + (32 - mx)*mod - err = canvas:encode_Text_xwidth(t_1, x1_1, x1_2, y_bl, 1) - assert(not err, err) + assert(canvas:encode_Text_xwidth(t_1, x1_1, x1_2, y_bl, 1)) local x2_1 = x0 + (35+mx)*mod local x2_2 = x0 + (64-mx)*mod - err = canvas:encode_Text_xwidth(t_2, x2_1, x2_2, y_bl, 1) - assert(not err, err) + assert(canvas:encode_Text_xwidth(t_2, x2_1, x2_2, y_bl, 1)) + if ean.text_guard_enabled and (not ean._addon_len) then + local Polyline = ean._libgeo.Polyline + local mm = 186467.98 + local alpha = 36 * math.pi/180 + local pw = 0.30*mm + local pb = 1.55*mm + local ph = pb * math.tan(alpha) + local dx = pw/(2*math.sin(alpha)) + local dy = (pw/2) * math.cos(alpha) + local px1, px2 = x0 - qzl + dx, x1 + qzr - dx + local p1 = Polyline:new() + p1:add_point(px1 + pb, y_bl - 2*ph - dy) + p1:add_point(px1, y_bl - ph - dy) + p1:add_point(px1 + pb, y_bl - dy) + local p2 = Polyline:new() + p2:add_point(px2 - pb, y_bl - 2*ph - dy) + p2:add_point(px2, y_bl - ph - dy) + p2:add_point(px2 - pb, y_bl - dy) + assert(canvas:encode_linewidth(pw)) + assert(canvas:encode_polyline(p1)) + assert(canvas:encode_polyline(p2)) + end end + return {x0, y0, x1, y1, qzl, nil, qzr, nil,} end -- draw EAN5 add-on symbol fn_append_ga_variant["5"] = function (ean, canvas, tx, ty, ax, ay, h) - local code = ean._code_data + local Codeset = ean._5_codeset_vbar + local queue = assert(Codeset:get("start")) local l1 = ean._main_len local i1; if l1 == 5 then i1 = 1 @@ -1165,81 +1203,70 @@ fn_append_ga_variant["5"] = function (ean, canvas, tx, ty, ax, ay, h) i1 = l1 + 1 end local i2 = i1 + 4 - local mod = ean.mod - local w = 47*mod - h = h or ean.height - 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 -- current insertion x-coord - local sym_w = 7*mod - local sep_w = 2*mod - -- draw the start symbol - local err - err = canvas:start_bbox_group(); assert(not err, err) - local start = ean._5_start_vbar - err = canvas:encode_Vbar(start, xpos, y0, y1); assert(not err, err) - xpos = xpos + 4*mod - local ck = checksum_5_2(code, i1, 5) - local codeset = ean._codeset_5[ck] - local sep = ean._5_sep_vbar - local t_vbar = ean._5_codeset_vbar + local mod = ean.mod -- draw the five digits + local code = ean._code_data + local ck = checksum_5_2(code, i1, 5) + local cset = ean._codeset_5[ck] local k = 0 for i = i1, i2 do k = k + 1 - local cs = codeset[k] -- 1 or 2 - local d = code[i] - local vbar = t_vbar[cs][d] - err = canvas:encode_Vbar(vbar, xpos, y0, y1); assert(not err, err) - xpos = xpos + sym_w + queue = queue + assert(Codeset:get(cset[k], code[i])) if k < 5 then - err = canvas:encode_Vbar(sep, xpos, y0, y1); assert(not err, err) - xpos = xpos + sep_w + queue = queue + assert(Codeset:get("sep")) end end + local w = 47*mod + h = h or ean.height + local x0 = tx - ax * w + local y0 = ty - ay * h + local x1 = x0 + w + local y1 = y0 + h + -- draw the symbol + assert(canvas:encode_disable_bbox()) + assert(canvas:encode_vbar_queue(queue, x0, y0, y1)) -- bounding box set up local qzl = ean.quietzone_left_factor * mod local qzr = ean.quietzone_right_factor * mod - err = canvas:stop_bbox_group(x0 - qzl, y0, x1 + qzr, y1) - assert(not err, err) + assert(canvas:encode_set_bbox(x0 - qzl, y0, x1 + qzr, y1)) + assert(canvas:encode_enable_bbox()) if ean.text_enabled then -- human readable text local Text = ean._libgeo.Text local txt = Text:from_digit_array(code, i1, i2) local y_bl = y1 + ean.text_ygap_factor * mod local x1_1 = x0 + 3*mod local x1_2 = x1 - 3*mod - err = canvas:encode_Text_xwidth(txt, x1_1, x1_2, y_bl, 0) - assert(not err, err) + assert(canvas:encode_Text_xwidth(txt, x1_1, x1_2, y_bl, 0)) + if ean.text_guard_enabled then + local Polyline = ean._libgeo.Polyline + local mm = 186467.98 + local alpha = 36 * math.pi/180 + local pw = 0.30*mm + local dx = pw/(2*math.sin(alpha)) + local dy = (pw/2) * math.cos(alpha) + local px = x1 + qzr - dx + local pb = 1.55*mm + local ph = pb * math.tan(alpha) + local p = Polyline:new() + p:add_point(px - pb, y_bl + dy) + p:add_point(px, y_bl + ph + dy) + p:add_point(px - pb, y_bl + 2*ph + dy) + assert(canvas:encode_linewidth(pw)) + assert(canvas:encode_polyline(p)) + end end + return {x0, y0, x1, y1, qzl, nil, qzr, nil,} end -- draw EAN2 symbol fn_append_ga_variant["2"] = function (ean, canvas, tx, ty, ax, ay, h) - local code = ean._code_data local l1 = ean._main_len local i1; if l1 == 2 then i1 = 1 else i1 = l1 + 1 end - local mod = ean.mod - local w = 20*mod - h = h or ean.height - local x0 = (tx or 0.0) - ax * w - local y0 = (ty or 0.0) - ay * h - local x1 = x0 + w - local y1 = y0 + h - local xpos = x0 -- current insertion x-coord - local sym_w = 7*mod - local sep_w = 2*mod - -- draw the start symbol - local err - err = canvas:start_bbox_group(); assert(not err, err) - local start = ean._2_start_vbar - err = canvas:encode_Vbar(start, xpos, y0, y1); assert(not err, err) - xpos = xpos + 4*mod + local code = ean._code_data local r = checksum_5_2(code, i1, 2) local s1, s2 if r == 0 then -- LL scheme @@ -1251,31 +1278,52 @@ fn_append_ga_variant["2"] = function (ean, canvas, tx, ty, ax, ay, h) else -- r == 3 -- GG scheme s1, s2 = 2, 2 end - local t_vbar = ean._2_codeset_vbar - local d1 = code[i1] -- render the first digit - local vb1 = t_vbar[s1][d1] - err = canvas:encode_Vbar(vb1, xpos, y0, y1); assert(not err, err) - xpos = xpos + sym_w - local sep = ean._2_sep_vbar - err = canvas:encode_Vbar(sep, xpos, y0, y1); assert(not err, err) - xpos = xpos + sep_w - local d2 = code[i1 + 1] -- render the second digit - local vb2 = t_vbar[s2][d2] - err = canvas:encode_Vbar(vb2, xpos, y0, y1); assert(not err, err) + local Codeset = ean._2_codeset_vbar + local queue = assert(Codeset:get("start")) + + assert(Codeset:get(s1, code[i1])) + -- first digit + assert(Codeset:get("sep")) + + assert(Codeset:get(s2, code[i1 + 1])) -- second digit + -- draw the symbol + local mod = ean.mod + local w = 20*mod + h = h or ean.height + local x0 = tx - ax * w + local y0 = ty - ay * h + local x1 = x0 + w + local y1 = y0 + h + assert(canvas:encode_disable_bbox()) + assert(canvas:encode_vbar_queue(queue, x0, y0, y1)) -- bounding box set up local qzl = ean.quietzone_left_factor * mod local qzr = ean.quietzone_right_factor * mod - err = canvas:stop_bbox_group(x0 - qzl, y0, x1 + qzr, y1) - assert(not err, err) + assert(canvas:encode_set_bbox(x0 - qzl, y0, x1 + qzr, y1)) + assert(canvas:encode_enable_bbox()) if ean.text_enabled then -- human readable text local Text = ean._libgeo.Text local txt = Text:from_digit_array(code, i1, i1 + 1) local y_bl = y1 + ean.text_ygap_factor * mod local x1_1 = x0 + 3*mod local x1_2 = x1 - 3*mod - err = canvas:encode_Text_xwidth(txt, x1_1, x1_2, y_bl, 0) - assert(not err, err) + assert(canvas:encode_Text_xwidth(txt, x1_1, x1_2, y_bl, 0)) + if ean.text_guard_enabled then + local Polyline = ean._libgeo.Polyline + local mm = 186467.98 + local alpha = 36 * math.pi/180 + local pw = 0.30*mm + local dx = pw/(2*math.sin(alpha)) + local dy = (pw/2) * math.cos(alpha) + local px = x1 + qzr - dx + local pb = 1.55*mm + local ph = pb * math.tan(alpha) + local p = Polyline:new() + p:add_point(px - pb, y_bl + dy) + p:add_point(px, y_bl + ph + dy) + p:add_point(px - pb, y_bl + 2*ph + dy) + assert(canvas:encode_linewidth(pw)) + assert(canvas:encode_polyline(p)) + end end + return {x0, y0, x1, y1, qzl, nil, qzr, nil,} end fn_append_ga_variant["13+5"] = function (ean, canvas, tx, ty, ax, ay) @@ -1283,14 +1331,15 @@ fn_append_ga_variant["13+5"] = function (ean, canvas, tx, ty, ax, ay) local bars_depth = mod * ean.bars_depth_factor local h = ean.height + bars_depth local w = (142 + ean.addon_xgap_factor) * mod - local x0 = (tx or 0) - ax * w - local y0 = (ty or 0) - ay * h + local x0 = tx - ax * w + local y0 = ty - ay * h local x1 = x0 + w local fn_ga = ean._append_ga_variant local fn_1 = fn_ga["13"] local fn_2 = fn_ga["5"] - fn_1(ean, canvas, x0, y0, 0, 0) - fn_2(ean, canvas, x1, y0, 1, 0, 0.85 * h) + local b1 = fn_1(ean, canvas, x0, y0, 0, 0) + local b2 = fn_2(ean, canvas, x1, y0, 1, 0, 0.85 * h) + return {b1[1], b1[2], b2[3], b1[4], b1[5], nil, b2[7], nil,} end fn_append_ga_variant["13+2"] = function (ean, canvas, tx, ty, ax, ay) @@ -1298,14 +1347,15 @@ fn_append_ga_variant["13+2"] = function (ean, canvas, tx, ty, ax, ay) local bars_depth = mod * ean.bars_depth_factor local h = ean.height + bars_depth local w = (115 + ean.addon_xgap_factor) * mod - local x0 = (tx or 0) - ax * w - local y0 = (ty or 0) - ay * h + local x0 = tx - ax * w + local y0 = ty - ay * h local x1 = x0 + w local fn_ga = ean._append_ga_variant local fn_1 = fn_ga["13"] local fn_2 = fn_ga["2"] - fn_1(ean, canvas, x0, y0, 0, 0) - fn_2(ean, canvas, x1, y0, 1, 0, 0.85 * h) + local b1 = fn_1(ean, canvas, x0, y0, 0, 0) + local b2 = fn_2(ean, canvas, x1, y0, 1, 0, 0.85 * h) + return {b1[1], b1[2], b2[3], b1[4], b1[5], nil, b2[7], nil,} end fn_append_ga_variant["8+5"] = function (ean, canvas, tx, ty, ax, ay) @@ -1313,14 +1363,15 @@ fn_append_ga_variant["8+5"] = function (ean, canvas, tx, ty, ax, ay) local bars_depth = mod * ean.bars_depth_factor local h = ean.height + bars_depth local w = (114 + ean.addon_xgap_factor) * mod - local x0 = (tx or 0) - ax * w - local y0 = (ty or 0) - ay * h + local x0 = tx - ax * w + local y0 = ty - ay * h local x1 = x0 + w local fn_ga = ean._append_ga_variant local fn_1 = fn_ga["8"] local fn_2 = fn_ga["5"] - fn_1(ean, canvas, x0, y0, 0, 0) - fn_2(ean, canvas, x1, y0, 1, 0, 0.85 * h) + local b1 = fn_1(ean, canvas, x0, y0, 0, 0) + local b2 = fn_2(ean, canvas, x1, y0, 1, 0, 0.85 * h) + return {b1[1], b1[2], b2[3], b1[4], b1[5], nil, b2[7], nil,} end fn_append_ga_variant["8+2"] = function (ean, canvas, tx, ty, ax, ay) @@ -1328,14 +1379,15 @@ fn_append_ga_variant["8+2"] = function (ean, canvas, tx, ty, ax, ay) local bars_depth = mod * ean.bars_depth_factor local h = ean.height + bars_depth local w = (87 + ean.addon_xgap_factor) * mod - local x0 = (tx or 0) - ax * w - local y0 = (ty or 0) - ay * h + local x0 = tx - ax * w + local y0 = ty - ay * h local x1 = x0 + w local fn_ga = ean._append_ga_variant local fn_1 = fn_ga["8"] local fn_2 = fn_ga["2"] - fn_1(ean, canvas, x0, y0, 0, 0) - fn_2(ean, canvas, x1, y0, 1, 0, 0.85 * h) + local b1 = fn_1(ean, canvas, x0, y0, 0, 0) + local b2 = fn_2(ean, canvas, x1, y0, 1, 0, 0.85 * h) + return {b1[1], b1[2], b2[3], b1[4], b1[5], nil, b2[7], nil,} end -- ISBN @@ -1350,12 +1402,11 @@ fn_append_ga_variant["issn+2"] = fn_append_ga_variant["13+2"] -- Drawing into the provided channel geometrical data -- tx, ty is the optional translation vector -- the function return the canvas reference to allow call chaining -function EAN:append_ga(canvas, tx, ty) --> canvas +function EAN:_append_ga(canvas, tx, ty) --> bbox local var = self._variant local fn_append_ga = assert(self._append_ga_variant[var]) local ax, ay = self.ax, self.ay - fn_append_ga(self, canvas, tx, ty, ax, ay) - return canvas + return fn_append_ga(self, canvas, tx, ty, ax, ay) end return EAN diff --git a/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-i2of5.lua b/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-i2of5.lua index 2e1dcd4a020..f0292639303 100644 --- a/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-i2of5.lua +++ b/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-i2of5.lua @@ -1,6 +1,6 @@ -- Interleaved 2 of 5 (ITF) barcode generator -- --- Copyright (C) 2020 Roberto Giacomelli +-- Copyright (C) 2019-2022 Roberto Giacomelli -- see LICENSE.txt file local ITF = { -- main container @@ -13,7 +13,7 @@ ITF._id_variant = { ITF14 = true, -- ITF 14 GS1 specification } -ITF._start = 111 -- nnnn +ITF._start = 1111 -- nnnn ITF._stop = 112 -- Wnn (integer must be in reverse order) ITF._pattern = { -- true -> narrow, false -> Wide @@ -40,6 +40,7 @@ ITF._par_order = { "bearer_bars_enabled", "bearer_bars_thickness", "bearer_bars_layout", + "text_enabled", } ITF._par_def = {} local pardef = ITF._par_def @@ -194,6 +195,19 @@ pardef.bearer_bars_layout = { -- enumeration end, } +-- human readable information +pardef.text_enabled = { -- boolean type + default = true, + isReserved = false, + fncheck = function (self, flag, _) --> boolean, err + if type(flag) == "boolean" then + return true, nil + else + return false, "[TypeErr] option 'text_enabled' is not a boolean value" + end + end, +} + -- ITF14: variant based on GS1 specification, see -- https://www.gs1.org/sites/default/files/docs/barcodes/GS1_General_Specifications.pdf -- GS1 ITF14 parameters vary in range and preferred value on the specific @@ -300,7 +314,7 @@ itf14_pdef.bearer_bars_enabled = { -- boolean type -- SHALL be a minimum of twice the width of a narrow bar (dark bar) and need -- only appear at the top and bottom of the symbol, butting directly against -- the top and bottom of the symbol bars (dark bars). -pardef.bearer_bars_thickness = { -- dimension +itf14_pdef.bearer_bars_thickness = { -- dimension default = 5 * 0.495 * 186467, -- 5 modules unit = "sp", -- scaled point isReserved = false, @@ -388,51 +402,50 @@ local function checkdigit(t, last, method) end end +local function itf14_parse_state() + return { + is_space = false, + is_popen = false, + itf14_len = 0, + } +end + -- group char for readibility '(' or ')' or ' ' -local function itf14_check_char(enc, c, parse_state) --> elem, err - if type(c) ~= "string" or #c ~= 1 then - return nil, "[InternalErr] invalid char" - end - if parse_state.itf14_code == nil then parse_state.itf14_code = {} end - local code = parse_state.itf14_code - if parse_state.is_space == nil then parse_state.is_space = false end - if parse_state.is_popen == nil then parse_state.is_popen = false end - if parse_state.itf14_len == nil then parse_state.itf14_len = 0 end - local itf14_len = parse_state.itf14_len +local function itf14_process_char(_, c, parse_state) --> e_data, e_text, err + local len14 = parse_state.itf14_len -- parsing if c == " " then - if itf14_len == 0 then -- ignore initial spaces - return nil, nil + if (len14 == 0) or (len14 == 14) then -- ignore initial or final spaces + return nil, nil, nil + end + if parse_state.is_space then -- ignore more than one consecutive space + return nil, nil, nil + else + parse_state.is_space = true + return nil, c, nil end - parse_state.is_space = true - return nil, nil elseif c == "(" then if parse_state.is_popen then - return nil, "[Err] a parenthesis group is already open" + return nil, nil, "[Err] a parenthesis is already opened" end parse_state.is_popen = true - code[#code + 1] = c - return nil, nil + return nil, c, nil elseif c == ")" then if not parse_state.is_popen then - return nil, "[Err] found a closing parenthesis without an opening one" + return nil, nil, "[Err] found a closing parenthesis without an opening one" end parse_state.is_popen = false - code[#code + 1] = c - return nil, nil + return nil, c, nil else -- c is at this point eventually a digit local n = string.byte(c) - 48 if n < 0 or n > 9 then return nil, "[ArgErr] found a not digit or a not grouping char" end if parse_state.is_space then - code[#code + 1] = " " parse_state.is_space = false end - code[#code + 1] = c - itf14_len = itf14_len + 1 - parse_state.itf14_len = itf14_len - return n, nil + parse_state.itf14_len = len14 + 1 + return n, c, nil end end @@ -443,23 +456,25 @@ function ITF:_config() --> ok, err local wide = narrow * self.ratio -- start symbol local Vbar = self._libgeo.Vbar -- Vbar class - self._vbar_start = Vbar:from_int_revpair(self._start, narrow, wide) - self._vbar_stop = Vbar:from_int_revpair(self._stop, narrow, wide) + local archive = self._libgeo.Archive -- Vbar_archive class + local Repo = archive:new() + self._vbar_data = Repo + Repo:insert(Vbar:from_int_revpair(self._start, narrow, wide), "start") + Repo:insert(Vbar:from_int_revpair(self._stop, narrow, wide), "stop") -- build every possible pair of digits from 00 to 99 - self._vbar_data = {} - local vbar = self._vbar_data local pattern = self._pattern for dec = 0, 9 do for unit = 0, 9 do local t1 = pattern[dec] local t2 = pattern[unit] local n = dec*10 + unit - vbar[n] = Vbar:from_two_tab(t1, t2, narrow, wide) + Repo:insert(Vbar:from_two_tab(t1, t2, narrow, wide), n) end end local variant = self._variant if variant == "ITF14" then - self._check_char = itf14_check_char + self._process_char = itf14_process_char + self._init_parse_state = itf14_parse_state end return true, nil end @@ -467,21 +482,24 @@ end -- internal methods for constructors -- input code post processing for ITF14 variant -local function itf14_finalize(enc) --> ok, err +local function itf14_finalize(enc, parse_state) --> ok, err + if parse_state.is_popen then + return false, "[ArgErr] unclosed group in input code" + end -- check digit action local policy = enc.check_digit_policy - local slen = enc._code_len local digits = enc._code_data + local slen = #digits local is_add if policy == "verify" then if slen ~= 14 then - return nil, "[DataErr] incorrect input lenght of "..slen.. + return false, "[DataErr] incorrect input lenght of "..slen.. " respect to 14 (checksum policy 'verify')" end is_add = false elseif policy == "add" then if slen ~= 13 then - return nil, "[DataErr] incorrect input lenght of "..slen.. + return false, "[DataErr] incorrect input lenght of "..slen.. " respect to 13 (checksum policy 'add')" end is_add = true @@ -491,18 +509,27 @@ local function itf14_finalize(enc) --> ok, err elseif slen == 13 then is_add = true else - return nil, "[DataErr] incorrect input lenght of "..slen.. - " respect to the policy '"..policy.."'" + return false, "[DataErr] incorrect input lenght of "..slen.. + " respect to the current policy '"..policy.."'" end else return false, "[InternalErr] incorrect policy enum value '"..policy.."'" end assert(type(is_add) == "boolean") local cs = checkdigit(digits, 13, enc.check_digit_method) - if is_add then + if is_add then -- add the final checkdigit digits[14] = cs - enc._code_len = 14 - else + local dtxt = enc._code_text + if dtxt then + local len = #dtxt + local last = dtxt[len] + if last == " " then + dtxt[len] = cs + else + dtxt[len + 1] = cs + end + end + else -- check only the final digit if cs ~= digits[14] then return false, "[DataErr] last digit is not equal to checksum" end @@ -514,13 +541,13 @@ end local function basic_finalize(enc) --> ok, err -- check digit action local policy = enc.check_digit_policy - local slen = enc._code_len - local is_even = (slen % 2 == 0) local digits = enc._code_data + local slen = #digits + local is_even = (slen % 2 == 0) + if policy == "none" then if not is_even then rshift(digits) -- add a heading zero for padding - slen = slen + 1 end elseif policy == "add" then if is_even then @@ -541,16 +568,15 @@ local function basic_finalize(enc) --> ok, err else return false, "[InternalError] wrong enum value" end - enc._code_len = slen return true, nil end -- input code post processing -function ITF:_finalize() --> ok, err +function ITF:_finalize(parse_state) --> ok, err local var = self._variant if var then assert(var == "ITF14") - return itf14_finalize(self) + return itf14_finalize(self, parse_state) else return basic_finalize(self) end @@ -569,59 +595,74 @@ end -- drawing function -- tx, ty is an optional translator vector -function ITF:append_ga(canvas, tx, ty) --> canvas - local err = canvas:start_bbox_group(); assert(not err, err) - -- draw the start symbol - local xdim = self.module - local ratio = self.ratio - local symb_len = 2 * xdim * (3 + 2*ratio) - local x0 = tx or 0 - local xpos = x0 - local y0 = ty or 0 - local y1 = y0 + self.height - local start = self._vbar_start - local err - err = canvas:encode_Vbar(start, xpos, y0, y1); assert(not err, err) - xpos = xpos + 4 * xdim - -- draw the code symbol +function ITF:_append_ga(canvas, tx, ty) --> bbox + local Repo = self._vbar_data + local queue = assert(Repo:get("start")) -- build the vbars queue local digits = self._code_data - local vbars = self._vbar_data - for i = 1, #digits, 2 do + local n = #digits + for i = 1, n, 2 do local index = 10 * digits[i] + digits[i+1] - local b = vbars[index] - err = canvas:encode_Vbar(b, xpos, y0, y1); assert(not err, err) - xpos = xpos + symb_len + queue = queue + assert(Repo:get(index)) end - -- draw the stop symbol - local stop = self._vbar_stop - err = canvas:encode_Vbar(stop, xpos, y0, y1); assert(not err, err) + queue = queue + assert(Repo:get("stop")) + -- draw the start symbol + local xdim = self.module + local ratio = self.ratio + local w = xdim * (6 + ratio + n * (3 + 2 * ratio)) + local ax, ay = self.ax, self.ay + local h = self.height + local x0 = tx - w * ax + local y0 = ty - h * ay + local y1 = y0 + h + assert(canvas:encode_disable_bbox()) + assert(canvas:encode_vbar_queue(queue, x0, y0, y1)) -- bounding box setting - local x1 = xpos + (2 + ratio)*xdim + local x1 = x0 + w local qz = self.quietzone - if self._variant then + if self._variant == "ITF14" then -- "ITF14" qz = qz * xdim end - local b1x, b1y = x0 - qz, y0 - local b2x, b2y = x1 + qz, y1 + local b1x, b1y = x0 - qz, y0 + local b2x, b2y = x1 + qz, y1 + local bq1, bq3 = qz, qz + local bq2, bq4 if self.bearer_bars_enabled then - local w = self.bearer_bars_thickness - err = canvas:encode_linethick(w); assert(not err, err) - b1y, b2y = b1y - w, b2y + w + local t = self.bearer_bars_thickness + local t2 = t/2 + assert(canvas:encode_linewidth(t)) + b1y, b2y = b1y - t, b2y + t + bq2, bq4 = t, t local layout = self.bearer_bars_layout if layout == "hbar" then - err = canvas:encode_hline(b1x, b2x, y0 - w/2); assert(not err, err) - err = canvas:encode_hline(b1x, b2x, y1 + w/2); assert(not err, err) + assert(canvas:encode_hline(b1x, b2x, y0 - t2)) + assert(canvas:encode_hline(b1x, b2x, y1 + t2)) elseif layout == "frame" then - err = canvas:encode_rectangle(b1x - w/2, y0 - w/2, b2x + w/2, y1 + w/2) - assert(not err, err) - b1x, b2x = b1x - w, b2x + w + assert(canvas:encode_rect(b1x - t2, y0 - t2, b2x + t2, y1 + t2)) + b1x, b2x = b1x - t, b2x + t + bq1, bq3 = bq1 + t, bq3 + t + else + error("[InternalErr] unexpected bearer bars layout option value") + end + end + assert(canvas:encode_set_bbox(b1x, b1y, b2x, b2y)) + assert(canvas:encode_enable_bbox()) + if self.text_enabled then + local Text = self._libgeo.Text + local t + if self._variant == "ITF14" then + local code = self._code_text + if code == nil then + code = self._code_data + end + t = Text:from_chars(code) else - error("[IntenalErr] bearer bars layout option is wrong") + t = Text:from_digit_array(self._code_data) end + local y_top = b1y - 0.6 * 186467 -- mm + local x_top = (b1x + b2x)/2 + assert(canvas:encode_Text(t, x_top, y_top, 0.50, 1)) end - local err = canvas:stop_bbox_group(b1x, b1y, b2x, b2y) - assert(not err, err) - return canvas + return {x0, y0, x1, y1, bq1, bq2, bq3, bq4,} end return ITF diff --git a/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-upc.lua b/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-upc.lua new file mode 100644 index 00000000000..99bfd4349e8 --- /dev/null +++ b/Master/texmf-dist/scripts/barracuda/lib-barcode/brcd-upc.lua @@ -0,0 +1,251 @@ +-- Universal Product Code +-- UPC family barcode generator +-- +-- Copyright (C) 2019-2022 Roberto Giacomelli +-- see LICENSE.txt file + +local UPC = { + _VERSION = "upc v0.0.2", + _NAME = "upc", + _DESCRIPTION = "UPC barcode encoder", +} + +UPC._pattern = {[0] = 3211, 2221, 2122, 1411, 1132, 1231, 1114, 1312, 1213, 3112,} +UPC._sepbar = 111 + +UPC._id_variant = { + A = true, -- UPC-A +} + +-- common family parameters +UPC._par_order = { + "mod", + "height", + "bars_depth_factor", + "quietzone_factor", + "text_enabled", + "text_ygap_factor", + "text_xgap_factor", +} +UPC._par_def = {} +local pardef = UPC._par_def + +-- from Wikipedia https://en.wikipedia.org/wiki/Universal_Product_Code +-- The x-dimension for the UPC-A at the nominal size is 0.33 mm (0.013"). +-- UPC-A can be reduced or magnified anywhere from 80% to 200%. +-- standard module is 0.33 mm but it may vary from 0.264 to 0.66mm +pardef.mod = { + default = 0.33 * 186467, -- (mm to sp) X dimension (original value 0.33) + unit = "sp", -- scaled point + isReserved = true, + fncheck = function (_self, x, _) --> boolean, err + local mm = 186467 + local min, max = 0.264*mm, 0.660*mm + if x < min then + return false, "[OutOfRange] too small value for mod" + elseif x > max then + return false, "[OutOfRange] too big value for mod" + end + return true, nil + end, +} + +-- Nominal symbol height for UPC-A is 25.9 mm (1.02") +pardef.height = { + default = 25.9 * 186467, + unit = "sp", + isReserved = false, + fncheck = function (self, h, _enc) --> boolean, err + if h > 0 then + return true, nil + else + return false, "[OutOfRange] non positive value for height" + end + end, +} + +-- The bars forming the S (start), M (middle), and E (end) guard patterns, are +-- extended downwards by 5 times x-dimension, with a resulting nominal symbol +-- height of 27.55 mm (1.08") +pardef.bars_depth_factor = { + default = 5, + unit = "absolute-number", + isReserved = false, + fncheck = function (_self, b, _) --> boolean, err + if b >= 0 then + return true, nil + else + return false, "[OutOfRange] non positive value for bars_depth_factor" + end + end, +} + +-- A quiet zone, with a width of at least 9 times the x-dimension, must be +-- present on each side of the scannable area of the UPC-A barcode. +pardef.quietzone_factor = { + default = 9, + unit = "absolute-number", + isReserved = false, + fncheck = function (_self, qzf, _) --> boolean, err + if qzf > 0 then + return true, nil + else + return false, "[OutOfRange] non positive value for quietzone_right_factor" + end + end, +} + +-- enable/disable human readble text +pardef.text_enabled = { -- boolean type + default = true, + isReserved = false, + fncheck = function (_self, flag, _) --> boolean, err + if type(flag) == "boolean" then + return true, nil + else + return false, "[TypeErr] not a boolean value for text_enabled" + end + end, +} + +-- vertical gap from text and symbol baseline +pardef.text_ygap_factor = { + default = 1.0, + unit = "absolute-number", + isReserved = false, + fncheck = function (_self, t, _) --> boolean, err + if t >= 0 then + return true, nil + else + return false, "[OutOfRange] non positive value for text_ygap_factor" + end + end, +} + +-- horizontal gap from text and symbol bars +pardef.text_xgap_factor = { + default = 0.75, + unit = "absolute-number", + isReserved = false, + fncheck = function (_self, t, _) --> boolean, err + if t >= 0 then + return true, nil + else + return false, "[OutOfRange] non positive value for text_xgap_factor" + end + end, +} + +-- utility function + +-- the checksum of UPC-A +-- 'data' is an array of digits +local function checkdigit_A(data) --> checksum, ok + local s = 0 + for i = 1, 11, 2 do + s = s + data[i] + end + s = 3*s + for i = 2, 10, 2 do + s = s + data[i] + end + local res = s % 10 + if res > 0 then + res = 10 - res + end + return res, data[12] == res +end + +-- internal methods for Barcode costructors + +-- config function called at the moment of encoder construction +function UPC:_config() --> ok, err + local variant = self._variant + if not variant then + return false, "[Err] variant is mandatory for the UPC family" + end + local Vbar = self._libgeo.Vbar -- Vbar class + local Vbar_archive = self._libgeo.Archive -- Archive class + local Codeset = Vbar_archive:new() + self._upca_codeset = Codeset + local mod = self.mod + local sepbar = self._sepbar + Codeset:insert(Vbar:from_int(sepbar, mod), "sepbar") + local symb = self._pattern + for i = 0, 9 do + Codeset:insert(Vbar:from_int(symb[i], mod, false), 0, i) + Codeset:insert(Vbar:from_int(symb[i], mod, true), 1, i) + end + return true, nil +end + +-- function called every time an input UPC-A code has been completely parsed +function UPC:_finalize() --> ok, err + local data = self._code_data + local symb_len = #data + if symb_len ~= 12 then + return false, "[ArgErr] not a 12-digit long array" + end + local ck, ok = checkdigit_A(data) + if not ok then -- is the last digit ok? + return false, + "[Err] incorrect last digit "..data[12]..", the checksum is "..ck + end + return true, nil +end + +-- Drawing into the provided channel geometrical data +-- tx, ty is the optional translation vector +-- the function return the canvas reference to allow call chaining +function UPC:_append_ga(canvas, tx, ty) --> bbox + local Codeset = self._upca_codeset + local code = self._code_data + local mod = self.mod + local sepbar = assert(Codeset:get("sepbar")) + local queue_0 = sepbar + assert(Codeset:get(0, code[1])) + + 36*mod + sepbar + 36*mod + + assert(Codeset:get(1, code[12])) + sepbar + local queue_1 = 10*mod + for i = 2, 6 do + queue_1 = queue_1 + assert(Codeset:get(0, code[i])) + end + queue_1 = queue_1 + 5*mod + for i = 7, 11 do + queue_1 = queue_1 + assert(Codeset:get(1, code[i])) + end + local bars_depth = mod * self.bars_depth_factor + local w, h = 95*mod, self.height + bars_depth + local x0 = tx - self.ax * w + local y0 = ty - self.ay * h + local x1 = x0 + w + local y1 = y0 + h + local ys = y0 + bars_depth + -- draw the symbol + assert(canvas:encode_disable_bbox()) + assert(canvas:encode_vbar_queue(queue_0, x0, y0, y1)) + assert(canvas:encode_vbar_queue(queue_1, x0, ys, y1)) + -- bounding box set up + local qz = self.quietzone_factor * mod + assert(canvas:encode_set_bbox(x0 - qz, y0, x1 + qz, y1)) + assert(canvas:encode_enable_bbox()) + if self.text_enabled then -- human readable text + local Text = self._libgeo.Text + local txt_1 = Text:from_digit_array(code, 1, 1) + local txt_2 = Text:from_digit_array(code, 2, 6) + local txt_3 = Text:from_digit_array(code, 7, 11) + local txt_4 = Text:from_digit_array(code, 12, 12) + local y_bl = ys - self.text_ygap_factor * mod + assert(canvas:encode_Text(txt_1, x0 - qz, y_bl, 0, 1)) + local mx = self.text_xgap_factor + local x2_1 = x0 + (10+mx)*mod + local x2_2 = x0 + (46-mx)*mod + assert(canvas:encode_Text_xwidth(txt_2, x2_1, x2_2, y_bl, 1)) + local x3_1 = x0 + (49+mx)*mod + local x3_2 = x0 + (85-mx)*mod + assert(canvas:encode_Text_xwidth(txt_3, x3_1, x3_2, y_bl, 1)) + assert(canvas:encode_Text(txt_4, x1 + qz, y_bl, 1, 1)) + end + return {x0, y0, x1, y1, qz, nil, qz, nil,} +end + +return UPC diff --git a/Master/texmf-dist/scripts/barracuda/lib-driver/brcd-driver.lua b/Master/texmf-dist/scripts/barracuda/lib-driver/brcd-driver.lua index 045a23e9d62..d2d86720aec 100644 --- a/Master/texmf-dist/scripts/barracuda/lib-driver/brcd-driver.lua +++ b/Master/texmf-dist/scripts/barracuda/lib-driver/brcd-driver.lua @@ -1,19 +1,16 @@ -- -- ga Intermediate Graphic Language for barcode drawing --- Copyright (C) 2020 Roberto Giacomelli +-- Copyright (C) 2019-2022 Roberto Giacomelli -- -- Basic driver interface --- drawing elementary vector shape +-- drawing elementary vector graphic -- All dimensions are in scaled point (sp) -local Driver = { - _VERSION = "Driver v0.0.9", - _NAME = "Driver", - _DESCRIPTION = "Driver for ga graphic assembler stream", -} - +local Driver = {_classname = "Driver"} Driver.__index = Driver Driver._drv_instance = {} -- driver instances repository +Driver.mm = 186467.98110236 -- conversion factor sp -> mm (millimeter) +Driver.bp = 65781.76 -- conversion factor sp -> bp (big point) -- driver_type/submodule name Driver._drv_available_drv = { -- lowercase keys please @@ -21,12 +18,13 @@ Driver._drv_available_drv = { -- lowercase keys please svg = "lib-driver.brcd-drv-svg", } +-- id_drv is specific driver identifier as a string function Driver:_get_driver(id_drv) --> object, err if type(id_drv) ~= "string" then - return nil, "[ArgErr] 'id_drv' is not a string" + return nil, "[ArgErr: id_drv] string expected" end if not self._drv_available_drv[id_drv] then - return nil, "[ArgErr] driver '"..id_drv.."' not found" + return nil, "[ArgErr: id_drv] driver '"..id_drv.."' not found" end local t_drv = self._drv_instance if t_drv[id_drv] then -- is the repo already loaded? @@ -39,47 +37,87 @@ function Driver:_get_driver(id_drv) --> object, err end end -function Driver:_new_state() --> a new state +function Driver:default_style() --> new table as default graphic init state return { - line_width = 65781.76, -- line width 1bp (in scaled point sp) - line_cap = 0, -- line cap style - line_join = 0, -- line join style - gtext = false, -- text group off - bb_on = true, -- bounding box checking activation - bb_x1 = nil, -- bounding box coordinates in sp (nil means no data) - bb_y1 = nil, - bb_x2 = nil, - bb_y2 = nil, - mm = 186467.98110236, -- conversion factor sp -> mm (millimeter) - bp = 65781.76, -- conversion factor sp -> bp (big point) + linewidth = 65781.76, -- line width 1bp (in scaled point sp) + linecap = 0, -- line cap style + linejoin = 0, -- line join style + dashpattern = nil, -- dash definition + dashphase = nil, -- dash phase definition } end -function Driver:_ga_process(drv, ga, st, bf, xt) +function Driver:_new_state() --> a new state + local st = self:default_style() + st.bb_on = true -- bounding box checking is active + st.gtext = false -- text group off + st.bb_x1 = nil -- bounding box coordinates in sp (nil means no data) + st.bb_y1 = nil + st.bb_x2 = nil + st.bb_y2 = nil + st.mm = self.mm -- conversion factor sp -> mm (millimeter) + st.bp = self.bp -- conversion factor sp -> bp (big point) + return st +end + +function Driver:_ga_init_style(drv, st, bf, xt) + local op_fn = self._opcode_v001 + -- linewidth + if not drv.append_001 then + error("[InternalErr] unimplemented opcode 1 for "..drv._drvname) + end + local w = st.linewidth + drv.append_001(st, bf, xt, w) + -- linecap + if not drv.append_002 then + error("[InternalErr] unimplemented opcode 2 for "..drv._drvname) + end + local linecap = st.linecap + drv.append_002(st, bf, xt, linecap) + -- line join style + if not drv.append_003 then + error("[InternalErr] unimplemented opcode 3 for "..drv._drvname) + end + local join = st.linejoin + drv.append_003(st, bf, xt, join) + -- dash pattern (reset) + if not drv.append_006 then + error("[InternalErr] unimplemented opcode 6 for "..drv._drvname) + end + drv.append_006(st, bf, xt) +end + +function Driver:_ga_process(drv, ga_stream, st, bf, xt) local op_fn = self._opcode_v001 local pc = 1 -- program counter - local data = ga._data - while data[pc] do -- stream processing - local opcode = data[pc] + while ga_stream[pc] do -- stream processing + local opcode = ga_stream[pc] local fn = assert(op_fn[opcode], "[InternalErr] unimpl opcode ".. opcode) - pc = fn(drv, st, pc + 1, data, bf, xt) + pc = fn(drv, st, pc + 1, ga_stream, bf, xt) end end -- save graphic data in an external file with the 'id_drv' format --- id_drv: specific driver output --- ga: ga object --- filename: file name --- ext: file extension (optional) +-- id_drv: specific driver output identifier +-- ga ::= gaCanvas class | ga stream table array +-- filename ::= string, file name +-- ext ::= string, file extension (optional, default SVG) function Driver:save(id_drv, ga, filename, ext) --> ok, err -- retrive the output library local drv, err = self:_get_driver(id_drv) if err then return false, err end + local ga_stream; if ga._classname == "gaCanvas" then + ga_stream = ga:get_stream() + else + ga_stream = ga + end -- init local state = self:_new_state() local buf, txt_buf = drv.init_buffer(state) -- a new buffer and text_buffer + -- send every defualt style parameter to the special driver + self:_ga_init_style(drv, state, buf, txt_buf) -- processing - self:_ga_process(drv, ga, state, buf, txt_buf) + self:_ga_process(drv, ga_stream, state, buf, txt_buf) -- buffer finalizing drv.close_buffer(state, buf, txt_buf) -- finalize the istruction -- file saving @@ -91,9 +129,19 @@ function Driver:save(id_drv, ga, filename, ext) --> ok, err return true, nil end --- insert a ga drawing in a TeX box +-- insert a ga drawing in a TeX hbox -- PDFnative only function +-- ga ::= gaCanvas class | ga stream table array +-- boxname ::= string function Driver:ga_to_hbox(ga, boxname) --> ok, err + if type(ga) ~= "table" then + return false, "[ArgErr: ga] table expected" + end + local ga_stream; if ga._classname == "gaCanvas" then + ga_stream = ga:get_stream() + else + ga_stream = ga + end -- retrive the output library local id_drv = "native" local drv, err = self:_get_driver(id_drv) @@ -101,13 +149,14 @@ function Driver:ga_to_hbox(ga, boxname) --> ok, err -- init process local state = self:_new_state() local buf, txt_buf = drv.init_buffer(state) -- a new buffer and text_buffer + -- send every defualt style parameter to the special driver + self:_ga_init_style(drv, state, buf, txt_buf) -- processing - self:_ga_process(drv, ga, state, buf, txt_buf) + self:_ga_process(drv, ga_stream, state, buf, txt_buf) -- finalizing drv.close_buffer(state, buf, txt_buf) -- finalize the istruction sequence -- build hbox - local ok, err = drv.hboxcreate(boxname, state, buf, txt_buf) - return ok, err + return drv.hboxcreate(boxname, state, buf, txt_buf) end @@ -124,38 +173,62 @@ end Driver._opcode_v001 = { [1] = function (drv, st, pc, ga, bf, xt) -- 1 ; set line width local w = ga[pc] - st.line_width = w - if not drv.append_001 then - error("[InternalErr] unimplemented opcode 1 for "..drv._NAME) - end + st.linewidth = w drv.append_001(st, bf, xt, w) return pc + 1 end, [2] = function (drv, st, pc, ga, bf, xt) -- 2 ; set line cap style local style = ga[pc] - st.line_cap = style + st.linecap = style if not drv.append_002 then - error("[InternalErr] unimplemented opcode 2 for "..drv._NAME) + error("[InternalErr] unimplemented opcode 2 for "..drv._drvname) end drv.append_002(st, bf, xt, style) return pc + 1 end, [3] = function (drv, st, pc, ga, bf, xt) -- 3 ; set line join style local join = ga[pc] - if not drv.append_003 then - error("[InternalErr] unimplemented opcode 3 for "..drv._NAME) - end + st.linejoin = join drv.append_003(st, bf, xt, join) return pc + 1 end, - [30] = function (drv, st, pc, ga, bf, xt) -- start_bbox_group - assert(st.bb_on) - st.bb_on = false + -- 5 , Dash pattern line style + -- phase n [bi ]+ + [5] = function (drv, st, pc, ga, bf, xt) + if not drv.append_005 then + error("[InternalErr] unimplemented opcode 5 for "..drv._drvname) + end + local phase = ga[pc]; pc = pc + 1 + local n = ga[pc]; pc = pc + 1 + assert(n > 0, "[Err] dash pattern needs one length or more ("..n..")") + st.dashphase = phase + local dash = {} + for i = pc, pc + n - 1 do + local v = ga[i] + dash[#dash + 1] = v + end + st.dashpattern = dash + drv.append_005(st, bf, xt, phase, dash) + return pc + n + end, + [6] = function (drv, st, pc, ga, bf, xt) -- 6 + if not drv.append_006 then + error("[InternalErr] unimplemented opcode 6 for "..drv._drvname) + end + st.dashpattern = nil -- reset dash pattern array and phase + st.dashphase = nil + drv.append_006(st, bf, xt) return pc end, - [31] = function (drv, st, pc, ga, bf, xt) -- end_bbox_group - assert(st.bb_on == false) + [29] = function (drv, st, pc, ga, bf, xt) -- enable_bbox st.bb_on = true + return pc + end, + [30] = function (drv, st, pc, ga, bf, xt) -- disable_bbox + st.bb_on = false + return pc + end, + [31] = function (drv, st, pc, ga, bf, xt) -- set_bbox local x1 = ga[pc]; pc = pc + 1 local y1 = ga[pc]; pc = pc + 1 local x2 = ga[pc]; pc = pc + 1 @@ -180,7 +253,7 @@ Driver._opcode_v001 = { local x2 = ga[pc]; pc = pc + 1 local y = ga[pc]; pc = pc + 1 if st.bb_on then -- eventually update bbox - local hw = st.line_width/2 + local hw = st.linewidth/2 local by1 = y - hw local by2 = y + hw if st.bb_x1 == nil then @@ -196,7 +269,7 @@ Driver._opcode_v001 = { end end if not drv.append_033 then - error("[InternalErr] unimplemented opcode 33 for "..drv._NAME) + error("[InternalErr] unimplemented opcode 33 for "..drv._drvname) end drv.append_033(st, bf, xt, x1, x2, y) return pc @@ -206,8 +279,8 @@ Driver._opcode_v001 = { local y1 = ga[pc]; pc = pc + 1 local y2 = ga[pc]; pc = pc + 1 local x = ga[pc]; pc = pc + 1 - if st.bb_on then -- eventually update the figure bounding box - local hw = st.line_width/2 + if st.bb_on then -- eventually update the figure's bounding box + local hw = st.linewidth/2 local bx1 = x - hw local bx2 = x + hw if st.bb_x1 == nil then @@ -223,7 +296,7 @@ Driver._opcode_v001 = { end end if not drv.append_034 then - error("[InternalErr] unimplemented opcode 34 for "..drv._NAME) + error("[InternalErr] unimplemented opcode 34 for "..drv._drvname) end drv.append_034(st, bf, xt, y1, y2, x) return pc @@ -277,16 +350,64 @@ Driver._opcode_v001 = { end return pc_next end, + -- draw a polyline + -- 38 ... + -- basic support for bounding box calculation + [38] = function(drv, st, pc, ga, bf, xt) -- polyline + local n = ga[pc]; pc = pc + 1; assert(n > 1) + local x1 = ga[pc]; pc = pc + 1 + local y1 = ga[pc]; pc = pc + 1 + if drv.append_038_start then + drv.append_038_start(st, bf, xt, n, x1, y1) + end + local pc_next = pc + 2*(n - 1) + local bx1, bx2, by1, by2 = x1, x1, y1, y1 -- simplified bb vertex + for i = pc, pc_next - 1, 2 do -- reading coordinates + local x = assert(ga[i], "[InternalErr] ga prematurely reached the end") + local y = assert(ga[i+1], "[InternalErr] ga prematurely reached the end") + drv.append_038_point(st, bf, xt, x, y) + -- check the bounding box only if the corresponding flag is true + if st.bb_on then + if x > bx2 then + bx2 = x + elseif x < bx1 then + bx1 = x + end + if y > by2 then + by2 = y + elseif y < by1 then + by1 = y + end + end + end + if drv.append_038_stop then + drv.append_038_stop(st, bf, xt) + end + if st.bb_on then -- eventually update bbox + if st.bb_x1 == nil then + st.bb_x1 = bx1 + st.bb_x2 = bx2 + st.bb_y1 = by1 + st.bb_y2 = by2 + else + if bx1 < st.bb_x1 then st.bb_x1 = bx1 end + if bx2 > st.bb_x2 then st.bb_x2 = bx2 end + if by1 < st.bb_y1 then st.bb_y1 = by1 end + if by2 > st.bb_y2 then st.bb_y2 = by2 end + end + end + return pc_next + end, -- draw a rectangle -- 48 [48] = function(drv, st, pc, ga, bf, xt) - local x1 = ga[pc]; pc = pc + 1 - local y1 = ga[pc]; pc = pc + 1 - local x2 = ga[pc]; pc = pc + 1 - local y2 = ga[pc]; pc = pc + 1 + local x1 = ga[pc]; pc = pc + 1 + local y1 = ga[pc]; pc = pc + 1 + local x2 = ga[pc]; pc = pc + 1 + local y2 = ga[pc]; pc = pc + 1 -- check the bounding box only if the flag is true if st.bb_on then - local hw = st.line_width/2 + local hw = st.linewidth/2 local bx1, bx2 = x1 - hw, x2 + hw local by1, by2 = y1 - hw, y2 + hw if st.bb_x1 == nil then @@ -302,7 +423,7 @@ Driver._opcode_v001 = { end end if not drv.append_048 then - error("[InternalErr] unimplemented opcode 48 for "..drv._NAME) + error("[InternalErr] unimplemented opcode 48 for "..drv._drvname) end drv.append_048(st, bf, xt, x1, y1, x2, y2) return pc diff --git a/Master/texmf-dist/scripts/barracuda/lib-driver/brcd-drv-pdfliteral.lua b/Master/texmf-dist/scripts/barracuda/lib-driver/brcd-drv-pdfliteral.lua index b36021f9b62..a5cfccf4cb8 100644 --- a/Master/texmf-dist/scripts/barracuda/lib-driver/brcd-drv-pdfliteral.lua +++ b/Master/texmf-dist/scripts/barracuda/lib-driver/brcd-drv-pdfliteral.lua @@ -1,22 +1,17 @@ -- -- ga graphic assembler or -- Intermediate Graphic Language for barcode drawing --- Copyright (C) 2020 Roberto Giacomelli +-- Copyright (C) 2019-2022 Roberto Giacomelli -- -- All dimensions are in scaled point (sp) -- ga LuaTeX Driver (native implementation node+pdfliteral) --- class for drawing elementary geometric elements -local PDFnative = { - _VERSION = "PDFnative v0.0.4", - _NAME = "PDFnative", - _DESCRIPTION = "A LuaTeX native pdfliteral driver for ga graphic stream", -} - local tex = assert(tex, "This require a Lua powered TeX engine") local node = assert(node, "This require a Lua powered TeX engine") local font = assert(font, "This require a Lua powered TeX engine") +-- class for drawing elementary geometric elements +local PDFnative = {_drvname = "PDFnative"} PDFnative.ext = "txt" -- file extension PDFnative.buf_sep = "\n" -- separation string for buffer concat @@ -24,6 +19,7 @@ function PDFnative.init_buffer(st) --> buffer, text buffer st.head = nil -- addition for text processing (to remember purpose) st.last = nil st.hbox = nil + st.dash_array = nil st.cw = nil st.x_hbox = nil st.char_counter = nil @@ -126,12 +122,27 @@ function PDFnative.append_003(st, bf, xt, j) bf[#bf + 1] = string.format("%d j", j) end +-- 5 dash_pattern +function PDFnative.append_005(st, bf, xt, phase, dash_array) + local bp = st.bp -- conversion factor bp -> sp + local t = {} + for _, d in ipairs(dash_array) do + t[#t + 1] = string.format("%0.6f", d/bp) + end + bf[#bf + 1] = string.format("[%s] %0.6f d",table.concat(t, " "), phase/bp) +end + +-- 6 reset_pattern +function PDFnative.append_006(st, bf, xt) + bf[#bf + 1] = string.format("[] 0 d") +end + -- draw an horizontal line -- 33 function PDFnative.append_033(st, bf, xt, x1, x2, y) local bp = st.bp -- conversion factor bp -> sp - bf[#bf + 1] = string.format("% 0.6f %0.6f m", x1/bp, y/bp) - bf[#bf + 1] = string.format("% 0.6f %0.6f l", x2/bp, y/bp) + bf[#bf + 1] = string.format("%0.6f %0.6f m", x1/bp, y/bp) + bf[#bf + 1] = string.format("%0.6f %0.6f l", x2/bp, y/bp) bf[#bf + 1] = "S" -- stroke end @@ -140,8 +151,8 @@ end -- 34 function PDFnative.append_034(st, bf, xt, y1, y2, x) local bp = st.bp -- conversion factor bp -> sp - bf[#bf + 1] = string.format("% 0.6f %0.6f m", x/bp, y1/bp) - bf[#bf + 1] = string.format("% 0.6f %0.6f l", x/bp, y2/bp) + bf[#bf + 1] = string.format("%0.6f %0.6f m", x/bp, y1/bp) + bf[#bf + 1] = string.format("%0.6f %0.6f l", x/bp, y2/bp) bf[#bf + 1] = "S" -- stroke end @@ -161,6 +172,21 @@ function PDFnative.append_036_stop(st, bf, xt, nbar, y1, y2) bf[#bf + 1] = "S" -- stroke end +-- Polyline +-- draw a polyline +-- 38 ... +function PDFnative.append_038_start(st, bf, xt, n, x1, y1) + local bp = st.bp -- conversion factor bp -> sp + bf[#bf + 1] = string.format("%0.6f %0.6f m", x1/bp, y1/bp) +end +function PDFnative.append_038_point(st, bf, xt, x, y) + local bp = st.bp -- conversion factor bp -> sp + bf[#bf + 1] = string.format("%0.6f %0.6f l", x/bp, y/bp) +end +function PDFnative.append_038_stop(st, bf, xt) + bf[#bf + 1] = "S" -- stroke +end + -- draw a rectangle -- 48 function PDFnative.append_048(st, bf, xt, x1, y1, x2, y2) diff --git a/Master/texmf-dist/scripts/barracuda/lib-driver/brcd-drv-svg.lua b/Master/texmf-dist/scripts/barracuda/lib-driver/brcd-drv-svg.lua index ce1f5bffbc2..df5ae0e366f 100644 --- a/Master/texmf-dist/scripts/barracuda/lib-driver/brcd-drv-svg.lua +++ b/Master/texmf-dist/scripts/barracuda/lib-driver/brcd-drv-svg.lua @@ -1,18 +1,13 @@ -- -- ga Intermediate Graphic Language for barcode drawing --- SVG library --- Copyright (C) 2020 Roberto Giacomelli +-- SVG driver for the ga graphic stream +-- Copyright (C) 2019-2022 Roberto Giacomelli -- -- All dimension in the ga stream are scaled point (sp) -- 1 pt = 65536 sp -- class for drawing elementary geometric elements -local SVG = { - _VERSION = "SVGdriver v0.0.1", - _NAME = "SVGdriver", - _DESCRIPTION = "A SVG driver for the ga graphic stream", -} - +local SVG = {_drvname = "SVG"} SVG.ext = "svg" -- file extension SVG.buf_sep = nil -- separation string for buffer concat @@ -30,10 +25,10 @@ function SVG.init_buffer(st) --> buffer, text buffer } -- additions for SVG driver to 'state' st.ident_lvl = 1 -- identation level - st.char_buf = nil + st.char_buf = nil -- character buffer local mm = st.mm st.h_char = 2.1 * mm -- char height (mm) - st.w_char = st.h_char / 1.303 -- avg char width (mm) + st.w_char = st.h_char / 1.303 -- average char width (mm) st.d_char = st.h_char / 3.7 -- char deep (mm) return bf, {} end @@ -42,7 +37,7 @@ function SVG.close_buffer(st, bf, xt) bf[#bf + 1] = '\n' -- close svg xml element bf[#bf + 1] = '\n' -- a last empty line local mm = st.mm - local x1, y1, x2, y2 = st.bb_x1, st.bb_y1, st.bb_x2, st.bb_y2 + local x1, y1, x2, y2 = st.bb_x1 or 0, st.bb_y1 or 0, st.bb_x2 or 0, st.bb_y2 or 0 local w = (x2 - x1)/mm local h = (y2 - y1)/mm local fmt_wh = bf[7] @@ -55,25 +50,55 @@ end -- SVG encoding functions -- 1 ; set line width -function SVG.append_001(st, bf, xt, w) - -- nothing to do +function SVG.append_001(st, bf, xt, w) -- nothing to do +end + +-- 2 ; set line cap style +function SVG.append_002(st, bf, xt, cap) -- nothing to do +end + +-- 3 ; set line join style +function SVG.append_003(st, bf, xt, j) -- nothing to do +end + +-- 5 +function SVG.append_005(st, bf, xt) -- nothing to do +end + +-- 6 +function SVG.append_006(st, bf, xt) -- nothing to do end --- draw an horizontal line +-- draw an horizontal line opcode -- 33 function SVG.append_033(st, bf, xt, x1, x2, y) local lvl = st.ident_lvl local ident = string.rep(" ", lvl) -- a couple of spaces as indentation local mm = st.mm -- conversion ratio sp -> bp bf[#bf + 1] = string.format( -- element - '%s= 0) + if phase > 0 then + bf[#bf + 1] = string.format( + '%s stroke-dashoffset="%0.6f"\n', ident, phase/mm + ) + end + end bf[#bf + 1] = ident..'/>\n' end @@ -83,14 +108,29 @@ function SVG.append_034(st, bf, xt, y1, y2, x) local ident = string.rep(" ", lvl) -- a couple of spaces as indentation local mm = st.mm -- conversion ratio sp -> mm bf[#bf + 1] = string.format( -- element - '%s= 0) + if phase > 0 then + bf[#bf + 1] = string.format( + '%s stroke-dashoffset="%0.6f"\n', ident, phase/mm + ) + end + end bf[#bf + 1] = ident..'/>\n' end @@ -118,6 +158,91 @@ function SVG.append_036_stop(st, bf, xt, nbar, y1, y2) bf[#bf + 1] = ident..'\n' -- end group end +-- Polyline +-- draw a polyline +-- 38 ... +function SVG.append_038_start(st, bf, xt, n, x1, y1) + local lvl = st.ident_lvl + local ident = string.rep(" ", lvl) + local mm = st.mm -- conversion factor mm -> sp + local style = string.format( + '%s= 0) + if phase > 0 then + style = string.format( + '%s stroke-dashoffset="%0.6f"', style, phase/mm + ) + end + end + bf[#bf + 1] = style..'>\n' + ident = ident .. " " + bf[#bf + 1] = string.format('%s sp + bf[#bf + 1] = string.format('\n%s%0.6f,%0.6f', ident, x/mm, -y/mm) +end +function SVG.append_038_stop(st, bf, xt) + bf[#bf + 1] = '"/>\n' -- closing tag + local lvl = st.ident_lvl - 1 + local ident = string.rep(" ", lvl) + st.ident_lvl = lvl + bf[#bf + 1] = ident..'\n' -- close the group +end + +-- draw a rectangle +-- 48 +function SVG.append_048(st, bf, xt, x1, y1, x2, y2) + local w = x2 - x1 -- rectangle width + assert(w > 0) + local h = y2 - y1 -- rectangle height + assert(h > 0) + local mm = st.mm -- conversion factor mm -> sp + local lvl = st.ident_lvl + local ident = string.rep(" ", lvl) + local fmt = '%s= 0) + if phase > 0 then + bf[#bf + 1] = string.format( + '%s stroke-dashoffset="%0.6f"\n', ident, phase/mm + ) + end + end + bf[#bf + 1] = ident..'/>\n' +end + -- Text -- 130 Text with several glyphs -- 130 diff --git a/Master/texmf-dist/scripts/barracuda/lib-geo/brcd-gacanvas.lua b/Master/texmf-dist/scripts/barracuda/lib-geo/brcd-gacanvas.lua index 86eba659d1b..fc58a85b7b5 100644 --- a/Master/texmf-dist/scripts/barracuda/lib-geo/brcd-gacanvas.lua +++ b/Master/texmf-dist/scripts/barracuda/lib-geo/brcd-gacanvas.lua @@ -1,86 +1,208 @@ -- class gaCanvas --- Copyright (C) 2020 Roberto Giacomelli +-- Copyright (C) 2019-2022 Roberto Giacomelli +-- encoding functions (see barracuda-manual.pdf) --- ga -- basic function - -local gaCanvas = { - _VERSION = "gacanvas v0.0.4", - _NAME = "gaCanvas", - _DESCRIPTION = "A library for dealing with ga stream", -} +local gaCanvas = {_classname = "gaCanvas"} gaCanvas.__index = gaCanvas +gaCanvas.mm = 186467.98110236 -- conversion factor sp -> mm (millimeter) +gaCanvas.bp = 65781.76 -- conversion factor sp -> bp (big point) +gaCanvas.pt = 65536 -- sp -> pt -- ga specification: see the file ga-grammar.pdf in the doc directory -- gaCanvas constructor function gaCanvas:new() --> object - local o = { - _data = {}, - _v = 100, -- version of the ga format - } + local drv = assert(self._driver_class) + -- graphic state fields returned by Driver:default_style() method + -- linewidth + -- linecap + -- linejoin + -- dashpattern + -- dashphase + local o = assert(drv:default_style()) -- initial graphic state + o._version = 100 -- version of the ga format + o._data = {} -- ga stream array + o.bb_on = true setmetatable(o, self) return o end --- Ipothetical another constructor +-- save graphic data in an external file with the 'id_drv' format +-- id_drv::= string, specific driver output identifier +-- filename ::= string, file name +-- ext ::= string, file extension (optional, default SVG) +function gaCanvas:save(id_drv, filename, ext) --> ok, err + local driver = self._driver_class + return driver:save(id_drv, self, filename, ext) +end + +-- insert a ga drawing in a TeX hbox +-- PDFnative only function +-- boxname ::= string +function gaCanvas:ga_to_hbox(boxname) --> ok, err + local driver = self._driver_class + return driver:ga_to_hbox(self, boxname) +end + +-- return a clone of the stream array +function gaCanvas:get_stream() --> table (as a flat array) + local data = self._data + local t = {} + for _, v in ipairs(data) do + t[#t + 1] = v + end + return t +end + +-- Ipothetical further constructor -- function gaCanvas:from_tcp_server() --> err -- end +-- function gaCanvas:get_bbox() +-- end + +-- function gaCanvas:check() --> ok, err +-- end + -- line width: opcode <1> -function gaCanvas:encode_linethick(w) --> err - if type(w) ~= "number" then return "[ArgErr] 'w' number expected" end - if w < 0 then return "[ArgErr] negative value for 'w'" end +function gaCanvas:encode_linewidth(w) --> ok, err + if type(w) ~= "number" then + return false, "[ArgErr 'w'] number expected" + end + if w < 0 then return false, "[ArgErr: w] negative number not allowed" end + self.linewidth = w local data = self._data data[#data + 1] = 1 -- opcode for line thickness data[#data + 1] = w + return true, nil end --- line cap style: opcode <2> --- 0 Butt cap --- 1 Round cap --- 2 Projecting square cap -function gaCanvas:encode_linecap(cap) --> err - if type(cap) ~= "number" then return "[ArgErr] 'cap' arg number expected" end - if cap == 0 or cap == 1 or cap == 2 then - local data = self._data - data[#data + 1] = 2 -- opcode for line cap style - data[#data + 1] = cap +-- line cap style: opcode <2> cap | +-- 0 | "butt": Butt cap +-- 1 | "round": Round cap +-- 2 | "proj": Projecting square cap +function gaCanvas:encode_linecap(cap) --> ok, err + local tcap = type(cap) + local cap_res + if tcap == "number" then + if cap == 0 or cap == 1 or cap == 2 then + cap_res = cap + else + return false, "[ArgErr: cap] 0, 1 or 2 expected" + end + elseif tcap == "string" then + if cap == "butt" then + cap_res = 0 + elseif cap == "round" then + cap_res = 1 + elseif cap == "proj" then + cap_res = 2 + else + return false, "[ArgErr: cap] 'butt', 'round' or 'proj' expected" + end else - return "[ArgErr] invalid value for 'cap'" + return false, "[ArgErr: cap] number or string expected" end + self.linecap = cap_res + local data = self._data + data[#data + 1] = 2 -- opcode for line cap style + data[#data + 1] = cap_res + return true, nil end --- line cap style: opcode <3> --- 0 Miter join --- 1 Round join --- 2 Bevel join -function gaCanvas:encode_linejoin(join) --> err - if type(join) ~= "number" then return "[ArgErr] 'join' arg number expected" end - if join == 0 or join == 1 or join == 2 then - local data = self._data - data[#data + 1] = 3 -- opcode for line join style - data[#data + 1] = join +-- line join style: opcode <3> join | +-- 0 | "miter" : Miter join +-- 1 | "round" : Round join +-- 2 | "bevel" : Bevel join +function gaCanvas:encode_linejoin(join) --> ok, err + local tjoin = type(join) + local join_res + if tjoin == "number" then + if join == 0 or join == 1 or join == 2 then + join_res = join + else + return false, "[ArgErr: join] 0, 1 or 2 integer expected" + end + elseif tjoin == "string" then + if join == "miter" then + join_res = 0 + elseif join == "round" then + join_res = 1 + elseif join == "bevel" then + join_res = 2 + else + return false, "[ArgErr: join] '"..join.."' join style not found" + end else - return "[ArgErr] invalid value for 'join'" + return false, "[ArgErr: join] number or string expected" + end + self.linejoin = join_res + local data = self._data + data[#data + 1] = 3 -- opcode for line join style + data[#data + 1] = join_res + return true, nil +end + +-- 5 , Dash pattern line style, p n bi +-- p: phase lenght +-- n: number of array element +-- [bi]: dash array of dash and gap lenghts +function gaCanvas:encode_dash_pattern(p, ...) --> ok, err + if type(p) ~= "number" then return false, "[ArgErr: phase] number expected" end + local t = {...} + for i, v in ipairs(t) do + if type(v) ~= "number" then + return false, "[ArgErr: array] found a not number value at index " .. i + end + end + self.dash_pattern = t + self.dash_phase = p + local n = #t + local d = self._data + d[#d + 1] = 5 + d[#d + 1] = p + d[#d + 1] = n + for _, b in ipairs(t) do + d[#d + 1] = b end + return true, nil +end + +-- 6 , set the continous line style +function gaCanvas:encode_reset_pattern() --> ok, err + self.dash_pattern = nil + self.dash_phase = nil + local d = self._data + d[#d + 1] = 6 + return true, nil +end + +-- checking the bounding box from now on +-- opcode: <29> +function gaCanvas:encode_enable_bbox() --> ok, err + self.bb_on = true + local data = self._data + data[#data + 1] = 29 + return true, nil end -- Stop checking the bounding box -- opcode: <30> -function gaCanvas:start_bbox_group() --> err +function gaCanvas:encode_disable_bbox() --> ok, err + self.bb_on = false local data = self._data data[#data + 1] = 30 + return true, nil end --- restart checking the bounding box --- and insert the specified bb for the entire object group +-- insert a figure bbox -- code: <31> x1 y1 x2 y2 -function gaCanvas:stop_bbox_group(x1, y1, x2, y2) --> err - if type(x1) ~= "number" then return "[ArgErr] 'x1' number expected" end - if type(y1) ~= "number" then return "[ArgErr] 'y1' number expected" end - if type(x2) ~= "number" then return "[ArgErr] 'x2' number expected" end - if type(y2) ~= "number" then return "[ArgErr] 'y2' number expected" end - if x1 > x2 then x1, x2 = x2, x1 end -- reorder coordinates +function gaCanvas:encode_set_bbox(x1, y1, x2, y2) --> ok, err + if type(x1) ~= "number" then return false, "[ArgErr: x1] number expected" end + if type(y1) ~= "number" then return false, "[ArgErr: y1] number expected" end + if type(x2) ~= "number" then return false, "[ArgErr: x2] number expected" end + if type(y2) ~= "number" then return false, "[ArgErr: y2] number expected" end + if x1 > x2 then x1, x2 = x2, x1 end -- re-order coordinates if y1 > y2 then y1, y2 = y2, y1 end local data = self._data data[#data + 1] = 31 -- bounding box of the object group @@ -88,174 +210,368 @@ function gaCanvas:stop_bbox_group(x1, y1, x2, y2) --> err data[#data + 1] = y1 data[#data + 1] = x2 data[#data + 1] = y2 + return true, nil end -- insert a line from point (x1, y1) to the point (x2, y2) +-- tx optional translator x-vector +-- ty optional translator y-vector -- <32> x1 y1 x2 y2 -function gaCanvas:encode_line(x1, y1, x2, y2) --> err - if type(x1) ~= "number" then return "[ArgErr] 'x1' number expected" end - if type(y1) ~= "number" then return "[ArgErr] 'y1' number expected" end - if type(x2) ~= "number" then return "[ArgErr] 'x2' number expected" end - if type(y2) ~= "number" then return "[ArgErr] 'y2' number expected" end +function gaCanvas:encode_line(x1, y1, x2, y2, tx, ty) --> ok, err + if type(x1) ~= "number" then return false, "[ArgErr: x1] number expected" end + if type(y1) ~= "number" then return false, "[ArgErr: y1] number expected" end + if type(x2) ~= "number" then return false, "[ArgErr: x2] number expected" end + if type(y2) ~= "number" then return false, "[ArgErr: y2] number expected" end + if tx ~= nil then + if type(tx) ~= "number" then return false, "[ArgErr: tx] number expected" end + x1 = x1 + tx + x2 = x2 + tx + end + if ty ~= nil then + if type(ty) ~= "number" then return false, "[ArgErr: ty] number expected" end + y1 = y1 + ty + y2 = y2 + ty + end local data = self._data data[#data + 1] = 32 -- append line data data[#data + 1] = x1 data[#data + 1] = y1 data[#data + 1] = x2 data[#data + 1] = y2 + return true, nil end -- insert an horizontal line from point (x1, y) to point (x2, y) +-- tx optional translator x-vector +-- ty optional translator y-vector -- <33> x1 x2 y -function gaCanvas:encode_hline(x1, x2, y) --> err - if type(x1) ~= "number" then return "[ArgErr] 'x1' number expected" end - if type(x2) ~= "number" then return "[ArgErr] 'x2' number expected" end - if type(y) ~= "number" then return "[ArgErr] 'y2' number expected" end +function gaCanvas:encode_hline(x1, x2, y, tx, ty) --> ok, err + if type(x1) ~= "number" then return false, "[ArgErr: x1] number expected" end + if type(x2) ~= "number" then return false, "[ArgErr: x2] number expected" end + if type(y) ~= "number" then return false, "[ArgErr: y] number expected" end + if tx ~= nil then + if type(tx) ~= "number" then return false, "[ArgErr: tx] number expected" end + x1 = x1 + tx + x2 = x2 + tx + end + if ty ~= nil then + if type(ty) ~= "number" then return false, "[ArgErr: ty] number expected" end + y = y + ty + end local data = self._data data[#data + 1] = 33 -- append hline data data[#data + 1] = x1 data[#data + 1] = x2 data[#data + 1] = y + return true, nil +end + +-- vline, Vertical line, from point (x, y1) to point (x, y2) +-- tx optional translator x-vector +-- ty optional translator y-vector +-- <34> y1 y2 x +function gaCanvas:encode_vline(y1, y2, x, tx, ty) --> ok, err + if type(y1) ~= "number" then return false, "[ArgErr] 'y1' number expected" end + if type(y2) ~= "number" then return false, "[ArgErr] 'y2' number expected" end + if type(x) ~= "number" then return false, "[ArgErr] 'x' number expected" end + if tx ~= nil then + if type(tx) ~= "number" then return false, "[ArgErr: tx] number expected" end + x = x + tx + end + if ty ~= nil then + if type(ty) ~= "number" then return false, "[ArgErr: ty] number expected" end + y1 = y1 + ty + y2 = y2 + ty + end + local data = self._data + data[#data + 1] = 34 -- append vline data + data[#data + 1] = y1 + data[#data + 1] = y2 + data[#data + 1] = x + return true, nil +end + +-- insert an open polyline +-- tx optional translator x-vector +-- ty optional translator y-vector +-- <38> x1 y1 x2, y2, ... , xn, yn +function gaCanvas:encode_polyline(point, tx, ty) --> ok, err + if type(point) ~= "table" then + return false, "[ArgErr: point] table expected" + end + local n, p + if point._classname == "Polyline" then + n, p = point:get_points() + else + local len = #point + if len == 0 then return false, "[Err] 'point' is an empty table" end + if (len % 2) ~= 0 then + return false, "[Err] 'point' is not an even long array" + end + n = len/2 + for _, coord in ipairs(point) do + if type(coord) ~= "number" then + return false, "[Err] found a non-number in the 'point' array" + end + end + p = point + end + if n < 2 then return + false, "[Err] a polyline must have at least two points" + end + if tx ~= nil then + if type(tx) ~= "number" then return false, "[ArgErr: tx] number expected" end + else + tx = 0 + end + if ty ~= nil then + if type(ty) ~= "number" then return false, "[ArgErr: ty] number expected" end + else + ty = 0 + end + local data = self._data + data[#data + 1] = 38 + data[#data + 1] = n + for i = 1, 2*n, 2 do + data[#data + 1] = p[i] + tx + data[#data + 1] = p[i + 1] + ty + end + return true, nil end -- insert a rectangle from point (x1, x2) to (x2, y2) +-- tx optional translator x-vector +-- ty optional translator y-vector -- <48> -function gaCanvas:encode_rectangle(x1, y1, x2, y2) --> err - if type(x1) ~= "number" then return "[ArgErr] 'x1' number expected" end - if type(y1) ~= "number" then return "[ArgErr] 'y1' number expected" end - if type(x2) ~= "number" then return "[ArgErr] 'x2' number expected" end - if type(y2) ~= "number" then return "[ArgErr] 'y2' number expected" end +function gaCanvas:encode_rect(x1, y1, x2, y2, tx, ty) --> ok, err + if type(x1) ~= "number" then return false, "[ArgErr] 'x1' number expected" end + if type(y1) ~= "number" then return false, "[ArgErr] 'y1' number expected" end + if type(x2) ~= "number" then return false, "[ArgErr] 'x2' number expected" end + if type(y2) ~= "number" then return false, "[ArgErr] 'y2' number expected" end + if tx ~= nil then + if type(tx) ~= "number" then return false, "[ArgErr: tx] number expected" end + x1 = x1 + tx + x2 = x2 + tx + end + if ty ~= nil then + if type(ty) ~= "number" then return false, "[ArgErr: ty] number expected" end + y1 = y1 + ty + y2 = y2 + ty + end local d = self._data d[#d + 1] = 48 -- append rectangle data d[#d + 1] = x1 d[#d + 1] = y1 d[#d + 1] = x2 d[#d + 1] = y2 + return true, nil end -- Vbar object: opcode <36> +-- tx optional translator x-vector +-- ty optional translator y-vector -- x0, y1, y2 ordinates -function gaCanvas:encode_Vbar(vbar, x0, y1, y2) --> err +function gaCanvas:encode_vbar(vbar, x0, y1, y2, tx, ty) --> ok, err if type(vbar) ~= "table" then - return "[ArgErr] table expected for 'vbar'" + return false, "[ArgErr: vbar] table expected" end if x0 == nil then x0 = 0 elseif type(x0) ~= "number" then - return "[ArgErr] 'x0' number expected" + return false, "[ArgErr: x0] number expected" end - if type(y1) ~= "number" then return "[ArgErr] 'y1' number expected" end - if type(y2) ~= "number" then return "[ArgErr] 'y2' number expected" end + if type(y1) ~= "number" then return false, "[ArgErr: y1] number expected" end + if type(y2) ~= "number" then return false, "[ArgErr: y2] number expected" end -- ordinates - if y1 == y2 then return "[ArgErr] 'y1' 'y2' are the same value" end + if y1 == y2 then return false, "[ArgErr: y1, y2] same value" end if y1 > y2 then y1, y2 = y2, y1 end - local bars = assert(vbar._yline, "[InternalErr] no '_yline' field") - local bdim = #bars - if bdim == 0 then return "[InternalErr] number of bars is zero" end - if bdim % 2 ~= 0 then - return "[InternalErr] '_yline' does not have an even number of elements" + local bdim, bars + if vbar._classname == "Vbar" then + bdim, bars = vbar:get_bars() + bdim = 2*bdim + else + bdim = #vbar + if bdim % 2 ~= 0 then -- bdim must be even + return false, "[Err: vbar] found an odd array of elements" + end + bars = vbar + end + if bdim == 0 then return false, "[Err: vbar] the number of bars is zero" end + if tx ~= nil then + if type(tx) ~= "number" then return false, "[ArgErr: tx] number expected" end + x0 = x0 + tx + end + if ty ~= nil then + if type(ty) ~= "number" then return false, "[ArgErr: ty] number expected" end + y1 = y1 + ty + y2 = y2 + ty end local data = self._data data[#data + 1] = 36 -- vbar sequence start data[#data + 1] = y1 data[#data + 1] = y2 - data[#data + 1] = bdim / 2 -- the number of bars + data[#data + 1] = bdim/2 -- the number of bars, pair for i = 1, bdim, 2 do local coord = bars[i] local width = bars[i + 1] if type(coord) ~= "number" then - return "[Err] a coordinates is not a number" + return false, "[Err] a coordinates is not a number" end if type(width) ~= "number" then - return "[Err] a width is not a number" + return false, "[Err] a width is not a number" end data[#data + 1] = coord + x0 data[#data + 1] = width end + return true, nil end --- [text] <130> ax ay x y chars -function gaCanvas:encode_Text(txt, xpos, ypos, ax, ay) --> err +-- print a Vbar queue starting at x position 'xpos', between the horizontal line +-- tx optional translator x-vector +-- ty optional translator y-vector +-- at y0 and y1 y-coordinates +function gaCanvas:encode_vbar_queue(queue, xpos, y0, y1, tx, ty) --> ok, err + -- check arg + if type(queue) ~= "table" then + return false, "[ArgErr: queue] table expected" + end + if type(xpos) ~= "number" then + return false, "[ArgErr: xpos] number expected" + end + if type(y0) ~= "number" then + return false, "[ArgErr: y0] number expected" + end + if type(y1) ~= "number" then + return false, "[ArgErr: y1] number expected" + end + local i = 2 + while queue[i] do + local x = queue[i - 1] + xpos + local vbar = queue[i] + local _, err = self:encode_vbar(vbar, x, y0, y1, tx, ty) + if err then return false, err end + i = i + 2 + end + return true, nil +end + +-- tx optional translator x-vector +-- ty optional translator y-vector +-- [text] <130> ax ay xpos ypos chars +function gaCanvas:encode_Text(txt, xpos, ypos, ax, ay, tx, ty) --> ok, err if type(txt) ~= "table" then - return "[ArgErr] 'txt' object table expected" + return false, "[ArgErr: txt] object table expected" end if type(xpos) ~= "number" then - return "[ArgErr] 'xpos' number expected" + return false, "[ArgErr: xpos] number expected" end if type(ypos) ~= "number" then - return "[ArgErr] 'ypos' number expected" + return false, "[ArgErr: ypos] number expected" end if ax == nil then ax = 0 elseif type(ax) ~= "number" then - return "[ArgErr] 'ax' number expected" + return false, "[ArgErr: ax] number expected" end if ay == nil then ay = 0 elseif type(ay) ~= "number" then - return "[ArgErr] 'ay' number expected" + return false, "[ArgErr: ay] number expected" end + if tx ~= nil then + if type(tx) ~= "number" then return false, "[ArgErr: tx] number expected" end + xpos = xpos + tx + end + if ty ~= nil then + if type(ty) ~= "number" then return false, "[ArgErr: ty] number expected" end + ypos = ypos + ty + end + local chars = assert(txt.codepoint, "[InternalErr: txt] no 'codepoint' found") + if #chars == 0 then return false, "[InternalErr: txt] no chars found" end local data = self._data data[#data + 1] = 130 data[#data + 1] = ax -- relative anchor x-coordinate data[#data + 1] = ay -- relative anchor y-coordinate data[#data + 1] = xpos -- text x-coordinate data[#data + 1] = ypos -- text y-coordinate - local chars = assert(txt.codepoint, "[InternalErr] no 'codepoint' field in txt") - if #chars == 0 then return "[InternalErr] 'txt' has no chars" end for _, c in ipairs(chars) do data[#data + 1] = c end data[#data + 1] = 0 -- end string signal + return true, nil end -- glyphs equally spaced along the baseline +-- tx optional translator x-vector +-- ty optional translator y-vector -- [text_xspaced] <131> x1 xgap ay ypos chars -function gaCanvas:encode_Text_xspaced(txt, x1, xgap, ypos, ay) --> err - if type(txt)~= "table" then return "[ArgErr] 'txt' object table expected" end - local chars = assert(txt.codepoint, "[InternalErr] no 'codepoint' field in txt") - if #chars == 0 then return "[InternalErr] 'txt' has no chars" end - if type(x1) ~= "number" then return "[ArgErr] 'x1' number expected" end - if type(xgap) ~= "number" then return "[ArgErr] 'xgap' number expected" end +function gaCanvas:encode_Text_xspaced(txt, x1, xgap, ypos, ay, tx, ty) --> ok, err + if type(txt)~= "table" then return false, "[ArgErr: txt] object table expected" end + local chars = assert(txt.codepoint, "[InternalErr: txt] no 'codepoint' found") + local n = #chars + if n == 0 then return false, "[InternalErr: txt] no chars found" end + if type(x1) ~= "number" then return false, "[ArgErr: x1] number expected" end + if type(xgap) ~= "number" then return false, "[ArgErr: xgap] number expected" end if xgap < 0 then local n = #chars x1 = x1 + (n - 1) * xgap xgap = -xgap end - if type(ypos) ~= "number" then return "[ArgErr] 'ypos' number expected" end + if type(ypos) ~= "number" then return false, "[ArgErr: ypos] number expected" end if ay == nil then ay = 0 elseif type(ay) ~= "number" then - return "[ArgErr] 'ay' number expected" + return false, "[ArgErr: ay] number expected" + end + if tx ~= nil then + if type(tx) ~= "number" then return false, "[ArgErr: tx] number expected" end + x1 = x1 + tx + end + if ty ~= nil then + if type(ty) ~= "number" then return false, "[ArgErr: ty] number expected" end + ypos = ypos + ty end local data = self._data data[#data + 1] = 131 data[#data + 1] = x1 -- x-coordinate of the first axis from left to right - data[#data + 1] = xgap -- axial distance among gliphs + data[#data + 1] = xgap -- axial distance between gliphs data[#data + 1] = ay -- anchor relative y-coordinate data[#data + 1] = ypos -- text y-coordinate for _, c in ipairs(chars) do data[#data + 1] = c end data[#data + 1] = 0 -- end string signal + return true, nil end -- text_xwidth +-- tx optional translator x-vector +-- ty optional translator y-vector -- text equally spaced but within [x1, x2] coordinate interval -- <132> -function gaCanvas:encode_Text_xwidth(txt, x1, x2, ypos, ay) --> err - if type(txt)~= "table" then return "[ArgErr] 'txt' object table expected" end - if type(x1) ~= "number" then return "[ArgErr] 'x1' number expected" end - if type(x2) ~= "number" then return "[ArgErr] 'x2' number expected" end - if type(ypos) ~= "number" then return "[ArgErr] 'ypos' number expected" end +function gaCanvas:encode_Text_xwidth(txt, x1, x2, ypos, ay, tx, ty) --> ok, err + if type(txt)~= "table" then return false, "[ArgErr: txt] object table expected" end + if type(x1) ~= "number" then return false, "[ArgErr: x1] number expected" end + if type(x2) ~= "number" then return false, "[ArgErr: x2] number expected" end + if type(ypos) ~= "number" then return false, "[ArgErr: ypos] number expected" end if ay == nil then ay = 0 elseif type(ay) ~= "number" then - return "[ArgErr] 'ay' number expected" + return false, "[ArgErr: ay] number expected" end - local chars = assert(txt.codepoint, "[InternalErr] no 'codepoint' field in txt") - if #chars == 0 then return "[InternalErr] 'txt' has no chars" end + local chars = assert(txt.codepoint, "[InternalErr: txt] no 'codepoint' found") + if #chars == 0 then return false, "[InternalErr: txt] no chars found" end if x1 > x2 then x1, x2 = x2, x1 end -- reorder coordinates + + if tx ~= nil then + if type(tx) ~= "number" then return false, "[ArgErr: tx] number expected" end + x1 = x1 + tx + x2 = x2 + tx + end + if ty ~= nil then + if type(ty) ~= "number" then return false, "[ArgErr: ty] number expected" end + ypos = ypos + ty + end local data = self._data data[#data + 1] = 132 data[#data + 1] = ay -- anchor relative y-coordinate @@ -266,32 +582,35 @@ function gaCanvas:encode_Text_xwidth(txt, x1, x2, ypos, ay) --> err data[#data + 1] = c end data[#data + 1] = 0 -- end string signal + return true, nil end -- experimental code section -- new opcodes under assessment -- [start_text_group] 140 -function gaCanvas:start_text_group() --> err +function gaCanvas:start_text_group() --> ok, err local data = self._data data[#data + 1] = 140 + return true, nil end -- [gtext] 141 -function gaCanvas:gtext(chars) --> err - if type(chars) ~= "table" then return "[ArgErr] 'chars' table expected" end +function gaCanvas:gtext(chars) --> ok, err + if type(chars) ~= "table" then return false, "[ArgErr: chars] table expected" end local data = self._data data[#data + 1] = 141 for _, c in ipairs(chars) do data[#data + 1] = c end data[#data + 1] = 0 -- end string signal + return true, nil end -- [gtext_spaced] 142 gap string -function gaCanvas:gtext_spaced(gap, chars) --> err - if type(gap) ~= "number" then return "[ArgErr] 'gap' number expected" end - if type(chars) ~= "table" then return "[ArgErr] 'chars' table expected" end +function gaCanvas:gtext_spaced(gap, chars) --> ok, err + if type(gap) ~= "number" then return false, "[ArgErr: gap] number expected" end + if type(chars) ~= "table" then return false, "[ArgErr: chars] table expected" end local data = self._data data[#data + 1] = 142 data[#data + 1] = gap @@ -299,42 +618,32 @@ function gaCanvas:gtext_spaced(gap, chars) --> err data[#data + 1] = c end data[#data + 1] = 0 -- end string signal + return true, nil end -- [gtext_space] 143 gap -function gaCanvas:gtext_gap(gap) --> err - if type(gap) ~= "number" then return "[ArgErr] 'gap' number expected" end +function gaCanvas:gtext_gap(gap) --> ok, err + if type(gap) ~= "number" then return false, "[ArgErr: gap] number expected" end local data = self._data data[#data + 1] = 143 data[#data + 1] = gap + return true, nil end -- [end_text_group] 149 ax ay x y -function gaCanvas:end_text_group(xpos, ypos, ax, ay) --> err - if type(xpos) ~= "number" then return "[ArgErr] 'xpos' number expected" end - if type(ypos) ~= "number" then return "[ArgErr] 'ypos' number expected" end - if type(ax) ~= "number" then return "[ArgErr] 'ax' number expected" end - if type(ay) ~= "number" then return "[ArgErr] 'ay' number expected" end +function gaCanvas:end_text_group(xpos, ypos, ax, ay) --> ok, err + if type(xpos) ~= "number" then return false, "[ArgErr: xpos] '' number expected" end + if type(ypos) ~= "number" then return false, "[ArgErr: ypos] number expected" end + if type(ax) ~= "number" then return false, "[ArgErr: ax] number expected" end + if type(ay) ~= "number" then return false, "[ArgErr: ay] number expected" end local data = self._data data[#data + 1] = 149 data[#data + 1] = ax -- anchor relative x-coordinate data[#data + 1] = ay -- anchor relative y-coordinate data[#data + 1] = xpos -- text x-coordinate data[#data + 1] = ypos -- text y-coordinate -end - --- amazing... -function gaCanvas:to_string() --> string - -end - -function gaCanvas:get_bbox() - -end - -function gaCanvas:check() --> boolean, err - + return true, nil end return gaCanvas diff --git a/Master/texmf-dist/scripts/barracuda/lib-geo/brcd-libgeo.lua b/Master/texmf-dist/scripts/barracuda/lib-geo/brcd-libgeo.lua index 630ed91b438..1e76ba1c678 100644 --- a/Master/texmf-dist/scripts/barracuda/lib-geo/brcd-libgeo.lua +++ b/Master/texmf-dist/scripts/barracuda/lib-geo/brcd-libgeo.lua @@ -1,20 +1,137 @@ - +-- this file is part of barracuda project +-- Copyright (C) 2019-2022 Roberto Giacomelli +-- see https://github.com/robitex/barracuda +-- -- libgeo simple Geometric Library --- Copyright (C) 2018 Roberto Giacomelli - --- All dimension must be in scaled point (sp) +-- All dimension must be in scaled point (sp) a TeX unit equal to 1/65536pt local libgeo = { - _VERSION = "libgeo v0.0.3", + _VERSION = "libgeo v0.0.6", _NAME = "libgeo", _DESCRIPTION = "simple geometric library", } --- VBar class +-- a simple tree structured Archive class +libgeo.Archive = {_classname = "Archive"} +local Archive = libgeo.Archive +Archive.__index = Archive + +function Archive:new() --> object + local o = { + _archive = {} + } + setmetatable(o, self) + return o +end + +function Archive:insert(o, ...) --> ok, err + if type(o) ~= "table" then + return false, "[Err] " + end + local a = self._archive + local keys = {...} + for i = 1, (#keys - 1) do -- dive into the tree + local k = keys[i] + local leaf = a[k] + if not leaf then + a[k] = {} + leaf = a[k] + end + a = leaf + end + local k = keys[#keys] + if a[k] ~= nil then + return false, "[Err] an object " + end + a[k] = o + return true, nil +end + +function Archive:contains_key(...) --> boolean + local a = self._archive + for _, k in ipairs{...} do + local leaf = a[k] + if leaf == nil then + return false + end + a = leaf + end + return true +end + +function Archive:get(...) --> object, err + local a = self._archive + for _, k in ipairs{...} do + local leaf = a[k] + if leaf == nil then + return nil, "[Err] key '"..k.."' not found" + end + a = leaf + end + return a, nil +end + +-- Queue Class +local VbarQueue = {_classname = "VbarQueue"} +libgeo.Vbar_queue = VbarQueue +VbarQueue.__index = VbarQueue + +function VbarQueue:new() + local o = { 0 } + setmetatable(o, self) + return o +end + +VbarQueue.__add = function (lhs, rhs) + if type(lhs) == "number" then -- dist + queue + local i = 1 + while rhs[i] do + rhs[i] = rhs[i] + lhs + i = i + 2 + end + return rhs + else -- queue + object + if type(rhs) == "number" then + lhs[#lhs] = lhs[#lhs] + rhs + return lhs + elseif type(rhs) == "table" then + if rhs._classname == "VbarQueue" then -- queue + queue + local q = {} + for _, v in ipairs(lhs) do + q[#q + 1] = v + end + local w = lhs[#lhs] + for i = 1, #rhs/2 do + q[#q + 1] = rhs[i] + w + q[#q + 1] = rhs[i + 1] + end + return q + elseif rhs._classname == "Vbar" then -- queue + vbar + local w = lhs[#lhs] + lhs[#lhs + 1] = rhs + lhs[#lhs + 1] = w + rhs._x_lim + return lhs + else + error("[Err] unsupported object type for queue operation") + end + else + error("[Err] unsupported type for queue operation") + end + end +end + +function VbarQueue:width() + return self[#self] - self[1] +end + +-- Vbar class -- a pure geometric entity of several infinite vertical lines -libgeo.Vbar = {} +libgeo.Vbar = {_classname = "Vbar"} local Vbar = libgeo.Vbar Vbar.__index = Vbar +Vbar.__add = function (lhs, rhs) + return VbarQueue:new() + lhs + rhs +end -- Vbar costructors @@ -23,6 +140,7 @@ function Vbar:from_array(yl_arr) --> assert(type(yl_arr) == "table", "'yline_array' is a mandatory arg") -- stream scanning local i = 1 + local nbars = 0 local xlim = 0.0 while yl_arr[i] do local x = yl_arr[i]; i = i + 1 @@ -30,12 +148,14 @@ function Vbar:from_array(yl_arr) --> assert(type(x) == "number", "[InternalErr] not a number") assert(type(w) == "number", "[InternalErr] not a number") xlim = x + w/2 + nbars = nbars + 1 end assert(i % 2 == 0, "[InternalErr] the index is not even") - assert(i > 0, "[InternalErr] empty array") + assert(nbars > 0, "[InternalErr] empty array") local o = { _yline = yl_arr, -- [, , ...] flat array _x_lim = xlim, -- right external bounding box coordinates + _nbars = nbars, -- number of bars } setmetatable(o, self) return o @@ -57,6 +177,7 @@ function Vbar:from_int(ngen, mod, is_bar) --> digits[#digits + 1] = d ngen = (ngen - d)/10 end + local nbars = 0 local x0 = 0.0 -- axis reference local yl = {} for k = #digits, 1, -1 do @@ -65,13 +186,16 @@ function Vbar:from_int(ngen, mod, is_bar) --> if is_bar then -- bar yl[#yl + 1] = x0 + w/2 yl[#yl + 1] = w + nbars = nbars + 1 end x0 = x0 + w is_bar = not is_bar end + assert(nbars > 0, "[InternalErr] no bars") local o = { _yline = yl, -- [, , ...] flat array _x_lim = x0, -- right external coordinate + _nbars = nbars, -- number of bars } setmetatable(o, self) return o @@ -86,7 +210,8 @@ function Vbar:from_int_revstep(ngen, mod, is_bar) --> if is_bar == nil then is_bar = true else assert(type(is_bar) == "boolean", "Invalid argument for is_bar") end - -- + -- + local nbars = 0 local x0 = 0.0 -- axis reference local i = 0 local yl = {} @@ -96,15 +221,18 @@ function Vbar:from_int_revstep(ngen, mod, is_bar) --> if is_bar then -- bar i = i + 1; yl[i] = x0 + w/2 i = i + 1; yl[i] = w + nbars = nbars + 1 end x0 = x0 + w is_bar = not is_bar ngen = (ngen - d)/10 end assert(not is_bar, "[InternalErr] the last element in not a bar") + assert(nbars > 0, "[InternalErr] no bars") local o = { _yline = yl, -- [, , ...] flat array _x_lim = x0, -- right external coordinate + _nbars = nbars, -- number of bars } setmetatable(o, self) return o @@ -126,8 +254,9 @@ function Vbar:from_int_revpair(ngen, mod, MOD, is_bar) --> else assert(type(is_bar) == "boolean", "Invalid argument for 'is_bar'") end + local nbars = 0 local yl = {} - local x0 = 0.0 + local x0 = 0.0 local k = 0 while ngen > 0 do local d = ngen % 10 -- digit @@ -140,14 +269,16 @@ function Vbar:from_int_revpair(ngen, mod, MOD, is_bar) --> if is_bar then -- bars k = k + 1; yl[k] = x0 + w/2 -- xcenter k = k + 1; yl[k] = w -- width + nbars = nbars + 1 end is_bar = not is_bar x0 = x0 + w end - assert(not is_bar, "[InternalErr] the last element is not a bar") + assert(nbars > 0, "[InternalErr] no bars") local o = { _yline = yl, -- [, , ...] flat array _x_lim = x0, -- external x coordinate + _nbars = nbars, -- number of bars } setmetatable(o, self) return o @@ -162,6 +293,7 @@ function Vbar:from_two_tab(tbar, tspace, mod, MOD) --> assert(type(mod) == "number", "Invalid argument for narrow module width") assert(type(MOD) == "number", "Invalid argument for wide module width") assert(mod < MOD, "Not ordered narrow/Wide values") + local nbars = 0 local x0 = 0.0 -- x-coordinate local yl = {} for i = 1, #tbar do @@ -176,6 +308,7 @@ function Vbar:from_two_tab(tbar, tspace, mod, MOD) --> yl[#yl + 1] = MOD -- bar width x0 = x0 + MOD end + nbars = nbars + 1 local is_narrow_space = tspace[i] assert(type(is_narrow_space) == "boolean", "[InternalErr] found a not boolean value") if is_narrow_space then @@ -184,17 +317,76 @@ function Vbar:from_two_tab(tbar, tspace, mod, MOD) --> x0 = x0 + MOD end end + assert(nbars > 0, "[InternalErr] no bars") local o = { _yline = yl, -- [, , ...] flat array _x_lim = x0, -- external x coordinate + _nbars = nbars, -- number of bars + } + setmetatable(o, self) + return o +end + +function Vbar:get_bars() --> nbars, + return self._nbars, self._yline +end + +-- Polyline class +local Polyline = {_classname = "Polyline"} +Polyline.__index = Polyline +libgeo.Polyline = Polyline + +-- optional argument a first point (x, y) +function Polyline:new(x, y) --> + local o = { + _point = {}, + _n = 0, } setmetatable(o, self) + if x ~= nil then + assert(type(x) == "number", "[Polyline:new()] Invalid type for x-coordinate") + assert(type(y) == "number", "[Polyline:new()] Invalid type for y-coordinate") + self:add_point(x, y) + end return o end +-- get a clone of points' coordinates +function Polyline:get_points() + local res = {} + local p = self._point + for i, c in ipairs(p) do + res[i] = c + end + return self._n, res +end + +-- append a new point with absolute coordinates +function Polyline:add_point(x, y) + assert(type(x) == "number", "[Polyline:add_point()] Invalid type for x-coordinate") + assert(type(y) == "number", "[Polyline:add_point()] Invalid type for y-coordinate") + local point = self._point + point[#point + 1] = x + point[#point + 1] = y + self._n = self._n + 1 +end + +-- append a new point with relative coordinates respect to the last one +function Polyline:add_relpoint(x, y) + assert(type(x) == "number", "Invalid type for x-coordinate") + assert(type(y) == "number", "Invalid type for y-coordinate") + local point = self._point + local n = self._n + assert(n > 0, "Attempt to add a relative point to an empty polyline") + local i = 2 * n + point[#point + 1] = point[i - 1] + x + point[#point + 1] = point[i] + y + self._n = n + 1 +end + -- Text class -libgeo.Text = {} +libgeo.Text = {_classname="Text"} local Text = libgeo.Text Text.__index = Text -- cgit v1.2.3