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

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

Webmaster