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