ff00f9e36c843d83454cf851e365bf96b7bdf84d
[ipdf/code.git] / src / debugscript.cpp
1 #include "debugscript.h"
2
3 #include <string>
4
5 using namespace IPDF;
6
7 void DebugScript::ParseAction(View * view, Screen * scr)
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         else if (actionType == "debugfont")
117         {
118                 currentAction.type = AT_DebugFont;
119                 inp >> currentAction.textargs;
120         }
121         else if (actionType == "approachz") // approach zenoistically
122         {
123                 currentAction.type = AT_ApproachBoundsZeno;
124                 std::string _x, _y, _w, _h, _z;
125                 inp >> _x >> _y >> _w >> _h >> _z;
126                 currentAction.x = RealFromStr(_x.c_str());
127                 currentAction.y = RealFromStr(_y.c_str());
128                 currentAction.w = RealFromStr(_w.c_str());
129                 currentAction.h = RealFromStr(_h.c_str());
130                 currentAction.z = RealFromStr(_z.c_str());
131         }
132         else if (actionType == "approachl") // approach linearly
133         {
134                 currentAction.type = AT_ApproachBoundsLinear;
135                 std::string _x, _y, _w, _h, _z;
136                 inp >> _x >> _y >> _w >> _h >> _z;
137                 currentAction.x = RealFromStr(_x.c_str());
138                 currentAction.y = RealFromStr(_y.c_str());
139                 currentAction.w = RealFromStr(_w.c_str());
140                 currentAction.h = RealFromStr(_h.c_str());
141                 currentAction.z = RealFromStr(_z.c_str());
142                 currentAction.x = (currentAction.x - view->GetBounds().x)/currentAction.z;
143                 currentAction.y = (currentAction.y - view->GetBounds().y)/currentAction.z;
144                 currentAction.w = (currentAction.w - view->GetBounds().w)/currentAction.z;
145                 currentAction.h = (currentAction.h - view->GetBounds().h)/currentAction.z;
146         }
147         else if (actionType == "setbounds")
148         {
149                 currentAction.type = AT_SetBounds;
150                 std::string _x, _y, _w, _h;
151                 inp >> _x >> _y >> _w >> _h;
152                 currentAction.x = RealFromStr(_x.c_str());
153                 currentAction.y = RealFromStr(_y.c_str());
154                 currentAction.w = RealFromStr(_w.c_str());
155                 currentAction.h = RealFromStr(_h.c_str());
156         }
157         else if (actionType == "querygpubounds")
158         {
159                 currentAction.type = AT_QueryGPUBounds;
160                 inp >> currentAction.textargs;
161         }
162         else if (actionType == "screenshot")
163         {
164                 currentAction.type = AT_ScreenShot;
165                 inp >> currentAction.textargs;  
166         }
167
168 }
169
170 bool DebugScript::Execute(View *view, Screen *scr)
171 {
172         if (currentAction.loops <= 0)
173         {
174                 if (m_index >= m_actions.size())
175                 {
176                         ParseAction(view, scr);
177                         if (m_labels.size() > 0)
178                         {
179                                 m_actions.push_back(currentAction);
180                                 m_index++;
181                         }
182                                 
183                 }
184                 else
185                         currentAction = m_actions[m_index++];
186         }
187
188         switch(currentAction.type)
189         {
190         case AT_Quit:
191                 return true;
192         case AT_WaitFrame:
193                 break;
194         case AT_Translate:
195                 view->Translate(currentAction.x, currentAction.y);
196                 break;
197         case AT_TranslatePx:
198                 view->Translate(Real(currentAction.ix)/Real(scr->ViewportWidth()), Real(currentAction.iy)/Real(scr->ViewportHeight()));
199                 break;
200         case AT_Zoom:
201                 view->ScaleAroundPoint(currentAction.x, currentAction.y, currentAction.z);
202                 break;
203         case AT_ZoomPx:
204                 view->ScaleAroundPoint(Real(currentAction.ix)/Real(scr->ViewportWidth()),Real(currentAction.iy)/Real(scr->ViewportHeight()), Real(expf(-currentAction.iz/20.f)));
205                 break;
206         case AT_SetGPURendering:
207                 view->SetGPURendering(true);
208                 break;
209         case AT_SetCPURendering:
210                 view->SetGPURendering(false);
211                 break;
212         case AT_EnableLazyRendering:
213                 view->SetLazyRendering(true);
214                 break;
215         case AT_DisableLazyRendering:
216                 view->SetLazyRendering(false);
217                 break;
218         case AT_LoadSVG:
219         {
220                 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
221                         view->Doc().LoadSVG(currentAction.textargs, Rect(Real(1)/Real(2),Real(1)/Real(2),Real(1)/Real(800),Real(1)/Real(600))); 
222                 #else
223                         const Rect & bounds = view->GetBounds();
224                         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)));
225                 #endif
226                 currentAction.type = AT_WaitFrame;
227                 view->ForceRenderDirty();
228                 view->ForceBufferDirty();
229                 view->ForceBoundsDirty();
230                 currentAction.loops = 1;
231                 break;
232         }
233         case AT_Label:
234                 m_labels[currentAction.textargs] = m_index;
235                 currentAction.type = AT_WaitFrame;
236                 currentAction.loops = 1;
237                 break;
238         case AT_Goto:
239                 m_index = m_labels[currentAction.textargs];
240                 currentAction.loops = 1;
241                 break;
242         case AT_Debug:
243                 Debug("View bounds: %s", view->GetBounds().Str().c_str());
244                 if (currentAction.textargs.size() > 0)
245                         Debug("%s", currentAction.textargs.c_str());
246                 break;
247         case AT_ClearDocument:
248                 view->Doc().ClearObjects();
249                 currentAction.loops = 1;
250                 break;
251         case AT_ClearPerformance:
252                 ClearPerformance(view, scr);
253                 currentAction.loops = 1;
254                 break;
255         case AT_PrintPerformance:
256                 PrintPerformance(view, scr);
257                 currentAction.loops = 1;        
258                 break;
259         case AT_RecordPerformance:
260                 PrintPerformance(view, scr);
261                 break;
262         case AT_DebugFont:
263                 scr->ShowDebugFont(currentAction.textargs == "1" || currentAction.textargs == "on");
264                 currentAction.loops = 1;
265                 break;
266                 
267         case AT_ApproachBoundsZeno:
268         {       
269                 VRect target(currentAction.x, currentAction.y, currentAction.w, currentAction.h);
270                 if (currentAction.z != VReal(1))
271                 {
272                         target.x = view->GetBounds().x + (target.x-view->GetBounds().x)/VReal(currentAction.z);
273                         target.y = view->GetBounds().y + (target.y-view->GetBounds().y)/VReal(currentAction.z);
274                         target.w = view->GetBounds().w + (target.w-view->GetBounds().w)/VReal(currentAction.z);
275                         target.h = view->GetBounds().h + (target.h-view->GetBounds().h)/VReal(currentAction.z);
276                 }
277                 
278
279                 VReal s = target.w/(view->GetBounds().w);
280                 if (Real(s) != 1)
281                 {
282                         VReal x0;
283                         VReal y0;
284                         x0 = (view->GetBounds().x - target.x)/((s - VReal(1))*view->GetBounds().w);
285                         y0 = (view->GetBounds().y - target.y)/((s - VReal(1))*view->GetBounds().h);
286                         view->ScaleAroundPoint(x0, y0, s);
287                         currentAction.loops++;
288                 }
289                 else
290                 {
291                         Debug("Already at target view; Waiting for remaining %d frames", currentAction.loops);
292                         currentAction.type = AT_WaitFrame;
293                 }
294                 break;
295         }
296         case AT_ApproachBoundsLinear:
297         {
298                 VRect target(currentAction.x, currentAction.y, currentAction.w, currentAction.h);
299                 target.x += view->GetBounds().x;
300                 target.y += view->GetBounds().y;
301                 target.w += view->GetBounds().w;
302                 target.h += view->GetBounds().h;
303                 VReal s = target.w/(view->GetBounds().w);
304                 if (Real(s) != 1)
305                 {
306                         VReal x0;
307                         VReal y0;
308                         x0 = (view->GetBounds().x - target.x)/((s - VReal(1))*view->GetBounds().w);
309                         y0 = (view->GetBounds().y - target.y)/((s - VReal(1))*view->GetBounds().h);
310                         view->ScaleAroundPoint(x0, y0, s);
311                         currentAction.loops++;
312                 }
313                 else
314                 {
315                         Debug("Already at target view; Waiting for remaining %d frames", currentAction.loops);
316                         currentAction.type = AT_WaitFrame;
317                 }
318                 break;
319         }
320         case AT_SetBounds:
321         {
322                 VRect target(currentAction.x, currentAction.y, currentAction.w, currentAction.h);
323                 view->SetBounds(target);
324                 break;
325         }
326         
327         case AT_QueryGPUBounds:
328         {
329                 view->QueryGPUBounds(currentAction.textargs.c_str(), "w");
330                 currentAction.loops = 1;
331                 break;
332         }
333         case AT_ScreenShot:
334         {
335                 view->SaveBMP(currentAction.textargs.c_str());
336                 currentAction.loops = 1;
337                 break;
338         }
339         default:
340                 Fatal("Unknown script command in queue.");
341         }
342         currentAction.loops--;
343         return false;
344 }
345
346 void DebugScript::ClearPerformance(View * view, Screen * scr)
347 {
348         m_perf_start.clock = clock();
349         m_perf_start.object_count = view->Doc().ObjectCount();
350         m_perf_start.view_bounds = view->GetBounds();
351         m_perf_last = m_perf_start;
352 }
353
354 void DebugScript::PrintPerformance(View * view, Screen * scr)
355 {
356         DebugScript::PerformanceData now;
357         now.clock = clock();
358         now.object_count = view->Doc().ObjectCount();
359         now.view_bounds = view->GetBounds();
360
361         // object_count  clock  delta_clock  x  Log10(x)  y  Log10(y)  w  Log10(w)  Size(w)
362         #ifdef QUADTREE_DISABLED
363         printf("%d\t%llu\t%llu\t%s\t%s\t%s\t%s\t%s\t%s\t%u\n",
364                 now.object_count, (long long unsigned)now.clock,
365                 (long long unsigned)(now.clock - m_perf_last.clock),
366                 Str(now.view_bounds.x).c_str(), Str(Log10(Abs(now.view_bounds.x))).c_str(),
367                 Str(now.view_bounds.y).c_str(), Str(Log10(Abs(now.view_bounds.y))).c_str(),
368                 Str(now.view_bounds.w).c_str(), Str(Log10(now.view_bounds.w)).c_str(),
369                 (unsigned)Size(now.view_bounds.w));
370         #endif
371         m_perf_last = now;
372 }

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