[%
    USE date(format = '%Y-%M-%DT%h%m%s');
    USE Math;
    USE String;

    aux.page = 1;

    MACRO date_format BLOCK;
        d = thedate.ymd('/') _ ' ' _ thedate.hms;
        date.format(d, format);
    END;

    MACRO date_now BLOCK;
        date.format(date.now(), format);
    END;

    MACRO svgopen BLOCK;
        aux.page = aux.page + 1;
        '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="' _ pagewidth _ 'mm" height="' _ pageheight _ 'mm" viewBox="0 0 595 842" server-process-units="' _ server_process_units _ '">';
        draw_background;
        IF open_g;
            '<g x="0" y="0">';
        END;
    END;
    
    MACRO svgclose BLOCK;
        IF close_g;
            '</g>';
        END;
        '</svg>';
    END;

    MACRO newpage BLOCK;
        svgclose(close_g=0);
        svgopen(open_g=0);
    END;

    MACRO check_pagebreak BLOCK;
        IF maxy <= (aux.lasty + following_height);
            svgclose(close_g=1);
            svgopen(open_g=1);
            aux.lasty = miny;
        END;
    END;

    MACRO zonelist BLOCK;
        fontfamily = fontfamily || 'Arial';
        fontsize = fontsize || 8;
        y = starty;
        FOR zone IN zones;
            x = startx;
            FOR f IN fields;
                anc = f.anchor || 'start';
                x = x + f.dx;
                format_field(field=f, val=zone.${f.name});
                val = aux.val;
                '<text font-family="' _ fontfamily  _ '" font-size="' _ fontsize _ apply_units('fontsize') _ '" x="' _ x _ apply_units('position') _ '" y="' _ y _ apply_units('position') _ '" text-anchor="'_ anc _'">';
                    val;
                '</text>';
            END;
            y = y + offsety;
            IF y >= maxy; 
                svgclose(close_g=1); 
                svgopen(open_g=1); 
                y = miny; 
            END;
        END;
        aux.lasty = y;
    END;
    
    MACRO apply_units(item) BLOCK;
        IF server_process_units == 'none';
            '';
        ELSIF item == 'fontsize' ;
            'pt';
        ELSIF item == 'position';
            'mm';
        END;
    END;
    MACRO calllist BLOCK;
        fontfamily = fontfamily || 'Arial';
        fontsize = fontsize || 8;
        y = starty;
        anc = anchor || 'start';
        FOR call IN calls;
            x = startx;
            FOR f IN fields;
                anc = f.anchor || 'start';
                x = x + f.dx;
                format_field(field=f, val=call.${f.name});
                val = aux.val;
                IF f.prefix.length;
                    val = f.prefix _ val;
                END;
                IF f.masklen;
                    l = f.masklen > val.length ? val.length : f.masklen;
                    val = val.substr(l - 1, val.length - 1, f.mask) | html_entity('<>&"#');
                END;
                '<text font-family="' _ fontfamily  _ '" font-size="' _ fontsize  _ apply_units('fontsize') _ '" x="' _ x _ apply_units('position') _ '" y="' _ y _ apply_units('position') _ '" text-anchor="'_ anc _'">';
                    val;
                '</text>';
            END;
            y = y + offsety;
            IF y >= maxy; 
                svgclose(close_g=1); 
                svgopen(open_g=1); 
                y = miny; 
            END;
        END;
        aux.lasty = y;
    END;

    MACRO timestamp2time BLOCK;
        t = Math.int(timestamp);

        h = Math.int(t / 3600);
        m = Math.int((t % 3600) / 60);
        s = Math.int((t % 3600) % 60);


        hs = String.new(h); hs = hs.format("%02d");
        ms = String.new(m); ms = ms.format("%02d");
        ss = String.new(s); ss = ss.format("%02d");

        aux.val = hs _ ':' _ ms _ ':' _ ss;
    END;

    MACRO money_format BLOCK;
    #Attention! usage supposes amount in CENTS!
        comma = comma || '.';
        amount = amount ? amount : 0 ;
        money_signs = money_signs || '2';
        base = 10;
        full = Math.int(amount / 100);
#        -2 because now amount is in cents, so we need to shift to signs amount, additional to current 2.
        cent = ( ( amount - full*100 ) * ( base.power( money_signs - 2) ) );
        cents = String.new(cent);
        cents = cents.format("%.0f",cents);
        cents_format_string = "%0" _ money_signs _ "d";
        cents = cents.format(cents_format_string).replace('^-', '');
        aux.val = full _ comma _ cents;
    END;

    MACRO format_field BLOCK;
        in = val; out = '';
        IF field.date_format;
            out = date.format(in, field.date_format);
        ELSIF field.timestamp2time;
            timestamp2time(timestamp=in);
            out = aux.val;
        ELSIF field.money_cents;
            money_format(amount=in, comma=field.comma, money_signs=field.money_signs);
            out = aux.val;
        ELSE;
            out = in;
        END;
        aux.val = out;
    END;

    MACRO print_money BLOCK;
        money_format(amount=amount, comma=comma);
        aux.val;
    END;

-%]
<!-- vim: set tabstop=4 syntax=html expandtab: -->
