summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/barracuda/lib/barracuda.lua
blob: 33e81be24f1e2422f5b2f9e594aa8a74d1f1a50a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
-- Welcome to the 'barracuda' barcode library
--
-- Encode a message into a barcode symbol, in Lua or within a LuaTeX source file
--
-- Copyright (C) 2019 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
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

-- Basic Conventions:
-- fields that start with an undercore are private
-- class name follows the snake case naming convention
-- the 'barracuda' table is the only global object to access every package
-- modules.

local Barracuda = {
    _VERSION     = "barracuda v0.0.9",
    _NAME        = "barracuda",
    _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.libgeo"      -- basic vectorial objects
Barracuda._gacanvas = require "lib-geo.gacanvas"    -- ga stream library
Barracuda._barcode  = require "lib-barcode.barcode" -- barcode abstract class

local Barcode = Barracuda._barcode
Barcode._libgeo = Barracuda._libgeo

-- encoder builder
function Barracuda:get_barcode_class() --> Barcode class object
    return self._barcode
end

-- where we place output driver library
function Barracuda:get_driver() --> Driver object, err
    if not self._lib_driver then
        self._lib_driver = require "lib-driver.driver"
    end
    return self._lib_driver
end

function Barracuda:new_canvas() --> driver
    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
function Barracuda:save(bc_type, data, filename, id_drv)
    local barcode = self:get_barcode_class()
    local enc, err = barcode:new_encoder(bc_type)
    assert(enc, err)
    local arg_data = type(data)
    local symb
    if arg_data == "number" then
        local err_data
        symb, err_data = enc:from_uint(data)
        asser(symb, err_data)
    elseif arg_data == "string" then
        local err_data
        symb, err_data = enc:from_string(data)
        assert(symb, err_data)
    else
        error("[argErr] unsupported 'data' type")
    end
    local canvas = self:new_canvas()
    symb:append_ga(canvas)
    local driver = self:get_driver()
    id_drv = id_drv or "svg"
    local ok, out_err = driver:save(id_drv, canvas, filename)
    assert(ok, out_err)
end

-- this is a only LuaTeX method
function Barracuda:hbox(bc_type, data, box_name)
    local barcode = self:get_barcode_class()
    local enc, err = barcode:new_encoder(bc_type)
    assert(enc, err)
    local arg_data = type(data)
    local symb
    if arg_data == "number" then
        local err_data
        symb, err_data = enc:from_uint(data)
        asser(symb, err_data)
    elseif arg_data == "string" then
        local err_data
        symb, err_data = enc:from_string(data)
        assert(symb, err_data)
    else
        error("[argErr] unsupported 'data' type")
    end
    local canvas = self:new_canvas()
    symb:append_ga(canvas)
    local driver = self:get_driver()
    local ok, err_hbox = driver:ga_to_hbox(canvas, box_name)
    assert(ok, err_hbox)
end

return Barracuda