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