% !TeX program = LuaTeX
% test for ga-canvas pdfliteral driver
%
% Copyright (C) 2019-2022 Roberto Giacomelli
% see LICENSE.txt file

---ga--- is a binary format as an istruction set similar to
a sort of assembler language that describes simple graphic
objects like lines and rectangles.
This file contains tests aiming to check the pdfliteral
driver capability to render such ga streams---usually a Lua
array.

The pdfliteral driver directly inserts PDF vector graphic
primitives within the output and should be intented as the
"native" driver of barracuda package.

The complete reference of the ---ga--- format is available
throgh out the content of the "ga-grammar.tex" file.

Please note that all dimensions are in scaled point, the
very small \TeX{} internal unit, in fact we have that
65536sp = 1pt.

Running the source file with luatex. The typesetting engine
executes the directlua macro, so vector graphics appear in
the PDF output file.

\newbox\mybox
\directlua{
    brcd = require "barracuda"
    driver = brcd:get_driver() % global Lua variables
    pt = tex.sp [[1pt]] % 1pt = 65536sp
}

Let's start drawing an horizontal line 100pt long:
\directlua{
   local ga = {33, 0*pt, 100*pt, 0*pt}
   driver:ga_to_hbox(ga, [[mybox]])
}\box\mybox

or two different parallel lines 24pt long:
\directlua{
   local ga = {33, 0*pt, 24*pt, 0*pt, 33, 0*pt, 24*pt, 5*pt}
   driver:ga_to_hbox(ga, [[mybox]])
}\box\mybox

and again two horizontal lines 10pt thick, touching a corner:
\directlua{
   local ga = {
       1,   10*pt,
       33, -24*pt,  0*pt, -5*pt,
       33,   0*pt, 24*pt,  5*pt,
   }
   driver:ga_to_hbox(ga, [[mybox]])
}\box\mybox

Several vertical lines with its horizontal limits:
\directlua{
   local ga = {
       34, -20*pt, 20*pt,  0*pt,
       34, -15*pt, 15*pt,  5*pt,
       34, -10*pt, 10*pt, 10*pt,
       34,  -5*pt,  5*pt, 15*pt,
       34, -.5*pt, .5*pt, 20*pt,
       34,  -5*pt,  5*pt, 25*pt,
       34, -10*pt, 10*pt, 30*pt,
       34, -15*pt, 15*pt, 35*pt,
       34, -20*pt, 20*pt, 40*pt,
        1, .05*pt,
       33,   0*pt, 40*pt, -20*pt,
       33,   0*pt, 40*pt,  20*pt,
   }
   driver:ga_to_hbox(ga, [[mybox]])
}\box\mybox

Finally a little rectangle:
\directlua{
   local ga = {
       1, 5*pt,
      48, 0*pt, 0*pt, 15*pt, 10*pt,
   }
   driver:ga_to_hbox(ga, [[mybox]])
}\box\mybox

\bigskip
Test number 1: a vbar 2pt width, 20pt height:
\directlua{
    % vbar: <36> y1 y2 <nbars> x1 w1 x2 w2 ... xn wn
    local ga = {36, 0, 20*pt, 1, 0.0, 2*pt}
    driver:ga_to_hbox(ga, [[mybox]])
}\box\mybox

