pdf-input.cpp revision d134dc862fc9dd6cd34d66260db954d8975dd9d3
/*
* Native PDF import using libpoppler.
*
* Authors:
* miklos erdelyi
* Abhishek Sharma
*
* Copyright (C) 2007 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "pdf-input.h"
#ifdef HAVE_POPPLER
#include <poppler/ErrorCodes.h>
#include <poppler/GlobalParams.h>
#ifdef HAVE_POPPLER_CAIRO
#endif
#include <gtkmm/alignment.h>
#include <gtkmm/checkbutton.h>
#include <gtkmm/comboboxtext.h>
#include <gtkmm/drawingarea.h>
#include <gtkmm/radiobutton.h>
#if WITH_GTKMM_3_0
#include <glibmm/miscutils.h>
#endif
#include "svg-builder.h"
#include "pdf-parser.h"
#include "document-private.h"
#include "document-undo.h"
#include "inkscape.h"
#include "ui/dialog-events.h"
#include "ui/widget/spinbutton.h"
namespace Inkscape {
namespace Extension {
namespace Internal {
/**
* \brief The PDF import dialog
*/
static const gchar * crop_setting_choices[] = {
//TRANSLATORS: The following are document crop settings for PDF import
N_("media box"),
N_("crop box"),
N_("trim box"),
N_("bleed box"),
N_("art box")
};
{
#ifdef HAVE_POPPLER_CAIRO
_poppler_doc = NULL;
#endif // HAVE_POPPLER_CAIRO
// Page number
#if WITH_GTKMM_3_0
Glib::RefPtr<Gtk::Adjustment> _pageNumberSpin_adj = Gtk::Adjustment::create(1, 1, _pdf_doc->getNumPages(), 1, 10, 0);
#else
_pageNumberSpin = Gtk::manage(new class Inkscape::UI::Widget::SpinButton(*_pageNumberSpin_adj, 1, 1));
#endif
// Disable the page selector when there's only one page
if ( num_pages == 1 ) {
_pageNumberSpin->set_sensitive(false);
} else {
// Display total number of pages
}
// Crop settings
for ( int i = 0 ; i < num_crop_choices ; i++ ) {
}
_cropTypeCombo->set_sensitive(false);
_labelPrecision = Gtk::manage(new class Gtk::Label(_("Precision of approximating gradient meshes:")));
_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.")));
#ifdef HAVE_POPPLER_CAIRO
_labelViaPoppler = Gtk::manage(new class Gtk::Label(_("Import via external library. Text consists of groups containing cloned glyphs where each glyph is a path. Images are stored internally. Meshes cause entire document to be rendered as a raster image.")));
_labelViaInternal = Gtk::manage(new class Gtk::Label(_("Import via internal (Poppler derived) library. Text is stored as text but white space is missing. Meshes are converted to tiles, the number depends on the precision set below.")));
#endif
#if WITH_GTKMM_3_0
#else
#endif
// Text options
// _labelText = Gtk::manage(new class Gtk::Label(_("Text handling:")));
// _textHandlingCombo = Gtk::manage(new class Gtk::ComboBoxText());
// _textHandlingCombo->append(_("Import text as text"));
// _textHandlingCombo->set_active_text(_("Import text as text"));
// hbox5 = Gtk::manage(new class Gtk::HBox(false, 4));
// Font option
_localFontsCheck = Gtk::manage(new class Gtk::CheckButton(_("Replace PDF fonts by closest-named installed fonts")));
_labelSelect->set_line_wrap(false);
_labelSelect->set_use_markup(false);
_labelSelect->set_selectable(false);
_pageNumberSpin->set_numeric(true);
_pageNumberSpin->set_wrap(false);
_labelTotalPages->set_line_wrap(false);
_labelTotalPages->set_use_markup(false);
_labelTotalPages->set_selectable(false);
_cropCheck->set_mode(true);
_cropCheck->set_active(false);
_labelPrecision->set_line_wrap(true);
_labelPrecision->set_use_markup(false);
_labelPrecision->set_selectable(false);
_labelPrecisionWarning->set_line_wrap(true);
_labelPrecisionWarning->set_use_markup(true);
_labelPrecisionWarning->set_selectable(false);
#ifdef HAVE_POPPLER_CAIRO
_importViaPoppler->set_mode(true);
_importViaPoppler->set_active(false);
_importViaInternal->set_mode(true);
_importViaInternal->set_active(true);
_labelViaPoppler->set_line_wrap(true);
_labelViaInternal->set_line_wrap(true);
#endif
_fallbackPrecisionSlider->set_inverted(false);
_labelPrecisionComment->set_line_wrap(false);
_labelPrecisionComment->set_use_markup(false);
_labelPrecisionComment->set_selectable(false);
// _labelText->set_alignment(0.5,0.5);
// _labelText->set_padding(4,0);
// _labelText->set_justify(Gtk::JUSTIFY_LEFT);
// _labelText->set_line_wrap(false);
// _labelText->set_use_markup(false);
// _labelText->set_selectable(false);
// hbox5->pack_start(*_labelText, Gtk::PACK_SHRINK, 0);
// hbox5->pack_start(*_textHandlingCombo, Gtk::PACK_SHRINK, 0);
_localFontsCheck->set_mode(true);
_localFontsCheck->set_active(true);
_embedImagesCheck->set_mode(true);
_embedImagesCheck->set_active(true);
#ifdef HAVE_POPPLER_CAIRO
#endif
// vbox3->pack_start(*hbox5, Gtk::PACK_SHRINK, 4);
#if WITH_GTKMM_3_0
get_content_area()->set_homogeneous(false);
get_content_area()->set_spacing(0);
#else
this->get_vbox()->set_homogeneous(false);
this->get_vbox()->set_spacing(0);
#endif
this->set_title(_("PDF Import Settings"));
this->set_modal(true);
this->set_resizable(true);
this->property_destroy_with_parent().set_value(false);
this->show_all();
// Connect signals
#if WITH_GTKMM_3_0
#else
_previewArea->signal_expose_event().connect(sigc::mem_fun(*this, &PdfImportDialog::_onExposePreview));
#endif
_pageNumberSpin_adj->signal_value_changed().connect(sigc::mem_fun(*this, &PdfImportDialog::_onPageNumberChanged));
_fallbackPrecisionSlider_adj->signal_value_changed().connect(sigc::mem_fun(*this, &PdfImportDialog::_onPrecisionChanged));
#ifdef HAVE_POPPLER_CAIRO
_importViaPoppler->signal_toggled().connect(sigc::mem_fun(*this, &PdfImportDialog::_onToggleImport));
#endif
_render_thumb = false;
#ifdef HAVE_POPPLER_CAIRO
_render_thumb = true;
// Create PopplerDocument
}
}
// Set sensitivity of some widgets based on selected import type.
#endif
// Set default preview size
_preview_width = 200;
_preview_height = 300;
// Init preview
_thumb_data = NULL;
_current_page = 1;
set_default (*okbutton);
}
#ifdef HAVE_POPPLER_CAIRO
if (_cairo_surface) {
}
if (_poppler_doc) {
}
#endif
if (_thumb_data) {
if (_render_thumb) {
delete _thumb_data;
} else {
}
}
}
bool PdfImportDialog::showDialog() {
show();
hide();
if ( b == Gtk::RESPONSE_OK ) {
return TRUE;
} else {
return FALSE;
}
}
int PdfImportDialog::getSelectedPage() {
return _current_page;
}
bool PdfImportDialog::getImportMethod() {
#ifdef HAVE_POPPLER_CAIRO
return _importViaPoppler->get_active();
#else
return false;
#endif
}
/**
* \brief Retrieves the current settings into a repr which SvgBuilder will use
* for determining the behaviour desired by the user
*/
if (_cropCheck->get_active()) {
int i = 0;
for ( ; i < num_crop_choices ; i++ ) {
if ( current_choice == _(crop_setting_choices[i]) ) {
break;
}
}
} else {
}
if (_localFontsCheck->get_active()) {
} else {
}
if (_embedImagesCheck->get_active()) {
} else {
}
#ifdef HAVE_POPPLER_CAIRO
if (_importViaPoppler->get_active()) {
} else {
}
#endif
}
/**
* \brief Redisplay the comment on the current approximation precision setting
* Evenly divides the interval of possible values between the available labels.
*/
void PdfImportDialog::_onPrecisionChanged() {
};
}
void PdfImportDialog::_onToggleCropping() {
}
void PdfImportDialog::_onPageNumberChanged() {
}
#ifdef HAVE_POPPLER_CAIRO
void PdfImportDialog::_onToggleImport() {
if( _importViaPoppler->get_active() ) {
hbox3->set_sensitive(false);
_localFontsCheck->set_sensitive(false);
_embedImagesCheck->set_sensitive(false);
hbox6->set_sensitive(false);
} else {
hbox3->set_sensitive();
hbox6->set_sensitive();
}
}
#endif
#ifdef HAVE_POPPLER_CAIRO
/**
* \brief Copies image data from a Cairo surface to a pixbuf
*
* Borrowed from libpoppler, from the file poppler-page.cc
* Copyright (C) 2005, Red Hat, Inc.
*
*/
unsigned char *data,
{
unsigned int *src;
int x, y;
cairo_data = data;
for (y = 0; y < cairo_height; y++)
{
for (x = 0; x < cairo_width; x++)
{
if (pixbuf_n_channels == 4)
dst += pixbuf_n_channels;
src++;
}
}
}
#endif
/**
* \brief Updates the preview area with the previously rendered thumbnail
*/
#if !WITH_GTKMM_3_0
}
#endif
// Check if we have a thumbnail at all
if (!_thumb_data) {
return true;
}
// Create the pixbuf for the thumbnail
if (_render_thumb) {
} else {
}
if (!thumb) {
return true;
}
// Set background to white
if (_render_thumb) {
}
#ifdef HAVE_POPPLER_CAIRO
// Copy the thumbnail image from the Cairo surface
if (_render_thumb) {
}
#endif
return true;
}
/**
* \brief Renders the given page's thumbnail using Cairo
*/
// Try to get a thumbnail from the PDF if possible
if (!_render_thumb) {
if (_thumb_data) {
_thumb_data = NULL;
}
return;
}
// Redraw preview area
return;
}
#ifdef HAVE_POPPLER_CAIRO
// Get page size by accounting for rotation
} else {
}
// Calculate the needed scaling for the page
// Create new Cairo surface
if (_thumb_data) {
delete _thumb_data;
}
if (_cairo_surface) {
}
// Render page
if (_poppler_doc != NULL) {
}
// Clean up
// Redraw preview area
#endif
}
////////////////////////////////////////////////////////////////////////////////
bool
PdfInput::wasCancelled () {
return _cancelled;
}
#ifdef HAVE_POPPLER_CAIRO
/// helper method
static cairo_status_t
{
return CAIRO_STATUS_SUCCESS;
}
#endif
/**
* Parses the selected page of the given PDF document using PdfParser.
*/
_cancelled = false;
// Initialize the globalParams variable for poppler
if (!globalParams) {
#ifdef ENABLE_OSX_APP_LOCATIONS
//
// data files for poppler are not relocatable (loaded from
// path defined at build time). This fails to work with relocatable
// application bundles for OS X.
//
// Workaround:
// 1. define $POPPLER_DATADIR env variable in app launcher script
// 2. pass custom $POPPLER_DATADIR via poppler's GlobalParams()
//
// relevant poppler commit:
// <http://cgit.freedesktop.org/poppler/poppler/commit/?id=869584a84eed507775ff1c3183fe484c14b6f77b>
//
// FIXES: Inkscape bug #956282, #1264793
// TODO: report RFE upstream (full relocation support for OS X packaging)
//
if (poppler_datadir != NULL) {
} else {
globalParams = new GlobalParams();
}
#else
globalParams = new GlobalParams();
#endif // ENABLE_OSX_APP_LOCATIONS
}
// PDFDoc is from poppler. PDFDoc is used for preview and for native import.
#ifndef WIN32
// poppler does not use glib g_open. So on win32 we must use unicode call. code was copied from
// glib gstdio.c
//delete filename_goo;
#else
return NULL;
}
PDFDoc *pdf_doc = new PDFDoc(wfilename, wcslen(wfilename), NULL, NULL, NULL); // TODO: Could ask for password
#endif
delete pdf_doc;
if (error == errEncrypted) {
g_message("Document is encrypted.");
} else if (error == errOpenFile) {
g_message("couldn't open the PDF file.");
} else if (error == errBadCatalog) {
g_message("couldn't read the page catalog.");
} else if (error == errDamaged) {
g_message("PDF file was damaged and couldn't be repaired.");
} else if (error == errHighlightFile) {
g_message("nonexistent or invalid highlight file.");
} else if (error == errBadPrinter) {
g_message("invalid printer.");
} else if (error == errPrinting) {
g_message("Error during printing.");
} else if (error == errPermission) {
g_message("PDF file does not allow that operation.");
} else if (error == errBadPageNum) {
g_message("invalid page number.");
g_message("file IO error.");
} else {
}
return NULL;
}
if (!dlg->showDialog()) {
_cancelled = true;
delete dlg;
delete pdf_doc;
return NULL;
}
}
// Get options
int page_num = 1;
bool is_importvia_poppler = false;
if (dlg) {
#ifdef HAVE_POPPLER_CAIRO
// printf("PDF import via %s.\n", is_importvia_poppler ? "poppler" : "native");
#endif
}
bool saved = false;
if(!is_importvia_poppler)
{
// native importer
// Create builder
if (dot) {
*dot = 0;
}
// Get preferences
if (dlg)
// Apply crop settings
double crop_setting;
int crop_choice = (int)crop_setting;
switch (crop_choice) {
case 0: // Media box
break;
case 1: // Crop box
break;
case 2: // Bleed box
break;
case 3: // Trim box
break;
case 4: // Art box
break;
default:
break;
}
}
// Create parser (extension/internal/pdfinput/pdf-parser.h)
// Set up approximation precision for parser. Used for convering Mesh Gradients into tiles.
double color_delta;
if ( color_delta <= 0.0 ) {
} else {
}
for ( int i = 1 ; i <= pdfNumShadingTypes ; i++ ) {
}
// Parse the document structure
}
// Cleanup
delete pdf_parser;
delete builder;
}
else
{
#ifdef HAVE_POPPLER_CAIRO
// the poppler import
}
/// @todo handle password
/// @todo check if win32 unicode needs special attention
}
{
cairo_surface_t* surface = cairo_svg_surface_create_for_stream(Inkscape::Extension::Internal::_write_ustring_cb,
// This magical function results in more fine-grain fallbacks. In particular, a mesh
// gradient won't necessarily result in the whole PDF being rasterized. Of course, SVG
// 1.2 never made it as a standard, but hey, we'll take what we can get. This trick was
// found by examining the 'pdftocairo' code.
// Cleanup
// delete output;
}
else
{
}
#endif
}
// Cleanup
delete pdf_doc;
delete dlg;
// Set viewBox if it doesn't exist
doc->setViewBox(Geom::Rect::from_xywh(0, 0, doc->getWidth().value(doc->getDisplayUnit()), doc->getHeight().value(doc->getDisplayUnit())));
}
// Restore undo
return doc;
}
#include "../clear-n_.h"
/* PDF in */
"<id>org.inkscape.input.pdf</id>\n"
"<input>\n"
"<extension>.pdf</extension>\n"
"<mimetype>application/pdf</mimetype>\n"
"</input>\n"
"</inkscape-extension>", new PdfInput());
/* AI in */
"<id>org.inkscape.input.ai</id>\n"
"<input>\n"
"<extension>.ai</extension>\n"
"<mimetype>image/x-adobe-illustrator</mimetype>\n"
"<filetypetooltip>" N_("Open files saved in Adobe Illustrator 9.0 and newer versions") "</filetypetooltip>\n"
"</input>\n"
"</inkscape-extension>", new PdfInput());
} // init
} } } /* namespace Inkscape, Extension, Implementation */
#endif /* HAVE_POPPLER */
/*
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 :