10 typedef struct Fontdata Fontdata;
16 "lucidasans/unicode.7", 0, 0,
17 "lucidasans/unicode.8", 0, 0,
18 "lucidasans/unicode.10", 0, 0,
19 "lucidasans/unicode.13", 0, 0,
21 "lucidasans/italicunicode.7", 0, 0,
22 "lucidasans/italicunicode.8", 0, 0,
23 "lucidasans/italicunicode.10", 0, 0,
24 "lucidasans/italicunicode.13", 0, 0,
26 "lucidasans/boldunicode.7", 0, 0,
27 "lucidasans/boldunicode.8", 0, 0,
28 "lucidasans/boldunicode.10", 0, 0,
29 "lucidasans/boldunicode.13", 0, 0,
31 "lucidasans/typeunicode.7", 0, 0,
32 "pelm/unicode.8", 0, 0,
33 "lucidasans/typeunicode.12", 0, 0,
34 "lucidasans/typeunicode.16", 0, 0,
36 Fontdata *pl_whichfont(int f, int s){
39 assert(f >= 0 && f < 4);
40 assert(s >= 0 && s < 4);
42 if(fontlist[f][s].font==0){
43 snprint(name, sizeof(name), "/lib/font/bit/%s.font", fontlist[f][s].name);
44 fontlist[f][s].font=openfont(display, name);
45 if(fontlist[f][s].font==0) fontlist[f][s].font=font;
46 fontlist[f][s].space=stringwidth(fontlist[f][s].font, "0");
48 return &fontlist[f][s];
57 void pl_pushstate(Hglob *g, int t){
59 if(g->state==&g->stack[NSTACK]){
60 htmlerror(g->name, g->lineno, "stack overflow at <%s>", tag[t].name);
63 g->state[0]=g->state[-1];
66 void pl_linespace(Hglob *g){
67 plrtbitmap(&g->dst->text, 1000000, 0, linespace, 0, 0);
75 int strtolength(Hglob *g, int dir, char *str)
80 if(cistrstr(str, "px"))
82 if(cistrstr(str, "%"))
83 return floor(f*((dir==HORIZ) ? Dx(g->dst->text->r) : Dy(g->dst->text->r))/100);
84 if(cistrstr(str, "em")){
86 z = stringsize(g->dst->text->font, "M");
87 return floor(f*((dir==HORIZ) ? z.x : z.y));
92 void pl_htmloutput(Hglob *g, int nsp, char *s, Field *field){
96 if(g->state->tag==Tag_title
97 /* || g->state->tag==Tag_textarea */
98 || g->state->tag==Tag_select){
100 if(g->tp!=g->text && g->tp!=g->etext && g->tp[-1]!=' ')
102 while(g->tp!=g->etext && *s) *g->tp++=*s++;
103 if(g->state->tag==Tag_title) g->dst->changed=1;
108 f=pl_whichfont(g->state->font, g->state->size);
110 indent=g->state->margin;
113 indent+=g->state->indent;
119 if(g->state->image[0]==0 && g->state->link[0]==0 && g->state->name[0]==0 && field==0)
122 ap=mallocz(sizeof(Action), 1);
124 if(g->state->image[0])
125 ap->image = strdup(g->state->image);
126 if(g->state->link[0])
127 ap->link = strdup(g->state->link);
128 if(g->state->name[0])
129 ap->name = strdup(g->state->name);
130 ap->ismap=g->state->ismap;
131 ap->width=g->state->width;
132 ap->height=g->state->height;
137 if(indent<0) indent=0;
138 if(g->state->pre && s[0]=='\t'){
150 plrtstr(&g->dst->text, space, indent, f->font, strdup(s),
151 g->state->link[0] || g->state->image[0], ap);
158 * Buffered read, no translation
161 int pl_bread(Hglob *g){
164 if(g->hbufp==g->ehbuf){
165 n=read(g->hfd, g->hbuf, NHBUF);
168 snprint(err, sizeof(err), "%r reading %s", g->name);
169 pl_htmloutput(g, 1, err, 0);
178 if(c=='\n') g->lineno++;
182 * Read a character, translating \r\n, \n\r, \r and \n into \n
184 int pl_readc(Hglob *g){
205 void pl_putback(Hglob *g, int c){
206 if(g->npeekc==NPEEKC) htmlerror(g->name, g->lineno, "too much putback!");
207 else if(c!=EOF) g->peekc[g->npeekc++]=c;
209 int pl_nextc(Hglob *g){
214 if(g->heof) return EOF;
215 if(g->npeekc!=0) return g->peekc[--g->npeekc];
223 if('a'<=c && c<='z' || 'A'<=c && c<='Z') return STAG;
227 if(c=='!' || 'a'<=c && c<='z' || 'A'<=c && c<='Z' || c=='?') return STAG;
230 if(c=='>') return ETAG;
235 if(fullrune(crune, n)){
236 chartorune(&r, crune);
246 return c=='#' || 'a'<=c && c<='z' || 'A'<=c && c<='Z' || '0'<=c && c<='9';
249 * remove entity references, in place.
251 * This doesn't work if removing an entity reference can lengthen the string!
252 * Fortunately, this doesn't happen.
254 void pl_rmentities(Hglob *g, char *s){
262 && ((*s=='#' && strchr("0123456789Xx", s[1]))
263 || 'a'<=*s && *s<='z'
264 || 'A'<=*s && *s<='Z')){
266 while(entchar(*s)) s++;
270 if(strcmp(u, "lt") == 0)
272 else if(strcmp(u, "gt") == 0)
274 else if(strcmp(u, "quot") == 0)
276 else if(strcmp(u, "apos") == 0)
278 else if(strcmp(u, "amp") == 0)
292 * Skip over white space
294 char *pl_white(char *s){
295 while(*s==' ' || *s=='\t' || *s=='\n' || *s=='\r') s++;
299 * Skip over HTML word
301 char *pl_word(char *s){
302 if ('a'<=*s && *s<='z' || 'A'<=*s && *s<='Z') {
304 while('a'<=*s && *s<='z' || 'A'<=*s && *s<='Z' || '0'<=*s && *s<='9' || *s=='-' || *s=='.') s++;
309 * Skip to matching quote
311 char *pl_quote(char *s){
314 while(*s!=q && *s!='\0') s++;
317 void pl_dnl(char *s){
319 for(t=s;*s;s++) if(*s!='\r' && *s!='\n') *t++=*s;
322 void pl_tagparse(Hglob *g, char *str){
323 char *s, *t, *name, c;
328 if(str[0]=='!'){ /* test should be strncmp(str, "!--", 3)==0 */
333 if(str[0]=='/') str++;
336 if(*s!=' ' && *s!='\n' && *s!='\t' && *s!='\0'){
337 htmlerror(g->name, g->lineno, "bad tag name in %s", str);
341 if(*s!='\0') *s++='\0';
342 for(t=name;t!=s;t++) if('A'<=*t && *t<='Z') *t+='a'-'A';
344 * Binary search would be faster here
346 for(tagp=tag;tagp->name;tagp++) if(strcmp(name, tagp->name)==0) break;
348 if(g->tag==Tag_end) htmlerror(g->name, g->lineno, "no tag %s", name);
360 for(s=ap->name;*s;s++) if('A'<=*s && *s<='Z') *s+='a'-'A';
363 if(*s=='\'' || *s=='"'){
367 htmlerror(g->name, g->lineno,
368 "No terminating quote in rhs of attribute %s",
377 /* read up to white space or > */
379 while(*s!=' ' && *s!='\t' && *s!='\n' && *s!='\0') s++;
380 if(*s!='\0') *s++='\0';
382 pl_rmentities(g, ap->value);
388 if(ap==&g->attr[NATTR-1])
389 htmlerror(g->name, g->lineno, "too many attributes!");
393 int pl_getcomment(Hglob *g){
395 if((c=pl_nextc(g))=='-' && (c=pl_nextc(g))=='-'){
396 /* <!-- eats everything until --> or EOF */
398 while((c=pl_nextc(g))!='-' && c!=EOF)
402 if((c=pl_nextc(g))=='-'){
403 while((c=pl_nextc(g))=='-')
405 if(c==ETAG || c==EOF)
410 /* <! eats everything until > or EOF */
411 while(c!=ETAG && c!=EOF)
415 htmlerror(g->name, g->lineno, "EOF in comment");
421 int lrunetochar(char *p, int v)
426 return runetochar(p, &r);
430 * Read a start or end tag -- the caller has read the initial <
432 int pl_gettag(Hglob *g){
436 if((c=pl_nextc(g))=='!' || c=='?')
437 return pl_getcomment(g);
439 while((c=pl_nextc(g))!=ETAG && c!=EOF)
440 if(tokp!=&g->token[NTOKEN-3]) tokp += lrunetochar(tokp, c);
442 if(c==EOF) htmlerror(g->name, g->lineno, "EOF in tag");
443 pl_tagparse(g, g->token);
444 if(g->token[0]!='/') return TAG;
445 if(g->attr[0].name!=0)
446 htmlerror(g->name, g->lineno, "end tag should not have attributes");
450 * The next token is a tag, an end tag or a sequence of
451 * non-white characters.
452 * If inside <pre>, newlines are converted to <br> and spaces are preserved.
453 * Otherwise, spaces and newlines are noted and discarded.
455 int pl_gettoken(Hglob *g){
458 if(g->state->pre) switch(c=pl_nextc(g)){
459 case STAG: return pl_gettag(g);
460 case EOF: return EOF;
462 pl_tagparse(g, "br");
467 if(tokp!=&g->token[NTOKEN-3]) tokp += lrunetochar(tokp, c);
470 while(c!='\t' && c!='\n' && c!=STAG && c!=EOF){
472 if(tokp!=&g->token[NTOKEN-3]) tokp += lrunetochar(tokp, c);
476 pl_rmentities(g, g->token);
482 while((c=pl_nextc(g))==' ' || c=='\t' || c=='\n')
486 case STAG: return pl_gettag(g);
487 case EOF: return EOF;
492 if(tokp!=&g->token[NTOKEN-3]) tokp += lrunetochar(tokp, c);
494 }while(c!=' ' && c!='\t' && c!='\n' && c!=STAG && c!=EOF);
496 pl_rmentities(g, g->token);
503 char *pl_getattr(Pair *attr, char *name){
504 for(;attr->name;attr++)
505 if(strcmp(attr->name, name)==0)
509 int pl_hasattr(Pair *attr, char *name){
510 for(;attr->name;attr++)
511 if(strcmp(attr->name, name)==0)
515 void plaintext(Hglob *g){
519 g->state->font=CWIDTH;
520 g->state->size=NORMAL;
526 if(c=='\n' || lp==elp){
529 pl_htmloutput(g, 0, line, 0);
533 do *lp++=' '; while(lp!=elp && utfnlen(line, lp-line)%8!=0);
536 lp += lrunetochar(lp, c);
541 pl_htmloutput(g, 0, line, 0);
544 void plrdplain(char *name, int fd, Www *dst){
547 g.state->tag=Tag_html;
548 g.state->font=CWIDTH;
549 g.state->size=NORMAL;
561 g.ehbuf=g.hbufp=g.hbuf;
569 g.etext=g.text+NTITLE-1;
572 strncpy(g.text, name, NTITLE);
576 void plrdhtml(char *name, int fd, Www *dst){
584 g.state->tag=Tag_html;
586 g.state->size=NORMAL;
600 g.ehbuf=g.hbufp=g.hbuf;
608 g.etext=g.text+NTITLE-1;
613 for(;;) switch(pl_gettoken(&g)){
615 switch(tag[g.tag].action){
617 for(sp=g.state;sp!=g.stack && sp->tag!=g.tag;--sp);
619 pl_pushstate(&g, g.tag);
621 for(;g.state!=sp;--g.state)
622 if(tag[g.state->tag].action!=OPTEND)
623 htmlerror(g.name, g.lineno,
624 "end tag </%s> missing",
625 tag[g.state->tag].name);
628 pl_pushstate(&g, g.tag);
633 htmlerror(g.name, g.lineno,
634 "unimplemented tag <%s>", tag[g.tag].name);
636 case Tag_end: /* unrecognized start tag */
641 if(str=pl_getattr(g.attr, "src"))
642 strncpy(g.state->image, str, sizeof(g.state->image));
643 g.state->ismap=pl_hasattr(g.attr, "ismap");
644 if(str=pl_getattr(g.attr, "width"))
645 g.state->width = strtolength(&g, HORIZ, str);
646 if(str=pl_getattr(g.attr, "height"))
647 g.state->height = strtolength(&g, VERT, str);
648 str=pl_getattr(g.attr, "alt");
650 if(g.state->image[0])
655 pl_htmloutput(&g, 0, str, 0);
674 if(g.state->table==1){
684 if(str=pl_getattr(g.attr, "href"))
685 strncpy(g.state->link, str, sizeof(g.state->link));
686 if(str=pl_getattr(g.attr, "name")){
687 strncpy(g.state->name, str, sizeof(g.state->name));
688 pl_htmloutput(&g, 0, "", 0);
692 pl_htmloutput(&g, 0, "FRAME: ", 0);
693 if(str=pl_getattr(g.attr, "src"))
694 strncpy(g.state->link, str, sizeof(g.state->link));
695 if(str=pl_getattr(g.attr, "name"))
696 strncpy(g.state->name, str, sizeof(g.state->name));
699 pl_htmloutput(&g, 0, str, 0);
709 g.state->size=NORMAL;
727 g.state->size=NORMAL;
740 g.state->font=ITALIC;
741 g.state->size=NORMAL;
744 g.state->font=CWIDTH;
745 g.state->size=NORMAL;
754 htmlerror(g.name, g.lineno, "<dfn> deprecated");
756 g.state->size=NORMAL;
760 g.state->size=NORMAL;
774 htmlerror(g.name, g.lineno, "<u> deprecated");
778 g.state->font=ITALIC;
783 g.state->size=ENORMOUS;
784 g.state->margin+=100;
790 g.state->size=ENORMOUS;
796 g.state->font=ITALIC;
797 g.state->size=ENORMOUS;
810 g.state->font=ITALIC;
823 plrtbitmap(&g.dst->text, 1000000, g.state->margin, hrule, 0, 0);
826 htmlerror(g.name, g.lineno, "<key> deprecated");
828 g.state->font=CWIDTH;
842 switch(g.state->tag){
844 htmlerror(g.name, g.lineno, "can't have <li> in <%s>",
845 tag[g.state->tag].name);
846 case Tag_dir: /* supposed to be multi-columns, can't do! */
852 snprint(buf, sizeof(buf), "%2d ", ++g.state->number);
853 pl_htmloutput(&g, 0, buf, 0);
859 plrtbitmap(&g.dst->text, 100000,
860 g.state->margin+g.state->indent, bullet, 0, 0);
871 htmlerror(g.name, g.lineno, "<%s> deprecated", tag[g.tag].name);
876 g.state->font=CWIDTH;
877 g.state->size=NORMAL;
881 g.state->font=CWIDTH;
882 g.state->size=NORMAL;
885 g.text=dst->title+strlen(dst->title);
887 g.etext=dst->title+NTITLE-1;
901 * ignore the content of these tags, eat tokens until we
902 * reach a matching endtag.
906 switch(pl_gettoken(&g)){
923 * If the end tag doesn't match the top, we try to uncover a match
926 if(g.state->tag!=g.tag){
928 for(sp=g.state;sp!=g.stack;--sp){
931 if(tag[g.state->tag].action!=OPTEND) tagerr++;
935 htmlerror(g.name, g.lineno,
936 "end tag mismatch <%s>...</%s>, ignored",
937 tag[g.state->tag].name, tag[g.tag].name);
941 htmlerror(g.name, g.lineno,
942 "end tag mismatch <%s>...</%s>, "
943 "intervening tags popped",
944 tag[g.state->tag].name, tag[g.tag].name);
948 else if(g.state==g.stack)
949 htmlerror(g.name, g.lineno, "end tag </%s> at stack bottom",
983 if(g.state->table==0)
992 pl_htmloutput(&g, g.nsp, g.token, 0);
995 for(;g.state!=g.stack;--g.state)
996 if(tag[g.state->tag].action!=OPTEND)
997 htmlerror(g.name, g.lineno,
998 "missing </%s> at EOF", tag[g.state->tag].name);
1001 getpix(dst->text, dst);