Created
September 6, 2015 15:31
-
-
Save kuuso/f096e802f5d31df285a1 to your computer and use it in GitHub Desktop.
postscriptWriterForMe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
using System.Linq; | |
class TEST{ | |
static void Main(){ | |
new TEST().CreateCode(); | |
} | |
void CreateCode(){ | |
PSWriter ps=new PSWriter(); | |
ps.BB(0,0,800,800); | |
ps.Translate(20,20); | |
ps.Grid(0,0,640,640,10); | |
ps.defDot(4); | |
ps.SetRGB(255,0,0); | |
for(int i=0;i<10;i++){ps.putDot(i,i);} | |
ps.SetRGB(0,0,0); | |
for(int i=0;i<10;i++){ps.putStr(i,i,String.Format("[{0},{1}]",i,i));} | |
ps.Dump(); | |
ps.Dump("test5.eps"); | |
} | |
static String rs(){return Console.ReadLine();} | |
static int ri(){return int.Parse(Console.ReadLine());} | |
static long rl(){return long.Parse(Console.ReadLine());} | |
static double rd(){return double.Parse(Console.ReadLine());} | |
static String[] rsa(){return Console.ReadLine().Split(' ');} | |
static int[] ria(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>int.Parse(e));} | |
static long[] rla(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>long.Parse(e));} | |
static double[] rda(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>double.Parse(e));} | |
} | |
partial class PSWriter{ | |
public static int H=10,W=10; | |
public static int Unit=4; | |
public void putRect(int i,int j,bool fill=true){ | |
int ri=H-1-i; | |
int rj=j; | |
Rect(rj*Unit,ri*Unit,Unit,Unit,fill); | |
} | |
public void putRect(int i,int j,int h,int w,bool fill=true){ | |
int ri=H-1-i-h+1; | |
int rj=j; | |
Rect(rj*Unit,ri*Unit,w*Unit,h*Unit,fill); | |
} | |
public void putRect2(int i,int j,int h,int w,bool fill=true){ | |
int ri=H-1-i-h+1; | |
int rj=j; | |
Rect(rj*Unit,ri*Unit+1,w*Unit-1,h*Unit-1,fill); | |
} | |
public void putMsg(int i,int j,String msg){ | |
int ri=H-1-i; | |
int rj=j; | |
Msg(rj*Unit+1,ri*Unit-1,msg); | |
} | |
public void putDot(int i,int j,bool fill=true){ | |
int ri=H-1-i; | |
int rj=j; | |
Dot(5+rj*10,5+ri*10,fill); | |
} | |
public void putX(int i,int j){ | |
int ri=H-1-i; | |
int rj=j; | |
Line(rj*10,ri*10,rj*10+10,ri*10+10); | |
Stroke(); | |
Line(rj*10,ri*10+10,rj*10+10,ri*10); | |
Stroke(); | |
} | |
public void putTextInfo(int i,String itemTitle,String sdata){ | |
int RowCnt=18; | |
int ri=H-1+RowCnt-i; | |
int rj=0; | |
String msg=itemTitle+":"+sdata; | |
Msg(rj*Unit+1,ri*Unit-1,msg); | |
} | |
public void putStr(int i,int j,String msg){ | |
int ri=63-i; | |
int rj=j; | |
Msg(5+rj*10+10,5+ri*10-5,msg); | |
} | |
} | |
partial class PSWriter{ | |
// main block | |
List<String> Buf; | |
bool en_Dot=false; | |
bool en_Arrow=false; | |
bool en_Grid=false; | |
bool en_setFont=false; | |
public PSWriter(){ | |
Buf=new List<String>(); | |
Buf.Add("%!PS-Adobe-3.0 EPSF-3.0"); | |
} | |
public void BB(int a,int b,int c,int d){ | |
Buf.Add(String.Format("%%BoundingBox: {0} {1} {2} {3}",a,b,c,d)); | |
} | |
public void Translate(int x,int y){ | |
Buf.Add(String.Format("{0} {1} translate",x,y)); | |
} | |
public void Scale (double xwidth,double xheight){ | |
Buf.Add(String.Format("{0:F3} {1:F3} scale",xwidth,xheight)); | |
} | |
public void AppendLib(String lib){ | |
if(lib=="dot")defDot(); | |
if(lib=="arrow")defArrow(); | |
if(lib=="grid")defGrid(); | |
if(lib=="font")defFontDefault(); | |
} | |
public void Polygon(int[] x,int[] y,bool fill=false){ | |
for(int i=0;i<Math.Min((x.Length),(y.Length));i++){ | |
if(i==0)Buf.Add(String.Format("{0} {1} moveto",x[i],y[i])); | |
if(i>0)Buf.Add(String.Format("{0} {1} lineto",x[i],y[i])); | |
} | |
Buf.Add("closepath"); | |
if(fill)Buf.Add("fill"); | |
} | |
public void Stroke(){Buf.Add("stroke");} | |
public void Fill(){Buf.Add("fill");} | |
public void ClosePath(){Buf.Add("closepath");} | |
String rgbSet=""; | |
public void SetRGB(int r,int g,int b){ | |
double rr=r/255.0; | |
double gg=g/255.0; | |
double bb=b/255.0; | |
String rgbCur=String.Format("{0:F3} {1:F3} {2:F3} setrgbcolor",rr,gg,bb); | |
if(rgbSet!=rgbCur){Buf.Add(rgbCur);rgbSet=rgbCur;} | |
} | |
public void SetLineWidth(double width=0.1){ | |
Buf.Add(String.Format("{0} setlinewidth",width)); | |
} | |
public void Rect(int xb,int yb,int width,int height,bool fill=false){ | |
if(!fill)Buf.Add(String.Format("{0} {1} {2} {3} rectstroke",xb,yb,width,height)); | |
if(fill)Buf.Add(String.Format("{0} {1} {2} {3} rectfill",xb,yb,width,height)); | |
} | |
public void MoveTo(int x,int y){ | |
Buf.Add(String.Format("{0} {1} moveto",x,y)); | |
} | |
public void LineTo(int x,int y){ | |
Buf.Add(String.Format("{0} {1} lineto",x,y)); | |
} | |
public void Arc(int x,int y,int r,int degf,int degt,bool clockwise=false){ | |
if(!clockwise)Buf.Add(String.Format("{0} {1} {2} {3} {4} arc",x,y,r,degf,degt)); | |
if(clockwise)Buf.Add(String.Format("{0} {1} {2} {3} {4} arcn",x,y,r,degf,degt)); | |
} | |
public void Line(int xf,int yf,int xt,int yt){ | |
//Buf.Add("newpath"); | |
MoveTo(xf,yf); | |
LineTo(xt,yt); | |
} | |
public void defDot(double rad=1.5){ | |
//rad*=3; | |
Buf.Add("\n% dot"); | |
Buf.Add(String.Format("/radius {0:F3} def",rad)); | |
Buf.Add("/dot {"); | |
Buf.Add("\tnewpath radius 0 360 arc"); | |
Buf.Add("} def"); | |
Buf.Add("% dot end\n"); | |
en_Dot=true; | |
} | |
public void Dot(int x,int y,bool fill=true){ | |
if(!en_Dot)defDot(); | |
if(fill)Buf.Add(String.Format("{0} {1} dot fill",x,y)); | |
if(!fill)Buf.Add(String.Format("{0} {1} dot",x,y)); | |
} | |
public void defArrow(){ | |
Buf.Add("\n% arrow"); | |
Buf.Add("/arrowdict 1 dict def"); | |
Buf.Add("arrowdict begin"); | |
Buf.Add("/mtrx matrix def"); | |
Buf.Add("end"); | |
Buf.Add("/arrow"); | |
Buf.Add("{ arrowdict begin"); | |
Buf.Add("/headlength exch def"); | |
Buf.Add("/halfheadthickness exch 2 div def"); | |
Buf.Add("/halfthickness exch 2 div def"); | |
Buf.Add("/tipy exch def /tipx exch def"); | |
Buf.Add("/taily exch def /tailx exch def"); | |
Buf.Add("/dx tipx tailx sub def"); | |
Buf.Add("/dy tipy taily sub def"); | |
Buf.Add("/arrowlength dx dx mul dy dy mul add"); | |
Buf.Add("sqrt def"); | |
Buf.Add("/angle dy dx atan def"); | |
Buf.Add("/base arrowlength headlength sub def"); | |
Buf.Add("/savematrix mtrx currentmatrix def"); | |
Buf.Add("tailx taily translate"); | |
Buf.Add("angle rotate"); | |
Buf.Add("0 halfthickness neg moveto"); | |
Buf.Add("base halfthickness neg lineto"); | |
Buf.Add("base halfheadthickness neg lineto"); | |
Buf.Add("arrowlength 0 lineto"); | |
Buf.Add("base halfheadthickness lineto"); | |
Buf.Add("base halfthickness lineto"); | |
Buf.Add("0 halfthickness lineto"); | |
Buf.Add("closepath"); | |
Buf.Add("savematrix setmatrix"); | |
Buf.Add("end"); | |
Buf.Add("} def"); | |
Buf.Add("% arrow end\n"); | |
en_Arrow=true; | |
} | |
public void Arrow(int xf,int yf,int xt,int yt,bool fill=true,int stemthick=6,int headthick=14,int headlength=28){ | |
if(!en_Arrow)defArrow(); | |
Buf.Add(String.Format("{0} {1} {2} {3} {4} {5} {6} arrow",xf,yf,xt,yt,stemthick,headthick,headlength)+(fill==true?" fill":"")); | |
} | |
public void defGrid(){ | |
Buf.Add("\n% grid"); | |
Buf.Add("/griddic 1 dict def"); | |
Buf.Add("/grid {"); | |
Buf.Add(" griddic begin"); | |
Buf.Add(" currentlinewidth "); | |
Buf.Add(" /lw exch def"); | |
Buf.Add(" currentrgbcolor 3 array astore"); | |
Buf.Add(" /rgb exch def"); | |
Buf.Add(" /sep exch def"); | |
Buf.Add(" /sep5 {sep 5 mul} def"); | |
Buf.Add(" /y2 exch def"); | |
Buf.Add(" /x2 exch def"); | |
Buf.Add(" /y1 exch def"); | |
Buf.Add(" /x1 exch def"); | |
Buf.Add(" 0.1 setlinewidth"); | |
Buf.Add(" 0.4 1.0 0.7 setrgbcolor"); | |
Buf.Add(" x1 sep x2{"); | |
Buf.Add(" dup y1 exch y2"); | |
Buf.Add(" moveto lineto stroke"); | |
Buf.Add(" }for"); | |
Buf.Add(" y1 sep y2 {"); | |
Buf.Add(" dup x1 3 1 roll"); | |
Buf.Add(" x2 exch"); | |
Buf.Add(" moveto lineto stroke"); | |
Buf.Add(" }for"); | |
Buf.Add(" 0.5 setlinewidth"); | |
Buf.Add(" 0 0.8 0.2 setrgbcolor"); | |
Buf.Add(" x1 sep5 x2{"); | |
Buf.Add(" dup y1 exch y2"); | |
Buf.Add(" moveto lineto stroke"); | |
Buf.Add(" }for"); | |
Buf.Add(" y1 sep5 y2 {"); | |
Buf.Add(" dup x1 3 1 roll"); | |
Buf.Add(" x2 exch"); | |
Buf.Add(" moveto lineto stroke"); | |
Buf.Add(" }for"); | |
Buf.Add(" lw setlinewidth"); | |
Buf.Add(" rgb aload pop setrgbcolor"); | |
Buf.Add(" end"); | |
Buf.Add("} def"); | |
Buf.Add("% grid end\n"); | |
en_Grid=true; | |
} | |
public void Grid(int xf,int yf,int xt,int yt,int sep){ | |
if(!en_Grid)defGrid(); | |
Buf.Add(String.Format("{0} {1} {2} {3} {4} grid",xf,yf,xt,yt,sep)); | |
} | |
static int FontSize=10; | |
static String FontStyle="Courier"; | |
static String[] Font=new String[]{ | |
"Times-Roman", | |
"Times-Italic", | |
"Times-Bold", | |
"Times-BoldItalic", | |
"Helvetica", | |
"Helvetica-Oblique", | |
"Helvetica-Bold", | |
"Helvetica-BoldOblique", | |
"Courier", | |
"Courier-Oblique", | |
"Courier-Bold", | |
"Courier-BoldOblique" | |
}; | |
public void defFontDefault(){ | |
FontSize=10; | |
FontStyle="Courier"; | |
SetFont(); | |
} | |
public void SetFontSize(int fsize){ | |
FontSize=fsize; | |
} | |
public void SetFontStyle(int idx=8){ | |
if(idx<0||idx>=Font.Length)idx=8; | |
FontStyle=Font[idx]; | |
} | |
String FontSet=""; | |
public void SetFont(){ | |
String FontCur="/"+FontStyle+" findfont "+FontSize+" scalefont setfont"; | |
if(FontSet!=FontCur){ | |
FontSet=FontCur; | |
Buf.Add(FontSet); | |
} | |
en_setFont=true; | |
} | |
public void Msg(int x,int y,String msg){ | |
if(!en_setFont)SetFont(); | |
Buf.Add(String.Format("{0} {1} moveto",x,y)); | |
Buf.Add("("+msg+") show"); | |
} | |
public void Dump(){ | |
//foreach(String s in Buf)Console.WriteLine(s); | |
Console.WriteLine(String.Join("\n",Buf)); | |
} | |
public void Dump(String flnm){ | |
Encoding sjisEnc = Encoding.GetEncoding("Shift_JIS"); | |
StringBuilder sb=new StringBuilder(); | |
foreach(String s in Buf)sb.Append(s+"\n"); | |
using(StreamWriter wr=new StreamWriter(flnm,false,sjisEnc)){ | |
//foreach(String s in Buf)wr.WriteLine(s); | |
// wr.WriteLine(String.Join("\n",Buf)); | |
wr.Write(sb.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment