⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pslib.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
/* 		bits=1<<bits;/* 	else{/* 		bits=1;/* 		def_font_type=bits;/* 		curfonttype=def_font_type;/* 	}/* 	font=font[2:];/* 	for(i:=0;i<len remap;i++){/* 		(tkfont,psfont)=remap[i];/* 		if (tkfont==font)/* 			break;/* 	}/* 	if (i==len remap)/* 		psfont="Times-Roman";/* 	(font,nil)=str->splitr(font,".");/* 	(nil,font)=str->splitr(font[0:len font-1],".");/* 	(fsize,nil):=str->toint(font,10);/* 	fsize=(PTPI*3*fsize)/(2*PXPI);/* 	enc_font:="f"+string bits+"."+string fsize;/* 	ps_func:="/"+enc_font+" /"+psfont+" findfont "+string fsize+/* 							" scalefont def\n";/* 	sy_font:="sy"+string fsize;/* 	xtra_func:="/"+sy_font+" /Symbol findfont "+string fsize+/* 							" scalefont def\n";/* 	for(i=0;i<len font_arr;i++){/* 		(j,font)=font_arr[i];/* 		if (j==-1) break;/* 	}/* 	if (j==len font_arr)/* 		return "Error";/* 	font_arr[i]=(bits,enc_font);/* 	if (bits==1)/* 		def_font=enc_font;/* 	curfont++;/* 	retval+= ps_func;/* 	retval+= xtra_func;	/* 	return retval;/* }/* /* deffont() : string/* {/* 	return def_font;/* }/* 	/* getline(k : int,  input : string) : (array of iteminfo, string)/* {/* 	lineval,args : string;/* 	j, nb : int;/* 	lw:=0;/* 	wid:=0;/* 	flags:=0;/* 	item_arr := array[32] of {* => iteminfo(-1,-1,-1,-1,-1,-1,"")};/* 	curitem:=0;/* 	while(input!=nil){/* 		(nil,input)=str->splitl(input,"[");/* 		if (input==nil)/* 			break;/* 		com:=input[1];/* 		input=input[2:];/* 		case com {/* 			'A' =>/* 				nb=0;/* 				# get the width of the item/* 				(wid,input)=str->toint(input,10);/* 				wid=(wid*PTPI)/PXPI;/* 				if (input[0]!='{')/* 					return (nil, sys->sprint(/* 						"line %d item %d Bad Syntax : '{' expected",/* 							k,curitem));/* 				# get the args./* 				(args,input)=str->splitl(input,"}");/* 				# get the flags./* 				# assume there is only one int flag../* 				(flags,args)=str->toint(args[1:],16);/* 				if (args!=nil && debug){/* 					sys->print("line %d item %d extra flags=%s\n",/* 							k,curitem,args);/* 				}/* 				if (flags<1024) flags=1;/* 				item_arr[curitem].font=flags;/* 				item_arr[curitem].offset=lw;/* 				item_arr[curitem].width=wid;/* 				lw+=wid;/* 				for(j=1;j<len input;j++){/* 					if ((input[j]==')')||(input[j]=='('))/* 							lineval[len lineval]='\\';/* 					if (input[j]=='[')/* 						nb++;/* 					if (input[j]==']')/* 						if (nb==0)/* 							break;/* 						else /* 							nb--;/* 					lineval[len lineval]=input[j];/* 				}/* 				if (j<len input)/* 					input=input[j:];/* 				item_arr[curitem].buf=lineval;/* 				item_arr[curitem].line=k;/* 				item_arr[curitem].itype=ASCII;/* 				curitem++;/* 				lineval="";/* 			'R' =>/* 				nb=0;/* 				# get the width of the item/* 				(wid,input)=str->toint(input,10);/* 				wid=(wid*PTPI)/PXPI;/* 				if (input[0]!='{')/* 					return (nil, "Bad Syntax : '{' expected");/* 				# get the args./* 				(args,input)=str->splitl(input,"}");/* 				# get the flags./* 				# assume there is only one int flag../* 				(flags,args)=str->toint(args[1:],16);/* 				if (args!=nil && debug){/* 					sys->print("line %d item %d Bad Syntax args=%s",/* 							k,curitem,args);/* 				}/* 				item_arr[curitem].font=flags;/* 				item_arr[curitem].offset=lw;/* 				item_arr[curitem].width=wid;/* 				lw+=wid;/* 				for(j=1;j<len input;j++){/* 					if (input[j]=='[')/* 						nb++;/* 					if (input[j]==']')/* 						if (nb==0)/* 							break;/* 						else /* 							nb--;/* 					case input[j] {/* 						8226 => # bullet/* 							lineval+="\\267 ";/* 						169 =>  # copyright/* 							lineval+="\\251 ";/* 							curitem++;			/* 						* =>/* 							lineval[len lineval]=input[j];/* 					}/* 				}/* 				if (j>len input)/* 					input=input[j:];/* 				item_arr[curitem].buf=lineval;/* 				item_arr[curitem].line=k;/* 				item_arr[curitem].itype=RUNE;/* 				curitem++;/* 				lineval="";/* 			'N' or 'C'=>/* 				# next item/* 				for(j=0;j<len input;j++)/* 					if (input[j]==']')/* 						break;/* 				if (j>len input)/* 					input=input[j:];/* 			'T' =>/* 				(wid,input)=str->toint(input,10);/* 				wid=(wid*PTPI)/PXPI;/* 				item_arr[curitem].offset=lw;/* 				item_arr[curitem].width=wid;/* 				lw+=wid;/* 				lineval[len lineval]='\t';/* 				# next item/* 				for(j=0;j<len input;j++)/* 					if (input[j]==']')/* 						break;/* 				if (j>len input)/* 					input=input[j:];/* 				item_arr[curitem].buf=lineval;/* 				item_arr[curitem].line=k;/* 				item_arr[curitem].itype=ASCII;/* 				curitem++;/* 				lineval="";/* 			'W' =>/* 				(wid,input)=str->toint(input,10);/* 				wid=(wid*PTPI)/PXPI;/* 				item_arr[curitem].offset=lw;/* 				item_arr[curitem].width=wid;/* 				item_arr[curitem].itype=IMAGE;/* 				lw+=wid;/* 				# next item/* 				for(j=1;j<len input;j++){/* 					if (input[j]==']')/* 						break;/* 					lineval[len lineval]=input[j];/* 				}/* 				item_arr[curitem].buf=lineval;/* 				if (j>len input)/* 					input=input[j:];/* 				curitem++;/* 				lineval="";/* 			* =>/* 				# next item/* 				for(j=0;j<len input;j++)/* 					if (input[j]==']')/* 						break;/* 				if (j>len input)/* 					input=input[j:];/* 				/* 		}/* 	}/* 	return (item_arr[0:curitem], "");	/* }*/voidcmap2ascii85(uchar *b, uchar *c) {	int i;	unsigned long i1;/*	fprintf(stderr, "addr=0x%x %x %x %x %x\n", b, b[0], b[1], b[2], b[3]); */	b--;	/* one-index b */	c--;	/* one-index c */	i1 = (b[1]<<24)+(b[2]<<16)+(b[3]<<8)+b[4];	if(i1 == 0){		c[1] = 'z';		c[2] = '\0';		return;	}	for(i=0; i<=4; i++){		c[5-i] = '!' + (i1 % 85);		i1 /= 85;	}	c[6] = '\0';}static uchar *arr = nil;ulong	onesbits = ~0;voidimagebits(Biobuf *ioutb, Memimage *im){	int spb;	int bitoff;	int j, n, n4, i, bpl, nrest;	int lsf;	uchar c85[6], *data, *src, *dst;	Memimage *tmp;	Rectangle r;	tmp = nil;	if (debug)		fprint(2, "imagebits, r=%d %d %d %d, depth=%d\n",			im->r.min.x, im->r.min.y, im->r.max.x, im->r.max.y, im->depth);	width = Dx(im->r);	height = Dy(im->r);	bps = im->depth;	/* # bits per sample */	bitoff = 0;		/* # bit offset of beginning sample within first byte */	if (bps < 8) {		spb = 8 / bps;		bitoff = (im->r.min.x % spb) * bps;	}	if (bitoff != 0) {/* 		# Postscript image wants beginning of line at beginning of byte */		r = im->r;		r.min.x -= bitoff/im->depth;		r.max.x -= bitoff/im->depth;		tmp = allocmemimage(r, im->chan);		if(tmp == nil){			fprint(2, "p9bitpost: allocmemimage failed: %r\n");			exits("alloc");		}		memimagedraw(tmp, r, im, im->r.min, nil, ZP, S);		im = tmp;	}	lsf = 0;	/* compact data to remove word-boundary padding */	bpl = bytesperline(im->r, im->depth);	n = bpl*Dy(im->r);	data = malloc(n);	if(data == nil){		fprint(2, "p9bitpost: malloc failed: %r\n");		exits("malloc");	}	for(i=0; i<Dy(im->r); i++){		/* memmove(data+bpl*i, byteaddr(im, Pt(im->r.min.x, im->r.min.y+i)), bpl); with inversion */		dst = data+bpl*i;		src = byteaddr(im, Pt(im->r.min.x, im->r.min.y+i));		for(j=0; j<bpl; j++)			*dst++ = 255 - *src++;	}	n4 = (n / 4) * 4;	for (i = 0; i < n4; i += 4){		cmap2ascii85(data+i, c85);		lsf += strlen((char *)c85);		Bprint(ioutb, "%s", c85);		if (lsf > 74) {			Bprint(ioutb, "\n");			lsf = 0;		}	}	nrest = n - n4;	if (nrest != 0) {		uchar foo[4];		for (i=0; i<nrest; i++)			foo[i] = data[n4+i];		for (i=nrest; i<4; i++)			foo[i] = '\0';		cmap2ascii85(foo, c85);		if (strcmp((char *)c85, "z") == 0 )			strcpy((char *)c85, "!!!!!");		Bprint(ioutb, "%.*s", nrest+1, c85);	}	Bprint(ioutb, "\n~>");	Bprint(ioutb, "\n");	freememimage(tmp);}intimage2psfile(int fd, Memimage *im, int dpi) {	Rectangle r;	Rectangle bbox;	int e;	int xmargin = 36;	int ymargin = 36;	double paperaspectratio;	double imageaspectratio;	Biobuf ioutb;	Memimage *tmp;	if(im->depth >= 8 && im->chan != CMAP8 && im->chan != GREY8){		/*		 * the postscript libraries can only handle [1248]-bit grey, 8-bit cmap,		 * and 24-bit color, so convert.		 */		tmp = allocmemimage(im->r, strtochan("b8g8r8"));		if(tmp == nil)			return 1;		memimagedraw(tmp, tmp->r, im, im->r.min, nil, ZP, S);		freememimage(im);		im = tmp;	}	Binit(&ioutb, fd, OWRITE); 	r = im->r;	width = Dx(r);	height = Dy(r);	imageaspectratio = (double) width / (double) height;	if (landscape) {		paperaspectratio = ((double)paperlength - (ymargin * 2)) / ((double)paperwidth - (xmargin * 2));		if (dpi > 0) {			iwidth = width * 72 / dpi;			iheight = height * 72 / dpi;		} else if (imageaspectratio > paperaspectratio) {			iwidth = paperlength - (ymargin * 2);			iheight = iwidth / imageaspectratio;		} else {			iheight = paperwidth - (xmargin * 2);			iwidth  = iheight * imageaspectratio;		}		xstart = paperwidth - xmargin - (iheight * ymagnification);		ystart = paperlength - ymargin;		rotation = -90;	} else {		paperaspectratio = ((double)paperwidth - (xmargin * 2)) / ((double)paperlength - (ymargin * 2));		if (dpi > 0) {			iwidth = width * 72 / dpi;			iheight = height * 72 / dpi;		} else if (imageaspectratio > paperaspectratio) {			iwidth = paperwidth - (xmargin * 2);			iheight = iwidth / imageaspectratio;		} else {			iheight = paperlength - (ymargin * 2);			iwidth  = iheight * imageaspectratio;		}		xstart = xmargin;		ystart = paperlength - ymargin - (iheight * ymagnification);		rotation = 0;	}	bbox = Rect(xstart,ystart,xstart+iwidth,ystart+iheight);	e = preamble(&ioutb, bbox);	if(e != 0)		return e;	Bprint(&ioutb, "%%%%Page: 1\n%%%%BeginPageSetup\n");	Bprint(&ioutb, "/pgsave save def\n");	Bprint(&ioutb, "%%%%EndPageSetup\n");	bps = im->depth;	Bprint(&ioutb, "%d 0 %d %d %d %d %d %d %s doimage\n", iheight, iwidth, ystart, xstart, height, width, bps, im->flags&Fgrey ? "true" : "false"); 	imagebits(&ioutb, im);	Bprint(&ioutb, "pgsave restore\nshowpage\n");	e = trailer(&ioutb, 1);	if(e != 0)		return e;	Bterm(&ioutb);	return 0;}/* set local variables by string and pointer to its value * the variables are: *   int magnification *   int landscape *   char *Patch */voidpsopt(char *s, void *val){	if(s == nil)		return;	if(strcmp("xmagnification", s) == 0)		xmagnification = *((double *)val);	if(strcmp("ymagnification", s) == 0)		ymagnification = *((double *)val);	if(strcmp("landscape", s) == 0)		landscape = *((int *)val);	if(strcmp("Patch", s) == 0)		Patch = *((char **)val);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -