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

Diff for /mandoc/out.c between version 1.13 and 1.40

version 1.13, 2010/04/07 07:49:38 version 1.40, 2011/04/09 15:29:40
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>   * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
    * Copyright (c) 2011 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 27 
Line 28 
 #include <string.h>  #include <string.h>
 #include <time.h>  #include <time.h>
   
   #include "mandoc.h"
 #include "out.h"  #include "out.h"
   
 /* See a2roffdeco(). */  static  void    tblcalc_data(struct rofftbl *, struct roffcol *,
 #define C2LIM(c, l) do { \                          const struct tbl *, const struct tbl_dat *);
         (l) = 1; \  static  void    tblcalc_literal(struct rofftbl *, struct roffcol *,
         if ('[' == (c) || '\'' == (c)) \                          const struct tbl_dat *);
                 (l) = 0; \  static  void    tblcalc_number(struct rofftbl *, struct roffcol *,
         else if ('(' == (c)) \                          const struct tbl *, const struct tbl_dat *);
                 (l) = 2; } \  
         while (/* CONSTCOND */ 0)  
   
 /* See a2roffdeco(). */  
 #define C2TERM(c, t) do { \  
         (t) = 0; \  
         if ('\'' == (c)) \  
                 (t) = 1; \  
         else if ('[' == (c)) \  
                 (t) = 2; \  
         else if ('(' == (c)) \  
                 (t) = 3; } \  
         while (/* CONSTCOND */ 0)  
   
 /*  /*
  * Convert a `scaling unit' to a consistent form, or fail.  Scaling   * Convert a `scaling unit' to a consistent form, or fail.  Scaling
  * units are documented in groff.7, mdoc.7, man.7.   * units are documented in groff.7, mdoc.7, man.7.
Line 136  a2roffsu(const char *src, struct roffsu *dst, enum rof
Line 125  a2roffsu(const char *src, struct roffsu *dst, enum rof
                 return(0);                  return(0);
         }          }
   
           /* FIXME: do this in the caller. */
         if ((dst->scale = atof(buf)) < 0)          if ((dst->scale = atof(buf)) < 0)
                 dst->scale = 0;                  dst->scale = 0;
         dst->unit = unit;          dst->unit = unit;
         dst->pt = hasd;  
   
         return(1);          return(1);
 }  }
   
