pdf.cpp revision b91d3d6c8f909bda909028eada6bf410d60c95a0
#define __SP_PDF_C__
/** \file
* PDF printing.
*/
/*
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* bulia byak <buliabyak@users.sf.net>
* Ulf Erikson <ulferikson@users.sf.net>
*
* Basic printing code, EXCEPT image and
* ascii85 filter is in public domain
*
* Image printing and Ascii85 filter:
*
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
* Copyright (C) 1997-98 Peter Kirchgessner
* George White <aa056@chebucto.ns.ca>
* Austin Donnelly <austin@gimp.org>
*
* Licensed under GNU GPL
*/
/* Plain Print */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <signal.h>
#include <errno.h>
#include <libnr/n-art-bpath.h>
#include <libnr/nr-point-matrix-ops.h>
#include <gtk/gtkstock.h>
#include <gtk/gtkframe.h>
#include <gtk/gtkradiobutton.h>
#include <gtk/gtkcombo.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkentry.h>
#include <gtk/gtktooltips.h>
#include "display/nr-arena-item.h"
#include "display/canvas-bpath.h"
#include "sp-item.h"
#include "style.h"
#include "sp-linear-gradient.h"
#include "sp-radial-gradient.h"
#include "libnrtype/font-instance.h"
#include "libnrtype/font-style-to-pos.h"
#include <unit-constants.h>
#include "pdf.h"
#include "pdf-mini.h"
namespace Inkscape {
namespace Extension {
namespace Internal {
_dpi(72),
_bitmap(false)
{
}
{
/* fixme: should really use pclose for popen'd streams */
/* restore default signal handling for SIGPIPE */
#endif
return;
}
unsigned int
{
static gchar const *const pdr[] = {"72", "75", "100", "144", "150", "200", "300", "360", "600", "1200", "2400", NULL};
#ifdef TED
#endif
/* Create dialog */
// SP_DT_WIDGET(SP_ACTIVE_DESKTOP)->window,
NULL,
NULL);
/* Print properties frame */
/* Print type */
_("Use PDF vector operators. The resulting image is usually smaller "
"in file size and can be arbitrarily scaled, but "
"patterns will be lost."), NULL);
rb = gtk_radio_button_new_with_label(gtk_radio_button_get_group((GtkRadioButton *) rb), _("Print as bitmap"));
_("Print everything as bitmap. The resulting image is usually larger "
"in file size and cannot be arbitrarily scaled without quality loss, "
"but all objects will be rendered exactly as displayed."), NULL);
/* Resolution */
_("Preferred resolution (dots per inch) of bitmap"), NULL);
/* Setup strings */
}
if (1) {
}
/* Print destination frame */
f = gtk_frame_new(_("Print destination"));
l = gtk_label_new(_("Printer name (as given by lpstat -p);\n"
"leave empty to use the system default printer.\n"
"Use '> filename' to print to file.\n"
"Use '| prog arg...' to pipe to a program."));
GtkWidget *e = gtk_entry_new();
if (1) {
? val
: "" ));
}
// pressing enter in the destination field is the same as clicking Print:
if (response == GTK_RESPONSE_OK) {
char const *sstr;
/* Arrgh, have to do something */
/* skip leading whitespace, bug #1068483 */
/* g_print("Printing to %s\n", fn); */
}
return ret;
}
unsigned int
{
_newlatin1font_proc_defined = false;
gsize bytesWritten = 0;
/* TODO: Replace the below fprintf's with something that does the right thing whether in
* gui or batch mode (e.g. --print=blah). Consider throwing an exception: currently one of
* the callers (sp_print_document_to_file, "ret = mod->begin(doc)") wrongly ignores the
* return code.
*/
if (*fn == '|') {
fn += 1;
#ifndef WIN32
#else
#endif
if (!osp) {
return 0;
}
} else if (*fn == '>') {
fn += 1;
if (!osf) {
return 0;
}
} else {
/* put cwd stuff in here */
: g_strdup("lpr") );
#ifndef WIN32
#else
#endif
if (!osp) {
return 0;
}
}
}
if (_stream) {
/* fixme: this is kinda icky */
#endif
}
// *doc_info << " /Title(" << "?" << ")\n";
// *doc_info << " /Author(" << "?" << ")\n";
// *doc_info << " /Subject(" << "?" << ")\n";
// *doc_info << " /Keywords(" << "?" << ")\n";
// *doc_info << " /Creator(" << "?" << ")\n";
// *doc_info << " /CreationDate(D:YYYYMMDDHHmmSSOHH'mm')\n";
/* flush this to test output stream as early as possible */
/*g_print("caught error in sp_module_print_plain_begin\n");*/
g_strerror(errno));
}
g_print("Printing failed\n");
/* fixme: should use pclose() for pipes */
return 0;
}
// width and height in pt
NRRect d;
bool pageBoundingBox;
// printf("Page Bounding Box: %s\n", pageBoundingBox ? "TRUE" : "FALSE");
if (pageBoundingBox) {
} else {
// convert from px to pt
}
if (!_bitmap) {
<< " cm\n";
// from now on we can output px, but they will be treated as pt
}
return 1;
}
unsigned int
{
if (!_stream) return 0;
if (_bitmap) {
double const x0 = 0.0;
double const y0 = 0.0;
for (int y = 0; y < height; y += 64) {
/* Set area of interest. */
/* Update to renderable state. */
nr_arena_item_invoke_update(mod->root, &bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
/* Render */
/* This should take guchar* instead of unsigned char*) */
/* Blitter goes here */
}
}
delete pdf_file;
/* Flush stream to be sure. */
/* fixme: should really use pclose for popen'd streams */
_stream = 0;
return 0;
}
unsigned int
{
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
if (_bitmap) return 0;
*page_stream << "q\n";
return 1;
}
unsigned int
{
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
if (_bitmap) return 0;
*page_stream << "Q\n";
return 1;
}
unsigned int
{
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
if (_bitmap) return 0;
return 1;
}
void
PrintPDF::print_fill_alpha(SVGOStringStream &/*os*/, SPStyle const *const style, NRRect const *pbox)
{
float alpha = 1.0;
if (alpha != 1.0) {
*pdf_alpha << "<< /Type /ExtGState\n";
*pdf_alpha << " /AIS false\n";
*pdf_alpha << ">>\n";
<< " gs\n";
}
} else {
// convert to userspace
}
double alpha = 1.0;
}
*pdf_gstate << "<< /Type /ExtGState\n";
*pdf_gstate << " /AIS false\n";
}
if (alpha != 1.0) {
*pdf_alpha << "<<\n/ShadingType 2\n/ColorSpace /DeviceGray\n";
*pdf_alpha << "/Coords [" << p1[NR::X] << " " << p1[NR::Y] << " " << p2[NR::X] << " " << p2[NR::Y] <<"]\n";
*pdf_alpha << "/Extend [true true]\n";
*pdf_alpha << "/Domain [0 1]\n";
*pdf_alpha << "/Function <<\n/FunctionType 3\n/Functions\n[\n";
*pdf_alpha << "<<\n/FunctionType 2\n/Domain [0 1]\n";
*pdf_alpha << "/N 1\n>>\n";
}
*pdf_alpha << "/Bounds [ ";
}
*pdf_alpha << "]\n";
*pdf_alpha << "/Encode [ ";
*pdf_alpha << "0 1 ";
}
*pdf_alpha << "]\n";
*pdf_alpha << ">>\n>>\n";
*pdf_smask << "<< /Type /Mask\n";
*pdf_smask << " /S /Alpha\n";
*pdf_smask << " /BC [0.0]\n";
*pdf_smask << ">>\n";
*pdf_xobj << "<< /Type /XObject\n";
*pdf_xobj << " /Subtype /Form\n";
*pdf_xobj << " /FormType 1\n";
*pdf_xobj << " /BBox ["
*pdf_xobj << " /Group\n";
*pdf_xobj << " << /Type /Group\n";
*pdf_xobj << " /S /Transparency\n";
*pdf_xobj << " /CS /DeviceGray \n";
*pdf_xobj << " >>\n";
os << "q\n"
<< "h\n"
<< "W* n\n";
if (g->gradientTransform_set) {
}
os << "Q\n";
*pdf_xobj << ">>\n";
*pdf_xobj << "stream\n";
*pdf_xobj << "endstream\n";
}
*pdf_gstate << ">>\n";
}
// convert to userspace
c *= bbox2user;
f *= bbox2user;
}
double alpha = 1.0;
}
*pdf_gstate << "<< /Type /ExtGState\n";
*pdf_gstate << " /AIS false\n";
}
if (alpha != 1.0) {
*pdf_alpha << "<<\n/ShadingType 3\n/ColorSpace /DeviceGray\n";
*pdf_alpha << "/Coords ["<< f[NR::X] <<" "<< f[NR::Y] <<" 0 "<< c[NR::X] <<" "<< c[NR::Y] <<" "<< r <<"]\n";
*pdf_alpha << "/Extend [true true]\n";
*pdf_alpha << "/Domain [0 1]\n";
*pdf_alpha << "/Function <<\n/FunctionType 3\n/Functions\n[\n";
*pdf_alpha << "<<\n/FunctionType 2\n/Domain [0 1]\n";
*pdf_alpha << "/N 1\n>>\n";
}
*pdf_alpha << "/Bounds [ ";
}
*pdf_alpha << "]\n";
*pdf_alpha << "/Encode [ ";
*pdf_alpha << "0 1 ";
}
*pdf_alpha << "]\n";
*pdf_alpha << ">>\n>>\n";
*pdf_smask << "<< /Type /Mask\n";
*pdf_smask << " /S /Alpha\n";
*pdf_smask << " /BC [0.0]\n";
*pdf_smask << ">>\n";
*pdf_xobj << "<< /Type /XObject\n";
*pdf_xobj << " /Subtype /Form\n";
*pdf_xobj << " /FormType 1\n";
*pdf_xobj << " /BBox ["
*pdf_xobj << " /Group\n";
*pdf_xobj << " << /Type /Group\n";
*pdf_xobj << " /S /Transparency\n";
*pdf_xobj << " /CS /DeviceGray \n";
*pdf_xobj << " >>\n";
os << "q\n"
<< "h\n"
<< "W* n\n";
if (g->gradientTransform_set) {
}
os << "Q\n";
*pdf_xobj << ">>\n";
*pdf_xobj << "stream\n";
*pdf_xobj << "endstream\n";
}
*pdf_gstate << ">>\n";
}
}
}
}
void
{
float rgb[3];
} else {
// convert to userspace
}
*pdf_shade << "<<\n/ShadingType 2\n/ColorSpace /DeviceRGB\n";
*pdf_shade << "/Coords [" << p1[NR::X] << " " << p1[NR::Y] << " " << p2[NR::X] << " " << p2[NR::Y] <<"]\n";
*pdf_shade << "/Extend [true true]\n";
*pdf_shade << "/Domain [0 1]\n";
*pdf_shade << "/Function <<\n/FunctionType 3\n/Functions\n[\n";
float rgb[3];
*pdf_shade << "<<\n/FunctionType 2\n/Domain [0 1]\n";
*pdf_shade << "/N 1\n>>\n";
}
*pdf_shade << "/Bounds [ ";
}
*pdf_shade << "]\n";
*pdf_shade << "/Encode [ ";
*pdf_shade << "0 1 ";
}
*pdf_shade << "]\n";
*pdf_shade << ">>\n>>\n";
// convert to userspace
c *= bbox2user;
f *= bbox2user;
}
*pdf_shade << "<<\n/ShadingType 3\n/ColorSpace /DeviceRGB\n";
*pdf_shade << "/Coords ["<< f[NR::X] <<" "<< f[NR::Y] <<" 0 "<< c[NR::X] <<" "<< c[NR::Y] <<" "<< r <<"]\n";
*pdf_shade << "/Extend [true true]\n";
*pdf_shade << "/Domain [0 1]\n";
*pdf_shade << "/Function <<\n/FunctionType 3\n/Functions\n[\n";
float rgb[3];
*pdf_shade << "<<\n/FunctionType 2\n/Domain [0 1]\n";
*pdf_shade << "/N 1\n>>\n";
}
*pdf_shade << "/Bounds [ ";
}
*pdf_shade << "]\n";
*pdf_shade << "/Encode [ ";
*pdf_shade << "0 1 ";
}
*pdf_shade << "]\n";
*pdf_shade << ">>\n>>\n";
}
}
}
void
{
float rgb[3];
float alpha = 1.0;
if (alpha != 1.0) {
*pdf_alpha << "<< /Type /ExtGState\n";
*pdf_alpha << " /AIS false\n";
*pdf_alpha << ">>\n";
}
// There are rare cases in which for a solid line stroke_dasharray_set is true. To avoid
// invalid PS-lines such as "[0.0000000 0.0000000] 0.0000000 setdash", which should be "[] 0 setdash",
// we first check if all components of stroke_dash.dash are 0.
bool LineSolid = true;
if (style->stroke_dasharray_set &&
{
int i = 0;
LineSolid = false;
i++;
}
if (!LineSolid) {
*page_stream << "[";
if (i > 0) {
*page_stream << " ";
}
}
} else {
*page_stream << "[] 0 d\n";
}
} else {
*page_stream << "[] 0 d\n";
}
}
}
unsigned int
PrintPDF::fill(Inkscape::Extension::Print *mod, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *const style,
{
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
if (_bitmap) return 0;
*page_stream << "q\n";
*page_stream << "f*\n";
} else {
*page_stream << "f\n";
}
*page_stream << "Q\n";
}
{
*page_stream << "q\n";
*page_stream << "W* n\n";
if (g->gradientTransform_set) {
*page_stream << "q\n";
}
*page_stream << "sh\n";
if (g->gradientTransform_set) {
*page_stream << "Q\n";
}
} else {
*page_stream << "W n\n";
if (g->gradientTransform_set) {
*page_stream << "q\n";
}
*page_stream << "sh\n";
if (g->gradientTransform_set) {
*page_stream << "Q\n";
}
}
*page_stream << "Q\n";
}
return 0;
}
unsigned int
PrintPDF::stroke(Inkscape::Extension::Print *mod, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *style,
{
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
if (_bitmap) return 0;
*page_stream << "q\n";
*page_stream << "S\n";
*page_stream << "Q\n";
}
return 0;
}
unsigned int
PrintPDF::image(Inkscape::Extension::Print *mod, guchar *px, unsigned int w, unsigned int h, unsigned int rs,
{
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
if (_bitmap) return 0;
return 0;
#if 0
transform->c[0],
transform->c[1],
transform->c[2],
transform->c[3],
transform->c[4],
transform->c[5]);
for (unsigned int r = 0; r < h; r++) {
guchar *s;
s += 4;
}
}
}
return 0;
#endif
}
char const *
{
font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value, font_style_to_pos(*style));
char const *n;
char name_buf[256];
if (tf) {
n = name_buf;
} else {
// this system does not have this font, so just use the name from SVG in the hope that PS interpreter will make sense of it
(style->font_weight.value >= SP_CSS_FONT_WEIGHT_500 && style->font_weight.value <= SP_CSS_FONT_WEIGHT_900);
n = g_strdup_printf("%s%s%s%s",
(b || i || o) ? "-" : "",
(b) ? "Bold" : "",
(i) ? "Italic" : ((o) ? "Oblique" : "") );
}
return g_strdup(n);
}
unsigned int
{
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
if (_bitmap) return 0;
return 0;
// Escape chars
if (c == '\\' || c == ')' || c == '(')
else if (c >= 0x80)
escaped_text << '\\' << c;
else
escaped_text << static_cast<char>(c);
}
os << "gsave\n";
// set font
if (!_newlatin1font_proc_defined) {
// input: newfontname, existingfontname
// output: new font object, also defined to newfontname
"{findfont dup length dict copy " // load the font and create a copy of it
"dup /Encoding ISOLatin1Encoding put " // change the encoding in the copy
"definefont} def\n"; // create the new font and leave it on the stack, define the proc
_newlatin1font_proc_defined = true;
}
} else
os << "setfont\n";
{
// set fill style
// FIXME: we don't know the pbox of text, so have to pass NULL. This means gradients with
// bbox units won't work with text. However userspace gradients don't work with text either
// (text is black) for some reason.
os << "newpath\n";
}
// set stroke style
// paint stroke
os << "newpath\n";
}
os << "grestore\n";
return 0;
}
/* PDF helpers */
void
{
bool closed = false;
case NR_MOVETO:
if (closed) {
*page_stream << "h\n";
}
closed = true;
break;
case NR_MOVETO_OPEN:
if (closed) {
*page_stream << "h\n";
}
closed = false;
break;
case NR_LINETO:
break;
case NR_CURVETO:
break;
default:
break;
}
bp += 1;
}
if (closed) {
*page_stream << "h\n";
}
}
/* The following code is licensed under GNU GPL.
** The packbits, ascii85 and imaging printing code
** is from the gimp's postscript.c.
*/
/**
* \param nin Number of bytes of source data.
* \param src Source data.
* \param nout Number of output bytes.
* \param dst Buffer for output.
*/
void
int *nout,
{
register guchar c;
for (;;) {
if (nin <= 0) break;
c = *run_start;
/* Search repeat bytes */
nrepeat = 1;
nin -= 2;
src += 2;
nrepeat++;
src++;
nin--;
}
/* Add two-byte repeat to last literal run ? */
if ( (nrepeat == 1)
{
*last_literal += 2;
*(dst++) = c;
*(dst++) = c;
continue;
}
/* Add repeat run */
*(dst++) = c;
last_literal = NULL;
continue;
}
/* Search literal bytes */
nliteral = 1;
nin--;
src++;
for (;;) {
if (nin <= 0) break;
break;
nliteral++;
nin--;
src++;
}
/* Could be added to last literal run ? */
*last_literal += nliteral;
} else {
last_literal = dst;
}
}
}
void
PrintPDF::ascii85_init(void)
{
ascii85_len = 0;
ascii85_linewidth = 0;
}
void
{
char c[5];
bool const zero_case = (ascii85_buf == 0);
static int const max_linewidth = 75;
for (int i = 4; i >= 0; i--) {
ascii85_buf /= 85;
}
/* check for special case: "!!!!!" becomes "z", but only if not
* at end of data. */
if (ascii85_linewidth >= max_linewidth) {
os << '\n';
ascii85_linewidth = 0;
}
os << 'z';
} else {
for (int i = 0; i < ascii85_len+1; i++) {
os << '\n';
ascii85_linewidth = 0;
}
os << c[i];
}
}
ascii85_len = 0;
ascii85_buf = 0;
}
inline void
{
if (ascii85_len == 4)
ascii85_buf <<= 8;
ascii85_buf |= byte;
ascii85_len++;
}
void
{
while (n-- > 0) {
uptr++;
}
}
void
{
if (ascii85_len) {
/* zero any unfilled buffer portion, then flush */
}
os << "~>\n";
}
unsigned int
PrintPDF::print_image(FILE *ofp, guchar *px, unsigned int width, unsigned int height, unsigned int rs,
{
return 0;
os << "gsave\n";
/* Write read image procedure */
os << "% Strings to hold RGB-samples per scanline\n";
os << "{currentfile /ASCII85Decode filter /RunLengthDecode filter rstr readstring pop}\n";
os << "{currentfile /ASCII85Decode filter /RunLengthDecode filter gstr readstring pop}\n";
os << "{currentfile /ASCII85Decode filter /RunLengthDecode filter bstr readstring pop}\n";
os << "true 3\n";
/* Allocate buffer for packbits data. Worst case: Less than 1% increase */
/* ps_begin_data(ofp); */
os << "colorimage\n";
/*#define GET_RGB_TILE(begin) \
* {int scan_lines; \
* scan_lines = (i+tile_height-1 < height) ? tile_height : (height-i); \
* gimp_pixel_rgn_get_rect(&pixel_rgn, begin, 0, i, width, scan_lines); \
* src = begin; }
*/
for (unsigned i = 0; i < height; i++) {
/* if ((i % tile_height) == 0) GET_RGB_TILE(data); */ /* Get more data */
/* Iterate over RGB */
for (unsigned j = 0; j < width; j++) {
src_ptr += 4;
}
int nout;
ascii85_init();
}
}
/* ps_end_data(ofp); */
#if 0
#endif
#if 0
g_message(_("write error occurred"));
return (FALSE);
}
#endif
os << "grestore\n";
return 0;
//#undef GET_RGB_TILE
}
bool
{
}
#include "clear-n_.h"
void
{
/* SVG in */
"<inkscape-extension>\n"
"<param name=\"bitmap\" type=\"boolean\">FALSE</param>\n"
"<param name=\"resolution\" type=\"string\">72</param>\n"
"<param name=\"destination\" type=\"string\">| lp</param>\n"
"<param name=\"pageBoundingBox\" type=\"boolean\">TRUE</param>\n"
"<param name=\"textToPath\" type=\"boolean\">TRUE</param>\n"
"<print/>\n"
"</inkscape-extension>", new PrintPDF());
}
} /* namespace Internal */
} /* namespace Extension */
} /* namespace Inkscape */
/* End of GNU GPL code */
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :