Code

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