Annotation of libwww/LineMode/src/GridText.c, revision 1.47

1.13      frystyk     1: /*                                                                  GridText.c
                      2: **     CHARACTER GRID HYPERTEXT OBJECT
                      3: **
1.17      frystyk     4: **     (c) COPYRIGHT MIT 1995.
1.13      frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
1.47    ! frystyk     6: **     @(#) $Id: GridText.c,v 1.46 1999/02/22 22:10:12 frystyk Exp $
1.13      frystyk     7: **
1.30      frystyk     8: **     This is the definition of the HyperDoc object and the HText interface
                      9: **     which is used in the current Library HTML parser
1.1       timbl      10: */
                     11: 
1.16      frystyk    12: #include <assert.h>
                     13: 
                     14: #include "WWWLib.h"
1.33      frystyk    15: #include "WWWCache.h"
1.40      frystyk    16: #include "WWWApp.h"
1.47    ! frystyk    17: #include "WWWHTML.h"
1.19      frystyk    18: #include "HTBrowse.h"
1.16      frystyk    19: #include "HTFont.h"
                     20: #include "GridStyle.h"
                     21: #include "GridText.h"
1.1       timbl      22: 
1.11      howcome    23: /* HWL 18/7/94: applied patch from agl@glas2.glas.apc.org (Anton Tropashko) */
                     24: #ifdef CYRILLIC
                     25: #include "a_stdio.h"
                     26: #endif
                     27: 
1.1       timbl      28: #define MAX_LINE       HTScreenWidth   /* No point in accumulating more */
1.44      frystyk    29: #define LOADED_LIMIT 40
1.1       timbl      30: 
                     31: #ifdef CURSES
                     32: #define DISPLAY_LINES (HTScreenHeight)
                     33: #define       TITLE_LINES      0
                     34: #else
1.19      frystyk    35: #define DISPLAY_LINES (HTScreenHeight - 2)   /* Exclude prompt line */
1.1       timbl      36: #define       TITLE_LINES      1
                     37: #endif
                     38: 
1.19      frystyk    39: struct _HTStream {                     /* only know it as object */
1.37      frystyk    40:     const HTStreamClass *      isa;
1.19      frystyk    41:     /* ... */
                     42: };
1.1       timbl      43: 
                     44: /*     From default style sheet:
                     45: */
                     46: extern HTStyleSheet * styleSheet;      /* Default or overridden */
                     47: 
                     48: /*     Exports
                     49: */ 
                     50: PUBLIC HText * HTMainText = 0;         /* Equivalent of main window */
                     51: PUBLIC HTParentAnchor * HTMainAnchor = 0;      /* Anchor for HTMainText */
                     52: 
                     53: typedef struct _line {
                     54:        struct _line    *next;
                     55:        struct _line    *prev;
                     56:        short unsigned  offset;         /* Implicit initial spaces */
                     57:        short unsigned  size;           /* Number of characters */
                     58:        BOOL    split_after;            /* Can we split after? */
                     59:        BOOL    bullet;                 /* Do we bullet? */
                     60:        char    data[1];                /* Space for terminator at least! */
                     61: } HTLine;
                     62: 
                     63: #define LINE_SIZE(l) (sizeof(HTLine)+(l))      /* allow for terminator */
                     64: 
                     65: typedef struct _TextAnchor {
                     66:        struct _TextAnchor *    next;
                     67:        int                     number;         /* For user interface */
                     68:        int                     start;          /* Characters */
                     69:        int                     extent;         /* Characters */
                     70:        HTChildAnchor *         anchor;
                     71: } TextAnchor;
                     72: 
                     73: 
                     74: /*     Notes on struct _Htext:
                     75: **     next_line is valid iff state is false.
                     76: **     top_of_screen line means the line at the top of the screen
                     77: **                     or just under the title if there is one.
                     78: */
                     79: struct _HText {
                     80:        HTParentAnchor *        node_anchor;
                     81:        char *                  title;
                     82:        HTLine *                last_line;
                     83:        int                     lines;          /* Number of them */
                     84:        int                     chars;          /* Number of them */
                     85:        TextAnchor *            first_anchor;   /* Singly linked list */
                     86:        TextAnchor *            last_anchor;
                     87:        int                     last_anchor_number;     /* user number */
                     88: /* For Internal use: */        
                     89:        HTStyle *               style;                  /* Current style */
                     90:        int                     display_on_the_fly;     /* Lines left */
1.3       timbl      91:        BOOL                    all_pages;              /* Loop on the fly */
1.1       timbl      92:        int                     top_of_screen;          /* Line number */
                     93:        HTLine *                top_of_screen_line;     /* Top */
                     94:        HTLine *                next_line;              /* Bottom + 1 */
                     95:        int                     permissible_split;      /* in last line */
                     96:        BOOL                    in_line_1;              /* of paragraph */
                     97:        BOOL                    stale;                  /* Must refresh */
                     98:        
                     99:        HTStream*               target;                 /* Output stream */
                    100:        HTStreamClass           targetClass;            /* Output routines */
1.36      eric      101:        LineMode *              pLm;
1.1       timbl     102: };
                    103: 
                    104: 
                    105: #define PUTC(c) (*text->targetClass.put_character)(text->target, c)
                    106: #define PUTS(s) (*text->targetClass.put_string)(text->target, s)
                    107: 
                    108: /*     Boring static variable used for moving cursor across
                    109: */
                    110: 
                    111: #define SPACES(n) (&space_string[HTScreenWidth - (n)])
                    112:                                    /* String containing blank spaces only */
                    113: PRIVATE char * space_string;
                    114: 
                    115: PRIVATE HTStyle default_style =
                    116:        { 0,  "(Unstyled)", "",
                    117:        (HTFont)0, 1.0, HT_BLACK,               0, 0,
                    118:        0, 0, 0, HT_LEFT,               1, 0,   0, 
                    119:        NO, NO, 0, 0,                   0 };    
                    120: 
                    121: 
1.29      frystyk   122: PUBLIC void clear_screen (void);       /* Forward */
1.1       timbl     123: 
                    124: PRIVATE HTList * loaded_texts; /* A list of all those in memory */
                    125: 
                    126: /*                     Creation Method
                    127: **                     ---------------
1.3       timbl     128: **
                    129: **     Interactive version
                    130: **
1.1       timbl     131: */
1.36      eric      132: extern LineMode * Context_getLineMode(HTRequest * request);
1.47    ! frystyk   133: PUBLIC HText * LMHText_new (
        !           134:        HTRequest * request,
        !           135:        HTParentAnchor * anchor,
        !           136:        HTStream *outstrm)
1.1       timbl     137: {
                    138:     HTLine * line;
1.34      frystyk   139:     HText * self;
1.37      frystyk   140:     if ((self = (HText  *) HT_CALLOC(1, sizeof(*self))) == NULL)
1.34      frystyk   141: /*        HT_OUTOFMEM("HText"); */
                    142:        return self;
1.1       timbl     143:     
1.36      eric      144:     self->pLm = Context_getLineMode(request);
1.1       timbl     145:     if (!loaded_texts) loaded_texts = HTList_new();
                    146:     HTList_addObject(loaded_texts, self);
                    147:     if (HTList_count(loaded_texts) >= LOADED_LIMIT) {
1.46      frystyk   148:         HTTRACE(CACHE_TRACE, "MemoryCache. Freeing off cached doc.\n"); 
1.1       timbl     149:         HText_free((HText *)HTList_removeFirstObject(loaded_texts));
                    150:     }
                    151:     
1.34      frystyk   152:     if ((line = self->last_line = (HTLine *) HT_MALLOC(LINE_SIZE(MAX_LINE))) == NULL)
                    153:        HT_OUTOFMEM("HText_New");
1.1       timbl     154:     line->next = line->prev = line;
                    155:     line->offset = line->size = 0;
                    156:     self->lines = self->chars = 0;
                    157:     self->title = 0;
                    158:     self->first_anchor = self->last_anchor = 0;
                    159:     self->style = &default_style;
                    160:     self->top_of_screen = 0;
                    161:     self->node_anchor = anchor;
                    162:     self->last_anchor_number = 0;      /* Numbering of them for references */
                    163:     self->stale = YES;
                    164:     
                    165:     self->target = NULL;
                    166:     
1.31      frystyk   167:     HTAnchor_setDocument(anchor, (void *) self);
1.1       timbl     168: 
                    169:     clear_screen();
                    170:     HTMainText = self;
                    171:     HTMainAnchor = anchor;
                    172:     self->display_on_the_fly = DISPLAY_LINES;
1.3       timbl     173:     self->all_pages = NO;      /* One page at a time on the fly */
1.1       timbl     174:     
                    175:     if (!space_string) {       /* Make a blank line */
                    176:         char *p;
1.34      frystyk   177:        if ((space_string = (char  *) HT_MALLOC(HTScreenWidth+1)) == NULL)
                    178:            HT_OUTOFMEM("HText_New");
1.1       timbl     179:         for (p=space_string; p<space_string+HTScreenWidth; p++) 
                    180:             *p = ' ';          /* Used for printfs later */
                    181:         space_string[HTScreenWidth] = '\0'; 
                    182:     }
                    183:     
                    184:     return self;
                    185: }
                    186: 
                    187: 
                    188: /*                     Creation Method 2
                    189: **                     ---------------
                    190: **
1.4       timbl     191: **     Non-interative  OR interactive if stream is NULL
1.1       timbl     192: **     Stream is assumed open and left open.
                    193: */
1.47    ! frystyk   194: PUBLIC HText * LMHText_new2 (HTRequest *               request,
1.31      frystyk   195:                            HTParentAnchor *    anchor,
                    196:                            HTStream *          stream)
1.1       timbl     197: {
1.47    ! frystyk   198:     HText * me = LMHText_new(request, anchor, stream);
1.1       timbl     199:         
                    200:     if (stream) {
1.3       timbl     201:         me->target = stream;
                    202:        me->targetClass = *stream->isa; /* copy action procedures */
1.4       timbl     203:        me->all_pages = YES;    /* Display whole file on the fly */    
1.3       timbl     204:     }
                    205:     return me;
1.1       timbl     206: }
                    207: 
1.31      frystyk   208: /*     Free a data object
                    209: **     ------------------
                    210: **     Removes the data object from the anchor
1.1       timbl     211: */
1.37      frystyk   212: PUBLIC void hyper_free (HText *  self)
1.1       timbl     213: {
1.35      frystyk   214:     if (self) {
                    215:        while (1) {                                   /* Free off line array */
                    216:            HTLine * last = self->last_line;
                    217:            if (last) {
                    218:                last->next->prev = last->prev;
                    219:                last->prev->next = last->next;
                    220:                self->last_line = last->prev;
                    221:                if (last == self->last_line) break;
                    222:                HT_FREE(last);
1.39      frystyk   223:            } else
                    224:                break;
1.35      frystyk   225:        }
                    226:        while (self->first_anchor) {                /* Free off anchor array */
                    227:            TextAnchor * last = self->first_anchor;
                    228:            self->first_anchor = last->next;
                    229:            HT_FREE(last);
                    230:        }
                    231:        if (self == HTMainText) HTMainText = NULL;
1.39      frystyk   232:        HT_FREE(self->last_line);
1.35      frystyk   233:        HT_FREE(self);
                    234:     }
1.18      frystyk   235: }
                    236: 
                    237: 
                    238: /*     Free Entire Text
                    239: **     ----------------
                    240: */
1.47    ! frystyk   241: PUBLIC int     HText_free (HText * self)
1.18      frystyk   242: {
1.35      frystyk   243:     if (self) {
                    244:        HTAnchor_setDocument(self->node_anchor, NULL);
1.37      frystyk   245:        hyper_free(self);
1.35      frystyk   246:     }
1.1       timbl     247: }
                    248: 
1.47    ! frystyk   249: PUBLIC BOOL LMHText_delete (HText * self)
        !           250: {
        !           251:     if (self) {
        !           252:        HTAnchor_setDocument(self->node_anchor, NULL);
        !           253:        hyper_free(self);
        !           254:        return YES;
        !           255:     }
        !           256:     return NO;
        !           257: }
        !           258: 
1.39      frystyk   259: /*
                    260: **     Free all registered hypertext documents in memory
                    261: */
                    262: PUBLIC BOOL HText_freeAll (void)
                    263: {
                    264:     if (loaded_texts) {
                    265:        HTList * cur = loaded_texts;
                    266:        HText * pres;
                    267:        while ((pres = (HText *) HTList_nextObject(cur)))
                    268:            HText_free(pres);
                    269:        HTList_delete(loaded_texts);
                    270:        return YES;
                    271:     }
                    272:     return NO;
                    273: }
                    274: 
1.1       timbl     275: 
                    276: /*             Display Methods
                    277: **             ---------------
                    278: */
1.22      frystyk   279: 
1.1       timbl     280: /*     Clear the screen (on screen-mode systems)
                    281: **     ----------------
                    282: */
1.29      frystyk   283: PUBLIC void clear_screen (void)
1.1       timbl     284: {
1.25      frystyk   285:     if (WWWTRACE)
1.22      frystyk   286:        return;                     /* in trace mode, don't clear trace away */
1.1       timbl     287: #ifdef CURSES
                    288:     if (w_text != NULL) {
1.22      frystyk   289:        wmove(w_text,0,0);
                    290:        wclear(w_text);
1.1       timbl     291:     }
1.22      frystyk   292: #endif /* Not CURSES */
1.1       timbl     293:     if (HTMainText) HTMainText->stale = YES;
                    294: }
                    295: 
                    296: 
                    297: /*     Output a line
                    298: **     -------------
                    299: */
1.29      frystyk   300: PRIVATE void display_line (HText * text, HTLine * line)
1.1       timbl     301: {
                    302: #ifdef CURSES
                    303:       int     y, x;
                    304: 
                    305:       waddstr(w_text, SPACES(line->offset));
                    306:       waddstr(w_text, line->data);
                    307:       getyx(w_text, y, x);
                    308:       if (y < DISPLAY_LINES-1) {
                    309:               wmove(w_text, ++y, 0);
                    310:       }
                    311: #else
                    312:    if (!text->target)
1.11      howcome   313:    {
                    314: #ifdef CYRILLIC
                    315:        /* HWL 18/7/94: applied patch from agl@glas2.glas.apc.org (Anton Tropashko) */
                    316:        a_print(SPACES(line->offset),H,stdout); 
                    317:        a_print(line->data,H,stdout);
                    318:        fputc('\n',stdout);
                    319: #else
1.36      eric      320:        OutputData(LineMode_getView(text->pLm), "%s%s\n", SPACES(line->offset), line->data);
1.11      howcome   321: #endif
                    322:    }
1.1       timbl     323:    else {
                    324:        PUTS(SPACES(line->offset));
                    325:        PUTS(line->data);
                    326:        PUTC('\n');
                    327:    }
                    328: #endif
                    329:    
                    330: }
                    331: 
                    332: /*     Output the title line
                    333: **     ---------------------
                    334: */
1.29      frystyk   335: PRIVATE void display_title (HText * text)
1.1       timbl     336: {
1.37      frystyk   337:     const char * title = HTAnchor_title(text->node_anchor);
1.1       timbl     338:     char percent[20], format[20];
                    339:     if (text->lines > (DISPLAY_LINES-1)) {
                    340: #ifdef NOPE
                    341:        sprintf(percent, " (p%d of %d)",
                    342:            (text->top_of_screen/(DISPLAY_LINES-1)) + 1,
                    343:            (text->lines-1)/(DISPLAY_LINES-1) + 1);
                    344:        sprintf(percent, " (%d%%)",
                    345:            100*(text->top_of_screen+DISPLAY_LINES-1)/  /* Seen */
                    346:                (text->lines));                         /* Total */
                    347: #else
                    348:        sprintf(percent, " (%d/%d)",
                    349:            text->top_of_screen+DISPLAY_LINES-1,        /* Seen */
                    350:            text->lines);                               /* Total */
                    351: #endif
                    352:     } else {
                    353:        percent[0] = 0; /* Null string */
                    354:     }
                    355: 
                    356:     sprintf(format, "%%%d.%ds%%s\n",   /* Generate format string */
                    357:                    (int)(HTScreenWidth-strlen(percent)),
                    358:                    (int)(HTScreenWidth-strlen(percent)) );
                    359: #ifdef CURSES
                    360:     mvwprintw(w_top, 0, 0, format, title, percent);
                    361:     wrefresh(w_top);
                    362: #else
1.36      eric      363:     if (!text->target) OutputData(LineMode_getView(text->pLm), format, title, percent);
1.1       timbl     364:     else {
1.34      frystyk   365:        char * line;
                    366:        if ((line = (char *) HT_MALLOC(HTScreenWidth+10)) == NULL)
                    367:            HT_OUTOFMEM("display_titile");
1.1       timbl     368:         sprintf(line, format, title, percent);
                    369:        PUTS(line);
1.34      frystyk   370:        HT_FREE(line);
1.1       timbl     371:     }
                    372: #endif
                    373: }
                    374: 
                    375: 
                    376: /*     Fill the screen with blank after the file
                    377: **     --------------------------
                    378: */
1.29      frystyk   379: PRIVATE void fill_screen (HText *  text, int n)
1.1       timbl     380: {
                    381:     if (n<=0 || n>1000) return;                /* Large value means no pagination */
                    382:     if (text->target) return;
                    383: #ifdef CURSES
                    384:     waddstr(w_text, end_mark);
                    385:     wclrtobot(w_text);
                    386:     wrefresh(w_text);
                    387: #else
                    388: #ifndef VIOLA    
1.36      eric      389:     if (!text->target) OutputData(LineMode_getView(text->pLm), "%s\n", end_mark);
1.1       timbl     390:     else { PUTS(end_mark); PUTC('\n'); }
                    391:     n--;
                    392:     
                    393:     for (; n; n--) {
1.36      eric      394:         if (!text->target) OutputData(LineMode_getView(text->pLm), "\n");
1.1       timbl     395:        else PUTC('\n');
                    396:     }
                    397: #endif
                    398: #endif        /* Not CURSES */
                    399: }
                    400: 
                    401: 
                    402: /*     Output a page
                    403: **     -------------
                    404: */
1.29      frystyk   405: PRIVATE void display_page (HText * text, int line_number)
1.1       timbl     406: {
                    407:     HTLine * line = text->last_line->prev;
                    408:     int i;
1.37      frystyk   409:     const char * title = HTAnchor_title(text->node_anchor);
1.1       timbl     410:     int lines_of_text = title ? (DISPLAY_LINES-TITLE_LINES) : DISPLAY_LINES;
                    411:     int last_screen = text->lines - lines_of_text;
                    412: 
                    413: /*     Constrain the line number to be within the document
                    414: */
                    415:     if (text->lines <= lines_of_text) line_number = 0;
                    416:     else if (line_number>last_screen) line_number = last_screen;
                    417:     else if (line_number < 0) line_number = 0;
                    418:     
                    419:     for(i=0,  line = text->last_line->next;            /* Find line */
                    420:        i<line_number && (line!=text->last_line);
                    421:       i++, line=line->next) /* Loop */ assert(line->next != NULL);
                    422: 
                    423:     while((line!=text->last_line) && (line->size==0)) {        /* Skip blank lines */
                    424:         assert(line->next != NULL);
                    425:       line = line->next;
                    426:         line_number ++;
                    427:     }
                    428: 
                    429: /*     Can we just scroll to it?
                    430: */ 
                    431: #ifndef VM                     /* No scrolling */
                    432:     if (!text->stale && (line_number>=text->top_of_screen) &&
                    433:        (line_number < text->top_of_screen + DISPLAY_LINES)) {  /* Yes */
                    434:        lines_of_text = line_number - text->top_of_screen;
                    435:        line = text->next_line;
                    436:         text->top_of_screen = line_number;
                    437: #ifdef CURSES
                    438:         scrollok(w_text, TRUE);
                    439:         for (i = 0; i < lines_of_text; i++) {
                    440:               scroll(w_text);
                    441:         }
                    442: #endif  
                    443:     } else
                    444: #endif
                    445:     {
                    446:        clear_screen();                                         /* No */
                    447:         text->top_of_screen = line_number;
                    448:        if (title) display_title(text);
                    449:     }
                    450:     
                    451: /* Bug: when we scroll to a part slightly futher down a page which previously
                    452:  fitted all on one screen including the [End], the code below will add an
                    453:  extra [End] to the screen, giving a total of two, one underneath the other.
                    454: */
                    455:     
                    456:  /*    print it
                    457:  */
                    458:     if (line) {
                    459:       for(i=0;
                    460:          (i< lines_of_text) && (line != text->last_line); i++)  {
                    461:       assert(line != NULL);
                    462:         display_line(text, line);
                    463:        line = line->next;
                    464:       }
                    465:       fill_screen(text, lines_of_text - i);
                    466:     }
                    467: 
                    468:     text->next_line = line;    /* Line after screen */
                    469:     text->stale = NO;          /* Display is up-to-date */
                    470: }
                    471: 
                    472: 
                    473: /*                     Object Building methods
                    474: **                     -----------------------
                    475: **
                    476: **     These are used by a parser to build the text in an object
                    477: */
1.29      frystyk   478: PUBLIC void HText_beginAppend (HText * text)
1.1       timbl     479: {
                    480:     text->permissible_split = 0;
                    481:     text->in_line_1 = YES;
                    482: }
                    483: 
                    484: 
                    485: /*     Add a new line of text
                    486: **     ----------------------
                    487: **
                    488: ** On entry,
                    489: **
                    490: **     split   is zero for newline function, else number of characters
                    491: **             before split.
                    492: **     text->display_on_the_fly
                    493: **             may be set to indicate direct output of the finished line.
1.3       timbl     494: **     text->all_pages
                    495: **             if set indicates all pages are to be done on the fly.
1.1       timbl     496: ** On exit,
                    497: **             A new line has been made, justified according to the
                    498: **             current style. Text after the split (if split nonzero)
                    499: **             is taken over onto the next line.
                    500: **
                    501: **             If display_on_the_fly is set, then it is decremented and
                    502: **             the finished line is displayed.
                    503: */
                    504: #define new_line(text) split_line(text, 0)
                    505: 
1.29      frystyk   506: PRIVATE void split_line (HText * text, int split)
1.1       timbl     507: {
                    508:     HTStyle * style = text->style;
                    509:     int spare;
1.14      frystyk   510:     int indent = (int) (text->in_line_1 ? text->style->indent1st
                    511:                        : text->style->leftIndent);
1.7       frystyk   512: 
1.1       timbl     513: /*     Make new line
                    514: */
                    515:     HTLine * previous = text->last_line;
1.34      frystyk   516:     HTLine * line;
                    517:     if ((line = (HTLine  *) HT_MALLOC(LINE_SIZE(MAX_LINE))) == NULL)
                    518:         HT_OUTOFMEM("split_line");
                    519: 
1.1       timbl     520:     text->lines++;
                    521:     
                    522:     previous->next->prev = line;
                    523:     line->prev = previous;
                    524:     line->next = previous->next;
                    525:     previous->next = line;
                    526:     text->last_line = line;
                    527:     line->size = 0;
                    528:     line->offset = 0;
                    529: 
                    530: /*     Split at required point
                    531: */    
                    532:     if (split) {       /* Delete space at "split" splitting line */
                    533:        char * p;
                    534:        previous->data[previous->size] = 0;
                    535:        for (p = &previous->data[split]; *p; p++)
                    536:            if (*p != ' ') break;
                    537:        strcpy(line->data, p);
                    538:        line->size = strlen(line->data);
                    539:        previous->size = split;
                    540:     }
                    541:     
                    542:     while ((previous->size > 0) &&
1.37      frystyk   543:           (previous->data[previous->size-1] == ' '))   /* Strip trailers */
1.1       timbl     544:         previous->size--;
1.7       frystyk   545: 
1.39      frystyk   546:     if ((previous = (HTLine *)
                    547:         HT_REALLOC(previous, LINE_SIZE(previous->size)))==NULL)
                    548:        HT_OUTOFMEM("split_line");
1.1       timbl     549:     previous->prev->next = previous;   /* Link in new line */
                    550:     previous->next->prev = previous;   /* Could be same node of course */
                    551: 
                    552: /*     Terminate finished line for printing
                    553: */
                    554:     previous->data[previous->size] = 0;
1.7       frystyk   555: 
1.1       timbl     556: /*     Align left, right or center
                    557: */
                    558: 
1.14      frystyk   559:     spare =  (int) (HTScreenWidth - style->rightIndent + style->leftIndent -
                    560:                    previous->size);                 /* @@ first line indent */
1.1       timbl     561:                
                    562:     switch (style->alignment) {
                    563:        case HT_CENTER :
                    564:            previous->offset = previous->offset + indent + spare/2;
                    565:            break;
                    566:        case HT_RIGHT :
                    567:            previous->offset = previous->offset + indent + spare;
                    568:            break;
                    569:        case HT_LEFT :
                    570:        case HT_JUSTIFY :               /* Not implemented */
                    571:        default:
                    572:            previous->offset = previous->offset + indent;
                    573:            break;
                    574:     } /* switch */
                    575: 
                    576:     text->chars = text->chars + previous->size + 1;    /* 1 for the line */
                    577:     text->in_line_1 = NO;              /* unless caller sets it otherwise */
                    578:     
                    579: /*     If displaying as we go, output it:
                    580: */
                    581:     if (text->display_on_the_fly) {
                    582:         if (text->display_on_the_fly == DISPLAY_LINES) { /* First line */
                    583:            if (previous->size == 0) {
                    584:                text->top_of_screen++;  /* Scroll white space off top */
                    585:                return;
                    586:            }
                    587:            if (HTAnchor_title(text->node_anchor)) { /* Title exists */
                    588:                display_title(text);
                    589:                text->display_on_the_fly--;
                    590:            }
                    591:        }
                    592:        display_line(text, previous);
                    593:        text->display_on_the_fly--;
1.3       timbl     594:        
                    595:        /* Loop to top of next page? */
                    596:        if (!text->display_on_the_fly && text->all_pages) {
                    597:            PUTS("\f\n"); /* Form feed on its own line a la rfc1111 */
                    598:            text->display_on_the_fly = DISPLAY_LINES;
                    599:        }
1.1       timbl     600:     }
                    601: } /* split_line */
                    602: 
                    603: 
                    604: /*     Allow vertical blank space
                    605: **     --------------------------
                    606: */
1.29      frystyk   607: PRIVATE void blank_lines (HText * text, int newlines)
1.1       timbl     608: {
                    609:     if (text->last_line->size == 0) {  /* No text on current line */
                    610:        HTLine * line = text->last_line->prev;
                    611:        while ((line!=text->last_line) && (line->size == 0)) {
                    612:            if (newlines==0) break;
                    613:            newlines--;         /* Don't bother: already blank */
                    614:            line = line->prev;
                    615:        }
                    616:     } else {
                    617:        newlines++;                     /* Need also to finish this line */
                    618:     }
                    619: 
                    620:     for(;newlines;newlines--) {
                    621:        new_line(text);
                    622:     }
                    623:     text->in_line_1 = YES;
                    624: }
                    625: 
                    626: 
                    627: /*     New paragraph in current style
                    628: **     ------------------------------
                    629: ** See also: setStyle.
                    630: */
                    631: 
1.29      frystyk   632: PUBLIC void HText_appendParagraph (HText * text)
1.1       timbl     633: {
1.14      frystyk   634:     int after = (int) text->style->spaceAfter;
                    635:     int before = (int) text->style->spaceBefore;
1.1       timbl     636:     blank_lines(text, after>before ? after : before);
                    637: }
                    638: 
                    639: 
                    640: /*     Set Style
                    641: **     ---------
                    642: **
                    643: **     Does not filter unnecessary style changes.
                    644: */
1.29      frystyk   645: PUBLIC void HText_setStyle (HText * text, HTStyle * style)
1.1       timbl     646: {
                    647:     int after, before;
                    648: 
                    649:     if (!style) return;                                /* Safety */
1.14      frystyk   650:     after = (int) text->style->spaceAfter;
                    651:     before = (int) style->spaceBefore;
1.46      frystyk   652:     HTTRACE(SGML_TRACE, "Rendering... Change to style %s\n" _ style->name);
1.1       timbl     653:     blank_lines (text, after>before ? after : before);
                    654:     text->style = style;
                    655: }
                    656: 
                    657: 
                    658: /*     Append a character to the text object
                    659: **     -------------------------------------
                    660: */
1.29      frystyk   661: PUBLIC void HText_appendCharacter (HText * text, char ch)
1.1       timbl     662: {
                    663:     HTLine * line = text->last_line;
                    664:     HTStyle * style = text->style;
1.14      frystyk   665:     int indent = (int)(text->in_line_1 ? style->indent1st : style->leftIndent);
1.1       timbl     666:     
                    667: /*             New Line
                    668: */
                    669:     if (ch == '\n') {
                    670:            new_line(text);
                    671:            text->in_line_1 = YES;      /* First line of new paragraph */
                    672:            return;
                    673:     }
                    674: 
                    675: /*             Tabs
                    676: */
                    677: 
                    678:     if (ch == '\t') {
                    679:         HTTabStop * tab;
                    680:        int target;     /* Where to tab to */
                    681:        int here = line->size + line->offset +indent;
                    682:         if (style->tabs) {     /* Use tab table */
                    683:            for (tab = style->tabs;
                    684:                tab->position <= here;
                    685:                tab++)
                    686:                if (!tab->position) {
                    687:                    new_line(text);
                    688:                    return;
                    689:                }
1.14      frystyk   690:            target = (int) tab->position;
1.1       timbl     691:        } else if (text->in_line_1) {   /* Use 2nd indent */
                    692:            if (here >= style->leftIndent) {
                    693:                new_line(text); /* wrap */
                    694:                return;
                    695:            } else {
1.14      frystyk   696:                target = (int) style->leftIndent;
1.1       timbl     697:            }
                    698:        } else {                /* Default tabs align with left indent mod 8 */
                    699: #ifdef DEFAULT_TABS_8
                    700:            target = ((line->offset + line->size + 8) & (-8))
                    701:                        + style->leftIndent;
                    702: #else
                    703:            new_line(text);
                    704:            return;
                    705: #endif
                    706:        }
                    707:        if (target > HTScreenWidth - style->rightIndent) {
                    708:            new_line(text);
                    709:            return;
                    710:        } else {
                    711:             text->permissible_split = line->size;      /* Can split here */
                    712:            if (line->size == 0) line->offset = line->offset + target - here;
                    713:            else for(; here<target; here++) {
                    714:                 line->data[line->size++] = ' ';        /* Put character into line */
                    715:            }
                    716:            return;
                    717:        }
                    718:        /*NOTREACHED*/
                    719:     } /* if tab */ 
                    720: 
                    721:     
                    722:     if (ch==' ') {
                    723:         text->permissible_split = line->size;  /* Can split here */
                    724:     }
                    725: 
                    726: /*     Check for end of line
                    727: */    
                    728:     if (indent + line->offset + line->size + style->rightIndent
                    729:                >= HTScreenWidth) {
                    730:         if (style->wordWrap) {
1.7       frystyk   731:            if(text->permissible_split > line->size)    /* HENRIK 21/02-94 */
                    732:                text->permissible_split = line->size;
1.1       timbl     733:            split_line(text, text->permissible_split);
                    734:            if (ch==' ') return;        /* Ignore space causing split */
                    735:        } else new_line(text);
                    736:     }
                    737: 
                    738: /*     Insert normal characters
                    739: */
                    740:     if (ch == HT_NON_BREAK_SPACE) {
                    741:         ch = ' ';
                    742:     }
                    743:     {
                    744:         HTLine * line = text->last_line;       /* May have changed */
                    745:         HTFont font = style->font;
                    746:         line->data[line->size++] =     /* Put character into line */
                    747:            font & HT_CAPITALS ? TOUPPER(ch) : ch;
                    748:         if (font & HT_DOUBLE)          /* Do again if doubled */
                    749:             HText_appendCharacter(text, HT_NON_BREAK_SPACE);
                    750:            /* NOT a permissible split */ 
                    751:     }
                    752: }
                    753: 
                    754: /*             Anchor handling
                    755: **             ---------------
                    756: */
                    757: /*     Start an anchor field
                    758: */
1.47    ! frystyk   759: PUBLIC void LMHText_beginAnchor (HText * text,
        !           760:     int elem_num, int attr_num, HTChildAnchor * anc,
        !           761:     const BOOL *present, const char **value)
1.1       timbl     762: {
1.2       timbl     763:     char marker[100];
1.34      frystyk   764:     TextAnchor * a;
                    765:     if ((a = (TextAnchor  *) HT_MALLOC(sizeof(*a))) == NULL)
                    766:         HT_OUTOFMEM("HText_beginAnchor");
1.1       timbl     767:     a->start = text->chars + text->last_line->size;
                    768:     a->extent = 0;
                    769:     if (text->last_anchor) {
                    770:         text->last_anchor->next = a;
                    771:     } else {
                    772:         text->first_anchor = a;
                    773:     }
                    774:     a->next = 0;
                    775:     a->anchor = anc;
                    776:     text->last_anchor = a;
                    777:     
                    778:     if (HTAnchor_followMainLink((HTAnchor*)anc)) {
                    779:         a->number = ++(text->last_anchor_number);
                    780:     } else {
                    781:         a->number = 0;
                    782:     }
                    783: }
                    784: 
                    785: 
1.47    ! frystyk   786: PUBLIC void LMHText_endAnchor (HText * text)
1.1       timbl     787: {
                    788:     TextAnchor * a = text->last_anchor;
                    789:     char marker[100];
                    790:     if (a->number && display_anchors) {         /* If it goes somewhere */
1.2       timbl     791:        sprintf(marker, end_reference, a->number);
1.1       timbl     792:        HText_appendText(text, marker);
                    793:     }
                    794:     a->extent = text->chars + text->last_line->size - a->start;
                    795: }
                    796: 
                    797: 
1.5       timbl     798: /*             IMAGES
                    799: */
1.29      frystyk   800: PUBLIC void HText_appendImage (
                    801:        HText *                 text,
                    802:        HTChildAnchor *         anc,
1.37      frystyk   803:        const char *            alt,
                    804:        const char *            alignment,
1.29      frystyk   805:        BOOL                    isMap)
1.5       timbl     806: {
1.9       frystyk   807:     HText_appendText(text, alt? alt : "[IMAGE]");
1.5       timbl     808: }
                    809:        
