~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/poppler/test/pdf-inspector.cc

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//========================================================================
 
2
//
 
3
// GDKSplashOutputDev.cc
 
4
//
 
5
// Copyright 2003 Glyph & Cog, LLC
 
6
// Copyright 2004 Red Hat, Inc. (GDK port)
 
7
//
 
8
//========================================================================
 
9
 
 
10
#include <config.h>
 
11
 
 
12
#ifdef USE_GCC_PRAGMAS
 
13
#pragma implementation
 
14
#endif
 
15
 
 
16
#include <goo/gmem.h>
 
17
#include <goo/GooHash.h>
 
18
#include <goo/GooTimer.h>
 
19
#include <splash/SplashTypes.h>
 
20
#include <splash/SplashBitmap.h>
 
21
#include "Object.h"
 
22
#include "ProfileData.h"
 
23
#include "GfxState.h"
 
24
 
 
25
#include <gdk/gdk.h>
 
26
#include "CairoOutputDev.h"
 
27
 
 
28
#include "PDFDoc.h"
 
29
#include "GlobalParams.h"
 
30
#include "ErrorCodes.h"
 
31
#include <gtk/gtk.h>
 
32
 
 
33
 
 
34
// Mapping
 
35
#include "pdf-operators.c"
 
36
 
 
37
enum {
 
38
  OP_STRING,
 
39
  OP_COUNT,
 
40
  OP_TOTAL,
 
41
  OP_MIN,
 
42
  OP_MAX,
 
43
  N_COLUMNS
 
44
};
 
45
 
 
46
class PdfInspector {
 
47
public:
 
48
 
 
49
  PdfInspector(void);
 
50
 
 
51
  void set_file_name (const char *file_name);
 
52
  void load (const char *file_name);
 
53
  void run (void);
 
54
  void error_dialog (const char *error_message);
 
55
  void analyze_page (int page);
 
56
 
 
57
private:
 
58
  static void on_file_activated (GtkWidget *widget, PdfInspector *inspector);
 
59
  static void on_selection_changed (GtkTreeSelection *selection, PdfInspector *inspector);
 
60
  static void on_analyze_clicked (GtkWidget *widget, PdfInspector *inspector);
 
61
 
 
62
  GtkBuilder* builder;
 
63
  GtkTreeModel *model;
 
64
  PDFDoc *doc;
 
65
  CairoOutputDev *output;
 
66
};
 
67
 
 
68
 
 
69
 
 
70
PdfInspector::PdfInspector(void)
 
71
{
 
72
  GtkWidget *widget;
 
73
  GError* error = NULL;
 
74
  
 
75
  builder = gtk_builder_new ();
 
76
 
 
77
  if (!gtk_builder_add_from_file (builder, "./pdf-inspector.ui", &error))
 
78
  {
 
79
    g_warning ("Couldn't load builder file: %s", error->message);
 
80
    g_error_free (error);
 
81
  }
 
82
 
 
83
  widget = GTK_WIDGET (gtk_builder_get_object (builder, "pdf_file_chooser_button"));
 
84
  g_signal_connect (widget, "selection-changed", G_CALLBACK (on_file_activated), this);
 
85
 
 
86
  widget = GTK_WIDGET (gtk_builder_get_object (builder, "analyze_button"));
 
87
  g_signal_connect (widget, "clicked", G_CALLBACK (on_analyze_clicked), this);
 
88
 
 
89
  // setup the TreeView 
 
90
  widget = GTK_WIDGET (gtk_builder_get_object (builder, "pdf_tree_view"));
 
91
  g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (widget)),
 
92
                    "changed", G_CALLBACK (on_selection_changed), this);
 
93
  model = (GtkTreeModel *)gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_INT,
 
94
                                              G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
 
95
  gtk_tree_view_set_model (GTK_TREE_VIEW (widget), model);
 
96
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (widget),
 
97
                                               0, "Operation", 
 
98
                                               gtk_cell_renderer_text_new (),
 
99
                                               "text", OP_STRING,
 
100
                                               NULL);
 
101
 
 
102
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (widget),
 
103
                                               1, "Count", 
 
104
                                               gtk_cell_renderer_text_new (),
 
105
                                               "text", OP_COUNT,
 
106
                                               NULL);
 
107
 
 
108
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (widget),
 
109
                                               2, "Elapsed", 
 
110
                                               gtk_cell_renderer_text_new (),
 
111
                                               "text", OP_TOTAL,
 
112
                                               NULL);
 
113
 
 
114
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (widget),
 
115
                                               3, "Min", 
 
116
                                               gtk_cell_renderer_text_new (),
 
117
                                               "text", OP_MIN,
 
118
                                               NULL);
 
119
 
 
120
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (widget),
 
121
                                               4, "Max", 
 
122
                                               gtk_cell_renderer_text_new (),
 
123
                                               "text", OP_MAX,
 
124
                                               NULL);
 
125
 
 
126
  for (int i = 0; i < N_COLUMNS; i++)
 
127
    {
 
128
      GtkTreeViewColumn *column;
 
129
      
 
130
      column = gtk_tree_view_get_column (GTK_TREE_VIEW (widget), i);
 
131
      gtk_tree_view_column_set_sort_column_id (column, i);
 
132
    }
 
133
  doc = NULL;
 
134
  output = new CairoOutputDev();
 
135
  output->setPrinting (gFalse);
 
136
 
 
137
  // set up initial widgets
 
138
  load (NULL);
 
139
}
 
140
    
 
141
void
 
142
PdfInspector::set_file_name(const char *file_name)
 
143
{
 
144
  GtkWidget *widget;
 
145
 
 
146
  widget = GTK_WIDGET (gtk_builder_get_object (builder, "pdf_file_chooser_button"));
 
147
  gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (widget), file_name);
 
148
}
 
149
 
 
150
void
 
151
PdfInspector::on_file_activated (GtkWidget *widget, PdfInspector *inspector)
 
152
{
 
153
  gchar *file_name;
 
154
 
 
155
  file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
 
156
  if (file_name)
 
157
    inspector->load (file_name);
 
158
 
 
159
  g_free (file_name);
 
160
}
 
161
 
 
162
void
 
163
PdfInspector::on_selection_changed (GtkTreeSelection *selection, PdfInspector *inspector)
 
164
{
 
165
  GtkWidget *label;
 
166
  size_t i;
 
167
  GtkTreeModel *model;
 
168
  GtkTreeIter iter;
 
169
  gchar *op = NULL;
 
170
 
 
171
  label = GTK_WIDGET (gtk_builder_get_object (inspector->builder, "description_label"));
 
172
  gtk_label_set_markup (GTK_LABEL (label), "<i>No Description</i>");
 
173
 
 
174
  if (gtk_tree_selection_get_selected (selection, &model, &iter))
 
175
    {
 
176
      gtk_tree_model_get (model, &iter,
 
177
                          OP_STRING, &op,
 
178
                          -1);
 
179
 
 
180
    }
 
181
 
 
182
  if (op == NULL)
 
183
    return;
 
184
 
 
185
  for (i = 0; i < G_N_ELEMENTS (op_mapping); i++)
 
186
    {
 
187
 
 
188
      if (!strcmp (op, op_mapping[i].op))
 
189
        {
 
190
          gchar *text;
 
191
          text = g_strdup_printf ("<i>%s</i>", op_mapping[i].description);
 
192
          gtk_label_set_markup (GTK_LABEL (label), text);
 
193
          g_free (text);
 
194
          break;
 
195
        }
 
196
    }
 
197
 
 
198
  g_free (op);  
 
199
}
 
200
 
 
201
void
 
202
PdfInspector::on_analyze_clicked (GtkWidget *widget, PdfInspector *inspector)
 
203
{
 
204
  GtkWidget *spin;
 
205
  int page;
 
206
 
 
207
  spin = GTK_WIDGET (gtk_builder_get_object (inspector->builder, "pdf_spin"));
 
208
 
 
209
  page = (int) gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin));
 
210
 
 
211
  inspector->analyze_page (page);
 
212
 
 
213
}
 
214
 
 
215
void
 
216
PdfInspector::analyze_page (int page)
 
217
{
 
218
  GooHashIter *iter;
 
219
  GooHash *hash;
 
220
  GooString *key;
 
221
  void *p;
 
222
  GtkWidget *label;
 
223
  char *text;
 
224
  cairo_t *cr;
 
225
  cairo_surface_t *surface;
 
226
 
 
227
  label = GTK_WIDGET (gtk_builder_get_object (builder, "pdf_total_label"));
 
228
 
 
229
  output->startProfile ();
 
230
  gtk_list_store_clear (GTK_LIST_STORE (model));
 
231
 
 
232
  GooTimer timer;
 
233
  surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
 
234
                                        doc->getPageCropWidth(page + 1),
 
235
                                        doc->getPageCropHeight(page + 1));
 
236
  cr = cairo_create (surface);
 
237
  cairo_surface_destroy (surface);
 
238
  output->setCairo (cr);
 
239
  cairo_destroy (cr);
 
240
  doc->displayPage (output, page + 1, 72, 72, 0, gFalse, gTrue, gFalse);
 
241
  output->setCairo (NULL);
 
242
 
 
243
  // Total time;
 
244
  text = g_strdup_printf ("%g", timer.getElapsed ());
 
245
  gtk_label_set_text (GTK_LABEL (label), text);
 
