shithub: riscv

Download patch

ref: 72a5fbd8ea41d44c5b8d6ab3483c7701e0126726
parent: c8dd01d5f673d62555ac773a61798901784445de
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Thu Sep 3 15:07:48 EDT 2015

mothra: subscript and superscript support

--- a/sys/src/cmd/mothra/html.h
+++ b/sys/src/cmd/mothra/html.h
@@ -29,6 +29,7 @@
 	int pre;		/* in preformatted text? */
 	int font;		/* typeface */
 	int size;		/* point size of text */
+	int sub;		/* < 0 superscript, > 0 subscript */
 	int margin;		/* left margin position */
 	int indent;		/* extra indent at paragraph start */
 	int number;		/* paragraph number */
@@ -195,6 +196,8 @@
 	Tag_strike,
 	Tag_strong,
 	Tag_style,
+	Tag_sub,
+	Tag_sup,
 	Tag_source,
 	Tag_table,	/* rm 3.8.00 */
 	Tag_td,
--- a/sys/src/cmd/mothra/html.syntax.c
+++ b/sys/src/cmd/mothra/html.syntax.c
@@ -73,6 +73,8 @@
 [Tag_strike]	"strike",	END,
 [Tag_strong]	"strong",	END,
 [Tag_style]	"style",	END,
+[Tag_sub]	"sub",		END,
+[Tag_sup]	"sup",		END,
 [Tag_source]	"source",	NOEND,
 [Tag_table]	"table",	END,
 [Tag_td]	"td",		END,
--- a/sys/src/cmd/mothra/libpanel/panel.h
+++ b/sys/src/cmd/mothra/libpanel/panel.h
@@ -13,6 +13,7 @@
 	void *user;		/* user data */
 	int space;		/* how much space before, if no break */
 	int indent;		/* how much space before, after a break */
+	int voff;		/* vertical offset (for subscripts and superscripts) */
 	Image *b;		/* what to display, if nonzero */
 	Panel *p;		/* what to display, if nonzero and b==0 */
 	Font *font;		/* font in which to draw text */
@@ -173,9 +174,9 @@
 /*
  * Rtext constructors & destructor
  */
-Rtext *plrtstr(Rtext **, int, int, Font *, char *, int, void *);
-Rtext *plrtbitmap(Rtext **, int, int, Image *, int, void *);
-Rtext *plrtpanel(Rtext **, int, int, Panel *, void *);
+Rtext *plrtstr(Rtext **, int, int, int, Font *, char *, int, void *);
+Rtext *plrtbitmap(Rtext **, int, int, int, Image *, int, void *);
+Rtext *plrtpanel(Rtext **, int, int, int, Panel *, void *);
 void plrtfree(Rtext *);
 void plrtseltext(Rtext *, Rtext *, Rtext *);
 char *plrtsnarftext(Rtext *);
--- a/sys/src/cmd/mothra/libpanel/rtext.c
+++ b/sys/src/cmd/mothra/libpanel/rtext.c
@@ -13,7 +13,7 @@
 #define LEAD	4	/* extra space between lines */
 #define BORD	2	/* extra border for images */
 
-Rtext *pl_rtnew(Rtext **t, int space, int indent, Image *b, Panel *p, Font *f, char *s, int flags, void *user){
+Rtext *pl_rtnew(Rtext **t, int space, int indent, int voff, Image *b, Panel *p, Font *f, char *s, int flags, void *user){
 	Rtext *new;
 	new=pl_emalloc(sizeof(Rtext));
 	new->flags=flags;
@@ -20,6 +20,7 @@
 	new->user=user;
 	new->space=space;
 	new->indent=indent;
+	new->voff=voff;
 	new->b=b;
 	new->p=p;
 	new->font=f;
@@ -34,14 +35,14 @@
 	(*t)->last=new;
 	return new;
 }
-Rtext *plrtpanel(Rtext **t, int space, int indent, Panel *p, void *user){
-	return pl_rtnew(t, space, indent, 0, p, 0, 0, 1, user);
+Rtext *plrtpanel(Rtext **t, int space, int indent, int voff, Panel *p, void *user){
+	return pl_rtnew(t, space, indent, voff, 0, p, 0, 0, 1, user);
 }
-Rtext *plrtstr(Rtext **t, int space, int indent, Font *f, char *s, int flags, void *user){
-	return pl_rtnew(t, space, indent, 0, 0, f, s, flags, user);
+Rtext *plrtstr(Rtext **t, int space, int indent, int voff, Font *f, char *s, int flags, void *user){
+	return pl_rtnew(t, space, indent, voff, 0, 0, f, s, flags, user);
 }
-Rtext *plrtbitmap(Rtext **t, int space, int indent, Image *b, int flags, void *user){
-	return pl_rtnew(t, space, indent, b, 0, 0, 0, flags, user);
+Rtext *plrtbitmap(Rtext **t, int space, int indent, int voff, Image *b, int flags, void *user){
+	return pl_rtnew(t, space, indent, voff, b, 0, 0, 0, flags, user);
 }
 void plrtfree(Rtext *t){
 	Rtext *next;
@@ -102,6 +103,7 @@
 				d=tp->font->height-a;
 				w=tp->wid=stringwidth(tp->font, tp->text);
 			}
+			a-=tp->voff,d+=tp->voff;
 			if(x+w+space>wid) break;
 			if(a>ascent) ascent=a;
 			if(d>descent) descent=d;
@@ -128,6 +130,7 @@
 		for(;;){
 			t->topy=topy;
 			t->r.min.x=p.x;
+			p.y+=t->voff;
 			if(t->b){
 				t->r.max.y=p.y+BORD;
 				t->r.min.y=p.y-(t->b->r.max.y-t->b->r.min.y)-BORD;
@@ -143,6 +146,7 @@
 				t->r.max.y=t->r.min.y+t->font->height;
 				p.x+=t->wid;
 			}
+			p.y-=t->voff;
 			t->r.max.x=p.x;
 			t->nextline=eline;
 			t=t->next;
--- a/sys/src/cmd/mothra/mothra.c
+++ b/sys/src/cmd/mothra/mothra.c
@@ -1124,7 +1124,7 @@
 			t->next = nil;
 			ap=emalloc(sizeof(Action));
 			ap->link = strdup(a->link);
-			plrtstr(&t->next, 0, 0, t->font, strdup("->"), PL_HOT, ap);
+			plrtstr(&t->next, 0, 0, 0, t->font, strdup("->"), PL_HOT, ap);
 			t->next->next = x;
 		} else {
 			if(x) {
--- a/sys/src/cmd/mothra/rdhtml.c
+++ b/sys/src/cmd/mothra/rdhtml.c
@@ -33,7 +33,8 @@
 	"terminus/unicode.16", 0, 0,
 	"terminus/unicode.18", 0, 0,
 };
-Fontdata *pl_whichfont(int f, int s){
+
+Font *pl_whichfont(int f, int s, int *space){
 	char name[NNAME];
 
 	assert(f >= 0 && f < 4);
@@ -45,14 +46,16 @@
 		if(fontlist[f][s].font==0) fontlist[f][s].font=font;
 		fontlist[f][s].space=stringwidth(fontlist[f][s].font, "0");
 	}
-	return &fontlist[f][s];
-	
+	if(space)
+		*space = fontlist[f][s].space;
+	return fontlist[f][s].font;
 }
+
 void getfonts(void){
 	int f, s;
 	for(f=0;f!=4;f++)
 		for(s=0;s!=4;s++)
-			pl_whichfont(f, s);
+			pl_whichfont(f, s, nil);
 }
 void pl_pushstate(Hglob *g, int t){
 	++g->state;
@@ -80,7 +83,7 @@
 }
 
 void pl_linespace(Hglob *g){
-	plrtbitmap(&g->dst->text, 1000000, 0, linespace, 0, 0);
+	plrtbitmap(&g->dst->text, 1000000, 0, 0, linespace, 0, 0);
 	g->para=0;
 	g->linebrk=0;
 }
@@ -93,7 +96,7 @@
 	if(cistrstr(str, "%"))
 		return 0;
 	if(cistrstr(str, "em")){
-		p=stringsize(pl_whichfont(g->state->font, g->state->size)->font, "M");
+		p=stringsize(pl_whichfont(g->state->font, g->state->size, nil), "M");
 		return floor(f*((dir==HORIZ) ? p.x : p.y));
 	}
 	return floor(f);
@@ -100,8 +103,8 @@
 }
 
 void pl_htmloutput(Hglob *g, int nsp, char *s, Field *field){
-	Fontdata *f;
-	int space, indent, flags;
+	Font *f;
+	int space, indent, flags, voff;
 	Action *ap;
 	if(g->state->tag==Tag_title
 /*	|| g->state->tag==Tag_textarea */
@@ -115,8 +118,13 @@
 		}
 		return;
 	}
-	f=pl_whichfont(g->state->font, g->state->size);
-	space=f->space;
+	voff = 0;
+	f=pl_whichfont(g->state->font, g->state->size, &space);
+	if(g->state->sub){
+		voff = g->state->sub * f->ascent / 2;
+		g->state->size = SMALL;
+		f=pl_whichfont(g->state->font, g->state->size, &space);
+	}
 	indent=g->state->margin;
 	if(g->para){
 		space=1000000;
@@ -160,7 +168,7 @@
 		flags |= PL_HOT;
 	if(g->state->strike)
 		flags |= PL_STR;
-	plrtstr(&g->dst->text, space, indent, f->font, strdup(s), flags, ap);
+	plrtstr(&g->dst->text, space, indent, voff, f, strdup(s), flags, ap);
 	g->para=0;
 	g->linebrk=0;
 	g->dst->changed=1;
@@ -636,6 +644,7 @@
 	int c;
 	g->state->font=CWIDTH;
 	g->state->size=NORMAL;
+	g->state->sub = 0;
 	elp=&line[NLINE-UTFmax-1];
 	lp=line;
 	for(;;){
@@ -665,6 +674,7 @@
 	g.state->tag=Tag_html;
 	g.state->font=CWIDTH;
 	g.state->size=NORMAL;
+	g.state->sub=0;
 	g.state->pre=0;
 	g.state->image=0;
 	g.state->link=0;
@@ -701,6 +711,7 @@
 	g.state->tag=Tag_html;
 	g.state->font=ROMAN;
 	g.state->size=NORMAL;
+	g.state->sub=0;
 	g.state->pre=0;
 	g.state->image=0;
 	g.state->link=0;
@@ -914,6 +925,12 @@
 		case Tag_del:
 			g.state->strike=1;
 			break;
+		case Tag_sub:
+			g.state->sub++;
+			break;
+		case Tag_sup:
+			g.state->sub--;
+			break;
 		case Tag_blockquot:
 			g.spacc=0;
 			g.linebrk=1;
@@ -1026,7 +1043,7 @@
 			break;
 		case Tag_hr:
 			g.spacc=0;
-			plrtbitmap(&g.dst->text, 1000000, g.state->margin, hrule, 0, 0);
+			plrtbitmap(&g.dst->text, 1000000, g.state->margin, 0, hrule, 0, 0);
 			break;
 		case Tag_key:
 			htmlerror(g.name, g.lineno, "<key> deprecated");
@@ -1063,7 +1080,7 @@
 				g.linebrk=0;
 				g.spacc=-1;
 				plrtbitmap(&g.dst->text, 100000,
-					g.state->margin+g.state->indent, bullet, 0, 0);
+					g.state->margin+g.state->indent, 0, bullet, 0, 0);
 				break;
 			}
 			break;