Line 186  time2a(time_t t, char *dst, size_t sz)
Line 174  time2a(time_t t, char *dst, size_t sz)
         (void)strftime(p, sz, "%Y", &tm);          (void)strftime(p, sz, "%Y", &tm);
 }  }
   
   /*
 /*   * Calculate the abstract widths and decimal positions of columns in a
  * Returns length of parsed string (the leading "\" should NOT be   * table.  This routine allocates the columns structures then runs over
  * included).  This can be zero if the current character is the nil   * all rows and cells in the table.  The function pointers in "tbl" are
  * terminator.  "d" is set to the type of parsed decorator, which may   * used for the actual width calculations.
  * have an adjoining "word" of size "sz" (e.g., "(ab" -> "ab", 2).  
  */   */
 int  void
 a2roffdeco(enum roffdeco *d,  tblcalc(struct rofftbl *tbl, const struct tbl_span *sp)
                 const char **word, size_t *sz)  
 {  {
         int              j, offs, term, lim;          const struct tbl_dat    *dp;
         const char      *wp, *sp;          const struct tbl_head   *hp;
           struct roffcol          *col;
   
         *d = DECO_NONE;          /*
         wp = *word;           * Allocate the master column specifiers.  These will hold the
         offs = 0;           * widths and decimal positions for all cells in the column.  It
            * must be freed and nullified by the caller.
            */
   
         switch (*wp) {          assert(NULL == tbl->cols);
         case ('\0'):          tbl->cols = mandoc_calloc
                 return(0);                  ((size_t)sp->tbl->cols, sizeof(struct roffcol));
   
         case ('('):          hp = sp->head;
                 if ('\0' == *(++wp))  
                         return(1);  
                 if ('\0' == *(wp + 1))  
                         return(2);  
   
                 *d = DECO_SPECIAL;          for ( ; sp; sp = sp->next) {
                 *sz = 2;                  if (TBL_SPAN_DATA != sp->pos)
                 *word = wp;                          continue;
                 return(3);                  /*
                    * Account for the data cells in the layout, matching it
                    * to data cells in the data section.
                    */
                   for (dp = sp->first; dp; dp = dp->next) {
                           assert(dp->layout);
                           col = &tbl->cols[dp->layout->head->ident];
                           tblcalc_data(tbl, col, sp->tbl, dp);
                   }
           }
   
         case ('*'):          /*
                 switch (*(++wp)) {           * Calculate width of the spanners.  These get one space for a
                 case ('\0'):           * vertical line, two for a double-vertical line.
                         return(1);           */
   
                 case ('('):          for ( ; hp; hp = hp->next) {
                         if ('\0' == *(++wp))                  col = &tbl->cols[hp->ident];
                                 return(2);                  switch (hp->pos) {
                         if ('\0' == *(wp + 1))                  case (TBL_HEAD_VERT):
                                 return(3);                          col->width = (*tbl->len)(1, tbl->arg);
   
                         *d = DECO_RESERVED;  
                         *sz = 2;  
                         *word = wp;  
                         return(4);  
   
                 case ('['):  
                         offs = 1;  
                         break;                          break;
                   case (TBL_HEAD_DVERT):
                           col->width = (*tbl->len)(2, tbl->arg);
                           break;
                 default:                  default:
                         *d = DECO_RESERVED;                          break;
                         *sz = 1;  
                         *word = wp;  
                         return(2);  
                 }                  }
           }
   }
   
   static void
   tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
                   const struct tbl *tp, const struct tbl_dat *dp)
   {
           size_t           sz;
   
           /* Branch down into data sub-types. */
   
           switch (dp->layout->pos) {
           case (TBL_CELL_HORIZ):
                   /* FALLTHROUGH */
           case (TBL_CELL_DHORIZ):
                   sz = (*tbl->len)(1, tbl->arg);
                   if (col->width < sz)
                           col->width = sz;
                 break;                  break;
           case (TBL_CELL_LONG):
                   /* FALLTHROUGH */
           case (TBL_CELL_CENTRE):
                   /* FALLTHROUGH */
           case (TBL_CELL_LEFT):
                   /* FALLTHROUGH */
           case (TBL_CELL_RIGHT):
                   tblcalc_literal(tbl, col, dp);
                   break;
           case (TBL_CELL_NUMBER):
                   tblcalc_number(tbl, col, tp, dp);
                   break;
           case (TBL_CELL_DOWN):
                   break;
           default:
                   abort();
                   /* NOTREACHED */
           }
   }
   
         case ('s'):  static void
                 sp = wp;  tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
                 if ('\0' == *(++wp))                  const struct tbl_dat *dp)
                         return(1);  {
           size_t           sz, bufsz, spsz;
           const char      *str;
   
                 C2LIM(*wp, lim);          /*
                 C2TERM(*wp, term);           * Calculate our width and use the spacing, with a minimum
            * spacing dictated by position (centre, e.g,. gets a space on
            * either side, while right/left get a single adjacent space).
            */
   
                 if (term)          bufsz = spsz = 0;
                         wp++;          str = dp->string ? dp->string : "";
           sz = (*tbl->slen)(str, tbl->arg);
   
                 *word = wp;          /* FIXME: TBL_DATA_HORIZ et al.? */
   
                 if (*wp == '+' || *wp == '-')          assert(dp->layout);
                         ++wp;          switch (dp->layout->pos) {
           case (TBL_CELL_LONG):
                   /* FALLTHROUGH */
           case (TBL_CELL_CENTRE):
                   bufsz = (*tbl->len)(1, tbl->arg);
                   break;
           default:
                   bufsz = (*tbl->len)(1, tbl->arg);
                   break;
           }
   
                 switch (*wp) {          if (dp->layout->spacing) {
                 case ('\''):                  spsz = (*tbl->len)(dp->layout->spacing, tbl->arg);
                         /* FALLTHROUGH */                  bufsz = bufsz > spsz ? bufsz : spsz;
                 case ('['):          }
                         /* FALLTHROUGH */  
                 case ('('):  
                         if (term)  
                                 return((int)(wp - sp));  
   
                         C2LIM(*wp, lim);          sz += bufsz;
                         C2TERM(*wp, term);          if (col->width < sz)
                         wp++;                  col->width = sz;
                         break;  }
                 default:  
                         break;  
                 }  
   
                 if ( ! isdigit((u_char)*wp))  static void
                         return((int)(wp - sp));  tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
                   const struct tbl *tp, const struct tbl_dat *dp)
   {
           int              i;
           size_t           sz, psz, ssz, d;
           const char      *str;
           char            *cp;
           char             buf[2];
   
                 for (j = 0; isdigit((u_char)*wp); j++) {          /*
                         if (lim && j >= lim)           * First calculate number width and decimal place (last + 1 for
                                 break;           * no-decimal numbers).  If the stored decimal is subsequent
                         ++wp;           * 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.
            */
   
                 if (term && term < 3) {          str = dp->string ? dp->string : "";
                         if (1 == term && *wp != '\'')          sz = (*tbl->slen)(str, tbl->arg);
                                 return((int)(wp - sp));  
                         if (2 == term && *wp != ']')  
                                 return((int)(wp - sp));  
                         ++wp;  
                 }  
   
                 *d = DECO_SIZE;          /* FIXME: TBL_DATA_HORIZ et al.? */
                 return((int)(wp - sp));  
   
         case ('f'):          buf[0] = tp->decimal;
                 switch (*(++wp)) {          buf[1] = '\0';
                 case ('\0'):  
                         return(1);  
                 case ('3'):  
                         /* FALLTHROUGH */  
                 case ('B'):  
                         *d = DECO_BOLD;  
                         break;  
                 case ('2'):  
                         /* FALLTHROUGH */  
                 case ('I'):  
                         *d = DECO_ITALIC;  
                         break;  
                 case ('P'):  
                         *d = DECO_PREVIOUS;  
                         break;  
                 case ('1'):  
                         /* FALLTHROUGH */  
                 case ('R'):  
                         *d = DECO_ROMAN;  
                         break;  
                 default:  
                         break;  
                 }  
   
                 return(2);          psz = (*tbl->slen)(buf, tbl->arg);
   
         case ('['):          if (NULL != (cp = strrchr(str, tp->decimal))) {
                 break;                  buf[1] = '\0';
                   for (ssz = 0, i = 0; cp != &str[i]; i++) {
                           buf[0] = str[i];
                           ssz += (*tbl->slen)(buf, tbl->arg);
                   }
                   d = ssz + psz;
           } else
                   d = sz + psz;
   
         case ('c'):          /* Padding. */
                 *d = DECO_NOSPACE;  
                 *sz = 1;  
                 return(1);  
   
         default:          sz += (*tbl->len)(2, tbl->arg);
                 *d = DECO_SPECIAL;          d += (*tbl->len)(1, tbl->arg);
                 *word = wp;  
                 *sz = 1;  
                 return(1);  
         }  
   
         *word = ++wp;          /* Adjust the settings for this column. */
   
         for (j = 0; *wp && ']' != *wp; wp++, j++)          if (col->decimal > d) {
                 /* Loop... */ ;                  sz += col->decimal - d;
                   d = col->decimal;
           } else
                   col->width += d - col->decimal;
   
         if ('\0' == *wp)          if (sz > col->width)
                 return(j + 1 + offs);                  col->width = sz;
           if (d > col->decimal)
                   col->decimal = d;
   
         *d = offs ? DECO_RESERVED : DECO_SPECIAL;          /* Adjust for stipulated width. */
         *sz = (size_t)j;  
   
         return (j + 2 + offs);          if (col->width < dp->layout->spacing)
                   col->width = dp->layout->spacing;
 }  }
   
   

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.40

CVSweb