\bigskip
Test number 2: ten vbars in a row equally spaced by 10pt:
\directlua{
    local ga = {36, 0, 10*pt, 10,}
    for i = 0, 9 do
        ga[i*2 + 5] = 5*pt + i*20*pt
        ga[i*2 + 6] = 10*pt
    end
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 3: two series of vbars 10pt and 5pt large:
\directlua{
    local ga = {36, 0, 10*pt, 10,}
    for i = 0, 9 do
        ga[i*2 + 5] = 5*pt + i*20*pt
        ga[i*2 + 6] = 10*pt
    end
    ga[25] = 36 % vbar opcode
    ga[26] = 2.5*pt % y1
    ga[27] = 7.5*pt % y2
    ga[28] = 9      % number of bars
    for i = 0, 8 do
        ga[i*2 + 29] = 15*pt + i*20*pt
        ga[i*2 + 30] = 5*pt
    end
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 4: a bunch of thin vertical bars (25):
\directlua{
    local ga = {}
    ga[1] = 36    % vbar opcode
    ga[2] = 5*pt  % y1
    ga[3] = 25*pt % y2
    ga[4] = 25    % number of bars
    for i = 0, 24 do
        ga[i*2 + 5] = 1*pt + i*4*pt
        ga[i*2 + 6] = 2*pt
    end
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule A\box\mybox A\vrule

\bigskip
Test number 5: two rows of a bunch of thin bars:
\directlua{
    local ga = {}
    ga[1] = 36    % vbar opcode
    ga[2] = 5*pt  % y1
    ga[3] = 25*pt % y2
    ga[4] = 25    % number of bars
    for i = 0, 24 do
        ga[i*2 + 5] = 1*pt + i*4*pt
        ga[i*2 + 6] = 2*pt
    end
    ga[55] = 36    % vbar opcode
    ga[56] = 25*pt % y1
    ga[57] = 45*pt % y2
    ga[58] = 24    % number of bars
    for i = 0, 23 do
        ga[i*2 + 59] = 3*pt + i*4*pt
        ga[i*2 + 60] = 2*pt
    end
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 6: staircase of Vbars (manually data definition):
\directlua{
    local ga = {
        36, % vbar opcode
      0*pt, % y1
     20*pt, % y2
         1, % number of bars
      5*pt, % x
     10*pt, % w
        36, % vbar opcode
     20*pt, % y1
     40*pt, % y2
         1, % number of bars
     15*pt, % x
     10*pt, % w
        36, % vbar opcode
     40*pt, % y1
     60*pt, % y2
         1, % number of bars
     25*pt, % x
     10*pt, % w
    }
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 7: vbars with spaced text, in three different rows:
\directlua{
    local ga = {}
    ga[1] =    36 % vbar opcode
    ga[2] =  0*pt % y1
    ga[3] = 20*pt % y2
    ga[4] =     8 % number of bars
    for i = 1,8 do
        ga[3 + i*2] = i * 2 * 5*pt % x coordinate of bar axis
        ga[4 + i*2] = 5*pt % bar width
    end
    ga[21] =    36 % vbar opcode
    ga[22] = 30*pt % y1
    ga[23] = 50*pt % y2
    ga[24] =     8 % number of bars
    for i = 1,8 do
        ga[23 + i*2] = i * 2 * 5*pt % x coordinate of bar axis
        ga[24 + i*2] = 5*pt % bar width
    end
    % 131 <x1: FLOAT> <xgap: FLOAT> <ay: DIM> <ypos: DIM> <c: CHARS>
    ga[41] = 131   % opcode text_xspaced
    ga[42] = 10*pt % x coordinate of the first glyph axis
    ga[43] = 10*pt % x gap among glyphs
    ga[44] = 0.5   % half height
    ga[45] = 25*pt % y coordinate of glyps
    ga[46] = 65 % A
    ga[47] = 66 % B
    ga[48] = 67 % C
    ga[49] = 68 % D
    ga[50] = 69 % E
    ga[51] = 70 % F
    ga[52] = 71 % G
    ga[53] = 72 % H
    ga[54] = 0
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 8: spaced text (checking the correct vertical alignment):
\directlua{
    local ga = {}
    ga[1] =    36 % vbar opcode
    ga[2] =  0*pt % y1
    ga[3] = 20*pt % y2
    ga[4] =     8 % number of bars
    for i = 1,8 do
        ga[3 + i*2] = i * 2 * 5*pt % x coordinate of bar axis
        ga[4 + i*2] = 1*pt % bar width
    end
    ga[21] =    36 % vbar opcode
    ga[22] = 40*pt % y1
    ga[23] = 60*pt % y2
    ga[24] =     8 % number of bars
    for i = 1,8 do
        ga[23 + i*2] = i * 2 * 5*pt % x coordinate of bar axis
        ga[24 + i*2] = 1*pt % bar width
    end
    % 131 <x1: FLOAT> <xgap: FLOAT> <ay: DIM> <ypos: DIM> <c: CHARS>
    ga[41] = 131   % opcode text_xspaced
    ga[42] = 10*pt % x coordinate of the first glyph axis
    ga[43] = 10*pt % x gap among glyphs
    ga[44] = 0.0   % half height
    ga[45] = 30*pt % y coordinate of glyps
    ga[46] = 65 % A
    ga[47] = 66 % B
    ga[48] = 67 % C
    ga[49] = string.byte("Q")
    ga[50] = 69 % E
    ga[51] = 70 % F
    ga[52] = 71 % G
    ga[53] = 72 % H
    ga[54] = 0
    ga[55] = 131   % opcode text_xspaced
    ga[56] = 10*pt % x coordinate of the first glyph axis
    ga[57] = 10*pt % x gap among glyphs
    ga[58] = 1.0   % half height
    ga[59] = 30*pt % y coordinate of glyps
    ga[60] = 49 % 1
    ga[61] = 50 % 2
    ga[62] = 51 % 3
    ga[63] = 52 % 4
    ga[64] = 53 % 5
    ga[65] = 54 % 6
    ga[66] = 55 % 7
    ga[67] = 56 % 8
    ga[68] = 0
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 9: spaced text, check correct vertical alignment:
\directlua{
    local ga = {}
    ga[1] =    36 % vbar opcode
    ga[2] =  0*pt % y1
    ga[3] = 20*pt % y2
    ga[4] =     8 % number of bars
    for i = 1,8 do
        ga[3 + i*2] = i * 2 * 5*pt % x coordinate of bar axis
        ga[4 + i*2] = 8*pt % bar width
    end
    ga[21] =    36 % vbar opcode
    ga[22] = 40*pt % y1
    ga[23] = 60*pt % y2
    ga[24] =     8 % number of bars
    for i = 1,8 do
        ga[23 + i*2] = i * 2 * 5*pt % x coordinate of bar axis
        ga[24 + i*2] = 8*pt % bar width
    end
    % 131 <x1: FLOAT> <xgap: FLOAT> <ay: DIM> <ypos: DIM> <c: CHARS>
    ga[41] = 131   % opcode text_xspaced
    ga[42] = 10*pt % x coordinate of the first glyph axis
    ga[43] = 10*pt % x gap among glyphs
    ga[44] = 0.0   % half height
    ga[45] = 20*pt % y coordinate of glyps
    ga[46] = 65 % A
    ga[47] = 66 % B
    ga[48] = 67 % C
    ga[49] = 68 % D
    ga[50] = 69 % E
    ga[51] = 70 % F
    ga[52] = 71 % G
    ga[53] = 72 % H
    ga[54] = 0
    ga[55] = 131   % opcode text_xspaced
    ga[56] = 10*pt % x coordinate of the first glyph axis
    ga[57] = 10*pt % x gap among glyphs
    ga[58] = 1.0   % half height
    ga[59] = 40*pt % y coordinate of glyps
    ga[60] = 49 % 1
    ga[61] = 50 % 2
    ga[62] = 51 % 3
    ga[63] = 52 % 4
    ga[64] = 53 % 5
    ga[65] = 54 % 6
    ga[66] = 55 % 7
    ga[67] = 56 % 8
    ga[68] = 0
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 10: two centered texts aligned to the baseline:
\directlua{
    local ga = {}
    % 130 <ax: FLOAT> <ay: FLOAT> <xpos: DIM> <ypos: DIM> <c: CHARS>
    ga[ 1] = 130 % opcode text
    ga[ 2] = 0.5 % ax relative x coordinate
    ga[ 3] = 1.0 % ay relative y coordinate
    ga[ 4] = 0.0 % x position
    ga[ 5] = 0.0 % y position
    ga[ 6] =  65 % A
    ga[ 7] =  string.byte("Q") % Q depth glyph
    ga[ 8] =  67 % C
    ga[ 9] =   0
    ga[10] = 130 % opcode text
    ga[11] = 0.5 % ax
    ga[12] = 0.0 % ay
    ga[13] = 0.0 % x
    ga[14] = 0.0 % y
    ga[15] =  48 % 0
    ga[16] =  49 % 1
    ga[17] =  50 % 2
    ga[18] =  51 % 3
    ga[19] =  52 % 4
    ga[20] =  53 % 5
    ga[21] =  54 % 6
    ga[22] =  55 % 7
    ga[23] =  56 % 8
    ga[24] =  57 % 9
    ga[25] =   0
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
So far, we have manually build data for ga stream. For now on, we are going to
use the ga-canvas library.

\bigskip
In fact, all the previous tests are rebuild with the ga-canvas library.

Test 1: a vbar 2pt width, 20pt height:
\directlua{
    local ga = brcd:new_canvas()
    local vbar = {0.0, 2*pt}
    assert(ga:encode_vbar(vbar, 0.0, 0.0, 20*pt)) % x, w
    driver:ga_to_hbox(ga, [[mybox]])
}\box\mybox

\bigskip
Test 2: ten vbars equally spaced by 10pt:
\directlua{
    local ga = brcd:new_canvas()
    local bars = {}
    for i = 0, 9 do
        bars[i*2 + 1] =  5*pt + i*20*pt % x
        bars[i*2 + 2] = 10*pt           % w
    end
    assert(ga:encode_vbar(bars, 0.0, 0.0, 10*pt))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test 3: two series of vbars 10pt and 5pt large:
\directlua{
    local b1 = {}
    for i = 0, 9 do
        b1[i*2 + 1] = i*20*pt
        b1[i*2 + 2] = 10*pt
    end
    local b2 = {}
    for i = 0, 8 do
        b2[i*2 + 1] = i*20*pt
        b2[i*2 + 2] = 5*pt
    end
    local ga = brcd:new_canvas()
    assert(ga:encode_vbar(b1, 0.0, 0.0, 10*pt))
    assert(ga:encode_vbar(b2, 10.0*pt, 2.5*pt, 7.5*pt))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test 4: a bunch of thin bars:
\directlua{
    local b = {}
    for i = 0, 24 do
        b[i*2 + 1] = 1*pt + i*4*pt
        b[i*2 + 2] = 2*pt
    end
    local ga = brcd:new_canvas()
    assert(ga:encode_vbar(b, 0.0, 5*pt, 25*pt))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule{ }\box\mybox{ }\vrule

\bigskip
Test 5: two levels of a bunch of thin bars:
\directlua{
    local b = {}
    for i = 0, 24 do
        b[i*2 + 1] = i*4*pt
        b[i*2 + 2] = 2*pt
    end
    local ga = brcd:new_canvas()
    assert(ga:encode_vbar(b, 0.0, 5*pt, 25*pt))
    assert(ga:encode_vbar(b, 2*pt, 25*pt, 45*pt, b))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 6: staircase of bars (manual insertion of data):
\directlua{
    local b = {0.0, 10*pt}
    local ga = brcd:new_canvas()
    assert(ga:encode_vbar(b, 0.0, 0.0, 20*pt))
    assert(ga:encode_vbar(b, 10*pt, 20*pt, 40*pt))
    assert(ga:encode_vbar(b, 20*pt, 40*pt, 60*pt))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 7: vbars with spaced text, all in three rows:
\directlua{
    local vb = {}
    for i = 0,7 do
        vb[i*2+1] = i*10*pt
        vb[i*2+2] = 5*pt
    end
    local ga = brcd:new_canvas()
    assert(ga:encode_vbar(vb, 0.0, 0.0, 20*pt))
    assert(ga:encode_vbar(vb, 0.0, 30*pt, 50*pt))
    local txt = {codepoint = {
        65, % A
        66, % B
        67, % C
        68, % D
        69, % E
        70, % F
        71, % G
        72, % H
    }}
    assert(ga:encode_Text_xspaced(txt, 0.0, 10*pt, 25*pt, 0.5))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test 8: spaced text, check correct vertical alignment:
\directlua{
    local b = {}
    for i = 0,7 do
        b[i*2+1] = i*10*pt
        b[i*2+2] = 2*pt
    end
    local ga = brcd:new_canvas()
    assert(ga:encode_vbar(b, 0.0, 0.0, 20*pt))
    assert(ga:encode_vbar(b, 0.0, 40*pt, 60*pt))
    local c = { codepoint = {
        65, % A
        66, % B
        67, % C
        string.byte("Q"),
        69, % E
        70, % F
        71, % G
        72, % H
    }}
    assert(ga:encode_Text_xspaced(c, 0.0, 10*pt, 30*pt, 0.0))
    local n = { codepoint = {
        49, % 1
        50, % 2
        51, % 3
        52, % 4
        53, % 5
        54, % 6
        55, % 7
        56, % 8
    }}
    assert(ga:encode_Text_xspaced(n, 0.0, 10*pt, 30*pt, 1.0))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 9: spaced text, check correct vertical alignment:
\directlua{
    local b = {}
    for i = 0,7 do
        b[i*2+1] = i*10*pt
        b[i*2+2] = 8*pt
    end
    local ga = brcd:new_canvas()
    assert(ga:encode_vbar(b, 0.0, 0.0, 20*pt))
    assert(ga:encode_vbar(b, 0.0, 40*pt, 60*pt))
    local c = { codepoint = {
        65, % A
        66, % B
        67, % C
        string.byte("Q"),
        69, % E
        70, % F
        71, % G
        72, % H
    }}
    assert(ga:encode_Text_xspaced(c, 0.0, 10*pt, 20*pt, 0.0))
    local n = { codepoint = {
        49, % 1
        50, % 2
        51, % 3
        52, % 4
        53, % 5
        54, % 6
        55, % 7
        56, % 8
    }}
    assert(ga:encode_Text_xspaced(n, 0.0, 10*pt, 40*pt, 1.0))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule{ }\box\mybox{ }\vrule

\bigskip
Test number 10: two centered texts and baseline aligned:
\directlua{
    local n = { codepoint = {
        48, % 0
        49, % 1
        50, % 2
        51, % 3
        52, % 4
        53, % 5
        54, % 6
        55, % 7
        56, % 8
        57, % 9
    }}
    local ga = brcd:new_canvas()
    assert(ga:encode_Text(n, 0, 0, 0.5, 0))
    local a = { codepoint = {
        65, % A
        string.byte("Q"), % Q
        67, % C
    }}
    assert(ga:encode_Text(a, 0, 0, 0.5, 1))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 11: two centered texts aligned:
\directlua{
    local n = { codepoint = {
        48, % 0
        49, % 1
        50, % 2
        51, % 3
        52, % 4
        53, % 5
        54, % 6
        55, % 7
        56, % 8
        57, % 9
    }}
    local ga = brcd:new_canvas()
    assert(ga:encode_Text(n, 0, 0, 0.5, 1))
    local a = { codepoint = {
        65, % A
        string.byte("Q"), % Q
        67, % C
    }}
    assert(ga:encode_Text(a, 0, 0, 0.5, 0))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 12: text\_xwidth opcode:

\directlua{
    local n = { codepoint = {
        48, % 0
        49, % 1
        50, % 2
        51, % 3
        52, % 4
        53, % 5
        54, % 6
        55, % 7
        56, % 8
        57, % 9
    }}
    local ga = brcd:new_canvas()
    assert(ga:encode_Text_xwidth(n, 0, tex.sp "5cm", 0, 0))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\vrule\hbox to 5cm{0 \hfill9}\vrule

\bigskip
Test number 13: text\_xwidth with different size:
\smallskip

\directlua{
    local n = {codepoint = {
        48, % 0
        49, % 1
        50, % 2
        51, % 3
        52, % 4
        53, % 5
        54, % 6
        55, % 7
        56, % 8
        57, % 9
    }}
    local ga = brcd:new_canvas()
    %                           (txt, x1, x2, ypos, ay) --> ok, err
    assert(ga:encode_Text_xwidth(n, tex.sp "0mm", tex.sp "50mm", 0, 0))
    assert(ga:encode_Text_xwidth(n, tex.sp "5mm", tex.sp "45mm", tex.sp "3mm", 0))
    assert(ga:encode_Text_xwidth(n, tex.sp "10mm", tex.sp "40mm", tex.sp "6mm", 0))
    assert(ga:encode_Text_xwidth(n, tex.sp "15mm", tex.sp "35mm", tex.sp "9mm", 0))
    assert(ga:encode_Text_xwidth(n, tex.sp "20mm", tex.sp "30mm", tex.sp "12mm", 0))
    assert(ga:encode_Text_xwidth(n, tex.sp "24mm", tex.sp "26mm", tex.sp "15mm", 0))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 14: place bars and text as text\_xwidth:
\smallskip

\directlua{
    local b = {}
    for i = 0, 9 do
        b[i*2 + 1] = i*12*pt
        b[i*2 + 2] = 0.4*pt
    end
    local n = {codepoint = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,}}
    local ga = brcd:new_canvas()
    %                    (vbar, x0, y1, y2) --> ok, err
    assert(ga:encode_vbar(b, 0.0,  0*pt, 25*pt))
    %                    (txt, x1, x2, ypos, ay) --> ok, err
    assert(ga:encode_Text_xwidth(n, 0*pt, 108*pt, 25*pt, 0))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\smallskip
\directlua{
    local b = {}
    for i = 0, 8 do
        b[i*2 + 1] = i*12*pt
        b[i*2 + 2] = 0.2*pt
    end
    local n = { 48, 49, 50, 51, string.byte([[x]]), 53, 54, 55, 56,}
    n = {codepoint = n}
    local ga = brcd:new_canvas()
    %                    x0,    y1,    y2, bars
    assert(ga:encode_vbar(b, 0.0,  0*pt, 25*pt))
    assert(ga:encode_vbar(b, 0.0, 32*pt, 42*pt))
    %                (txt, x1, x2, ypos, ay) --> ok, err
    assert(ga:encode_Text_xwidth(n, 0*pt, 96*pt, 25*pt, 0))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bigskip
Test number 15: place text\_xwidth when text is only two chars long:

\directlua{
    local n = {codepoint = { 48, 56,}} % 0 and 8
    local ga = brcd:new_canvas()
    %                   (txt, x1, x2, ypos, ay) --> ok, err
    assert(ga:encode_Text_xwidth(n, 0*pt, 32*pt, 25*pt, 0))
    driver:ga_to_hbox(ga, [[mybox]])
}\vrule\box\mybox\vrule

\bye
