9bb5ea640c69e7f395b20bf6a794345b6c146781
[ipdf/code.git] / src / debugscript.cpp
1 #include "debugscript.h"
2
3 #include <string>
4
5 using namespace IPDF;
6
7 void DebugScript::ParseAction()
8 {
9         std::string actionType;
10         inp >> actionType;
11         // Skip comments
12         while (actionType[0] == '#')
13         {
14                 std::string tmp;
15                 std::getline(inp, tmp);
16                 inp >> std::ws >> actionType;
17         }
18         if (actionType == "loop")
19         {
20                 inp >> currentAction.loops >> actionType;
21         }
22         else
23         {
24                 currentAction.loops = 1;
25         }
26
27         if (actionType == "wait")
28         {
29                 currentAction.type = AT_WaitFrame;
30                 return;
31         }
32         else if (actionType == "translate")
33         {
34                 std::string _x, _y;
35                 inp >> _x >> _y;
36                 currentAction.type = AT_Translate;
37                 currentAction.x = RealFromStr(_x.c_str());
38                 currentAction.y = RealFromStr(_y.c_str());
39                 return;
40         }
41         else if (actionType == "zoom")
42         {
43                 std::string _x, _y, _z;
44                 inp >> _x >> _y >> _z;
45                 currentAction.type = AT_Zoom;
46                 currentAction.x = RealFromStr(_x.c_str());
47                 currentAction.y = RealFromStr(_y.c_str());
48                 currentAction.z = RealFromStr(_z.c_str());
49         }
50         else if (actionType == "pxtranslate")
51         {
52                 inp >> currentAction.ix >> currentAction.iy;
53                 currentAction.type = AT_TranslatePx;
54         }
55         else if (actionType == "pxzoom")
56         {
57                 inp >> currentAction.ix >> currentAction.iy >> currentAction.iz;
58                 currentAction.type = AT_ZoomPx;
59         }
60         else if (actionType == "gpu")
61         {
62                 currentAction.type = AT_SetGPURendering;
63         }
64         else if (actionType == "cpu")
65         {
66                 currentAction.type = AT_SetCPURendering;
67         }
68         else if (actionType == "lazy")
69         {
70                 currentAction.type = AT_EnableLazyRendering;
71         }
72         else if (actionType == "nolazy")
73         {
74                 currentAction.type = AT_DisableLazyRendering;
75         }
76         else if (actionType == "quit")
77         {
78                 currentAction.type = AT_Quit;
79         }
80         else if (actionType == "loadsvg")
81         {
82                 currentAction.type = AT_LoadSVG;
83                 inp >> currentAction.textargs;
84         }
85         else if (actionType == "label")
86         {
87                 currentAction.type = AT_Label;
88                 inp >> currentAction.textargs;
89         }
90         else if (actionType == "goto")
91         {
92                 currentAction.type = AT_Goto;
93                 inp >> currentAction.textargs;
94         }
95         else if (actionType == "debug")
96         {
97                 currentAction.type = AT_Debug;
98                 getline(inp,currentAction.textargs);
99         }
100         else if (actionType == "clear")
101         {
102                 currentAction.type = AT_ClearDocument;
103         }
104         else if (actionType == "clearperf")
105         {
106                 currentAction.type = AT_ClearPerformance;
107         }
108         else if (actionType == "printperf")
109         {
110                 currentAction.type = AT_PrintPerformance;
111         }
112         else if (actionType == "recordperf")
113         {
114                 currentAction.type = AT_RecordPerformance;
115         }
116 }
117
118 bool DebugScript::Execute(View *view, Screen *scr)
119 {
120         if (currentAction.loops <= 0)
121         {
122                 if (m_index >= m_actions.size())
123                 {
124                         ParseAction();
125                         if (m_labels.size() > 0)
126                         {
127                                 m_actions.push_back(currentAction);
128                                 m_index++;
129                         }
130                                 
131                 }
132                 else
133                         currentAction = m_actions[m_index++];
134         }
135
136         switch(currentAction.type)
137         {
138         case AT_Quit:
139                 return true;
140         case AT_WaitFrame:
141                 break;
142         case AT_Translate:
143                 view->Translate(currentAction.x, currentAction.y);
144                 break;
145         case AT_TranslatePx:
146                 view->Translate(Real(currentAction.ix)/Real(scr->ViewportWidth()), Real(currentAction.iy)/Real(scr->ViewportHeight()));
147                 break;
148         case AT_Zoom:
149                 view->ScaleAroundPoint(currentAction.x, currentAction.y, currentAction.z);
150                 break;
151         case AT_ZoomPx:
152                 view->ScaleAroundPoint(Real(currentAction.ix)/Real(scr->ViewportWidth()),Real(currentAction.iy)/Real(scr->ViewportHeight()), Real(expf(-currentAction.iz/20.f)));
153                 break;
154         case AT_SetGPURendering:
155                 view->SetGPURendering(true);
156                 break;
157         case AT_SetCPURendering:
158                 view->SetGPURendering(false);
159                 break;
160         case AT_EnableLazyRendering:
161                 view->SetLazyRendering(true);
162                 break;
163         case AT_DisableLazyRendering:
164                 view->SetLazyRendering(false);
165                 break;
166         case AT_LoadSVG:
167         {
168                 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
169                         view->Doc().LoadSVG(currentAction.textargs, Rect(Real(1)/Real(2),Real(1)/Real(2),Real(1)/Real(800),Real(1)/Real(600))); 
170                 #else
171                         const Rect & bounds = view->GetBounds();
172                         view->Doc().LoadSVG(currentAction.textargs, Rect(bounds.x+bounds.w/Real(2),bounds.y+bounds.h/Real(2),bounds.w/Real(800),bounds.h/Real(600)));
173                 #endif
174                 currentAction.type = AT_WaitFrame;
175                 view->ForceRenderDirty();
176                 view->ForceBufferDirty();
177                 view->ForceBoundsDirty();
178                 currentAction.loops = 1;
179                 break;
180         }
181         case AT_Label:
182                 m_labels[currentAction.textargs] = m_index;
183                 currentAction.type = AT_WaitFrame;
184                 currentAction.loops = 1;
185                 break;
186         case AT_Goto:
187                 m_index = m_labels[currentAction.textargs];
188                 currentAction.loops = 1;
189                 break;
190         case AT_Debug:
191                 Debug("View bounds: %s", view->GetBounds().Str().c_str());
192                 if (currentAction.textargs.size() > 0)
193                         Debug("%s", currentAction.textargs.c_str());
194                 break;
195         case AT_ClearDocument:
196                 view->Doc().ClearObjects();
197                 currentAction.loops = 1;
198                 break;
199         case AT_ClearPerformance:
200                 ClearPerformance(view, scr);
201                 currentAction.loops = 1;
202                 break;
203         case AT_PrintPerformance:
204                 PrintPerformance(view, scr);
205                 currentAction.loops = 1;        
206                 break;
207         case AT_RecordPerformance:
208                 PrintPerformance(view, scr);
209                 break;
210         default:
211                 Fatal("Unknown script command in queue.");
212         }
213         currentAction.loops--;
214         return false;
215 }
216
217 void DebugScript::ClearPerformance(View * view, Screen * scr)
218 {
219         m_perf_start.clock = clock();
220         m_perf_start.object_count = view->Doc().ObjectCount();
221         m_perf_start.view_bounds = view->GetBounds();
222         m_perf_last = m_perf_start;
223 }
224
225 void DebugScript::PrintPerformance(View * view, Screen * scr)
226 {
227         DebugScript::PerformanceData now;
228         now.clock = clock();
229         now.object_count = view->Doc().ObjectCount();
230         now.view_bounds = view->GetBounds();
231         // object_count delta clock delta x deltax y deltay w deltaw h deltah
232         printf("%d\t%d\t%lu\t%lu\t%e\t%e\t%e\t%e\t%e\t%e\t%e\t%e\n", 
233                 now.object_count, now.object_count - m_perf_last.object_count,
234                 (uint64_t)now.clock, (uint64_t)(now.clock - m_perf_last.clock),
235                 Double(now.view_bounds.x), Double(now.view_bounds.x - m_perf_last.view_bounds.x),
236                 Double(now.view_bounds.y), Double(now.view_bounds.y - m_perf_last.view_bounds.y),
237                 Double(now.view_bounds.w), Double(now.view_bounds.w - m_perf_last.view_bounds.w),
238                 Double(now.view_bounds.h), Double(now.view_bounds.h - m_perf_last.view_bounds.h));
239         m_perf_last = now;
240 }

UCC git Repository :: git.ucc.asn.au