OSDN Git Service

Initial Commit
[qcad/qcad.git] / qcadwin / QPSDraw.cpp
1 //---------------------------------------------------------------------------\r
2 // PostScript\r
3 //---------------------------------------------------------------------------\r
4 #include <iostream>\r
5 #include <string>\r
6 #include <sstream>\r
7 #include "QPSDraw.h"\r
8 using namespace std;\r
9 //---------------------------------------------------------------------------\r
10 QPSDraw::QPSDraw(void) : QAbstractDraw() {\r
11   FontSize = 14*0.8;\r
12   Width = 0;\r
13   Height = 0;\r
14 }\r
15 //---------------------------------------------------------------------------\r
16 QPSDraw::~QPSDraw(void) {}\r
17 //---------------------------------------------------------------------------\r
18 void\r
19 QPSDraw::AddText(const char *str) {\r
20   osEPS << str << endl;\r
21 }\r
22 //---------------------------------------------------------------------------\r
23 string\r
24 QPSDraw::GetText(void) {\r
25 \r
26   ostringstream os;\r
27   os << "%!PS-Adobe-2.0" << endl;\r
28   os << "%%Title: QCAD" << endl;\r
29   os << "%%Creator: QCAD(kaityo)" << endl;\r
30   os << "%%BoundingBox: 0 0 " << Width+LeftMargin << " " << Height+TopMargin << endl;\r
31   os << "%%Orientation: Portrait" << endl;\r
32   os << "%%Pages: 1" << endl;\r
33   os << "%%Page: 1 1" << endl;\r
34 \r
35   os << "/G " << GridSize << " def" << endl;\r
36   os << "/U " << UnitSize << " def" << endl;\r
37   os << "/UH " << UnitSize/2 << " def" << endl;\r
38 \r
39   os << "/M { gsave translate newpath 0 0 UH 0 180 arc closepath 0 0 moveto UH UH lineto stroke grestore } def" << endl;\r
40 \r
41   os << "/mydict 120 dict def" << endl;\r
42   os << "mydict begin" << endl;\r
43   os << "gsave" << endl;\r
44   os << LeftMargin << " " << TopMargin << " translate" << endl;\r
45 \r
46   os << "/Helvetica findfont 14 scalefont setfont" << endl;\r
47 \r
48   os << osEPS.str() << endl;\r
49 \r
50   os << "stroke" << endl;\r
51   os << "showpage" << endl;\r
52   return os.str();\r
53 }\r
54 //---------------------------------------------------------------------------\r
55 // Drawing Method\r
56 //---------------------------------------------------------------------------\r
57 void\r
58 QPSDraw::DrawLine(int x1,int y1,int x2,int y2) {\r
59 \r
60   ostringstream os;\r
61   os << x1 << " " << y1 << " moveto ";\r
62   os << x2 << " " << y2 << " lineto stroke";\r
63   AddText(os.str().c_str());\r
64 }\r
65 //---------------------------------------------------------------------------\r
66 void\r
67 QPSDraw::DrawDashedLine(int x1,int y1,int x2,int y2) {\r
68   AddText("[3] 0 setdash");\r
69   DrawLine(x1,y1,x2,y2);\r
70   AddText("[] 0 setdash");\r
71 }\r
72 //---------------------------------------------------------------------------\r
73 void\r
74 QPSDraw::TextOut(int x1,int y1,string text) {\r
75 \r
76   x1-=FontSize/2*text.length();\r
77   y1-=FontSize/2;\r
78 \r
79   ostringstream os;\r
80   os << x1 << " " << y1 << " moveto ";\r
81   os << "(" << text << ") show ";\r
82   AddText(os.str().c_str());\r
83 }\r
84 //---------------------------------------------------------------------------\r
85 void\r
86 QPSDraw::FrameRect(int x1, int y1, int x2, int y2) {\r
87 \r
88   ostringstream os;\r
89   os << "newpath" << endl;\r
90   os << x1 << " " << y1 << " moveto" << endl;\r
91   os << x1 << " " << y2 << " lineto" << endl;\r
92   os << x2 << " " << y2 << " lineto" << endl;\r
93   os << x2 << " " << y1 << " lineto" << endl;\r
94   os << "closepath stroke" << endl;\r
95   AddText(os.str().c_str());\r
96 \r
97 }\r
98 //---------------------------------------------------------------------------\r
99 void\r
100 QPSDraw::FillRect(int x1, int y1, int x2, int y2) {\r
101   ostringstream os;\r
102 \r
103   os << "newpath" << endl;\r
104   os << x1 << " " << y1 << " moveto" << endl;\r
105   os << x1 << " " << y2 << " lineto" << endl;\r
106   os << x2 << " " << y2 << " lineto" << endl;\r
107   os << x2 << " " << y1 << " lineto" << endl;\r
108   os << "closepath 1 setgray fill stroke 0 setgray" << endl;\r
109   AddText(os.str().c_str());\r
110 }\r
111 //---------------------------------------------------------------------------\r
112 void\r
113 QPSDraw::DrawCircle(int x1,int y1,int r) {\r
114 \r
115   ostringstream os;\r
116   os << "newpath" << endl;\r
117   os << x1 << " " << y1 << " " << r << " 0 360 arc closepath stroke" << endl;\r
118   AddText(os.str().c_str());\r
119 }\r
120 //---------------------------------------------------------------------------\r
121 void\r
122 QPSDraw::DrawFillCircle(int x1,int y1,int r) {\r
123 \r
124   ostringstream os;\r
125   os << "newpath" << endl;\r
126   os << x1 << " " << y1 << " " << r << " 0 360 arc fill closepath stroke" << endl;\r
127   AddText(os.str().c_str());\r
128 }\r
129 //---------------------------------------------------------------------------\r
130 void\r
131 QPSDraw::DrawMeasure(int x1,int y1) {\r
132   ostringstream os;\r
133   os << x1 << " " << y1 << " M" << endl;\r
134   AddText(os.str().c_str());\r
135 }\r
136 //---------------------------------------------------------------------------\r
137 void\r
138 QPSDraw::DrawGrid(int line, bool enabled) {\r
139   if (!enabled) {\r
140     int x1 = Width/2;\r
141     int y1 = line*GridSize;\r
142     int x2 = x1;\r
143     int y2 = y1+GridSize;\r
144     DrawDashedLine(x1,y1,x2,y2);\r
145   } else {\r
146     DrawLine(0,line*GridSize+GridSize/2,Width,line*GridSize+GridSize/2);\r
147   }\r
148 }\r
149 //---------------------------------------------------------------------------\r
150 \r