Print this page
9718 update mandoc to 1.14.4
   1 /*      $Id: eqn_term.c,v 1.13 2017/07/08 14:51:04 schwarze Exp $ */
   2 /*
   3  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
   4  * Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
   5  *
   6  * Permission to use, copy, modify, and distribute this software for any
   7  * purpose with or without fee is hereby granted, provided that the above
   8  * copyright notice and this permission notice appear in all copies.
   9  *
  10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17  */
  18 #include "config.h"
  19 
  20 #include <sys/types.h>
  21 
  22 #include <assert.h>

  23 #include <stdio.h>
  24 #include <stdlib.h>
  25 #include <string.h>
  26 
  27 #include "mandoc.h"
  28 #include "out.h"
  29 #include "term.h"
  30 
  31 static  const enum termfont fontmap[EQNFONT__MAX] = {
  32         TERMFONT_NONE, /* EQNFONT_NONE */
  33         TERMFONT_NONE, /* EQNFONT_ROMAN */
  34         TERMFONT_BOLD, /* EQNFONT_BOLD */
  35         TERMFONT_BOLD, /* EQNFONT_FAT */
  36         TERMFONT_UNDER /* EQNFONT_ITALIC */
  37 };
  38 
  39 static void     eqn_box(struct termp *, const struct eqn_box *);
  40 
  41 
  42 void
  43 term_eqn(struct termp *p, const struct eqn_box *bp)
  44 {
  45 
  46         eqn_box(p, bp);
  47         p->flags &= ~TERMP_NOSPACE;
  48 }
  49 
  50 static void
  51 eqn_box(struct termp *p, const struct eqn_box *bp)
  52 {
  53         const struct eqn_box *child;

  54         int delim;
  55 
  56         /* Delimiters around this box? */
  57 
  58         if ((bp->type == EQN_LIST && bp->expectargs > 1) ||
  59             (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
  60             (bp->parent != NULL && (bp->parent->pos == EQNPOS_SQRT ||
  61             /* Diacritic followed by ^ or _. */
  62             ((bp->top != NULL || bp->bottom != NULL) &&
  63              bp->parent->type == EQN_SUBEXPR &&
  64              bp->parent->pos != EQNPOS_OVER && bp->next != NULL) ||
  65             /* Nested over, sub, sup, from, to. */
  66             (bp->type == EQN_SUBEXPR && bp->pos != EQNPOS_SQRT &&
  67              ((bp->parent->type == EQN_LIST && bp->expectargs == 1) ||
  68               (bp->parent->type == EQN_SUBEXPR &&
  69                bp->pos != EQNPOS_SQRT)))))) {
  70                 if (bp->parent->type == EQN_SUBEXPR && bp->prev != NULL)









  71                         p->flags |= TERMP_NOSPACE;
  72                 term_word(p, bp->left != NULL ? bp->left : "(");
  73                 p->flags |= TERMP_NOSPACE;
  74                 delim = 1;
  75         } else
  76                 delim = 0;
  77 
  78         /* Handle Fonts and text. */
  79 
  80         if (bp->font != EQNFONT_NONE)
  81                 term_fontpush(p, fontmap[(int)bp->font]);
  82 
  83         if (bp->text != NULL)


  84                 term_word(p, bp->text);







  85 
  86         /* Special box types. */
  87 
  88         if (bp->pos == EQNPOS_SQRT) {
  89                 term_word(p, "sqrt");
  90                 if (bp->first != NULL) {
  91                         p->flags |= TERMP_NOSPACE;
  92                         eqn_box(p, bp->first);
  93                 }
  94         } else if (bp->type == EQN_SUBEXPR) {
  95                 child = bp->first;
  96                 eqn_box(p, child);
  97                 p->flags |= TERMP_NOSPACE;
  98                 term_word(p, bp->pos == EQNPOS_OVER ? "/" :
  99                     (bp->pos == EQNPOS_SUP ||
 100                      bp->pos == EQNPOS_TO) ? "^" : "_");
 101                 p->flags |= TERMP_NOSPACE;
 102                 child = child->next;
 103                 if (child != NULL) {

 104                         eqn_box(p, child);
 105                         if (bp->pos == EQNPOS_FROMTO ||
 106                             bp->pos == EQNPOS_SUBSUP) {
 107                                 p->flags |= TERMP_NOSPACE;
 108                                 term_word(p, "^");
 109                                 p->flags |= TERMP_NOSPACE;
 110                                 child = child->next;
 111                                 if (child != NULL)
 112                                         eqn_box(p, child);
 113                         }
 114                 }
 115         } else {
 116                 child = bp->first;
 117                 if (bp->type == EQN_MATRIX &&
 118                     child != NULL &&
 119                     child->type == EQN_LIST &&
 120                     child->expectargs > 1)
 121                         child = child->first;
 122                 while (child != NULL) {
 123                         eqn_box(p,


   1 /*      $Id: eqn_term.c,v 1.17 2017/08/23 21:56:20 schwarze Exp $ */
   2 /*
   3  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
   4  * Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
   5  *
   6  * Permission to use, copy, modify, and distribute this software for any
   7  * purpose with or without fee is hereby granted, provided that the above
   8  * copyright notice and this permission notice appear in all copies.
   9  *
  10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17  */
  18 #include "config.h"
  19 
  20 #include <sys/types.h>
  21 
  22 #include <assert.h>
  23 #include <ctype.h>
  24 #include <stdio.h>
  25 #include <stdlib.h>
  26 #include <string.h>
  27 
  28 #include "mandoc.h"
  29 #include "out.h"
  30 #include "term.h"
  31 
  32 static  const enum termfont fontmap[EQNFONT__MAX] = {
  33         TERMFONT_NONE, /* EQNFONT_NONE */
  34         TERMFONT_NONE, /* EQNFONT_ROMAN */
  35         TERMFONT_BOLD, /* EQNFONT_BOLD */
  36         TERMFONT_BOLD, /* EQNFONT_FAT */
  37         TERMFONT_UNDER /* EQNFONT_ITALIC */
  38 };
  39 
  40 static void     eqn_box(struct termp *, const struct eqn_box *);
  41 
  42 
  43 void
  44 term_eqn(struct termp *p, const struct eqn_box *bp)
  45 {
  46 
  47         eqn_box(p, bp);
  48         p->flags &= ~TERMP_NOSPACE;
  49 }
  50 
  51 static void
  52 eqn_box(struct termp *p, const struct eqn_box *bp)
  53 {
  54         const struct eqn_box *child;
  55         const char *cp;
  56         int delim;
  57 
  58         /* Delimiters around this box? */
  59 
  60         if ((bp->type == EQN_LIST && bp->expectargs > 1) ||
  61             (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
  62             (bp->parent != NULL && (bp->parent->pos == EQNPOS_SQRT ||
  63             /* Diacritic followed by ^ or _. */
  64             ((bp->top != NULL || bp->bottom != NULL) &&
  65              bp->parent->type == EQN_SUBEXPR &&
  66              bp->parent->pos != EQNPOS_OVER && bp->next != NULL) ||
  67             /* Nested over, sub, sup, from, to. */
  68             (bp->type == EQN_SUBEXPR && bp->pos != EQNPOS_SQRT &&
  69              ((bp->parent->type == EQN_LIST && bp->expectargs == 1) ||
  70               (bp->parent->type == EQN_SUBEXPR &&
  71                bp->pos != EQNPOS_SQRT)))))) {
  72                 if ((bp->parent->type == EQN_SUBEXPR && bp->prev != NULL) ||
  73                     (bp->type == EQN_LIST &&
  74                      bp->first != NULL &&
  75                      bp->first->type != EQN_PILE &&
  76                      bp->first->type != EQN_MATRIX &&
  77                      bp->prev != NULL &&
  78                      (bp->prev->type == EQN_LIST ||
  79                       (bp->prev->type == EQN_TEXT &&
  80                        (*bp->prev->text == '\\' ||
  81                         isalpha((unsigned char)*bp->prev->text))))))
  82                         p->flags |= TERMP_NOSPACE;
  83                 term_word(p, bp->left != NULL ? bp->left : "(");
  84                 p->flags |= TERMP_NOSPACE;
  85                 delim = 1;
  86         } else
  87                 delim = 0;
  88 
  89         /* Handle Fonts and text. */
  90 
  91         if (bp->font != EQNFONT_NONE)
  92                 term_fontpush(p, fontmap[(int)bp->font]);
  93 
  94         if (bp->text != NULL) {
  95                 if (strchr("!\"'),.:;?]}", *bp->text) != NULL)
  96                         p->flags |= TERMP_NOSPACE;
  97                 term_word(p, bp->text);
  98                 if ((cp = strchr(bp->text, '\0')) > bp->text &&
  99                     (strchr("\"'([{", cp[-1]) != NULL ||
 100                      (bp->prev == NULL && (cp[-1] == '-' ||
 101                       (cp >= bp->text + 5 &&
 102                        strcmp(cp - 5, "\\[mi]") == 0)))))
 103                         p->flags |= TERMP_NOSPACE;
 104         }
 105 
 106         /* Special box types. */
 107 
 108         if (bp->pos == EQNPOS_SQRT) {
 109                 term_word(p, "sqrt");
 110                 if (bp->first != NULL) {
 111                         p->flags |= TERMP_NOSPACE;
 112                         eqn_box(p, bp->first);
 113                 }
 114         } else if (bp->type == EQN_SUBEXPR) {
 115                 child = bp->first;
 116                 eqn_box(p, child);
 117                 p->flags |= TERMP_NOSPACE;
 118                 term_word(p, bp->pos == EQNPOS_OVER ? "/" :
 119                     (bp->pos == EQNPOS_SUP ||
 120                      bp->pos == EQNPOS_TO) ? "^" : "_");

 121                 child = child->next;
 122                 if (child != NULL) {
 123                         p->flags |= TERMP_NOSPACE;
 124                         eqn_box(p, child);
 125                         if (bp->pos == EQNPOS_FROMTO ||
 126                             bp->pos == EQNPOS_SUBSUP) {
 127                                 p->flags |= TERMP_NOSPACE;
 128                                 term_word(p, "^");
 129                                 p->flags |= TERMP_NOSPACE;
 130                                 child = child->next;
 131                                 if (child != NULL)
 132                                         eqn_box(p, child);
 133                         }
 134                 }
 135         } else {
 136                 child = bp->first;
 137                 if (bp->type == EQN_MATRIX &&
 138                     child != NULL &&
 139                     child->type == EQN_LIST &&
 140                     child->expectargs > 1)
 141                         child = child->first;
 142                 while (child != NULL) {
 143                         eqn_box(p,