1.47    ! frystyk   810: /* LMHText_addText() satisfies HText callback requirement.  */
        !           811: PUBLIC void LMHText_addText (HText * text, const char * str, int length)
        !           812: {
        !           813:     const char * p;
        !           814:     int i;
        !           815:     for (i=0,p=str; i<length; ++i,++p) {
        !           816:         HText_appendCharacter(text, *p);
        !           817:     }
        !           818: }
        !           819: 
        !           820: PUBLIC int HText_appendText (HText * text, const char * str)
1.1       timbl     821: {
1.37      frystyk   822:     const char * p;
1.1       timbl     823:     for(p=str; *p; p++) {
                    824:         HText_appendCharacter(text, *p);
                    825:     }
                    826: }
                    827: 
                    828: 
1.29      frystyk   829: PUBLIC void HText_endAppend (HText * text)
1.1       timbl     830: {
                    831:     new_line(text);
                    832:     
                    833:     if (text->display_on_the_fly) {            /* Not finished? */
                    834:         fill_screen(text, text->display_on_the_fly);   /* Finish it */
                    835:        text->display_on_the_fly = 0;
                    836:        text->next_line = text->last_line;      /* Bug fix after EvA 920117 */
                    837:        text->stale = NO;
                    838:     }
                    839: }
                    840: 
1.45      frystyk   841: PUBLIC void HText_appendObject (HText * text, int element_number,
                    842:                                const BOOL * present, const char ** value)
                    843: {
                    844: }
1.1       timbl     845: 
1.45      frystyk   846: PUBLIC void HText_appendLink (HText * text, HTChildAnchor * anchor,
                    847:                              const BOOL * present, const char ** value)
                    848: {
                    849: }
1.1       timbl     850: 
                    851: /*     Return the anchor associated with this node
                    852: */
1.29      frystyk   853: PUBLIC HTParentAnchor * HText_nodeAnchor (HText * text)
1.1       timbl     854: {
                    855:     return text->node_anchor;
                    856: }
                    857: 
                    858: /*                             GridText specials
                    859: **                             =================
                    860: */
                    861: /*     Return the anchor with index N
                    862: **
                    863: **     The index corresponds to the number we print in the anchor.
                    864: */
1.29      frystyk   865: PUBLIC HTChildAnchor * HText_childNumber (HText * text, int number)
1.1       timbl     866: {
                    867:     TextAnchor * a;
                    868:     for (a = text->first_anchor; a; a = a->next) {
                    869:         if (a->number == number) return a->anchor;
                    870:     }
                    871:     return (HTChildAnchor *)0; /* Fail */
                    872: }
                    873: 
1.29      frystyk   874: PUBLIC void HText_setStale (HText * text)
1.1       timbl     875: {
1.16      frystyk   876:     if (text)
                    877:        text->stale = YES;
1.1       timbl     878: }
                    879: 
1.29      frystyk   880: PUBLIC void HText_refresh (HText * text)
1.1       timbl     881: {
1.16      frystyk   882:     if (text && text->stale)
                    883:        display_page(text, text->top_of_screen);
1.1       timbl     884: }
                    885: 
1.29      frystyk   886: PUBLIC int HText_sourceAnchors (HText * text)
1.1       timbl     887: {
1.16      frystyk   888:     return (text ? text->last_anchor_number : -1);
1.1       timbl     889: }
                    890: 
1.29      frystyk   891: PUBLIC BOOL HText_canScrollUp (HText * text)
1.1       timbl     892: {
1.16      frystyk   893:     return (text && text->top_of_screen != 0);
1.1       timbl     894: }
                    895: 
1.29      frystyk   896: PUBLIC BOOL HText_canScrollDown (HText * text)
1.1       timbl     897: {
1.16      frystyk   898:     return (text && (text->top_of_screen + DISPLAY_LINES -
                    899:                     (text->title ? TITLE_LINES : 0)) < text->lines);
1.1       timbl     900: }
                    901: 
                    902: /*             Scroll actions
                    903: */
1.29      frystyk   904: PUBLIC void HText_scrollTop (HText * text)
1.1       timbl     905: {
                    906:     display_page(text, 0);
                    907: }
                    908: 
1.29      frystyk   909: PUBLIC void HText_scrollDown (HText * text)
1.1       timbl     910: {
                    911:     display_page(text, text->top_of_screen + DISPLAY_LINES -1);
                    912: }
                    913: 
1.29      frystyk   914: PUBLIC void HText_scrollUp (HText * text)
1.1       timbl     915: {
                    916:     display_page(text, text->top_of_screen - DISPLAY_LINES +1);
                    917: }
                    918: 
1.29      frystyk   919: PUBLIC void HText_scrollBottom (HText * text)
1.1       timbl     920: {
                    921:     display_page(text, text->lines - DISPLAY_LINES +1);
                    922: }
                    923: 
                    924: 
                    925: /*             Browsing functions
                    926: **             ==================
                    927: */
                    928: 
                    929: /* Bring to front and highlight it
                    930: */
                    931: 
1.29      frystyk   932: PRIVATE int line_for_char (HText * text, int char_num)
1.1       timbl     933: {
                    934:     int line_number =0;
                    935:     int characters = 0;
                    936:     HTLine * line = text->last_line->next;
                    937:     for(;;) {
                    938:        if (line == text->last_line) return 0;  /* Invalid */
                    939:         characters = characters + line->size + 1;
                    940:        if (characters > char_num) return line_number;
                    941:        line_number ++;
                    942:        line = line->next;
                    943:     }
                    944: }
                    945: 
1.29      frystyk   946: PUBLIC BOOL HText_select (HText * text)
1.1       timbl     947: {
1.13      frystyk   948:     if (text) {
1.1       timbl     949:         HTMainText = text;
                    950:        HTMainAnchor = text->node_anchor;
                    951:        display_page(text, text->top_of_screen);
1.13      frystyk   952:        return YES;
1.1       timbl     953:     }
1.46      frystyk   954:     HTTRACE(SGML_TRACE, "Rendering... Nothing to select!\n");
1.13      frystyk   955:     return NO;
1.1       timbl     956: }
                    957: 
1.29      frystyk   958: PUBLIC BOOL HText_selectAnchor (HText * text, HTChildAnchor * anchor)
1.1       timbl     959: {
                    960:     TextAnchor * a;
                    961: 
                    962:     for(a=text->first_anchor; a; a=a->next) {
                    963:         if (a->anchor == anchor) break;
                    964:     }
                    965:     if (!a) {
1.46      frystyk   966:         HTTRACE(SGML_TRACE, "Rendering... No such anchor in this text!\n");
1.1       timbl     967:         return NO;
                    968:     }
                    969: 
                    970:     if (text != HTMainText) {          /* Comment out by ??? */
                    971:         HTMainText = text;             /* Put back in by tbl 921208 */
                    972:        HTMainAnchor = text->node_anchor;
                    973:     }
                    974: 
                    975:     {
1.20      frystyk   976:        int l = line_for_char(text, a->start);
1.46      frystyk   977:        HTTRACE(SGML_TRACE, "Rendering... Selecting anchor [%d] at char %d, line %d\n" _ 
                    978:                    a->number _ a->start _ l);
1.1       timbl     979: 
                    980:        if ( !text->stale &&
1.20      frystyk   981:            (l >= text->top_of_screen) &&
                    982:            ( l < text->top_of_screen + DISPLAY_LINES-1))
                    983:            return YES;
1.1       timbl     984:         display_page(text, l - (DISPLAY_LINES/3));     /* Scroll to it */
                    985:     }
                    986:     return YES;
                    987: }
                    988:  
                    989: 
                    990: /*             Editing functions               - NOT IMPLEMENTED
                    991: **             =================
                    992: **
                    993: **     These are called from the application. There are many more functions
                    994: **     not included here from the orginal text object.
                    995: */
                    996: 
                    997: /*     Style handling:
                    998: */
                    999: /*     Apply this style to the selection
                   1000: */
1.29      frystyk  1001: PUBLIC void HText_applyStyle (HText *  me, HTStyle * style)
1.1       timbl    1002: {
                   1003:     
                   1004: }
                   1005: 
                   1006: 
                   1007: /*     Update all text with changed style.
                   1008: */
1.29      frystyk  1009: PUBLIC void HText_updateStyle (HText *  me, HTStyle * style)
1.1       timbl    1010: {
                   1011:     
                   1012: }
                   1013: 
                   1014: 
                   1015: /*     Return style of  selection
                   1016: */
1.29      frystyk  1017: PUBLIC HTStyle * HText_selectionStyle (
                   1018:        HText * me,
                   1019:        HTStyleSheet * sheet)
1.1       timbl    1020: {
                   1021:     return 0;
                   1022: }
                   1023: 
                   1024: 
                   1025: /*     Paste in styled text
                   1026: */
1.29      frystyk  1027: PUBLIC void HText_replaceSel (
                   1028:        HText * me,
1.37      frystyk  1029:        const char * aString, 
1.29      frystyk  1030:        HTStyle * aStyle)
1.1       timbl    1031: {
                   1032: }
                   1033: 
                   1034: 
                   1035: /*     Apply this style to the selection and all similarly formatted text
                   1036: **     (style recovery only)
                   1037: */
1.29      frystyk  1038: PUBLIC void HTextApplyToSimilar (HText * me, HTStyle * style)
1.1       timbl    1039: {
                   1040:     
                   1041: }
                   1042: 
                   1043:  
                   1044: /*     Select the first unstyled run.
                   1045: **     (style recovery only)
                   1046: */
1.29      frystyk  1047: PUBLIC void HTextSelectUnstyled (HText * me, HTStyleSheet * sheet)
1.1       timbl    1048: {
                   1049:     
                   1050: }
                   1051: 
                   1052: 
                   1053: /*     Anchor handling:
                   1054: */
1.29      frystyk  1055: PUBLIC void            HText_unlinkSelection (HText * me)
1.1       timbl    1056: {
                   1057:     
                   1058: }
                   1059: 
1.29      frystyk  1060: PUBLIC HTAnchor *      HText_referenceSelected (HText * me)
1.1       timbl    1061: {
                   1062:      return 0;   
                   1063: }
                   1064: 
                   1065: 
                   1066: #ifdef CURSES
1.29      frystyk  1067: PUBLIC int HText_getTopOfScreen (HText * text)
1.1       timbl    1068: {
                   1069:       return text->top_of_screen;
                   1070: }
                   1071: 
1.29      frystyk  1072: PUBLIC int HText_getLines (HText * text)
1.1       timbl    1073: {
                   1074:       return text->lines;
                   1075: }
                   1076: #endif
1.29      frystyk  1077: PUBLIC HTAnchor *      HText_linkSelTo (HText * me, HTAnchor * anchor)
1.1       timbl    1078: {
                   1079:     return 0;
1.47    ! frystyk  1080: }
        !          1081: 
        !          1082: /*     HTML callback functions
        !          1083: */
        !          1084: PUBLIC void LMHText_beginElement (HText * text,
        !          1085:     int elem_num, const BOOL * present, const char ** value)
        !          1086: {
        !          1087:     return;
        !          1088: }
        !          1089: 
        !          1090: PUBLIC void LMHText_endElement (HText * text, int elem_num)
        !          1091: {
        !          1092:     switch (elem_num) {
        !          1093:     case HTML_A:
        !          1094:        LMHText_endAnchor (text);
        !          1095:        break;
        !          1096:     default:
        !          1097:        break;
        !          1098:     }
        !          1099:     return;   
1.1       timbl    1100: }

Webmaster