PS文件生成驱动

PostScript是一种页面描述性的编程语言,广泛用于打印图片和文本。PostScript脚本需要用一定的解读器解读,我们平时使用的PS文件阅读器就应该包含PostScript脚本解读模块。

一个完整的PS文件包括三部分:文件头,PostScript语言, 和文件尾。

文件头给出了PS文件的基本信息,如标题,创建者,创建时间等等;文件尾标志着PS文件结束。PostScript语言分为两块,首先是一些前言(prolog),创建者可以自己定义了一些命令,方面后面的脚本书写;然后是脚本,是PS文件的主体,用于生成图片和文本。

需要注意的是,在Linux系统下,文件开头一般都有一个magic number用于指示文件的类型,PS文件以%!PS为开头,其对应的十六进制数为0x2125 5350,这可以用命令od -x -N 64 file.ps来查看。

这道这些以后,我们就基本可以写一个PS文件生成驱动了。下面是一个简单的例子:

%!PS-Adobe-3.0 EPSF-3.0
%%For: Liyropt
%%Title: Hello World
%%Creator: Liyropt
%%CreationDate: May 29 2011
%%BoundingBox: (atend)
%%DocumentFonts: (atend)
%%LanguageLevel: 1
%%Orientation: Landscape
%%Pages: (atend)
%%EndComments


%Begin Programming

%%BeginProlog
/L {moveto rlineto currentpoint stroke moveto} bind def
/C {rlineto currentpoint stroke moveto} bind def
/D {moveto 0 0 rlineto currentpoint stroke moveto} bind def
/LW {5 mul setlinewidth} bind def
%%EndProlog
%%BeginSetup
/#copies 1 def
%%EndSetup
%%Page: 1 1
%%BeginPageSetup
0.072 0.072 scale
8149 250 translate 90 rotate
1 setlinejoin 1 setlinecap 1 LW 1
%%EndPageSetup
4 LW 8000 0 1150 1560 L 0 3000 5000 1200 L
1 0.5 0 setrgbcolor
/Times-Roman findfont
200 scalefont
setfont
newpath
4283 2545 moveto
(Hello, world!) show
closepath
newpath
1500 0 6500 1500 L 0 1500 8000 1500 L -1500 0 8000 3000 L 0 -1500 6500 3000 L 
closepath
/inch {1500 mul} def
newpath
1 inch 1 inch moveto
2 inch 1 inch lineto
2 inch 2 inch lineto
1 inch 2 inch lineto
closepath
fill
stroke
save restore showpage


%End Programming

%%PageBoundingBox: 220 42 529 708
%%BoundingBox: 220 42 529 708
%%Pages: 1
%%EOF

下面是产生的效果,用evince打开

产生PS文件的C语言驱动如下

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
  FILE *fp;
  fp=fopen("pstest","w");
  fprintf(fp,"%s\n","%!PS-Adobe-3.0 EPSF-3.0"); 
  fprintf(fp,"%s\n","%%For: Liyropt");
  fprintf(fp,"%s\n","%%Title: Hello World");
  fprintf(fp,"%s\n","%%Creator: Liyropt");
  fprintf(fp,"%s\n","%%CreationDate: May 29 2011");
  fprintf(fp,"%s\n","%%BoundingBox: (atend)");
  fprintf(fp,"%s\n","%%DocumentFonts: (atend)");
  fprintf(fp,"%s\n","%%LanguageLevel: 1");
  fprintf(fp,"%s\n","%%Orientation: Landscape");
  fprintf(fp,"%s\n","%%Pages: (atend)");
  fprintf(fp,"%s\n","%%EndComments");
  
  fprintf(fp, "\n\n%s\n\n","%Begin Programming");
  
  fprintf(fp,"%s\n","%%BeginProlog");
  
  fprintf(fp,"%s\n","/L {moveto rlineto currentpoint stroke moveto} bind def");
  fprintf(fp,"%s\n","/C {rlineto currentpoint stroke moveto} bind def");
  fprintf(fp,"%s\n","/D {moveto 0 0 rlineto currentpoint stroke moveto} bind def");
  fprintf(fp,"%s\n","/LW {5 mul setlinewidth} bind def");
  fprintf(fp,"%s\n","%%EndProlog");
  fprintf(fp,"%s\n","%%BeginSetup");
  fprintf(fp,"%s\n","/#copies 1 def");
  fprintf(fp,"%s\n","%%EndSetup");

  fprintf(fp,"%s\n","%%Page: 1 1");
  fprintf(fp,"%s\n","%%BeginPageSetup");
  fprintf(fp,"%s\n","0.072 0.072 scale");
  fprintf(fp,"%s\n","8149 250 translate 90 rotate");
  fprintf(fp,"%s\n","1 setlinejoin 1 setlinecap 1 LW 1");
  fprintf(fp,"%s\n","%%EndPageSetup");
  
  fprintf(fp,"%s\n","4 LW 8000 0 1150 1560 L 0 3000 5000 1200 L");

  fprintf(fp,"%s\n","1 0.5 0 setrgbcolor");

  fprintf(fp,"%s\n","/Times-Roman findfont");   
  fprintf(fp,"%s\n","200 scalefont");           
  fprintf(fp,"%s\n","setfont");                 

  fprintf(fp,"%s\n","newpath");                 
  fprintf(fp,"%s\n","4283 2545 moveto");            
  fprintf(fp,"%s\n","(Hello, world!) show");    
  fprintf(fp,"%s\n","closepath");

  fprintf(fp,"%s\n","newpath");
  fprintf(fp,"%s\n","1500 0 6500 1500 L 0 1500 8000 1500 L -1500 0 8000 3000 L 0 -1500 6500 3000 L ");
  fprintf(fp,"%s\n","closepath");

  fprintf(fp,"%s\n","/inch {1500 mul} def");
  fprintf(fp,"%s\n","newpath");
  fprintf(fp,"%s\n","1 inch 1 inch moveto");
  fprintf(fp,"%s\n","2 inch 1 inch lineto");
  fprintf(fp,"%s\n","2 inch 2 inch lineto");
  fprintf(fp,"%s\n","1 inch 2 inch lineto");
  fprintf(fp,"%s\n","closepath");
  fprintf(fp,"%s\n","fill");
  fprintf(fp,"%s\n","stroke");
  
  fprintf(fp,"%s\n","save restore showpage");
  
  fprintf(fp, "\n\n%s\n\n","%End Programming");
  
  fprintf(fp,"%s\n","%%PageBoundingBox: 220 42 529 708");
  fprintf(fp,"%s\n","%%BoundingBox: 220 42 529 708");
  fprintf(fp,"%s\n","%%Pages: 1");
  fprintf(fp,"%s\n","%%EOF");
  
  fclose(fp);
}
posted @ 2011-05-29 17:45  liyropt  阅读(1371)  评论(1编辑  收藏  举报