[BACK]Return to tbl_term.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Diff for /mandoc/tbl_term.c between version 1.10 and 1.41

version 1.10, 2011/01/04 15:02:00 version 1.41, 2015/09/26 00:54:04
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>   * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
    * Copyright (c) 2011, 2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above   * purpose with or without fee is hereby granted, provided that the above
Line 14 
Line 15 
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */   */
 #ifdef HAVE_CONFIG_H  
 #include "config.h"  #include "config.h"
 #endif  
   
   #include <sys/types.h>
   
 #include <assert.h>  #include <assert.h>
 #include <stdio.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
Line 27 
Line 28 
 #include "out.h"  #include "out.h"
 #include "term.h"  #include "term.h"
   
 /* FIXME: `n' modifier doesn't always do the right thing. */  static  size_t  term_tbl_len(size_t, void *);
 /* FIXME: `n' modifier doesn't use the cell-spacing buffer. */  static  size_t  term_tbl_strlen(const char *, void *);
   static  void    tbl_char(struct termp *, char, size_t);
   static  void    tbl_data(struct termp *, const struct tbl_opts *,
                           const struct tbl_dat *,
                           const struct roffcol *);
   static  void    tbl_literal(struct termp *, const struct tbl_dat *,
                           const struct roffcol *);
   static  void    tbl_number(struct termp *, const struct tbl_opts *,
                           const struct tbl_dat *,
                           const struct roffcol *);
   static  void    tbl_hrule(struct termp *, const struct tbl_span *, int);
   static  void    tbl_word(struct termp *, const struct tbl_dat *);
   
 static  inline void      tbl_char(struct termp *, char, int);  
 static  void             tbl_hframe(struct termp *,  
                                 const struct tbl_span *);  
 static  void             tbl_data_number(struct termp *,  
                                 const struct tbl *,  
                                 const struct tbl_dat *,  
                                 const struct termp_tbl *);  
 static  void             tbl_data_literal(struct termp *,  
                                 const struct tbl_dat *,  
                                 const struct termp_tbl *);  
 static  void             tbl_data(struct termp *, const struct tbl *,  
                                 const struct tbl_dat *,  
                                 const struct termp_tbl *);  
 static  void             tbl_spanner(struct termp *,  
                                 const struct tbl_head *);  
 static  void             tbl_hrule(struct termp *,  
                                 const struct tbl_span *);  
 static  void             tbl_vframe(struct termp *,  
                                 const struct tbl *);  
 static  void             tbl_calc(struct termp *,  
                                 const struct tbl_span *);  
 static  void             tbl_calc_data(struct termp *,  
                                 const struct tbl *,  
                                 const struct tbl_dat *,  
                                 struct termp_tbl *);  
 static  void             tbl_calc_data_literal(struct termp *,  
                                 const struct tbl_dat *,  
                                 struct termp_tbl *);  
 static  void             tbl_calc_data_number(struct termp *,  
                                 const struct tbl *,  
                                 const struct tbl_dat *,  
                                 struct termp_tbl *);  
   
   static size_t
   term_tbl_strlen(const char *p, void *arg)
   {
   
           return(term_strlen((const struct termp *)arg, p));
   }
   
   static size_t
   term_tbl_len(size_t sz, void *arg)
   {
   
           return(term_len((const struct termp *)arg, sz));
   }
   
 void  void
 term_tbl(struct termp *tp, const struct tbl_span *sp)  term_tbl(struct termp *tp, const struct tbl_span *sp)
 {  {
         const struct tbl_head *hp;          const struct tbl_cell   *cp;
         const struct tbl_dat *dp;          const struct tbl_dat    *dp;
           static size_t            offset;
           size_t                   rmargin, maxrmargin, tsz;
           int                      ic, horiz, spans, vert;
   
           rmargin = tp->rmargin;
           maxrmargin = tp->maxrmargin;
   
           tp->rmargin = tp->maxrmargin = TERM_MAXMARGIN;
   
         /* Inhibit printing of spaces: we do padding ourselves. */          /* Inhibit printing of spaces: we do padding ourselves. */
   
         tp->flags |= TERMP_NONOSPACE;          tp->flags |= TERMP_NONOSPACE;
         tp->flags |= TERMP_NOSPACE;          tp->flags |= TERMP_NOSPACE;
   
         /*          /*
          * The first time we're invoked for a given table block, create           * The first time we're invoked for a given table block,
          * the termp_tbl structure.  This contains the column           * calculate the table widths and decimal positions.
          * configuration for the entire table, e.g., table-wide column  
          * width, decimal point, etc.  
          */           */
   
         if (TBL_SPAN_FIRST & sp->flags) {          if (tp->tbl.cols == NULL) {
                 assert(NULL == tp->tbl);                  tp->tbl.len = term_tbl_len;
                 tp->tbl = calloc                  tp->tbl.slen = term_tbl_strlen;
                         (sp->tbl->cols, sizeof(struct termp_tbl));                  tp->tbl.arg = tp;
                 if (NULL == tp->tbl) {  
                         perror(NULL);  
                         exit(EXIT_FAILURE);  
                 }  
                 tbl_calc(tp, sp);  
   
                 /* Flush out any preceding data. */                  tblcalc(&tp->tbl, sp, rmargin - tp->offset);
                 term_flushln(tp);  
         }  
   
         /* Horizontal frame at the start of boxed tables. */                  /* Center the table as a whole. */
   
         if (TBL_SPAN_FIRST & sp->flags)                  offset = tp->offset;
                 tbl_hframe(tp, sp);                  if (sp->opts->opts & TBL_OPT_CENTRE) {
                           tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
                               ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
                           for (ic = 0; ic < sp->opts->cols; ic++)
                                   tsz += tp->tbl.cols[ic].width + 3;
                           tsz -= 3;
                           if (offset + tsz > rmargin)
                                   tsz -= 1;
                           tp->offset = (offset + rmargin > tsz) ?
                               (offset + rmargin - tsz) / 2 : 0;
                   }
   
                   /* Horizontal frame at the start of boxed tables. */
   
                   if (sp->opts->opts & TBL_OPT_DBOX)
                           tbl_hrule(tp, sp, 2);
                   if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
                           tbl_hrule(tp, sp, 1);
           }
   
         /* Vertical frame at the start of each row. */          /* Vertical frame at the start of each row. */
   
         tbl_vframe(tp, sp->tbl);          horiz = sp->pos == TBL_SPAN_HORIZ || sp->pos == TBL_SPAN_DHORIZ;
   
           if (sp->layout->vert ||
               (sp->prev != NULL && sp->prev->layout->vert) ||
               sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX))
                   term_word(tp, horiz ? "+" : "|");
           else if (sp->opts->lvert)
                   tbl_char(tp, horiz ? '-' : ASCII_NBRSP, 1);
   
         /*          /*
          * Now print the actual data itself depending on the span type.           * Now print the actual data itself depending on the span type.
          * Spanner spans get a horizontal rule; data spanners have their           * Match data cells to column numbers.
          * data printed by matching data to header.  
          */           */
   
         switch (sp->pos) {          if (sp->pos == TBL_SPAN_DATA) {
         case (TBL_SPAN_HORIZ):                  cp = sp->layout->first;
                 /* FALLTHROUGH */  
         case (TBL_SPAN_DHORIZ):  
                 tbl_hrule(tp, sp);  
                 break;  
         case (TBL_SPAN_DATA):  
                 /* Iterate over template headers. */  
                 dp = sp->first;                  dp = sp->first;
                 for (hp = sp->head; hp; hp = hp->next) {                  spans = 0;
                         switch (hp->pos) {                  for (ic = 0; ic < sp->opts->cols; ic++) {
                         case (TBL_HEAD_VERT):  
                                 /* FALLTHROUGH */                          /*
                         case (TBL_HEAD_DVERT):                           * Remeber whether we need a vertical bar
                                 tbl_spanner(tp, hp);                           * after this cell.
                            */
   
                           vert = cp == NULL ? 0 : cp->vert;
   
                           /*
                            * Print the data and advance to the next cell.
                            */
   
                           if (spans == 0) {
                                   tbl_data(tp, sp->opts, dp, tp->tbl.cols + ic);
                                   if (dp != NULL) {
                                           spans = dp->spans;
                                           dp = dp->next;
                                   }
                           } else
                                   spans--;
                           if (cp != NULL)
                                   cp = cp->next;
   
                           /*
                            * Separate columns, except in the middle
                            * of spans and after the last cell.
                            */
   
                           if (ic + 1 == sp->opts->cols || spans)
                                 continue;                                  continue;
                         case (TBL_HEAD_DATA):  
                                 break;  
                         }  
                         tbl_data(tp, sp->tbl, dp,  
                                 &tp->tbl[hp->ident]);  
   
                         /* Go to the next data cell. */                          tbl_char(tp, ASCII_NBRSP, 1);
                         if (dp)                          if (vert > 0)
                                 dp = dp->next;                                  tbl_char(tp, '|', vert);
                           if (vert < 2)
                                   tbl_char(tp, ASCII_NBRSP, 2 - vert);
                 }                  }
                 break;          } else if (horiz)
         }                  tbl_hrule(tp, sp, 0);
   
         tbl_vframe(tp, sp->tbl);          /* Vertical frame at the end of each row. */
   
           if (sp->layout->last->vert ||
               (sp->prev != NULL && sp->prev->layout->last->vert) ||
               (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)))
                   term_word(tp, horiz ? "+" : " |");
           else if (sp->opts->rvert)
                   tbl_char(tp, horiz ? '-' : ASCII_NBRSP, 1);
         term_flushln(tp);          term_flushln(tp);
   
         /*          /*
Line 147  term_tbl(struct termp *tp, const struct tbl_span *sp)
Line 187  term_tbl(struct termp *tp, const struct tbl_span *sp)
          * existing table configuration and set it to NULL.           * existing table configuration and set it to NULL.
          */           */
   
         if (TBL_SPAN_LAST & sp->flags) {          if (sp->next == NULL) {
                 tbl_hframe(tp, sp);                  if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
                 assert(tp->tbl);                          tbl_hrule(tp, sp, 1);
                 free(tp->tbl);                          tp->skipvsp = 1;
                 tp->tbl = NULL;                  }
                   if (sp->opts->opts & TBL_OPT_DBOX) {
                           tbl_hrule(tp, sp, 2);
                           tp->skipvsp = 2;
                   }
                   assert(tp->tbl.cols);
                   free(tp->tbl.cols);
                   tp->tbl.cols = NULL;
                   tp->offset = offset;
         }          }
   
         tp->flags &= ~TERMP_NONOSPACE;          tp->flags &= ~TERMP_NONOSPACE;
           tp->rmargin = rmargin;
           tp->maxrmargin = maxrmargin;
 }  }
   
   /*
    * Kinds of horizontal rulers:
    * 0: inside the table (single or double line with crossings)
    * 1: inner frame (single line with crossings and ends)
    * 2: outer frame (single line without crossings with ends)
    */
 static void  static void
 tbl_hrule(struct termp *tp, const struct tbl_span *sp)  tbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
 {  {
         const struct tbl_head *hp;          const struct tbl_cell *c1, *c2;
         char             c;          int      vert;
         int              width;          char     line, cross;
   
         /*          line = (kind == 0 && TBL_SPAN_DHORIZ == sp->pos) ? '=' : '-';
          * An hrule extends across the entire table and is demarked by a          cross = (kind < 2) ? '+' : '-';
          * standalone `_' or whatnot in lieu of a table row.  Spanning  
          * headers are marked by a `+', as are table boundaries.  
          */  
   
         c = '-';          if (kind)
         if (TBL_SPAN_DHORIZ == sp->pos)                  term_word(tp, "+");
                 c = '=';          c1 = sp->layout->first;
           c2 = sp->prev == NULL ? NULL : sp->prev->layout->first;
         /* FIXME: don't use `+' between data and a spanner! */          if (c2 == c1)
                   c2 = NULL;
         for (hp = sp->head; hp; hp = hp->next) {          for (;;) {
                 width = tp->tbl[hp->ident].width;                  tbl_char(tp, line, tp->tbl.cols[c1->col].width + 1);
                 switch (hp->pos) {                  vert = c1->vert;
                 case (TBL_HEAD_DATA):                  if ((c1 = c1->next) == NULL)
                         tbl_char(tp, c, width);                           break;
                         break;                  if (c2 != NULL) {
                 case (TBL_HEAD_DVERT):                          if (vert < c2->vert)
                         tbl_char(tp, '+', width);                                  vert = c2->vert;
                         /* FALLTHROUGH */                          c2 = c2->next;
                 case (TBL_HEAD_VERT):  
                         tbl_char(tp, '+', width);  
                         break;  
                 default:  
                         abort();  
                         /* NOTREACHED */  
                 }                  }
                   if (vert)
                           tbl_char(tp, cross, vert);
                   if (vert < 2)
                           tbl_char(tp, line, 2 - vert);
         }          }
 }          if (kind) {
   
 static void  
 tbl_hframe(struct termp *tp, const struct tbl_span *sp)  
 {  
         const struct tbl_head *hp;  
         int              width;  
   
         if ( ! (TBL_OPT_BOX & sp->tbl->opts ||  
                         TBL_OPT_DBOX & sp->tbl->opts))  
                 return;  
   
         /*  
          * Print out the horizontal part of a frame or double frame.  A  
          * double frame has an unbroken `-' outer line the width of the  
          * table, bordered by `+'.  The frame (or inner frame, in the  
          * case of the double frame) is a `-' bordered by `+' and broken  
          * by `+' whenever a span is encountered.  
          */  
   
         if (TBL_OPT_DBOX & sp->tbl->opts) {  
                 term_word(tp, "+");                  term_word(tp, "+");
                 for (hp = sp->head; hp; hp = hp->next) {  
                         width = tp->tbl[hp->ident].width;  
                         tbl_char(tp, '-', width);  
                 }  
                 term_word(tp, "+");  
                 term_flushln(tp);                  term_flushln(tp);
         }          }
   
         term_word(tp, "+");  
         for (hp = sp->head; hp; hp = hp->next) {  
                 width = tp->tbl[hp->ident].width;  
                 switch (hp->pos) {  
                 case (TBL_HEAD_DATA):  
                         tbl_char(tp, '-', width);  
                         break;  
                 default:  
                         tbl_char(tp, '+', width);  
                         break;  
                 }  
         }  
         term_word(tp, "+");  
         term_flushln(tp);  
 }  }
   
 static void  static void
 tbl_data(struct termp *tp, const struct tbl *tbl,  tbl_data(struct termp *tp, const struct tbl_opts *opts,
                 const struct tbl_dat *dp,          const struct tbl_dat *dp,
                 const struct termp_tbl *tbp)          const struct roffcol *col)
 {  {
         enum tbl_cellt   pos;  
   
         if (NULL == dp) {          if (dp == NULL) {
                 tbl_char(tp, ASCII_NBRSP, tbp->width);                  tbl_char(tp, ASCII_NBRSP, col->width);
                 return;                  return;
         }          }
   
         switch (dp->pos) {          switch (dp->pos) {
         case (TBL_DATA_NONE):          case TBL_DATA_NONE:
                 tbl_char(tp, ASCII_NBRSP, tbp->width);                  tbl_char(tp, ASCII_NBRSP, col->width);
                 return;                  return;
         case (TBL_DATA_HORIZ):          case TBL_DATA_HORIZ:
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         case (TBL_DATA_NHORIZ):          case TBL_DATA_NHORIZ:
                 tbl_char(tp, '-', tbp->width);                  tbl_char(tp, '-', col->width);
                 return;                  return;
         case (TBL_DATA_NDHORIZ):          case TBL_DATA_NDHORIZ:
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         case (TBL_DATA_DHORIZ):          case TBL_DATA_DHORIZ:
                 tbl_char(tp, '=', tbp->width);                  tbl_char(tp, '=', col->width);
                 return;                  return;
         default:          default:
                 break;                  break;
         }          }
   
         pos = dp->layout ? dp->layout->pos : TBL_CELL_LEFT;  
   
         switch (pos) {          switch (dp->layout->pos) {
         case (TBL_CELL_HORIZ):          case TBL_CELL_HORIZ:
                 tbl_char(tp, '-', tbp->width);                  tbl_char(tp, '-', col->width);
                 break;                  break;
         case (TBL_CELL_DHORIZ):          case TBL_CELL_DHORIZ:
                 tbl_char(tp, '=', tbp->width);                  tbl_char(tp, '=', col->width);
                 break;                  break;
         case (TBL_CELL_LONG):          case TBL_CELL_LONG:
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         case (TBL_CELL_CENTRE):          case TBL_CELL_CENTRE:
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         case (TBL_CELL_LEFT):          case TBL_CELL_LEFT:
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         case (TBL_CELL_RIGHT):          case TBL_CELL_RIGHT:
                 tbl_data_literal(tp, dp, tbp);                  tbl_literal(tp, dp, col);
                 break;                  break;
         case (TBL_CELL_NUMBER):          case TBL_CELL_NUMBER:
                 tbl_data_number(tp, tbl, dp, tbp);                  tbl_number(tp, opts, dp, col);
                 break;                  break;
           case TBL_CELL_DOWN:
                   tbl_char(tp, ASCII_NBRSP, col->width);
                   break;
         default:          default:
                 abort();                  abort();
                 /* NOTREACHED */  
         }          }
 }  }
 static void  
 tbl_spanner(struct termp *tp, const struct tbl_head *hp)  
 {  
   
         switch (hp->pos) {  
         case (TBL_HEAD_VERT):  
                 term_word(tp, "|");  
                 break;  
         case (TBL_HEAD_DVERT):  
                 term_word(tp, "||");  
                 break;  
         default:  
                 break;  
         }  
 }  
   
 static void  static void
 tbl_vframe(struct termp *tp, const struct tbl *tbl)  tbl_char(struct termp *tp, char c, size_t len)
 {  {
         /* Always just a single vertical line. */          size_t          i, sz;
   
         if (TBL_OPT_BOX & tbl->opts || TBL_OPT_DBOX & tbl->opts)  
                 term_word(tp, "|");  
 }  
   
 static inline void  
 tbl_char(struct termp *tp, char c, int len)  
 {  
         int             i, sz;  
         char            cp[2];          char            cp[2];
   
         cp[0] = c;          cp[0] = c;
Line 337  tbl_char(struct termp *tp, char c, int len)
Line 322  tbl_char(struct termp *tp, char c, int len)
 }  }
   
 static void  static void
 tbl_data_literal(struct termp *tp,  tbl_literal(struct termp *tp, const struct tbl_dat *dp,
                 const struct tbl_dat *dp,                  const struct roffcol *col)
                 const struct termp_tbl *tblp)  
 {  {
         int              padl, padr, ssz;          size_t           len, padl, padr, width;
         enum tbl_cellt   pos;          int              ic, spans;
   
         padl = padr = 0;          assert(dp->string);
           len = term_strlen(tp, dp->string);
           width = col->width;
           ic = dp->layout->col;
           spans = dp->spans;
           while (spans--)
                   width += tp->tbl.cols[++ic].width + 3;
   
         pos = dp->layout ? dp->layout->pos : TBL_CELL_LEFT;          padr = width > len ? width - len : 0;
         ssz = term_len(tp, 1);          padl = 0;
   
         switch (pos) {          switch (dp->layout->pos) {
         case (TBL_CELL_LONG):          case TBL_CELL_LONG:
                 padl = ssz;                  padl = term_len(tp, 1);
                 padr = tblp->width - term_strlen(tp, dp->string) - ssz;                  padr = padr > padl ? padr - padl : 0;
                 break;                  break;
         case (TBL_CELL_CENTRE):          case TBL_CELL_CENTRE:
                 padl = tblp->width - term_strlen(tp, dp->string);                  if (2 > padr)
                 if (padl % 2)                          break;
                         padr++;                  padl = padr / 2;
                 padl /= 2;                  padr -= padl;
                 padr += padl;  
                 break;                  break;
         case (TBL_CELL_RIGHT):          case TBL_CELL_RIGHT:
                 padl = tblp->width - term_strlen(tp, dp->string);                  padl = padr;
                   padr = 0;
                 break;                  break;
         default:          default:
                 padr = tblp->width - term_strlen(tp, dp->string);  
                 break;                  break;
         }          }
   
         tbl_char(tp, ASCII_NBRSP, padl);          tbl_char(tp, ASCII_NBRSP, padl);
         term_word(tp, dp->string);          tbl_word(tp, dp);
         tbl_char(tp, ASCII_NBRSP, padr);          tbl_char(tp, ASCII_NBRSP, padr);
 }  }
   
 static void  static void
 tbl_data_number(struct termp *tp, const struct tbl *tbl,  tbl_number(struct termp *tp, const struct tbl_opts *opts,
                 const struct tbl_dat *dp,                  const struct tbl_dat *dp,
                 const struct termp_tbl *tblp)                  const struct roffcol *col)
 {  {
         char            *decp, buf[2];          char            *cp;
         int              d, padl, sz, psz, ssz, i;          char             buf[2];
           size_t           sz, psz, ssz, d, padl;
           int              i;
   
         /*          /*
          * See calc_data_number().  Left-pad by taking the offset of our           * See calc_data_number().  Left-pad by taking the offset of our
          * and the maximum decimal; right-pad by the remaining amount.           * and the maximum decimal; right-pad by the remaining amount.
          */           */
   
           assert(dp->string);
   
         sz = term_strlen(tp, dp->string);          sz = term_strlen(tp, dp->string);
         psz = term_strlen(tp, ".");  
   
         if (NULL != (decp = strchr(dp->string, tbl->decimal))) {          buf[0] = opts->decimal;
                 buf[1] = '\0';          buf[1] = '\0';
                 for (ssz = i = 0; decp != &dp->string[i]; i++) {  
                         buf[0] = dp->string[i];  
                         ssz += term_strlen(tp, buf);  
                 }  
                 d = ssz + psz;  
         } else  
                 d = sz + psz;  
   
         assert(d <= tblp->decimal);          psz = term_strlen(tp, buf);
         assert(sz - d <= tblp->width - tblp->decimal);  
   
         padl = tblp->decimal - d + term_len(tp, 1);          if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
         assert(tblp->width - sz - padl);                  for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
   
         tbl_char(tp, ASCII_NBRSP, padl);  
         term_word(tp, dp->string);  
         tbl_char(tp, ASCII_NBRSP, tblp->width - sz - padl);  
 }  
   
 static void  
 tbl_calc(struct termp *tp, const struct tbl_span *sp)  
 {  
         const struct tbl_dat *dp;  
         const struct tbl_head *hp;  
         struct termp_tbl *p;  
   
         /* Calculate width as the max of column cells' widths. */  
   
         hp = sp->head;  
   
         for ( ; sp; sp = sp->next) {  
                 if (TBL_SPAN_DATA != sp->pos)  
                         continue;  
   
                 for (dp = sp->first; dp; dp = dp->next) {  
                         if (NULL == dp->layout)  
                                 continue;  
                         p = &tp->tbl[dp->layout->head->ident];  
                         tbl_calc_data(tp, sp->tbl, dp, p);  
                 }  
         }  
   
         /* Calculate width as the simple spanner value. */  
   
         for ( ; hp; hp = hp->next)  
                 switch (hp->pos) {  
                 case (TBL_HEAD_VERT):  
                         tp->tbl[hp->ident].width = term_len(tp, 1);  
                         break;  
                 case (TBL_HEAD_DVERT):  
                         tp->tbl[hp->ident].width = term_len(tp, 2);  
                         break;  
                 default:  
                         break;  
                 }  
 }  
   
 static void  
 tbl_calc_data(struct termp *tp, const struct tbl *tbl,  
                 const struct tbl_dat *dp, struct termp_tbl *tblp)  
 {  
         int              sz;  
   
         /* Branch down into data sub-types. */  
   
         switch (dp->layout->pos) {  
         case (TBL_CELL_HORIZ):  
                 /* FALLTHROUGH */  
         case (TBL_CELL_DHORIZ):  
                 sz = term_len(tp, 1);  
                 if (tblp->width < sz)  
                         tblp->width = sz;  
                 break;  
         case (TBL_CELL_LONG):  
                 /* FALLTHROUGH */  
         case (TBL_CELL_CENTRE):  
                 /* FALLTHROUGH */  
         case (TBL_CELL_LEFT):  
                 /* FALLTHROUGH */  
         case (TBL_CELL_RIGHT):  
                 tbl_calc_data_literal(tp, dp, tblp);  
                 break;  
         case (TBL_CELL_NUMBER):  
                 tbl_calc_data_number(tp, tbl, dp, tblp);  
                 break;  
         default:  
                 abort();  
                 /* NOTREACHED */  
         }  
 }  
   
 static void  
 tbl_calc_data_number(struct termp *tp, const struct tbl *tbl,  
                 const struct tbl_dat *dp, struct termp_tbl *tblp)  
 {  
         int              sz, d, psz, i, ssz;  
         char            *cp, buf[2];  
   
         /*  
          * First calculate number width and decimal place (last + 1 for  
          * no-decimal numbers).  If the stored decimal is subsequent  
          * ours, make our size longer by that difference  
          * (right-"shifting"); similarly, if ours is subsequent the  
          * stored, then extend the stored size by the difference.  
          * Finally, re-assign the stored values.  
          */  
   
         /* TODO: use spacing modifier. */  
   
         assert(dp->string);  
         sz = term_strlen(tp, dp->string);  
         psz = term_strlen(tp, ".");  
   
         if (NULL != (cp = strchr(dp->string, tbl->decimal))) {  
                 buf[1] = '\0';  
                 for (ssz = i = 0; cp != &dp->string[i]; i++) {  
                         buf[0] = dp->string[i];                          buf[0] = dp->string[i];
                         ssz += term_strlen(tp, buf);                          ssz += term_strlen(tp, buf);
                 }                  }
Line 515  tbl_calc_data_number(struct termp *tp, const struct tb
Line 396  tbl_calc_data_number(struct termp *tp, const struct tb
         } else          } else
                 d = sz + psz;                  d = sz + psz;
   
         sz += term_len(tp, 2);          if (col->decimal > d && col->width > sz) {
                   padl = col->decimal - d;
         if (tblp->decimal > d) {                  if (padl + sz > col->width)
                 sz += tblp->decimal - d;                          padl = col->width - sz;
                 d = tblp->decimal;                  tbl_char(tp, ASCII_NBRSP, padl);
         } else          } else
                 tblp->width += d - tblp->decimal;                  padl = 0;
           tbl_word(tp, dp);
         if (sz > tblp->width)          if (col->width > sz + padl)
                 tblp->width = sz;                  tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
         if (d > tblp->decimal)  
                 tblp->decimal = d;  
 }  }
   
 static void  static void
 tbl_calc_data_literal(struct termp *tp,  tbl_word(struct termp *tp, const struct tbl_dat *dp)
                 const struct tbl_dat *dp,  
                 struct termp_tbl *tblp)  
 {  {
         int              sz, bufsz, spsz;          int              prev_font;
   
         /*          prev_font = tp->fonti;
          * Calculate our width and use the spacing, with a minimum          if (dp->layout->flags & TBL_CELL_BOLD)
          * spacing dictated by position (centre, e.g,. gets a space on                  term_fontpush(tp, TERMFONT_BOLD);
          * either side, while right/left get a single adjacent space).          else if (dp->layout->flags & TBL_CELL_ITALIC)
          */                  term_fontpush(tp, TERMFONT_UNDER);
   
         assert(dp->string);          term_word(tp, dp->string);
         sz = term_strlen(tp, dp->string);  
   
         switch (dp->layout->pos) {          term_fontpopq(tp, prev_font);
         case (TBL_CELL_LONG):  
                 /* FALLTHROUGH */  
         case (TBL_CELL_CENTRE):  
                 bufsz = term_len(tp, 2);  
                 break;  
         default:  
                 bufsz = term_len(tp, 1);  
                 break;  
         }  
   
         spsz = 0;  
         if (dp->layout->spacing)  
                 spsz = term_len(tp, dp->layout->spacing);  
   
         if (spsz)  
                 bufsz = bufsz > spsz ?  bufsz : spsz;  
   
         sz += bufsz;  
         if (tblp->width < sz)  
                 tblp->width = sz;  
 }  }

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.41

CVSweb