246
  g_free (text);
 
247
 
 
248
  // Individual times;
 
249
  hash = output->endProfile ();
 
250
  hash->startIter(&iter);
 
251
  while (hash->getNext(&iter, &key, &p))
 
252
    {
 
253
      GtkTreeIter tree_iter;
 
254
      ProfileData *data_p = (ProfileData *) p;
 
255
 
 
256
      gtk_list_store_append (GTK_LIST_STORE (model), &tree_iter);
 
257
      gtk_list_store_set (GTK_LIST_STORE (model), &tree_iter,
 
258
                          OP_STRING, key->getCString(),
 
259
                          OP_COUNT, data_p->getCount (),
 
260
                          OP_TOTAL, data_p->getTotal (),
 
261
                          OP_MIN, data_p->getMin (),
 
262
                          OP_MAX, data_p->getMax (),
 
263
                          -1);
 
264
    }
 
265
  hash->killIter(&iter);
 
266
  deleteGooHash (hash, ProfileData);
 
267
}
 
268
 
 
269
void
 
270
PdfInspector::load(const char *file_name)
 
271
{
 
272
  GtkWidget *spin;
 
273
  GtkWidget *button;
 
274
  GtkWidget *label;
 
275
 
 
276
  // kill the old PDF file
 
277
  if (doc != NULL)
 
278
    {
 
279
      delete doc;
 
280
      doc = NULL;
 
281
    }
 
282
 
 
283
  // load the new file
 
284
  if (file_name)
 
285
    {
 
286
      GooString *filename_g;
 
287
 
 
288
      filename_g = new GooString (file_name);
 
289
      doc = new PDFDoc(filename_g, 0, 0);
 
290
      delete filename_g;
 
291
    }
 
292
  
 
293
  if (doc && !doc->isOk())
 
294
    {
 
295
      this->error_dialog ("Failed to load file.");
 
296
      delete doc;
 
297
      doc = NULL;
 
298
    }
 
299
 
 
300
  spin = GTK_WIDGET (gtk_builder_get_object (builder, "pdf_spin"));
 
301
  button = GTK_WIDGET (gtk_builder_get_object (builder, "analyze_button"));
 
302
  label = GTK_WIDGET (gtk_builder_get_object (builder, "pdf_total_label"));
 
303
  gtk_label_set_text (GTK_LABEL (label), "");
 
304
 
 
305
  if (doc)
 
306
    {
 
307
      gtk_widget_set_sensitive (spin, TRUE);
 
308
      gtk_widget_set_sensitive (button, TRUE);
 
309
      gtk_widget_set_sensitive (label, TRUE);
 
310
      gtk_spin_button_set_range (GTK_SPIN_BUTTON (spin), 0, doc->getNumPages()-1);
 
311
      gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin), 0);
 
312
 
 
313
      output->startDoc (doc->getXRef(), doc->getCatalog());
 
314
    }
 
315
  else
 
316
    {      
 
317
      gtk_widget_set_sensitive (spin, FALSE);
 
318
      gtk_widget_set_sensitive (button, FALSE);
 
319
      gtk_widget_set_sensitive (label, FALSE);
 
320
    }
 
321
}
 
322
 
 
323
void
 
324
PdfInspector::error_dialog (const char *error_message)
 
325
{
 
326
  g_warning ("%s", error_message);
 
327
}
 
328
 
 
329
void
 
330
PdfInspector::run()
 
331
{
 
332
  GtkWidget *dialog;
 
333
 
 
334
  dialog = GTK_WIDGET (gtk_builder_get_object (builder, "pdf_dialog"));
 
335
 
 
336
  gtk_dialog_run (GTK_DIALOG (dialog));
 
337
}
 
338
 
 
339
 
 
340
 
 
341
int
 
342
main (int argc, char *argv [])
 
343
{
 
344
  const char *file_name = NULL;
 
345
  PdfInspector *inspector;
 
346
  
 
347
  gtk_init (&argc, &argv);
 
348
  
 
349
  globalParams = new GlobalParams();
 
350
  globalParams->setProfileCommands (true);
 
351
  globalParams->setPrintCommands (true);
 
352
 
 
353
  if (argc == 2)
 
354
    file_name = argv[1];
 
355
  else if (argc > 2)
 
356
    {
 
357
      fprintf (stderr, "usage: %s [PDF-FILE]\n", argv[0]);
 
358
      return -1;
 
359
    }
 
360
 
 
361
  inspector = new PdfInspector ();
 
362
 
 
363
  if (file_name)
 
364
    inspector->set_file_name (file_name);
 
365
 
 
366
  inspector->run ();
 
367
 
 
368
  delete inspector;
 
369
  delete globalParams;
 
370
  
 
371
  return 0;
 
372
}
 
373
 
 
374