pdf-input-cairo.cpp revision f466f1e98686d23a3bbe572e4fb227e64c6436f3
5909N/A /*
5909N/A * Simple PDF import extension using libpoppler and Cairo's SVG surface.
5909N/A *
5909N/A * Authors:
5909N/A * miklos erdelyi
5909N/A * Abhishek Sharma
5909N/A *
5909N/A * Copyright (C) 2007 Authors
5909N/A *
5909N/A * Released under GNU GPL, read the file 'COPYING' for more information
5909N/A *
5909N/A */
5909N/A
5909N/A#ifdef HAVE_CONFIG_H
5909N/A# include <config.h>
5909N/A#endif
5909N/A
5909N/A#ifdef HAVE_POPPLER_GLIB
5909N/A#ifdef HAVE_POPPLER_CAIRO
5909N/A
5909N/A#include "pdf-input-cairo.h"
5909N/A#include "extension/system.h"
5909N/A#include "extension/input.h"
5909N/A#include "dialogs/dialog-events.h"
5909N/A#include "document.h"
5909N/A
5909N/A#include "inkscape.h"
5909N/A
5909N/A#include <cairo-svg.h>
5909N/A#include <poppler/glib/poppler.h>
5909N/A#include <poppler/glib/poppler-document.h>
5909N/A#include <poppler/glib/poppler-page.h>
5909N/A
5909N/A#include "ui/widget/spinbutton.h"
5909N/A#include "ui/widget/frame.h"
5909N/A
5909N/A#include <gdkmm/general.h>
5909N/A
5909N/Anamespace Inkscape {
5909N/Anamespace Extension {
5909N/Anamespace Internal {
5909N/A
5909N/A
5909N/A/**
5909N/A * \brief The PDF import dialog
5909N/A * FIXME: Probably this should be placed into src/ui/dialog
5909N/A */
5909N/A
5994N/Astatic const gchar * crop_setting_choices[] = {
5994N/A //TRANSLATORS: The following are document crop settings for PDF import
5994N/A // more info: http://www.acrobatusers.com/tech_corners/javascript_corner/tips/2006/page_bounds/
5994N/A N_("media box"),
5994N/A N_("crop box"),
5909N/A N_("trim box"),
5909N/A N_("bleed box"),
5909N/A N_("art box")
5909N/A};
5909N/A
5909N/APdfImportCairoDialog::PdfImportCairoDialog(PopplerDocument *doc)
5909N/A{
5909N/A if(doc == NULL) {
5909N/A // if there is no document, throw exception here
5909N/A throw;
5909N/A }
5909N/A
5909N/A _poppler_doc = doc;
5909N/A
5909N/A cancelbutton = Gtk::manage(new class Gtk::Button(Gtk::StockID("gtk-cancel")));
5909N/A okbutton = Gtk::manage(new class Gtk::Button(Gtk::StockID("gtk-ok")));
5909N/A _labelSelect = Gtk::manage(new class Gtk::Label(_("Select page:")));
5909N/A
5909N/A // Page number
5909N/A int num_pages = poppler_document_get_n_pages(_poppler_doc);
5909N/A Gtk::Adjustment *_pageNumberSpin_adj = Gtk::manage(new class Gtk::Adjustment(1, 1, num_pages, 1, 10, 0));
5994N/A _pageNumberSpin = Gtk::manage(new class Inkscape::UI::Widget::SpinButton(*_pageNumberSpin_adj, 1, 1));
5994N/A _labelTotalPages = Gtk::manage(new class Gtk::Label());
5994N/A hbox2 = Gtk::manage(new class Gtk::HBox(false, 0));
5994N/A // Disable the page selector when there's only one page
5994N/A if ( num_pages == 1 ) {
5994N/A _pageNumberSpin->set_sensitive(false);
5994N/A } else {
5994N/A // Display total number of pages
5909N/A gchar *label_text = g_strdup_printf(_("out of %i"), num_pages);
5909N/A _labelTotalPages->set_label(label_text);
5909N/A g_free(label_text);
5909N/A }
5909N/A
5909N/A // Crop settings
5909N/A _cropCheck = Gtk::manage(new class Gtk::CheckButton(_("Clip to:")));
5909N/A _cropTypeCombo = Gtk::manage(new class Gtk::ComboBoxText());
5909N/A int num_crop_choices = sizeof(crop_setting_choices) / sizeof(crop_setting_choices[0]);
5909N/A for ( int i = 0 ; i < num_crop_choices ; i++ ) {
5909N/A#if WITH_GTKMM_2_24
5909N/A _cropTypeCombo->append(_(crop_setting_choices[i]));
5909N/A#else
5909N/A _cropTypeCombo->append_text(_(crop_setting_choices[i]));
5909N/A#endif
5909N/A }
5994N/A _cropTypeCombo->set_active_text(_(crop_setting_choices[0]));
5909N/A _cropTypeCombo->set_sensitive(false);
5909N/A
5909N/A hbox3 = Gtk::manage(new class Gtk::HBox(false, 4));
5909N/A vbox2 = Gtk::manage(new class Gtk::VBox(false, 4));
5909N/A _pageSettingsFrame = Gtk::manage(new class Inkscape::UI::Widget::Frame(_("Page settings")));
5909N/A _labelPrecision = Gtk::manage(new class Gtk::Label(_("Precision of approximating gradient meshes:")));
5909N/A _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.")));
5909N/A
5909N/A _fallbackPrecisionSlider_adj = Gtk::manage(new class Gtk::Adjustment(2, 1, 256, 1, 10, 10));
5909N/A _fallbackPrecisionSlider = Gtk::manage(new class Gtk::HScale(*_fallbackPrecisionSlider_adj));
5909N/A _fallbackPrecisionSlider->set_value(2.0);
5909N/A _labelPrecisionComment = Gtk::manage(new class Gtk::Label(_("rough")));
5994N/A hbox6 = Gtk::manage(new class Gtk::HBox(false, 4));
5909N/A
5909N/A // Text options
5909N/A _labelText = Gtk::manage(new class Gtk::Label(_("Text handling:")));
5909N/A _textHandlingCombo = Gtk::manage(new class Gtk::ComboBoxText());
5909N/A#if WITH_GTKMM_2_24
5909N/A _textHandlingCombo->append(_("Import text as text"));
5909N/A#else
5909N/A _textHandlingCombo->append_text(_("Import text as text"));
5909N/A#endif
5909N/A _textHandlingCombo->set_active_text(_("Import text as text"));
5909N/A _localFontsCheck = Gtk::manage(new class Gtk::CheckButton(_("Replace PDF fonts by closest-named installed fonts")));
5909N/A
5909N/A hbox5 = Gtk::manage(new class Gtk::HBox(false, 4));
5909N/A _embedImagesCheck = Gtk::manage(new class Gtk::CheckButton(_("Embed images")));
5909N/A vbox3 = Gtk::manage(new class Gtk::VBox(false, 4));
5994N/A _importSettingsFrame = Gtk::manage(new class Inkscape::UI::Widget::Frame(_("Import settings")));
5994N/A vbox1 = Gtk::manage(new class Gtk::VBox(false, 4));
5994N/A _previewArea = Gtk::manage(new class Gtk::DrawingArea());
5994N/A hbox1 = Gtk::manage(new class Gtk::HBox(false, 4));
5994N/A cancelbutton->set_can_focus();
5909N/A cancelbutton->set_can_default();
5909N/A cancelbutton->set_relief(Gtk::RELIEF_NORMAL);
5909N/A okbutton->set_can_focus();
5909N/A okbutton->set_can_default();
5909N/A okbutton->set_relief(Gtk::RELIEF_NORMAL);
5909N/A this->get_action_area()->property_layout_style().set_value(Gtk::BUTTONBOX_END);
5909N/A _labelSelect->set_alignment(0.5,0.5);
5909N/A _labelSelect->set_padding(4,0);
5909N/A _labelSelect->set_justify(Gtk::JUSTIFY_LEFT);
5909N/A _labelSelect->set_line_wrap(false);
5909N/A _labelSelect->set_use_markup(false);
5909N/A _labelSelect->set_selectable(false);
5909N/A _pageNumberSpin->set_can_focus();
5909N/A _pageNumberSpin->set_update_policy(Gtk::UPDATE_ALWAYS);
5909N/A _pageNumberSpin->set_numeric(true);
5909N/A _pageNumberSpin->set_digits(0);
5909N/A _pageNumberSpin->set_wrap(false);
5909N/A _labelTotalPages->set_alignment(0.5,0.5);
5909N/A _labelTotalPages->set_padding(4,0);
5909N/A _labelTotalPages->set_justify(Gtk::JUSTIFY_LEFT);
5909N/A _labelTotalPages->set_line_wrap(false);
5909N/A _labelTotalPages->set_use_markup(false);
5909N/A _labelTotalPages->set_selectable(false);
5909N/A hbox2->pack_start(*_labelSelect, Gtk::PACK_SHRINK, 4);
5909N/A hbox2->pack_start(*_pageNumberSpin, Gtk::PACK_SHRINK, 4);
5909N/A hbox2->pack_start(*_labelTotalPages, Gtk::PACK_SHRINK, 4);
5909N/A _cropCheck->set_can_focus();
5909N/A _cropCheck->set_relief(Gtk::RELIEF_NORMAL);
5909N/A _cropCheck->set_mode(true);
5909N/A _cropCheck->set_active(false);
5909N/A _cropTypeCombo->set_border_width(1);
5909N/A hbox3->pack_start(*_cropCheck, Gtk::PACK_SHRINK, 4);
5909N/A hbox3->pack_start(*_cropTypeCombo, Gtk::PACK_SHRINK, 0);
5909N/A vbox2->pack_start(*hbox2);
5909N/A vbox2->pack_start(*hbox3);
5909N/A _pageSettingsFrame->add(*vbox2);
5909N/A _pageSettingsFrame->set_border_width(4);
5909N/A _labelPrecision->set_alignment(0,0.5);
5909N/A _labelPrecision->set_padding(4,0);
5909N/A _labelPrecision->set_justify(Gtk::JUSTIFY_LEFT);
5909N/A _labelPrecision->set_line_wrap(true);
5909N/A _labelPrecision->set_use_markup(false);
5909N/A _labelPrecision->set_selectable(false);
5909N/A _labelPrecisionWarning->set_alignment(0,0.5);
5909N/A _labelPrecisionWarning->set_padding(4,0);
5909N/A _labelPrecisionWarning->set_justify(Gtk::JUSTIFY_LEFT);
5909N/A _labelPrecisionWarning->set_line_wrap(true);
5909N/A _labelPrecisionWarning->set_use_markup(true);
5909N/A _labelPrecisionWarning->set_selectable(false);
5909N/A _fallbackPrecisionSlider->set_size_request(180,-1);
5909N/A _fallbackPrecisionSlider->set_can_focus();
5909N/A _fallbackPrecisionSlider->set_inverted(false);
5909N/A _fallbackPrecisionSlider->set_digits(1);
5909N/A _fallbackPrecisionSlider->set_draw_value(true);
5909N/A _fallbackPrecisionSlider->set_value_pos(Gtk::POS_TOP);
5909N/A _labelPrecisionComment->set_size_request(90,-1);
5909N/A _labelPrecisionComment->set_alignment(0.5,0.5);
5909N/A _labelPrecisionComment->set_padding(4,0);
5909N/A _labelPrecisionComment->set_justify(Gtk::JUSTIFY_LEFT);
5909N/A _labelPrecisionComment->set_line_wrap(false);
5909N/A _labelPrecisionComment->set_use_markup(false);
5909N/A _labelPrecisionComment->set_selectable(false);
5909N/A hbox6->pack_start(*_fallbackPrecisionSlider, Gtk::PACK_SHRINK, 4);
5909N/A hbox6->pack_start(*_labelPrecisionComment, Gtk::PACK_SHRINK, 0);
5909N/A _labelText->set_alignment(0.5,0.5);
5909N/A _labelText->set_padding(4,0);
5909N/A _labelText->set_justify(Gtk::JUSTIFY_LEFT);
5909N/A _labelText->set_line_wrap(false);
5909N/A _labelText->set_use_markup(false);
5909N/A _labelText->set_selectable(false);
5909N/A hbox5->pack_start(*_labelText, Gtk::PACK_SHRINK, 0);
5909N/A hbox5->pack_start(*_textHandlingCombo, Gtk::PACK_SHRINK, 0);
5909N/A _localFontsCheck->set_can_focus();
5909N/A _localFontsCheck->set_relief(Gtk::RELIEF_NORMAL);
5909N/A _localFontsCheck->set_mode(true);
5909N/A _localFontsCheck->set_active(true);
5909N/A _embedImagesCheck->set_can_focus();
5909N/A _embedImagesCheck->set_relief(Gtk::RELIEF_NORMAL);
5909N/A _embedImagesCheck->set_mode(true);
5909N/A _embedImagesCheck->set_active(true);
5909N/A vbox3->pack_start(*_labelPrecision, Gtk::PACK_SHRINK, 0);
5909N/A vbox3->pack_start(*hbox6, Gtk::PACK_SHRINK, 0);
5909N/A vbox3->pack_start(*_labelPrecisionWarning, Gtk::PACK_SHRINK, 0);
5909N/A vbox3->pack_start(*hbox5, Gtk::PACK_SHRINK, 4);
5909N/A vbox3->pack_start(*_localFontsCheck, Gtk::PACK_SHRINK, 0);
5909N/A vbox3->pack_start(*_embedImagesCheck, Gtk::PACK_SHRINK, 0);
5909N/A _importSettingsFrame->add(*vbox3);
5909N/A _importSettingsFrame->set_border_width(4);
5909N/A vbox1->pack_start(*_pageSettingsFrame, Gtk::PACK_EXPAND_PADDING, 0);
5909N/A vbox1->pack_start(*_importSettingsFrame, Gtk::PACK_EXPAND_PADDING, 0);
5909N/A hbox1->pack_start(*vbox1);
5909N/A hbox1->pack_start(*_previewArea, Gtk::PACK_EXPAND_WIDGET, 4);
5909N/A this->get_vbox()->set_homogeneous(false);
5909N/A this->get_vbox()->set_spacing(0);
5909N/A this->get_vbox()->pack_start(*hbox1);
5909N/A this->set_title(_("PDF Import Settings"));
5909N/A this->set_modal(true);
5909N/A sp_transientize((GtkWidget *)this->gobj()); //Make transient
5909N/A this->property_window_position().set_value(Gtk::WIN_POS_NONE);
5909N/A this->set_resizable(true);
5909N/A this->property_destroy_with_parent().set_value(false);
5909N/A this->set_has_separator(true);
5909N/A this->add_action_widget(*cancelbutton, -6);
5909N/A this->add_action_widget(*okbutton, -5);
5909N/A cancelbutton->show();
5909N/A okbutton->show();
5909N/A _labelSelect->show();
5909N/A _pageNumberSpin->show();
5909N/A _labelTotalPages->show();
5909N/A hbox2->show();
5909N/A _cropCheck->show();
5909N/A _cropTypeCombo->show();
5909N/A hbox3->show();
5909N/A vbox2->show();
5909N/A _pageSettingsFrame->show();
5909N/A _labelPrecision->show();
5909N/A _labelPrecisionWarning->show();
5909N/A _fallbackPrecisionSlider->show();
5909N/A _labelPrecisionComment->show();
5909N/A hbox6->show();
5909N/A _labelText->show();
5909N/A _textHandlingCombo->show();
5909N/A hbox5->show();
5909N/A _localFontsCheck->show();
5909N/A _embedImagesCheck->show();
5909N/A vbox3->show();
5909N/A _importSettingsFrame->show();
5909N/A vbox1->show();
5909N/A _previewArea->show();
5909N/A hbox1->show();
5909N/A
5909N/A // Connect signals
5909N/A _previewArea->signal_expose_event().connect(sigc::mem_fun(*this, &PdfImportCairoDialog::_onExposePreview));
5909N/A _pageNumberSpin_adj->signal_value_changed().connect(sigc::mem_fun(*this, &PdfImportCairoDialog::_onPageNumberChanged));
5909N/A _cropCheck->signal_toggled().connect(sigc::mem_fun(*this, &PdfImportCairoDialog::_onToggleCropping));
5909N/A _fallbackPrecisionSlider_adj->signal_value_changed().connect(sigc::mem_fun(*this, &PdfImportCairoDialog::_onPrecisionChanged));
5909N/A
5909N/A _render_thumb = false;
5909N/A _cairo_surface = NULL;
5909N/A _render_thumb = true;
5909N/A
5909N/A // Set default preview size
5909N/A _preview_width = 200;
5909N/A _preview_height = 300;
5909N/A
5909N/A // Init preview
5909N/A _thumb_data = NULL;
5909N/A _pageNumberSpin_adj->set_value(1.0);
5909N/A _current_page = 1;
5909N/A _setPreviewPage(_current_page);
5909N/A
5909N/A set_default (*okbutton);
5909N/A set_focus (*okbutton);
5909N/A}
5909N/A
5909N/APdfImportCairoDialog::~PdfImportCairoDialog() {
5909N/A if (_cairo_surface) {
5909N/A cairo_surface_destroy(_cairo_surface);
5909N/A }
5909N/A if (_thumb_data) {
5909N/A if (_render_thumb) {
5909N/A delete _thumb_data;
5909N/A } else {
5909N/A // -->gfree(_thumb_data);
5909N/A delete _thumb_data;
5909N/A }
5909N/A }
5909N/A}
5909N/A
5909N/Abool PdfImportCairoDialog::showDialog() {
5909N/A show();
5909N/A gint b = run();
5909N/A hide();
5909N/A if ( b == Gtk::RESPONSE_OK ) {
5909N/A return TRUE;
5909N/A } else {
5909N/A return FALSE;
5909N/A }
5909N/A}
5909N/A
5909N/Aint PdfImportCairoDialog::getSelectedPage() {
5909N/A return _current_page;
5909N/A}
5909N/A
5909N/A/**
5909N/A * \brief Retrieves the current settings into a repr which SvgBuilder will use
5909N/A * for determining the behaviour desired by the user
5909N/A */
5909N/Avoid PdfImportCairoDialog::getImportSettings(Inkscape::XML::Node *prefs) {
5909N/A sp_repr_set_svg_double(prefs, "selectedPage", (double)_current_page);
5909N/A if (_cropCheck->get_active()) {
5909N/A Glib::ustring current_choice = _cropTypeCombo->get_active_text();
5909N/A int num_crop_choices = sizeof(crop_setting_choices) / sizeof(crop_setting_choices[0]);
5909N/A int i = 0;
5909N/A for ( ; i < num_crop_choices ; i++ ) {
5909N/A if ( current_choice == _(crop_setting_choices[i]) ) {
5909N/A break;
5909N/A }
5909N/A }
5909N/A sp_repr_set_svg_double(prefs, "cropTo", (double)i);
5909N/A } else {
5909N/A sp_repr_set_svg_double(prefs, "cropTo", -1.0);
5909N/A }
5909N/A sp_repr_set_svg_double(prefs, "approximationPrecision",
5909N/A _fallbackPrecisionSlider->get_value());
5909N/A if (_localFontsCheck->get_active()) {
5909N/A prefs->setAttribute("localFonts", "1");
5909N/A } else {
5909N/A prefs->setAttribute("localFonts", "0");
5909N/A }
5909N/A if (_embedImagesCheck->get_active()) {
5909N/A prefs->setAttribute("embedImages", "1");
5909N/A } else {
5909N/A prefs->setAttribute("embedImages", "0");
5909N/A }
5909N/A}
5909N/A
5909N/A/**
5909N/A * \brief Redisplay the comment on the current approximation precision setting
5909N/A * Evenly divides the interval of possible values between the available labels.
5909N/A */
5909N/Avoid PdfImportCairoDialog::_onPrecisionChanged() {
5909N/A
5909N/A static Glib::ustring precision_comments[] = {
5909N/A Glib::ustring(C_("PDF input precision", "rough")),
5909N/A Glib::ustring(C_("PDF input precision", "medium")),
5909N/A Glib::ustring(C_("PDF input precision", "fine")),
5909N/A Glib::ustring(C_("PDF input precision", "very fine"))
5909N/A };
5909N/A
5909N/A double min = _fallbackPrecisionSlider_adj->get_lower();
5909N/A double max = _fallbackPrecisionSlider_adj->get_upper();
5909N/A int num_intervals = sizeof(precision_comments) / sizeof(precision_comments[0]);
5909N/A double interval_len = ( max - min ) / (double)num_intervals;
5909N/A double value = _fallbackPrecisionSlider_adj->get_value();
5909N/A int comment_idx = (int)floor( ( value - min ) / interval_len );
5909N/A _labelPrecisionComment->set_label(precision_comments[comment_idx]);
5909N/A}
5909N/A
5909N/Avoid PdfImportCairoDialog::_onToggleCropping() {
5909N/A _cropTypeCombo->set_sensitive(_cropCheck->get_active());
5909N/A}
5909N/A
5909N/Avoid PdfImportCairoDialog::_onPageNumberChanged() {
5909N/A int page = _pageNumberSpin->get_value_as_int();
5909N/A _current_page = CLAMP(page, 1, poppler_document_get_n_pages(_poppler_doc));
5909N/A _setPreviewPage(_current_page);
5909N/A}
5909N/A
5909N/A/**
5909N/A * \brief Copies image data from a Cairo surface to a pixbuf
5909N/A *
5909N/A * Borrowed from libpoppler, from the file poppler-page.cc
5909N/A * Copyright (C) 2005, Red Hat, Inc.
5909N/A *
5909N/A */
5909N/Astatic void copy_cairo_surface_to_pixbuf (cairo_surface_t *surface,
5909N/A unsigned char *data,
5909N/A GdkPixbuf *pixbuf)
5909N/A{
5909N/A int cairo_width, cairo_height, cairo_rowstride;
5909N/A unsigned char *pixbuf_data, *dst, *cairo_data;
5909N/A int pixbuf_rowstride, pixbuf_n_channels;
5909N/A unsigned int *src;
5909N/A int x, y;
5909N/A
5909N/A cairo_width = cairo_image_surface_get_width (surface);
5909N/A cairo_height = cairo_image_surface_get_height (surface);
5909N/A cairo_rowstride = cairo_width * 4;
5909N/A cairo_data = data;
5909N/A
5909N/A pixbuf_data = gdk_pixbuf_get_pixels (pixbuf);
5909N/A pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
5909N/A pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
5909N/A
5909N/A if (cairo_width > gdk_pixbuf_get_width (pixbuf))
5909N/A cairo_width = gdk_pixbuf_get_width (pixbuf);
5909N/A if (cairo_height > gdk_pixbuf_get_height (pixbuf))
5909N/A cairo_height = gdk_pixbuf_get_height (pixbuf);
5909N/A for (y = 0; y < cairo_height; y++)
5909N/A {
5909N/A src = (unsigned int *) (cairo_data + y * cairo_rowstride);
5909N/A dst = pixbuf_data + y * pixbuf_rowstride;
5909N/A for (x = 0; x < cairo_width; x++)
5909N/A {
5909N/A dst[0] = (*src >> 16) & 0xff;
5909N/A dst[1] = (*src >> 8) & 0xff;
5909N/A dst[2] = (*src >> 0) & 0xff;
5909N/A if (pixbuf_n_channels == 4)
5909N/A dst[3] = (*src >> 24) & 0xff;
5909N/A dst += pixbuf_n_channels;
5909N/A src++;
5909N/A }
5909N/A }
5909N/A}
5909N/A
5909N/A/**
* \brief Updates the preview area with the previously rendered thumbnail
*/
bool PdfImportCairoDialog::_onExposePreview(GdkEventExpose */*event*/) {
// Check if we have a thumbnail at all
if (!_thumb_data) {
return true;
}
// Create the pixbuf for the thumbnail
Glib::RefPtr<Gdk::Pixbuf> thumb;
if (_render_thumb) {
thumb = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, true,
8, _thumb_width, _thumb_height);
} else {
thumb = Gdk::Pixbuf::create_from_data(_thumb_data, Gdk::COLORSPACE_RGB,
false, 8, _thumb_width, _thumb_height, _thumb_rowstride);
}
if (!thumb) {
return true;
}
// Set background to white
if (_render_thumb) {
thumb->fill(0xffffffff);
Glib::RefPtr<Gdk::Pixmap> back_pixmap = Gdk::Pixmap::create(
_previewArea->get_window(), _thumb_width, _thumb_height, -1);
if (!back_pixmap) {
return true;
}
Cairo::RefPtr<Cairo::Context> cr = back_pixmap->create_cairo_context();
Gdk::Cairo::set_source_pixbuf(cr, thumb, 0, 0);
cr->paint();
_previewArea->get_window()->set_back_pixmap(back_pixmap, false);
_previewArea->get_window()->clear();
}
// Copy the thumbnail image from the Cairo surface
if (_render_thumb) {
copy_cairo_surface_to_pixbuf(_cairo_surface, _thumb_data, thumb->gobj());
}
Cairo::RefPtr<Cairo::Context> cr = _previewArea->get_window()->create_cairo_context();
Gdk::Cairo::set_source_pixbuf(cr, thumb, 0, _render_thumb ? 0 : 20);
cr->paint();
return true;
}
/**
* \brief Renders the given page's thumbnail using Cairo
*/
void PdfImportCairoDialog::_setPreviewPage(int page) {
PopplerPage *_previewed_page = poppler_document_get_page(_poppler_doc, page);
// Try to get a thumbnail from the PDF if possible
if (!_render_thumb) {
if (_thumb_data) {
// --> gfree(_thumb_data);
free(_thumb_data);
_thumb_data = NULL;
}
/*
--> if (!_previewed_page->loadThumb(&_thumb_data,
&_thumb_width, &_thumb_height, &_thumb_rowstride)) {
return;
}
*/
// Redraw preview area
_previewArea->set_size_request(_thumb_width, _thumb_height + 20);
_previewArea->queue_draw();
return;
}
// Get page size by accounting for rotation
double width, height;
// --> int rotate = _previewed_page->getRotate();
int rotate = 0;
if ( rotate == 90 || rotate == 270 ) {
// --> height = _previewed_page->getCropWidth();
// --> width = _previewed_page->getCropHeight();
} else {
poppler_page_get_size (_previewed_page, &width, &height);
// --> width = _previewed_page->getCropWidth();
// --> height = _previewed_page->getCropHeight();
}
// Calculate the needed scaling for the page
double scale_x = (double)_preview_width / width;
double scale_y = (double)_preview_height / height;
double scale_factor = ( scale_x > scale_y ) ? scale_y : scale_x;
// Create new Cairo surface
_thumb_width = (int)ceil( width * scale_factor );
_thumb_height = (int)ceil( height * scale_factor );
_thumb_rowstride = _thumb_width * 4;
if (_thumb_data) {
delete _thumb_data;
}
_thumb_data = new unsigned char[ _thumb_rowstride * _thumb_height ];
if (_cairo_surface) {
cairo_surface_destroy(_cairo_surface);
}
_cairo_surface = cairo_image_surface_create_for_data(_thumb_data,
CAIRO_FORMAT_ARGB32, _thumb_width, _thumb_height, _thumb_rowstride);
cairo_t *cr = cairo_create(_cairo_surface);
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0); // Set fill color to white
cairo_paint(cr); // Clear it
cairo_scale(cr, scale_factor, scale_factor); // Use Cairo for resizing the image
// Render page
if (_poppler_doc != NULL) {
PopplerPage *poppler_page = poppler_document_get_page(_poppler_doc, page - 1);
poppler_page_render(poppler_page, cr);
g_object_unref(G_OBJECT(poppler_page));
}
// Clean up
cairo_destroy(cr);
// Redraw preview area
_previewArea->set_size_request(_preview_width, _preview_height);
_previewArea->queue_draw();
}
static cairo_status_t _write_ustring_cb(void *closure, const unsigned char *data, unsigned int length);
SPDocument *
PdfInputCairo::open(Inkscape::Extension::Input * /*mod*/, const gchar * uri) {
g_message("Attempting to open using PdfInputCairo\n");
gchar* filename_uri = g_filename_to_uri(uri, NULL, NULL);
GError *error = NULL;
/// @todo handle passwort
/// @todo check if win32 unicode needs special attention
PopplerDocument* document = poppler_document_new_from_file(filename_uri, NULL, &error);
if(error != NULL) {
g_message("Unable to read file: %s\n", error->message);
g_error_free (error);
}
if (document == NULL) {
return NULL;
}
// create and show the import dialog
PdfImportCairoDialog *dlg = NULL;
if (inkscape_use_gui()) {
dlg = new PdfImportCairoDialog(document);
if (!dlg->showDialog()) {
delete dlg;
return NULL;
}
}
// Get needed page
int page_num;
if (dlg) {
page_num = dlg->getSelectedPage();
delete dlg;
}
else
page_num = 1;
double width, height;
PopplerPage* page = poppler_document_get_page(document, page_num - 1);
poppler_page_get_size(page, &width, &height);
Glib::ustring* output = new Glib::ustring("");
cairo_surface_t* surface = cairo_svg_surface_create_for_stream(Inkscape::Extension::Internal::_write_ustring_cb,
output, width, height);
cairo_t* cr = cairo_create(surface);
poppler_page_render(page, cr);
cairo_show_page(cr);
cairo_destroy(cr);
cairo_surface_destroy(surface);
SPDocument * doc = SPDocument::createNewDocFromMem(output->c_str(), output->length(), TRUE);
delete output;
g_object_unref(page);
g_object_unref(document);
return doc;
}
static cairo_status_t
_write_ustring_cb(void *closure, const unsigned char *data, unsigned int length)
{
Glib::ustring* stream = (Glib::ustring*)closure;
stream->append((const char*)data, length);
return CAIRO_STATUS_SUCCESS;
}
#include "clear-n_.h"
void
PdfInputCairo::init(void) {
Inkscape::Extension::Extension * ext;
ext = Inkscape::Extension::build_from_mem(
"<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
"<name>PDF Input</name>\n"
"<id>org.inkscape.input.cairo-pdf</id>\n"
"<input>\n"
"<extension>.pdf</extension>\n"
"<mimetype>application/pdf</mimetype>\n"
"<filetypename>Adobe PDF via poppler-cairo (*.pdf)</filetypename>\n"
"<filetypetooltip>PDF Document</filetypetooltip>\n"
"</input>\n"
"</inkscape-extension>", new PdfInputCairo());
} // init
} } } /* namespace Inkscape, Extension, Implementation */
#endif /* HAVE_POPPLER_CAIRO */
#endif /* HAVE_POPPLER_GLIB */
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :