Code

Clear Cairo surface to white before rendering 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);
283 PdfImportDialog::~PdfImportDialog() {
284 #ifdef HAVE_POPPLER_CAIRO
285     if (_preview_output_dev) {
286         delete _preview_output_dev;
287     }
288     if (_cairo_surface) {
289         cairo_surface_destroy(_cairo_surface);
290     }
291 #endif
292     if (_thumb_data) {
293         if (_render_thumb) {
294             delete _thumb_data;
295         } else {
296             gfree(_thumb_data);
297         }
298     }
301 bool PdfImportDialog::showDialog() {
302     show();
303     gint b = run();
304     hide();
305     if ( b == Gtk::RESPONSE_OK ) {
306         return TRUE;
307     } else {
308         return FALSE;
309     }
312 int PdfImportDialog::getSelectedPage() {
313     return _current_page;
316 /**
317  * \brief Retrieves the current settings into a repr which SvgBuilder will use
318  *        for determining the behaviour desired by the user
319  */
320 void PdfImportDialog::getImportSettings(Inkscape::XML::Node *prefs) {
321     sp_repr_set_svg_double(prefs, "selectedPage", (double)_current_page);
322     if (_cropCheck->get_active()) {
323         Glib::ustring current_choice = _cropTypeCombo->get_active_text();
324         int num_crop_choices = sizeof(crop_setting_choices) / sizeof(crop_setting_choices[0]);
325         int i = 0;
326         for ( ; i < num_crop_choices ; i++ ) {
327             if ( current_choice == crop_setting_choices[i] ) {
328                 break;
329             }
330         }
331         sp_repr_set_svg_double(prefs, "cropTo", (double)i);
332     } else {
333         sp_repr_set_svg_double(prefs, "cropTo", -1.0);
334     }
335     sp_repr_set_svg_double(prefs, "approximationPrecision",
336                            _fallbackPrecisionSlider->get_value());
337     if (_embedImagesCheck->get_active()) {
338         prefs->setAttribute("embedImages", "1");
339     } else {
340         prefs->setAttribute("embedImages", "0");
341     }
344 /**
345  * \brief Redisplay the comment on the current approximation precision setting
346  * Evenly divides the interval of possible values between the available labels.
347  */
348 void PdfImportDialog::_onPrecisionChanged() {
350     static Glib::ustring precision_comments[] = {
351         Glib::ustring(_("rough")),
352         Glib::ustring(_("medium")),
353         Glib::ustring(_("fine")),
354         Glib::ustring(_("very fine"))
355     };
357     double min = _fallbackPrecisionSlider_adj->get_lower();
358     double max = _fallbackPrecisionSlider_adj->get_upper();
359     int num_intervals = sizeof(precision_comments) / sizeof(precision_comments[0]);
360     double interval_len = ( max - min ) / (double)num_intervals;
361     double value = _fallbackPrecisionSlider_adj->get_value();
362     int comment_idx = (int)floor( ( value - min ) / interval_len );
363     _labelPrecisionComment->set_label(precision_comments[comment_idx]);
366 void PdfImportDialog::_onToggleCropping() {
367     _cropTypeCombo->set_sensitive(_cropCheck->get_active());
370 void PdfImportDialog::_onPageNumberChanged() {
371     int page = _pageNumberSpin->get_value_as_int();
372     _current_page = CLAMP(page, 1, _pdf_doc->getCatalog()->getNumPages());
373     _setPreviewPage(_current_page);
376 #ifdef HAVE_POPPLER_CAIRO
377 /**
378  * \brief Copies image data from a Cairo surface to a pixbuf
379  *
380  * Borrowed from libpoppler, from the file poppler-page.cc
381  * Copyright (C) 2005, Red Hat, Inc.
382  *
383  */
384 static void copy_cairo_surface_to_pixbuf (cairo_surface_t *surface,
385                                           unsigned char   *data,
386                                           GdkPixbuf       *pixbuf)
388     int cairo_width, cairo_height, cairo_rowstride;
389     unsigned char *pixbuf_data, *dst, *cairo_data;
390     int pixbuf_rowstride, pixbuf_n_channels;
391     unsigned int *src;
392     int x, y;
394     cairo_width = cairo_image_surface_get_width (surface);
395     cairo_height = cairo_image_surface_get_height (surface);
396     cairo_rowstride = cairo_width * 4;
397     cairo_data = data;
399     pixbuf_data = gdk_pixbuf_get_pixels (pixbuf);
400     pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
401     pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
403     if (cairo_width > gdk_pixbuf_get_width (pixbuf))
404         cairo_width = gdk_pixbuf_get_width (pixbuf);
405     if (cairo_height > gdk_pixbuf_get_height (pixbuf))
406         cairo_height = gdk_pixbuf_get_height (pixbuf);
407     for (y = 0; y < cairo_height; y++)
408     {
409         src = (unsigned int *) (cairo_data + y * cairo_rowstride);
410         dst = pixbuf_data + y * pixbuf_rowstride;
411         for (x = 0; x < cairo_width; x++)
412         {
413             dst[0] = (*src >> 16) & 0xff;
414             dst[1] = (*src >> 8) & 0xff;
415             dst[2] = (*src >> 0) & 0xff;
416             if (pixbuf_n_channels == 4)
417                 dst[3] = (*src >> 24) & 0xff;
418             dst += pixbuf_n_channels;
419             src++;
420         }
421     }
423 #endif
425 /**
426  * \brief Updates the preview area with the previously rendered thumbnail
427  */
428 bool PdfImportDialog::_onExposePreview(GdkEventExpose *event) {
430     // Check if we have a thumbnail at all
431     if (!_thumb_data) {
432         return true;
433     }
435     // Create the pixbuf for the thumbnail
436     Glib::RefPtr<Gdk::Pixbuf> thumb;
437     if (_render_thumb) {
438         thumb = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, true,
439                                     8, _thumb_width, _thumb_height);
440     } else {
441         thumb = Gdk::Pixbuf::create_from_data(_thumb_data, Gdk::COLORSPACE_RGB,
442             false, 8, _thumb_width, _thumb_height, _thumb_rowstride);
443     }
444     if (!thumb) {
445         return true;
446     }
448     // Set background to white
449     if (_render_thumb) {
450         thumb->fill(0xffffffff);
451         Glib::RefPtr<Gdk::Pixmap> back_pixmap = Gdk::Pixmap::create(
452                 _previewArea->get_window(), _thumb_width, _thumb_height, -1);
453         if (!back_pixmap) {
454             return true;
455         }
456         back_pixmap->draw_pixbuf(Glib::RefPtr<Gdk::GC>(), thumb, 0, 0, 0, 0,
457                                  _thumb_width, _thumb_height,
458                                  Gdk::RGB_DITHER_NONE, 0, 0);
459         _previewArea->get_window()->set_back_pixmap(back_pixmap, false);
460         _previewArea->get_window()->clear();
461     }
462 #ifdef HAVE_POPPLER_CAIRO
463     // Copy the thumbnail image from the Cairo surface
464     if (_render_thumb) {
465         copy_cairo_surface_to_pixbuf(_cairo_surface, _thumb_data, thumb->gobj());
466     }
467 #endif
468     _previewArea->get_window()->draw_pixbuf(Glib::RefPtr<Gdk::GC>(), thumb,
469                                             0, 0, 0, _render_thumb ? 0 : 20,
470                                             -1, -1, Gdk::RGB_DITHER_NONE, 0, 0);
472     return true;
475 /**
476  * \brief Renders the given page's thumbnail using Cairo
477  */
478 void PdfImportDialog::_setPreviewPage(int page) {
480     _previewed_page = _pdf_doc->getCatalog()->getPage(page);
481     // Try to get a thumbnail from the PDF if possible
482     if (!_render_thumb) {
483         if (_thumb_data) {
484             gfree(_thumb_data);
485             _thumb_data = NULL;
486         }
487         if (!_previewed_page->loadThumb(&_thumb_data,
488              &_thumb_width, &_thumb_height, &_thumb_rowstride)) {
489             return;
490         }
491         // Redraw preview area
492         _previewArea->set_size_request(_thumb_width, _thumb_height + 20);
493         _previewArea->queue_draw();
494         return;
495     }
496 #ifdef HAVE_POPPLER_CAIRO
497     // Get page size by accounting for rotation
498     double width, height;
499     int rotate = _previewed_page->getRotate();
500     if ( rotate == 90 || rotate == 270 ) {
501         height = _previewed_page->getCropWidth();
502         width = _previewed_page->getCropHeight();
503     } else {
504         width = _previewed_page->getCropWidth();
505         height = _previewed_page->getCropHeight();
506     }
507     // Calculate the needed scaling for the page
508     double scale_x = (double)_preview_width / width;
509     double scale_y = (double)_preview_height / height;
510     double scale_factor = ( scale_x > scale_y ) ? scale_y : scale_x;
511     // Create new Cairo surface
512     _thumb_width = (int)ceil( width * scale_factor );
513     _thumb_height = (int)ceil( height * scale_factor );
514     _thumb_rowstride = _thumb_width * 4;
515     if (_thumb_data) {
516         delete _thumb_data;
517     }
518     _thumb_data = new unsigned char[ _thumb_rowstride * _thumb_height ];
519     if (_cairo_surface) {
520         cairo_surface_destroy(_cairo_surface);
521     }
522     _cairo_surface = cairo_image_surface_create_for_data(_thumb_data,
523             CAIRO_FORMAT_ARGB32, _thumb_width, _thumb_height, _thumb_rowstride);
524     cairo_t *cr = cairo_create(_cairo_surface);
525     cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);  // Set fill color to white
526     cairo_paint(cr);    // Clear it
527     cairo_scale(cr, scale_factor, scale_factor);    // Use Cairo for resizing the image
528     _preview_output_dev->setCairo(cr);
529     // Render page
530     _previewed_page->displaySlice(_preview_output_dev,
531                        72.0, 72.0, 0,
532                        FALSE, /* useMediaBox */
533                        TRUE, /* crop */
534                        0, 0,
535                        (int)ceil(_previewed_page->getCropWidth()),
536                        (int)ceil(_previewed_page->getCropHeight()),
537                        FALSE, /* printing */
538                        _pdf_doc->getCatalog());
539     // Clean up
540     _preview_output_dev->setCairo(NULL);
541     cairo_destroy(cr);
542     // Redraw preview area
543     _previewArea->set_size_request(_preview_width, _preview_height);
544     _previewArea->queue_draw();
545 #endif
548 ////////////////////////////////////////////////////////////////////////////////
550 /**
551  * Parses the selected page of the given PDF document using PdfParser.
552  */
553 SPDocument *
554 PdfInput::open(::Inkscape::Extension::Input * mod, const gchar * uri) {
556     // Initialize the globalParams variable for poppler
557     if (!globalParams) {
558         globalParams = new GlobalParams();
559     }
560     GooString *filename_goo = new GooString(uri);
561     PDFDoc *pdf_doc = new PDFDoc(filename_goo, NULL, NULL, NULL);   // TODO: Could ask for password
562     if (!pdf_doc->isOk()) {
563         int error = pdf_doc->getErrorCode();
564         delete pdf_doc;
565         if (error == errEncrypted) {
566             g_message("Document is encrypted.");
567         } else {
568             g_message("Failed to load document from data (error %d)", error);
569         }
570  
571         return NULL;
572     }
573     PdfImportDialog *dlg = new PdfImportDialog(pdf_doc);
574     if (!dlg->showDialog()) {
575         delete dlg;
576         delete pdf_doc;
578         return NULL;
579     }
581     // Get needed page
582     int page_num = dlg->getSelectedPage();
583     Catalog *catalog = pdf_doc->getCatalog();
584     Page *page = catalog->getPage(page_num);
586     SPDocument *doc = sp_document_new(NULL, TRUE, TRUE);
587     bool saved = sp_document_get_undo_sensitive(doc);
588     sp_document_set_undo_sensitive(doc, false); // No need to undo in this temporary document
590     // Create builder
591     gchar *docname = g_path_get_basename(uri);
592     gchar *dot = g_strrstr(docname, ".");
593     if (dot) {
594         *dot = 0;
595     }
596     SvgBuilder *builder = new SvgBuilder(doc, docname, pdf_doc->getXRef());
598     // Get preferences
599     Inkscape::XML::Node *prefs = builder->getPreferences();
600     dlg->getImportSettings(prefs);
602     // Apply crop settings
603     PDFRectangle *clipToBox = NULL;
604     double crop_setting;
605     sp_repr_get_double(prefs, "cropTo", &crop_setting);
606     if ( crop_setting >= 0.0 ) {    // Do page clipping
607         int crop_choice = (int)crop_setting;
608         switch (crop_choice) {
609             case 0: // Media box
610                 clipToBox = page->getMediaBox();
611                 break;
612             case 1: // Crop box
613                 clipToBox = page->getCropBox();
614                 break;
615             case 2: // Bleed box
616                 clipToBox = page->getBleedBox();
617                 break;
618             case 3: // Trim box
619                 clipToBox = page->getTrimBox();
620                 break;
621             case 4: // Art box
622                 clipToBox = page->getArtBox();
623                 break;
624             default:
625                 break;
626         }
627     }
629     // Create parser
630     PdfParser *pdf_parser = new PdfParser(pdf_doc->getXRef(), builder, page_num-1, page->getRotate(),
631                                           page->getResourceDict(), page->getCropBox(), clipToBox);
633     // Set up approximation precision for parser
634     double color_delta;
635     sp_repr_get_double(prefs, "approximationPrecision", &color_delta);
636     if ( color_delta <= 0.0 ) {
637         color_delta = 1.0 / 2.0;
638     } else {
639         color_delta = 1.0 / color_delta;
640     }
641     for ( int i = 1 ; i <= pdfNumShadingTypes ; i++ ) {
642         pdf_parser->setApproximationPrecision(i, color_delta, 6);
643     }
644     
645     // Parse the document structure
646     Object obj;
647     page->getContents(&obj);
648     if (!obj.isNull()) {
649         pdf_parser->parse(&obj);
650     }
651     
652     // Cleanup
653     obj.free();
654     delete pdf_parser;
655     delete builder;
656     g_free(docname);
657     delete pdf_doc;
659     // Restore undo
660     sp_document_set_undo_sensitive(doc, saved);
662     return doc;
665 #include "../clear-n_.h"
667 void
668 PdfInput::init(void) {
669     Inkscape::Extension::Extension * ext;
671     /* PDF in */
672     ext = Inkscape::Extension::build_from_mem(
673         "<inkscape-extension>\n"
674             "<name>PDF Input</name>\n"
675             "<id>org.inkscape.input.pdf</id>\n"
676             "<input>\n"
677                 "<extension>.pdf</extension>\n"
678                 "<mimetype>application/pdf</mimetype>\n"
679                 "<filetypename>Adobe PDF (*.pdf) [via poppler]</filetypename>\n"
680                 "<filetypetooltip>Adobe Portable Document Format</filetypetooltip>\n"
681             "</input>\n"
682         "</inkscape-extension>", new PdfInput());
684     /* AI in */
685     ext = Inkscape::Extension::build_from_mem(
686         "<inkscape-extension>\n"
687             "<name>AI Input</name>\n"
688             "<id>org.inkscape.input.ai</id>\n"
689             "<input>\n"
690                 "<extension>.ai</extension>\n"
691                 "<mimetype>image/x-adobe-illustrator</mimetype>\n"
692                 "<filetypename>Adobe Illustrator (*.ai) [PDF-based]</filetypename>\n"
693                 "<filetypetooltip>Open files saved with recent versions of Adobe Illustrator</filetypetooltip>\n"
694             "</input>\n"
695         "</inkscape-extension>", new PdfInput());
696 } // init
698 } } }  /* namespace Inkscape, Extension, Implementation */
700 #endif /* HAVE_POPPLER */
702 /*
703   Local Variables:
704   mode:c++
705   c-file-style:"stroustrup"
706   c-file-offsets:((innamespace . 0)(inline-open . 0))
707   indent-tabs-mode:nil
708   fill-column:99
709   End:
710 */
711 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :