Code

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