Code

initialize page number and preview
[inkscape.git] / src / extension / internal / pdfinput / pdf-input.cpp
1  /** \file
2  * Native PDF import using libpoppler.
3  * 
4  * Authors:
5  *   miklos erdelyi
6  *
7  * Copyright (C) 2007 Authors
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  *
11  */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #ifdef HAVE_POPPLER
19 #include "goo/GooString.h"
20 #include "ErrorCodes.h"
21 #include "GlobalParams.h"
22 #include "PDFDoc.h"
23 #include "Page.h"
24 #include "Catalog.h"
26 #include "pdf-input.h"
27 #include "extension/system.h"
28 #include "extension/input.h"
29 #include "svg-builder.h"
30 #include "pdf-parser.h"
32 #include "document-private.h"
34 #include <gtk/gtkdialog.h>
36 namespace Inkscape {
37 namespace Extension {
38 namespace Internal {
40 /**
41  * \brief The PDF import dialog
42  * FIXME: Probably this should be placed into src/ui/dialog
43  */
45 static Glib::ustring crop_setting_choices[] = {
46     Glib::ustring(_("media box")),
47     Glib::ustring(_("crop box")),
48     Glib::ustring(_("trim box")),
49     Glib::ustring(_("bleed box")),
50     Glib::ustring(_("art box"))
51 };
53 PdfImportDialog::PdfImportDialog(PDFDoc *doc)
54 {
56     _pdf_doc = doc;
58     cancelbutton = Gtk::manage(new class Gtk::Button(Gtk::StockID("gtk-cancel")));
59     okbutton = Gtk::manage(new class Gtk::Button(Gtk::StockID("gtk-ok")));
60     _labelSelect = Gtk::manage(new class Gtk::Label(_("Select page:")));
62     // Page number
63     Gtk::Adjustment *_pageNumberSpin_adj = Gtk::manage(
64             new class Gtk::Adjustment(1, 1, _pdf_doc->getNumPages(), 1, 10, 0));
65     _pageNumberSpin = Gtk::manage(new class Gtk::SpinButton(*_pageNumberSpin_adj, 1, 1));
66     _labelTotalPages = Gtk::manage(new class Gtk::Label());
67     hbox2 = Gtk::manage(new class Gtk::HBox(false, 0));
68     // Disable the page selector when there's only one page
69     int num_pages = _pdf_doc->getCatalog()->getNumPages();
70     if ( num_pages == 1 ) {
71         _pageNumberSpin->set_sensitive(false);
72     } else {
73         // Display total number of pages
74         gchar *label_text = g_strdup_printf("/ %i", num_pages);
75         _labelTotalPages->set_label(label_text);
76         g_free(label_text);
77     }
79     // Crop settings
80     _cropCheck = Gtk::manage(new class Gtk::CheckButton(_("Crop to:")));
81     _cropTypeCombo = Gtk::manage(new class Gtk::ComboBoxText());
82     int num_crop_choices = sizeof(crop_setting_choices) / sizeof(crop_setting_choices[0]);
83     for ( int i = 0 ; i < num_crop_choices ; i++ ) {
84         _cropTypeCombo->append_text(crop_setting_choices[i]);
85     }
86     _cropTypeCombo->set_active_text(crop_setting_choices[0]);
87     _cropTypeCombo->set_sensitive(false);
89     hbox3 = Gtk::manage(new class Gtk::HBox(false, 0));
90     vbox2 = Gtk::manage(new class Gtk::VBox(false, 0));
91     alignment3 = Gtk::manage(new class Gtk::Alignment(0.5, 0.5, 1, 1));
92     _labelPageSettings = Gtk::manage(new class Gtk::Label(_("Page Settings")));
93     _pageSettingsFrame = Gtk::manage(new class Gtk::Frame());
94     _labelPrecision = Gtk::manage(new class Gtk::Label(_("Precision of approximation for gradient meshes:")));
95    
96     _fallbackPrecisionSlider_adj = Gtk::manage(new class Gtk::Adjustment(2, 1, 256, 1, 10, 10));
97     _fallbackPrecisionSlider = Gtk::manage(new class Gtk::HScale(*_fallbackPrecisionSlider_adj));
98     _fallbackPrecisionSlider->set_value(2.0);
99     _labelPrecisionComment = Gtk::manage(new class Gtk::Label(_("rough")));
100     hbox6 = Gtk::manage(new class Gtk::HBox(false, 0));
102     // Text options
103     _labelText = Gtk::manage(new class Gtk::Label(_("Text handling:")));
104     _textHandlingCombo = Gtk::manage(new class Gtk::ComboBoxText());
105     _textHandlingCombo->append_text(_("import text as text"));
106     _textHandlingCombo->set_active_text(_("import text as text"));
107     
108     hbox5 = Gtk::manage(new class Gtk::HBox(false, 0));
109     _embedImagesCheck = Gtk::manage(new class Gtk::CheckButton(_("Embed images")));
110     vbox3 = Gtk::manage(new class Gtk::VBox(false, 0));
111     alignment4 = Gtk::manage(new class Gtk::Alignment(0.5, 0.5, 1, 1));
112     _labelImportSettings = Gtk::manage(new class Gtk::Label(_("Import Settings")));
113     _importSettingsFrame = Gtk::manage(new class Gtk::Frame());
114     vbox1 = Gtk::manage(new class Gtk::VBox(false, 0));
115     _previewArea = Gtk::manage(new class Gtk::DrawingArea());
116     hbox1 = Gtk::manage(new class Gtk::HBox(false, 0));
117     cancelbutton->set_flags(Gtk::CAN_FOCUS);
118     cancelbutton->set_flags(Gtk::CAN_DEFAULT);
119     cancelbutton->set_relief(Gtk::RELIEF_NORMAL);
120     okbutton->set_flags(Gtk::CAN_FOCUS);
121     okbutton->set_flags(Gtk::CAN_DEFAULT);
122     okbutton->set_relief(Gtk::RELIEF_NORMAL);
123     this->get_action_area()->property_layout_style().set_value(Gtk::BUTTONBOX_END);
124     _labelSelect->set_alignment(0.5,0.5);
125     _labelSelect->set_padding(0,0);
126     _labelSelect->set_justify(Gtk::JUSTIFY_LEFT);
127     _labelSelect->set_line_wrap(false);
128     _labelSelect->set_use_markup(false);
129     _labelSelect->set_selectable(false);
130     _pageNumberSpin->set_flags(Gtk::CAN_FOCUS);
131     _pageNumberSpin->set_update_policy(Gtk::UPDATE_ALWAYS);
132     _pageNumberSpin->set_numeric(true);
133     _pageNumberSpin->set_digits(0);
134     _pageNumberSpin->set_wrap(false);
135     _labelTotalPages->set_alignment(0.5,0.5);
136     _labelTotalPages->set_padding(0,0);
137     _labelTotalPages->set_justify(Gtk::JUSTIFY_LEFT);
138     _labelTotalPages->set_line_wrap(false);
139     _labelTotalPages->set_use_markup(false);
140     _labelTotalPages->set_selectable(false);
141     hbox2->pack_start(*_labelSelect, Gtk::PACK_SHRINK, 0);
142     hbox2->pack_start(*_pageNumberSpin, Gtk::PACK_SHRINK, 4);
143     hbox2->pack_start(*_labelTotalPages, Gtk::PACK_SHRINK, 0);
144     _cropCheck->set_flags(Gtk::CAN_FOCUS);
145     _cropCheck->set_relief(Gtk::RELIEF_NORMAL);
146     _cropCheck->set_mode(true);
147     _cropCheck->set_active(false);
148     _cropTypeCombo->set_border_width(1);
149     hbox3->pack_start(*_cropCheck, Gtk::PACK_SHRINK, 0);
150     hbox3->pack_start(*_cropTypeCombo, Gtk::PACK_SHRINK, 4);
151     vbox2->pack_start(*hbox2);
152     vbox2->pack_start(*hbox3);
153     alignment3->add(*vbox2);
154     _labelPageSettings->set_alignment(0.5,0.5);
155     _labelPageSettings->set_padding(0,0);
156     _labelPageSettings->set_justify(Gtk::JUSTIFY_LEFT);
157     _labelPageSettings->set_line_wrap(false);
158     _labelPageSettings->set_use_markup(true);
159     _labelPageSettings->set_selectable(false);
160     _pageSettingsFrame->set_border_width(4);
161     _pageSettingsFrame->set_shadow_type(Gtk::SHADOW_ETCHED_IN);
162     _pageSettingsFrame->set_label_align(0,0.5);
163     _pageSettingsFrame->add(*alignment3);
164     _pageSettingsFrame->set_label_widget(*_labelPageSettings);
165     _labelPrecision->set_alignment(0.5,0.5);
166     _labelPrecision->set_padding(0,0);
167     _labelPrecision->set_justify(Gtk::JUSTIFY_LEFT);
168     _labelPrecision->set_line_wrap(false);
169     _labelPrecision->set_use_markup(false);
170     _labelPrecision->set_selectable(false);
171     _fallbackPrecisionSlider->set_size_request(180,-1);
172     _fallbackPrecisionSlider->set_flags(Gtk::CAN_FOCUS);
173     _fallbackPrecisionSlider->set_update_policy(Gtk::UPDATE_CONTINUOUS);
174     _fallbackPrecisionSlider->set_inverted(false);
175     _fallbackPrecisionSlider->set_digits(1);
176     _fallbackPrecisionSlider->set_draw_value(true);
177     _fallbackPrecisionSlider->set_value_pos(Gtk::POS_TOP);
178     _labelPrecisionComment->set_size_request(90,-1);
179     _labelPrecisionComment->set_alignment(0.5,0.5);
180     _labelPrecisionComment->set_padding(0,0);
181     _labelPrecisionComment->set_justify(Gtk::JUSTIFY_LEFT);
182     _labelPrecisionComment->set_line_wrap(false);
183     _labelPrecisionComment->set_use_markup(false);
184     _labelPrecisionComment->set_selectable(false);
185     hbox6->pack_start(*_fallbackPrecisionSlider, Gtk::PACK_SHRINK, 4);
186     hbox6->pack_start(*_labelPrecisionComment, Gtk::PACK_SHRINK, 4);
187     _labelText->set_alignment(0.5,0.5);
188     _labelText->set_padding(0,0);
189     _labelText->set_justify(Gtk::JUSTIFY_LEFT);
190     _labelText->set_line_wrap(false);
191     _labelText->set_use_markup(false);
192     _labelText->set_selectable(false);
193     hbox5->pack_start(*_labelText, Gtk::PACK_SHRINK, 0);
194     hbox5->pack_start(*_textHandlingCombo, Gtk::PACK_SHRINK, 0);
195     _embedImagesCheck->set_flags(Gtk::CAN_FOCUS);
196     _embedImagesCheck->set_relief(Gtk::RELIEF_NORMAL);
197     _embedImagesCheck->set_mode(true);
198     _embedImagesCheck->set_active(true);
199     vbox3->pack_start(*_labelPrecision, Gtk::PACK_SHRINK, 4);
200     vbox3->pack_start(*hbox6, Gtk::PACK_SHRINK, 0);
201     vbox3->pack_start(*hbox5);
202     vbox3->pack_start(*_embedImagesCheck, Gtk::PACK_SHRINK, 0);
203     alignment4->add(*vbox3);
204     _labelImportSettings->set_alignment(0.5,0.5);
205     _labelImportSettings->set_padding(0,0);
206     _labelImportSettings->set_justify(Gtk::JUSTIFY_LEFT);
207     _labelImportSettings->set_line_wrap(false);
208     _labelImportSettings->set_use_markup(true);
209     _labelImportSettings->set_selectable(false);
210     _importSettingsFrame->set_border_width(4);
211     _importSettingsFrame->set_shadow_type(Gtk::SHADOW_ETCHED_IN);
212     _importSettingsFrame->set_label_align(0,0.5);
213     _importSettingsFrame->add(*alignment4);
214     _importSettingsFrame->set_label_widget(*_labelImportSettings);
215     vbox1->pack_start(*_pageSettingsFrame, Gtk::PACK_EXPAND_PADDING, 0);
216     vbox1->pack_start(*_importSettingsFrame, Gtk::PACK_EXPAND_PADDING, 0);
217     hbox1->pack_start(*vbox1);
218     hbox1->pack_start(*_previewArea, Gtk::PACK_EXPAND_WIDGET, 4);
219     this->get_vbox()->set_homogeneous(false);
220     this->get_vbox()->set_spacing(0);
221     this->get_vbox()->pack_start(*hbox1);
222     this->set_title(_("PDF Import Settings"));
223     this->set_modal(true);
224     this->property_window_position().set_value(Gtk::WIN_POS_NONE);
225     this->set_resizable(true);
226     this->property_destroy_with_parent().set_value(false);
227     this->set_has_separator(true);
228     this->add_action_widget(*cancelbutton, -6);
229     this->add_action_widget(*okbutton, -5);
230     cancelbutton->show();
231     okbutton->show();
232     _labelSelect->show();
233     _pageNumberSpin->show();
234     _labelTotalPages->show();
235     hbox2->show();
236     _cropCheck->show();
237     _cropTypeCombo->show();
238     hbox3->show();
239     vbox2->show();
240     alignment3->show();
241     _labelPageSettings->show();
242     _pageSettingsFrame->show();
243     _labelPrecision->show();
244     _fallbackPrecisionSlider->show();
245     _labelPrecisionComment->show();
246     hbox6->show();
247     _labelText->show();
248     _textHandlingCombo->show();
249     hbox5->show();
250     _embedImagesCheck->show();
251     vbox3->show();
252     alignment4->show();
253     _labelImportSettings->show();
254     _importSettingsFrame->show();
255     vbox1->show();
256     _previewArea->show();
257     hbox1->show();
259     // Connect signals
260     _previewArea->signal_expose_event().connect(sigc::mem_fun(*this, &PdfImportDialog::_onExposePreview));
261     _pageNumberSpin_adj->signal_value_changed().connect(sigc::mem_fun(*this, &PdfImportDialog::_onPageNumberChanged));
262     _cropCheck->signal_toggled().connect(sigc::mem_fun(*this, &PdfImportDialog::_onToggleCropping));
263     _fallbackPrecisionSlider_adj->signal_value_changed().connect(sigc::mem_fun(*this, &PdfImportDialog::_onPrecisionChanged));
265     _render_thumb = false;
266 #ifdef HAVE_POPPLER_CAIRO
267     // Create an OutputDev
268     _preview_output_dev = new CairoOutputDev();
269     _preview_output_dev->startDoc(_pdf_doc->getXRef());
270     _cairo_surface = NULL;
271     _render_thumb = true;
272 #endif
274     // Set default preview size
275     _preview_width = 200;
276     _preview_height = 300;
278     // Init preview
279     _thumb_data = NULL;
280     _pageNumberSpin_adj->set_value(1.0);
281     _current_page = 1;
282     _setPreviewPage(_current_page);
285 PdfImportDialog::~PdfImportDialog() {
286 #ifdef HAVE_POPPLER_CAIRO
287     if (_preview_output_dev) {
288         delete _preview_output_dev;
289     }
290     if (_cairo_surface) {
291         cairo_surface_destroy(_cairo_surface);
292     }
293 #endif
294     if (_thumb_data) {
295         if (_render_thumb) {
296             delete _thumb_data;
297         } else {
298             gfree(_thumb_data);
299         }
300     }
303 bool PdfImportDialog::showDialog() {
304     show();
305     gint b = run();
306     hide();
307     if ( b == Gtk::RESPONSE_OK ) {
308         return TRUE;
309     } else {
310         return FALSE;
311     }
314 int PdfImportDialog::getSelectedPage() {
315     return _current_page;
318 /**
319  * \brief Retrieves the current settings into a repr which SvgBuilder will use
320  *        for determining the behaviour desired by the user
321  */
322 void PdfImportDialog::getImportSettings(Inkscape::XML::Node *prefs) {
323     sp_repr_set_svg_double(prefs, "selectedPage", (double)_current_page);
324     if (_cropCheck->get_active()) {
325         Glib::ustring current_choice = _cropTypeCombo->get_active_text();
326         int num_crop_choices = sizeof(crop_setting_choices) / sizeof(crop_setting_choices[0]);
327         int i = 0;
328         for ( ; i < num_crop_choices ; i++ ) {
329             if ( current_choice == crop_setting_choices[i] ) {
330                 break;
331             }
332         }
333         sp_repr_set_svg_double(prefs, "cropTo", (double)i);
334     } else {
335         sp_repr_set_svg_double(prefs, "cropTo", -1.0);
336     }
337     sp_repr_set_svg_double(prefs, "approximationPrecision",
338                            _fallbackPrecisionSlider->get_value());
339     if (_embedImagesCheck->get_active()) {
340         prefs->setAttribute("embedImages", "1");
341     } else {
342         prefs->setAttribute("embedImages", "0");
343     }
346 /**
347  * \brief Redisplay the comment on the current approximation precision setting
348  * Evenly divides the interval of possible values between the available labels.
349  */
350 void PdfImportDialog::_onPrecisionChanged() {
352     static Glib::ustring precision_comments[] = {
353         Glib::ustring(_("rough")),
354         Glib::ustring(_("medium")),
355         Glib::ustring(_("fine")),
356         Glib::ustring(_("very fine"))
357     };
359     double min = _fallbackPrecisionSlider_adj->get_lower();
360     double max = _fallbackPrecisionSlider_adj->get_upper();
361     int num_intervals = sizeof(precision_comments) / sizeof(precision_comments[0]);
362     double interval_len = ( max - min ) / (double)num_intervals;
363     double value = _fallbackPrecisionSlider_adj->get_value();
364     int comment_idx = (int)floor( ( value - min ) / interval_len );
365     _labelPrecisionComment->set_label(precision_comments[comment_idx]);
368 void PdfImportDialog::_onToggleCropping() {
369     _cropTypeCombo->set_sensitive(_cropCheck->get_active());
372 void PdfImportDialog::_onPageNumberChanged() {
373     int page = _pageNumberSpin->get_value_as_int();
374     _current_page = CLAMP(page, 1, _pdf_doc->getCatalog()->getNumPages());
375     _setPreviewPage(_current_page);
378 #ifdef HAVE_POPPLER_CAIRO
379 /**
380  * \brief Copies image data from a Cairo surface to a pixbuf
381  *
382  * Borrowed from libpoppler, from the file poppler-page.cc
383  * Copyright (C) 2005, Red Hat, Inc.
384  *
385  */
386 static void copy_cairo_surface_to_pixbuf (cairo_surface_t *surface,
387                                           unsigned char   *data,
388                                           GdkPixbuf       *pixbuf)
390     int cairo_width, cairo_height, cairo_rowstride;
391     unsigned char *pixbuf_data, *dst, *cairo_data;
392     int pixbuf_rowstride, pixbuf_n_channels;
393     unsigned int *src;
394     int x, y;
396     cairo_width = cairo_image_surface_get_width (surface);
397     cairo_height = cairo_image_surface_get_height (surface);
398     cairo_rowstride = cairo_width * 4;
399     cairo_data = data;
401     pixbuf_data = gdk_pixbuf_get_pixels (pixbuf);
402     pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
403     pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
405     if (cairo_width > gdk_pixbuf_get_width (pixbuf))
406         cairo_width = gdk_pixbuf_get_width (pixbuf);
407     if (cairo_height > gdk_pixbuf_get_height (pixbuf))
408         cairo_height = gdk_pixbuf_get_height (pixbuf);
409     for (y = 0; y < cairo_height; y++)
410     {
411         src = (unsigned int *) (cairo_data + y * cairo_rowstride);
412         dst = pixbuf_data + y * pixbuf_rowstride;
413         for (x = 0; x < cairo_width; x++)
414         {
415             dst[0] = (*src >> 16) & 0xff;
416             dst[1] = (*src >> 8) & 0xff;
417             dst[2] = (*src >> 0) & 0xff;
418             if (pixbuf_n_channels == 4)
419                 dst[3] = (*src >> 24) & 0xff;
420             dst += pixbuf_n_channels;
421             src++;
422         }
423     }
425 #endif
427 /**
428  * \brief Updates the preview area with the previously rendered thumbnail
429  */
430 bool PdfImportDialog::_onExposePreview(GdkEventExpose *event) {
432     // Check if we have a thumbnail at all
433     if (!_thumb_data) {
434         return true;
435     }
437     // Create the pixbuf for the thumbnail
438     Glib::RefPtr<Gdk::Pixbuf> thumb;
439     if (_render_thumb) {
440         thumb = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, true,
441                                     8, _thumb_width, _thumb_height);
442     } else {
443         thumb = Gdk::Pixbuf::create_from_data(_thumb_data, Gdk::COLORSPACE_RGB,
444             false, 8, _thumb_width, _thumb_height, _thumb_rowstride);
445     }
446     if (!thumb) {
447         return true;
448     }
450     // Set background to white
451     if (_render_thumb) {
452         thumb->fill(0xffffffff);
453         Glib::RefPtr<Gdk::Pixmap> back_pixmap = Gdk::Pixmap::create(
454                 _previewArea->get_window(), _thumb_width, _thumb_height, -1);
455         if (!back_pixmap) {
456             return true;
457         }
458         back_pixmap->draw_pixbuf(Glib::RefPtr<Gdk::GC>(), thumb, 0, 0, 0, 0,
459                                  _thumb_width, _thumb_height,
460                                  Gdk::RGB_DITHER_NONE, 0, 0);
461         _previewArea->get_window()->set_back_pixmap(back_pixmap, false);
462         _previewArea->get_window()->clear();
463     }
464 #ifdef HAVE_POPPLER_CAIRO
465     // Copy the thumbnail image from the Cairo surface
466     if (_render_thumb) {
467         copy_cairo_surface_to_pixbuf(_cairo_surface, _thumb_data, thumb->gobj());
468     }
469 #endif
470     _previewArea->get_window()->draw_pixbuf(Glib::RefPtr<Gdk::GC>(), thumb,
471                                             0, 0, 0, _render_thumb ? 0 : 20,
472                                             -1, -1, Gdk::RGB_DITHER_NONE, 0, 0);
474     return true;
477 /**
478  * \brief Renders the given page's thumbnail using Cairo
479  */
480 void PdfImportDialog::_setPreviewPage(int page) {
482     _previewed_page = _pdf_doc->getCatalog()->getPage(page);
483     // Try to get a thumbnail from the PDF if possible
484     if (!_render_thumb) {
485         if (_thumb_data) {
486             gfree(_thumb_data);
487             _thumb_data = NULL;
488         }
489         if (!_previewed_page->loadThumb(&_thumb_data,
490              &_thumb_width, &_thumb_height, &_thumb_rowstride)) {
491             return;
492         }
493         // Redraw preview area
494         _previewArea->set_size_request(_thumb_width, _thumb_height + 20);
495         _previewArea->queue_draw();
496         return;
497     }
498 #ifdef HAVE_POPPLER_CAIRO
499     // Get page size by accounting for rotation
500     double width, height;
501     int rotate = _previewed_page->getRotate();
502     if ( rotate == 90 || rotate == 270 ) {
503         height = _previewed_page->getCropWidth();
504         width = _previewed_page->getCropHeight();
505     } else {
506         width = _previewed_page->getCropWidth();
507         height = _previewed_page->getCropHeight();
508     }
509     // Calculate the needed scaling for the page
510     double scale_x = (double)_preview_width / width;
511     double scale_y = (double)_preview_height / height;
512     double scale_factor = ( scale_x > scale_y ) ? scale_y : scale_x;
513     // Create new Cairo surface
514     _thumb_width = (int)ceil( width * scale_factor );
515     _thumb_height = (int)ceil( height * scale_factor );
516     _thumb_rowstride = _thumb_width * 4;
517     if (_thumb_data) {
518         delete _thumb_data;
519     }
520     _thumb_data = new unsigned char[ _thumb_rowstride * _thumb_height ];
521     if (_cairo_surface) {
522         cairo_surface_destroy(_cairo_surface);
523     }
524     _cairo_surface = cairo_image_surface_create_for_data(_thumb_data,
525             CAIRO_FORMAT_ARGB32, _thumb_width, _thumb_height, _thumb_rowstride);
526     cairo_t *cr = cairo_create(_cairo_surface);
527     cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);  // Set fill color to white
528     cairo_paint(cr);    // Clear it
529     cairo_scale(cr, scale_factor, scale_factor);    // Use Cairo for resizing the image
530     _preview_output_dev->setCairo(cr);
531     // Render page
532     _previewed_page->displaySlice(_preview_output_dev,
533                        72.0, 72.0, 0,
534                        FALSE, /* useMediaBox */
535                        TRUE, /* crop */
536                        0, 0,
537                        (int)ceil(_previewed_page->getCropWidth()),
538                        (int)ceil(_previewed_page->getCropHeight()),
539                        FALSE, /* printing */
540                        _pdf_doc->getCatalog());
541     // Clean up
542     _preview_output_dev->setCairo(NULL);
543     cairo_destroy(cr);
544     // Redraw preview area
545     _previewArea->set_size_request(_preview_width, _preview_height);
546     _previewArea->queue_draw();
547 #endif
550 ////////////////////////////////////////////////////////////////////////////////
552 /**
553  * Parses the selected page of the given PDF document using PdfParser.
554  */
555 SPDocument *
556 PdfInput::open(::Inkscape::Extension::Input * mod, const gchar * uri) {
558     // Initialize the globalParams variable for poppler
559     if (!globalParams) {
560         globalParams = new GlobalParams();
561     }
562     GooString *filename_goo = new GooString(uri);
563     PDFDoc *pdf_doc = new PDFDoc(filename_goo, NULL, NULL, NULL);   // TODO: Could ask for password
564     if (!pdf_doc->isOk()) {
565         int error = pdf_doc->getErrorCode();
566         delete pdf_doc;
567         if (error == errEncrypted) {
568             g_message("Document is encrypted.");
569         } else {
570             g_message("Failed to load document from data (error %d)", error);
571         }
572  
573         return NULL;
574     }
575     PdfImportDialog *dlg = new PdfImportDialog(pdf_doc);
576     if (!dlg->showDialog()) {
577         delete dlg;
578         delete pdf_doc;
580         return NULL;
581     }
583     // Get needed page
584     int page_num = dlg->getSelectedPage();
585     Catalog *catalog = pdf_doc->getCatalog();
586     Page *page = catalog->getPage(page_num);
588     SPDocument *doc = sp_document_new(NULL, TRUE, TRUE);
589     bool saved = sp_document_get_undo_sensitive(doc);
590     sp_document_set_undo_sensitive(doc, false); // No need to undo in this temporary document
592     // Create builder
593     gchar *docname = g_path_get_basename(uri);
594     gchar *dot = g_strrstr(docname, ".");
595     if (dot) {
596         *dot = 0;
597     }
598     SvgBuilder *builder = new SvgBuilder(doc, docname, pdf_doc->getXRef());
600     // Get preferences
601     Inkscape::XML::Node *prefs = builder->getPreferences();
602     dlg->getImportSettings(prefs);
604     // Apply crop settings
605     PDFRectangle *clipToBox = NULL;
606     double crop_setting;
607     sp_repr_get_double(prefs, "cropTo", &crop_setting);
608     if ( crop_setting >= 0.0 ) {    // Do page clipping
609         int crop_choice = (int)crop_setting;
610         switch (crop_choice) {
611             case 0: // Media box
612                 clipToBox = page->getMediaBox();
613                 break;
614             case 1: // Crop box
615                 clipToBox = page->getCropBox();
616                 break;
617             case 2: // Bleed box
618                 clipToBox = page->getBleedBox();
619                 break;
620             case 3: // Trim box
621                 clipToBox = page->getTrimBox();
622                 break;
623             case 4: // Art box
624                 clipToBox = page->getArtBox();
625                 break;
626             default:
627                 break;
628         }
629     }
631     // Create parser
632     PdfParser *pdf_parser = new PdfParser(pdf_doc->getXRef(), builder, page_num-1, page->getRotate(),
633                                           page->getResourceDict(), page->getCropBox(), clipToBox);
635     // Set up approximation precision for parser
636     double color_delta;
637     sp_repr_get_double(prefs, "approximationPrecision", &color_delta);
638     if ( color_delta <= 0.0 ) {
639         color_delta = 1.0 / 2.0;
640     } else {
641         color_delta = 1.0 / color_delta;
642     }
643     for ( int i = 1 ; i <= pdfNumShadingTypes ; i++ ) {
644         pdf_parser->setApproximationPrecision(i, color_delta, 6);
645     }
646     
647     // Parse the document structure
648     Object obj;
649     page->getContents(&obj);
650     if (!obj.isNull()) {
651         pdf_parser->parse(&obj);
652     }
653     
654     // Cleanup
655     obj.free();
656     delete pdf_parser;
657     delete builder;
658     g_free(docname);
659     delete pdf_doc;
661     // Restore undo
662     sp_document_set_undo_sensitive(doc, saved);
664     return doc;
667 #include "../clear-n_.h"
669 void
670 PdfInput::init(void) {
671     Inkscape::Extension::Extension * ext;
673     /* PDF in */
674     ext = Inkscape::Extension::build_from_mem(
675         "<inkscape-extension>\n"
676             "<name>PDF Input</name>\n"
677             "<id>org.inkscape.input.pdf</id>\n"
678             "<input>\n"
679                 "<extension>.pdf</extension>\n"
680                 "<mimetype>application/pdf</mimetype>\n"
681                 "<filetypename>Adobe PDF (*.pdf) [via poppler]</filetypename>\n"
682                 "<filetypetooltip>Adobe Portable Document Format</filetypetooltip>\n"
683             "</input>\n"
684         "</inkscape-extension>", new PdfInput());
686     /* AI in */
687     ext = Inkscape::Extension::build_from_mem(
688         "<inkscape-extension>\n"
689             "<name>AI Input</name>\n"
690             "<id>org.inkscape.input.ai</id>\n"
691             "<input>\n"
692                 "<extension>.ai</extension>\n"
693                 "<mimetype>image/x-adobe-illustrator</mimetype>\n"
694                 "<filetypename>Adobe Illustrator (*.ai) [PDF-based]</filetypename>\n"
695                 "<filetypetooltip>Open files saved with recent versions of Adobe Illustrator</filetypetooltip>\n"
696             "</input>\n"
697         "</inkscape-extension>", new PdfInput());
698 } // init
700 } } }  /* namespace Inkscape, Extension, Implementation */
702 #endif /* HAVE_POPPLER */
704 /*
705   Local Variables:
706   mode:c++
707   c-file-style:"stroustrup"
708   c-file-offsets:((innamespace . 0)(inline-open . 0))
709   indent-tabs-mode:nil
710   fill-column:99
711   End:
712 */
713 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :