18. Layout

This section outlines a set of tools used to help assist building layouts. Layouts are created by making a region that is subregion of another region.

The operations below are designed to take in a one region and output a region. Both regions need to be pre-allocated ahead of time. If something goes wrong, these operations will return an error code of some kind.

18.1. Outline

The function btprnt_layout_outline draws box outside of the region. This can be used for debugging and visualization.

<<funcdefs>>=
void btprnt_layout_outline(btprnt_region *r, int c);
<<funcs>>=
void btprnt_layout_outline(btprnt_region *r, int c)
{
    btprnt_region o;
    o = *r;
    o.x--;
    o.y--;
    o.w += 2;
    o.h += 2;
    btprnt_draw_rect(&o, 0, 0, o.w, o.h, c);
}

18.2. Centerbox

The function btprnt_layout_centerbox configures a subregion of a particular width and height that is centered in the middle of another region. If the width or height are out of bounds, an error is returned.

<<funcdefs>>=
int btprnt_layout_centerbox(btprnt_region *r,
                            int w, int h,
                            btprnt_region *s);
<<funcs>>=
int btprnt_layout_centerbox(btprnt_region *r,
                            int w, int h,
                            btprnt_region *s)
{
    int vmargin;
    int hmargin;

    hmargin = (r->w - w) / 2;
    vmargin = (r->h - h) / 2;


    if (hmargin < 0 || vmargin < 0) {
        s->c = r->c;
        s->x = 0;
        s->y = 0;
        s->w = 0;
        s->h = 0;
        return BTPRNT_NOT_OK;
    }

    s->c = r->c;
    s->x = r->x + hmargin;
    s->y = r->y + vmargin;
    s->w = w;
    s->h = h;

    return BTPRNT_OK;
}

18.3. Border

The function btprnt_layout_border configures a subregions with equal-sized margins surrounding it.

<<funcdefs>>=
int btprnt_layout_border(btprnt_region *r,
                         int border,
                         btprnt_region *s);
<<funcs>>=
int btprnt_layout_border(btprnt_region *r,
                         int border,
                         btprnt_region *s)
{
    if (r->w < (2 * border) || r->h < (2 * border)) {
        return BTPRNT_NOT_OK;
    }

    s->c = r->c;
    s->x = r->x + border;
    s->y = r->y + border;
    s->w = r->w - 2*border;
    s->h = r->h - 2*border;

    return BTPRNT_OK;
}

18.4. Grid

The function btprnt_layout_grid returns a region that belongs to a grid of arbitrary rows and columns.

<<funcdefs>>=
int btprnt_layout_grid(btprnt_region *r,
                       int nrows,
                       int ncols,
                       int row,
                       int col,
                       btprnt_region *s);
<<funcs>>=
int btprnt_layout_grid(btprnt_region *r,
                       int nrows,
                       int ncols,
                       int row,
                       int col,
                       btprnt_region *s)
{
    int hspace;
    int vspace;
    int err;

    hspace = r->w / nrows;
    vspace = r->h / ncols;

    err = hspace <= 0 || vspace <= 0;
    err = err || row >= nrows || col >= ncols;

    if (err) {
        s->c = r->c;
        s->x = 0;
        s->y = 0;
        s->w = 0;
        s->h = 0;
        return BTPRNT_NOT_OK;
    }

    s->c = r->c;
    s->x = r->x + hspace * row;
    s->y = r->y + vspace * col;
    s->w = hspace;
    s->h = vspace;

    if (row == (nrows - 1)) s->w = r->w - (hspace * row);
    if (col == (ncols - 1)) s->h = r->h - (vspace * col);

    return BTPRNT_OK;
}

Any subregion that is in the last row or column will contain any "carry-over" that occurs from things not dividing properly.



prev | home | next