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