16 drawtable(Box *b, Page *p, Image *im)
22 t = ((Itable *)b->i)->table;
23 r = rectsubpt(b->r, p->pos);
24 draw(im, r, getcolor(t->background.color), nil, ZP);
26 border(im, r, t->border, display->black, ZP);
27 for(c=t->cells; c!=nil; c=c->next){
28 cr = rectsubpt(c->lay->r, p->pos);
29 if(c->background.color != t->background.color)
30 draw(im, cr, getcolor(c->background.color), nil, ZP);
32 border(im, cr, t->border, display->black, ZP);
33 laydraw(p, im, c->lay);
51 for(c=t->cells; c!=nil; c=c->next){
52 lay = layitems(c->content, Rect(0,0,0,0), FALSE);
55 if(dimenkind(c->wspec) == Dnone){
56 lay = layitems(c->content, Rect(0,0,Tablemax,0), FALSE);
71 for(i=p->items; i!=nil; i=i->next)
72 if(i->tag == Itabletag)
73 ((Itable *)i)->table->flags |= Ttoplevel;
75 for(t=p->doc->tables; t!=nil; t=t->next)
81 cellwidth(Table *t, Tablecell *c, int sep)
87 return t->cols[c->col].width;
91 w = t->cellspacing*(n-1) + n*sep;
92 for(i=c->col; i<c->col+n; i++)
93 w += t->cols[i].width;
100 cellheight(Table *t, Tablecell *c, int sep)
106 return t->rows[c->row].height;
108 n = t->nrow - c->row;
110 h = t->cellspacing*(n-1) + n*sep;
111 for(i=c->row; i<c->row+n; i++)
112 h += t->rows[i].height;
119 getwidth(int *w, int n)
132 fixcols(Table *t, int *width, int sep, int domax)
135 int w, aw, i, d, n, rem;
138 for(c=t->cells; c!=nil; c=c->next){
144 n = t->ncol - c->col;
146 w = domax ? c->maxw : c->minw;
147 w -= t->cellspacing*(n-1) + n*sep;
150 for(i=c->col; i<c->col+n; i++)
157 for(i=c->col; i<c->col+n; i++){
170 tablewidth(Table *t, int tw, int sep)
173 int i, w, tmin, tmax, d;
177 maxw = emalloc(sizeof(int)*t->ncol);
178 minw = emalloc(sizeof(int)*t->ncol);
179 for(c=t->cells; c!=nil; c=c->next){
180 if(dimenkind(c->wspec) != Dnone){
182 c->minw = c->maxw = max(dimwidth(c->wspec, tw), c->minw);
187 maxw[c->col] = max(maxw[c->col], c->maxw);
188 minw[c->col] = max(minw[c->col], c->minw);
191 fixcols(t, maxw, sep, TRUE);
192 tmax = getwidth(maxw, t->ncol);
195 if(tw>tmax && dimenkind(t->width)!=Dnone && t->availw!=Tablemax)
196 d = (tw-tmax)/t->ncol;
197 for(i=0; i<t->ncol; i++){
198 t->cols[i].width = maxw[i] + d;
199 totw += t->cols[i].width;
202 fixcols(t, minw, sep, FALSE);
203 tmin = getwidth(minw, t->ncol);
206 for(i=0; i<t->ncol; i++){
208 t->cols[i].width = minw[i];
210 t->cols[i].width = minw[i] + (maxw[i] - minw[i])*w/d;
211 totw += t->cols[i].width;
222 fixrows(Table *t, int sep)
226 int h, ah, i, d, n, rem;
228 for(c=t->cells; c!=nil; c=c->next){
232 if(n==0 || c->row+n>t->nrow)
233 n = t->nrow - c->row;
235 lay = layitems(c->content, Rect(0,0,cellwidth(t, c, sep),0), FALSE);
236 h = max(Dy(lay->r), c->hspec);
238 h -= t->cellspacing*(n-1) + n*sep;
240 for(i=c->row; i<c->row+n; i++)
241 ah += t->rows[i].height;
247 for(i=c->row; i<c->row+n; i++){
249 d = t->rows[i].height*100/ah;
254 t->rows[i].height += d;
261 tableheight(Table *t, int sep)
267 for(i=0; i<t->nrow; i++){
269 for(c=t->rows[i].cells; c!=nil; c=c->nextinrow){
272 lay = layitems(c->content, Rect(0, 0, cellwidth(t, c, sep), 0), FALSE);
273 h = max(h, max(Dy(lay->r), c->hspec));
276 t->rows[i].height = h;
280 for(i=0; i<t->nrow; i++)
281 toth += t->rows[i].height;
287 tablesize(Table *t, int availw)
289 int w, sep, hsep, vsep;
292 sep = 2*(t->border+t->cellpadding);
293 hsep = t->cellspacing*(t->ncol+1) + 2*t->border + t->ncol*sep;
294 vsep = t->cellspacing*(t->nrow+1) + 2*t->border + t->nrow*sep;
295 w = dimwidth(t->width, availw);
298 t->totw = tablewidth(t, w, sep);
300 t->toth = tableheight(t, sep);
305 laytable(Itable *it, Rectangle r)
315 sep = (t->cellpadding+t->border) * 2;
316 r = insetrect(r, t->cellspacing+t->border);
317 for(c=t->cells; c!=nil; c=c->next){
318 w = cellwidth(t, c, sep);
319 h = cellheight(t, c, sep);
322 for(i=0; i<c->col; i++)
323 x += t->cols[i].width + sep + t->cellspacing;
326 for(i=0;i <c->row; i++)
327 y += t->rows[i].height + sep + t->cellspacing;
328 cr = Rect(x, y, x+w+sep, y+h+sep);
329 c->lay = layitems(c->content, insetrect(cr, sep/2), TRUE);