Code

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