8
* Lauris Kaplinski <lauris@kaplinski.com>
9
* bulia byak <buliabyak@users.sf.net>
11
* Basic printing code, EXCEPT image and
12
* ascii85 filter is in public domain
14
* Image printing and Ascii85 filter:
16
* Copyright (C) 2006 Johan Engelen
17
* Copyright (C) 1997-98 Peter Kirchgessner
18
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
19
* George White <aa056@chebucto.ns.ca>
20
* Austin Donnelly <austin@gimp.org>
22
* Licensed under GNU GPL
34
#include <libnr/nr-matrix-fns.h>
36
#include <glib/gmem.h>
37
#include <glib/gstrfuncs.h>
38
#include <gtk/gtkstock.h>
39
#include <gtk/gtkvbox.h>
40
#include <gtk/gtkframe.h>
41
#include <gtk/gtkradiobutton.h>
42
#include <gtk/gtkcombo.h>
43
#include <gtk/gtklabel.h>
44
#include <gtk/gtkentry.h>
45
#include <gtk/gtktooltips.h>
47
#include <glibmm/i18n.h>
48
#include "display/nr-arena-item.h"
49
#include "display/canvas-bpath.h"
52
#include "sp-linear-gradient.h"
53
#include "sp-radial-gradient.h"
55
#include "libnrtype/font-instance.h"
56
#include "libnrtype/font-style-to-pos.h"
58
#include <unit-constants.h>
61
#include "extension/system.h"
62
#include "extension/print.h"
67
#include FT_FREETYPE_H
69
#include <pango/pangoft2.h>
78
#include <2geom/sbasis-to-bezier.h>
79
#include <2geom/bezier-curve.h>
80
#include <2geom/hvlinesegment.h>
81
#include "helper/geom-curves.h"
90
using std::fprintf; --> this line will result 'std::libintl_fprintf' has not been declared
104
namespace Extension {
113
_fontTypesMap["Type 1"] = FONT_TYPE1;
114
_fontTypesMap["TrueType"] = FONT_TRUETYPE;
115
//TODO: support other font types (cf. embed_font())
118
PrintPS::~PrintPS(void)
120
/* fixme: should really use pclose for popen'd streams */
121
if (_stream) fclose(_stream);
122
if(_begin_stream) fclose(_begin_stream);
123
if(_fonts) g_tree_destroy(_fonts);
125
/* restore default signal handling for SIGPIPE */
126
#if !defined(_WIN32) && !defined(__WIN32__)
127
(void) signal(SIGPIPE, SIG_DFL);
134
PrintPS::setup(Inkscape::Extension::Print * mod)
136
static gchar const *const pdr[] = {"72", "75", "100", "144", "150", "200", "300", "360", "600", "1200", "2400", NULL};
139
Inkscape::XML::Node *repr = ((SPModule *) mod)->repr;
142
unsigned int ret = FALSE;
143
gchar const *destination = mod->get_param_string("destination");
146
GtkTooltips *tt = gtk_tooltips_new();
147
g_object_ref((GObject *) tt);
148
gtk_object_sink((GtkObject *) tt);
150
GtkWidget *dlg = gtk_dialog_new_with_buttons(
151
destination ? _("Print Configuration") : _("Print Destination"),
152
// SP_DT_WIDGET(SP_ACTIVE_DESKTOP)->window,
154
(GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR | GTK_DIALOG_DESTROY_WITH_PARENT),
157
destination ? GTK_STOCK_GO_FORWARD : GTK_STOCK_PRINT,
161
gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_OK);
163
GtkWidget *vbox = GTK_DIALOG(dlg)->vbox;
164
gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
165
/* Print properties frame */
166
GtkWidget *f = gtk_frame_new(_("Print properties"));
167
gtk_box_pack_start(GTK_BOX(vbox), f, FALSE, FALSE, 4);
168
GtkWidget *vb = gtk_vbox_new(FALSE, 4);
169
gtk_container_add(GTK_CONTAINER(f), vb);
170
gtk_container_set_border_width(GTK_CONTAINER(vb), 4);
172
bool const p2bm = mod->get_param_bool("bitmap");
173
GtkWidget *rb = gtk_radio_button_new_with_label(NULL, _("Print using PostScript operators"));
174
gtk_tooltips_set_tip((GtkTooltips *) tt, rb,
175
_("Use PostScript vector operators. The resulting image is usually smaller "
176
"in file size and can be arbitrarily scaled, but alpha transparency "
177
"and patterns will be lost."), NULL);
178
if (!p2bm) gtk_toggle_button_set_active((GtkToggleButton *) rb, TRUE);
179
gtk_box_pack_start(GTK_BOX(vb), rb, FALSE, FALSE, 0);
180
rb = gtk_radio_button_new_with_label(gtk_radio_button_get_group((GtkRadioButton *) rb), _("Print as bitmap"));
181
gtk_tooltips_set_tip((GtkTooltips *) tt, rb,
182
_("Print everything as bitmap. The resulting image is usually larger "
183
"in file size and cannot be arbitrarily scaled without quality loss, "
184
"but all objects will be rendered exactly as displayed."), NULL);
185
if (p2bm) gtk_toggle_button_set_active((GtkToggleButton *) rb, TRUE);
186
gtk_box_pack_start(GTK_BOX(vb), rb, FALSE, FALSE, 0);
188
GtkWidget *hb = gtk_hbox_new(FALSE, 4);
189
gtk_box_pack_start(GTK_BOX(vb), hb, FALSE, FALSE, 0);
190
GtkWidget *combo = gtk_combo_new();
191
gtk_combo_set_value_in_list(GTK_COMBO(combo), FALSE, FALSE);
192
gtk_combo_set_use_arrows(GTK_COMBO(combo), TRUE);
193
gtk_combo_set_use_arrows_always(GTK_COMBO(combo), TRUE);
194
gtk_widget_set_size_request(combo, 64, -1);
195
gtk_tooltips_set_tip((GtkTooltips *) tt, GTK_COMBO(combo)->entry,
196
_("Preferred resolution (dots per inch) of bitmap"), NULL);
199
for (unsigned i = 0; pdr[i] != NULL; i++) {
200
sl = g_list_prepend(sl, (gpointer) pdr[i]);
202
sl = g_list_reverse(sl);
203
gtk_combo_set_popdown_strings(GTK_COMBO(combo), sl);
206
gchar const *val = mod->get_param_string("resolution");
207
gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), val);
209
gtk_box_pack_end(GTK_BOX(hb), combo, FALSE, FALSE, 0);
210
GtkWidget *l = gtk_label_new(_("Resolution:"));
211
gtk_box_pack_end(GTK_BOX(hb), l, FALSE, FALSE, 0);
214
/* if the destination isn't already set, we must prompt for it */
216
/* Print destination frame */
217
f = gtk_frame_new(_("Print destination"));
218
gtk_box_pack_start(GTK_BOX(vbox), f, FALSE, FALSE, 4);
219
vb = gtk_vbox_new(FALSE, 4);
220
gtk_container_add(GTK_CONTAINER(f), vb);
221
gtk_container_set_border_width(GTK_CONTAINER(vb), 4);
223
l = gtk_label_new(_("Printer name (as given by lpstat -p);\n"
224
"leave empty to use the system default printer.\n"
225
"Use '> filename' to print to file.\n"
226
"Use '| prog arg...' to pipe to a program."));
227
gtk_box_pack_start(GTK_BOX(vb), l, FALSE, FALSE, 0);
230
gtk_entry_set_text(GTK_ENTRY(e), ( destination != NULL
233
gtk_box_pack_start(GTK_BOX(vb), e, FALSE, FALSE, 0);
235
// pressing enter in the destination field is the same as clicking Print:
236
gtk_entry_set_activates_default(GTK_ENTRY(e), TRUE);
239
gtk_widget_show_all(vbox);
241
int const response = gtk_dialog_run(GTK_DIALOG(dlg));
243
g_object_unref((GObject *) tt);
245
if (response == GTK_RESPONSE_OK) {
249
_bitmap = gtk_toggle_button_get_active((GtkToggleButton *) rb);
250
sstr = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)->entry));
251
_dpi = (unsigned int) MAX((int)(atof(sstr)), 1);
253
/* if the destination was prompted for, record the new value */
255
/* Arrgh, have to do something */
256
fn = gtk_entry_get_text(GTK_ENTRY(e));
257
/* skip leading whitespace, bug #1068483 */
258
while (fn && *fn==' ') { fn++; }
259
/* g_print("Printing to %s\n", fn); */
261
mod->set_param_string("destination", (gchar *)fn);
263
mod->set_param_bool("bitmap", _bitmap);
264
mod->set_param_string("resolution", (gchar *)sstr);
268
gtk_widget_destroy(dlg);
274
PrintPS::begin(Inkscape::Extension::Print *mod, SPDocument *doc)
276
gboolean epsexport = false;
279
_latin1_encoded_fonts.clear();
280
_newlatin1font_proc_defined = false;
284
FILE *osf_tmp = NULL;
287
gsize bytesWritten = 0;
288
GError *error = NULL;
289
//check whether fonts have to be embedded in the (EPS only) output
290
bool font_embedded = mod->fontEmbedded();
291
gchar const *utf8_fn = mod->get_param_string("destination");
292
gchar *local_fn = g_filename_from_utf8( utf8_fn,
293
-1, &bytesRead, &bytesWritten, &error);
294
gchar const *fn = local_fn;
296
/* TODO: Replace the below fprintf's with something that does the right thing whether in
297
* gui or batch mode (e.g. --print=blah). Consider throwing an exception: currently one of
298
* the callers (sp_print_document_to_file, "ret = mod->begin(doc)") wrongly ignores the
304
while (g_ascii_isspace(*fn)) fn += 1;
306
osp = popen(fn, "w");
308
osp = _popen(fn, "w");
311
fprintf(stderr, "inkscape: popen(%s): %s\n",
312
fn, strerror(errno));
315
_stream = _begin_stream = osp;
316
} else if (*fn == '>') {
318
epsexport = g_str_has_suffix(fn,".eps");
319
while (isspace(*fn)) fn += 1;
320
Inkscape::IO::dump_fopen_call(fn, "K");
321
osf = Inkscape::IO::fopen_utf8name(fn, "w+");
323
fprintf(stderr, "inkscape: fopen(%s): %s\n",
324
fn, strerror(errno));
328
/* if font embedding is requested for EPS export...
329
* TODO:could be extended to PS export if texttopath=FALSE possible
331
if(font_embedded && epsexport)
334
* Create temporary file where to print the main "script" part of the EPS document.
335
* Use an extra stream (_begin_stream) to print the prolog and document setup sections.
336
* Thus, once all the (main) script part printed, all the fonts used are known and can be embedded
337
* just after the prolog section, in a Begin(End)Setup section (document setup),
338
* one Begin(End)Resource (DSC comment) section for each font embedded.
339
* Then, append the final script part from the temporary file (_stream in this case).
340
* Reference: Adobe Technical note 5001, "PostScript Document Struturing Conventions Specifications"
346
g_warning("Could not create a temporary file for font embedding. Font embedding canceled.");
347
mod->set_param_bool("fontEmbedded", false);
348
font_embedded = false;
350
} else _stream = osf_tmp;
351
} else _stream = osf;
353
/* put cwd stuff in here */
355
? g_strdup_printf("lpr -P %s", fn) /* FIXME: quote fn */
358
osp = popen(qn, "w");
360
osp = _popen(qn, "w");
363
fprintf(stderr, "inkscape: popen(%s): %s\n",
364
qn, strerror(errno));
368
_stream = _begin_stream = osp;
375
/* fixme: this is kinda icky */
376
#if !defined(_WIN32) && !defined(__WIN32__)
377
(void) signal(SIGPIPE, SIG_IGN);
381
int const res = fprintf(_begin_stream, ( epsexport
382
? "%%!PS-Adobe-3.0 EPSF-3.0\n"
383
: "%%!PS-Adobe-3.0\n" ));
384
/* flush this to test output stream as early as possible */
385
if (fflush(_begin_stream)) {
386
/*g_print("caught error in sp_module_print_plain_begin\n");*/
387
if (ferror(_begin_stream)) {
388
g_print("Error %d on output stream: %s\n", errno,
391
g_print("Printing failed\n");
392
/* fixme: should use pclose() for pipes */
393
fclose(_begin_stream);
394
_begin_stream = NULL;
398
//TODO: do this same test on _stream
400
// width and height in pt
401
_width = sp_document_width(doc) * PT_PER_PX;
402
_height = sp_document_height(doc) * PT_PER_PX;
405
bool pageBoundingBox;
407
pageBoundingBox = mod->get_param_bool("pageBoundingBox");
408
// printf("Page Bounding Box: %s\n", pageBoundingBox ? "TRUE" : "FALSE");
409
if (pageBoundingBox) {
412
d.y1 = ceil(_height);
414
SPItem* doc_item = SP_ITEM(sp_document_root(doc));
415
sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
416
// convert from px to pt
423
Inkscape::SVGOStringStream os;
426
os << "%%Creator: " << PACKAGE_STRING << "\n";
427
// This will become problematic if inkscape gains the
428
// ability to handle multi paged documents. If this is
429
// the case the %%Orientation: comments should be
430
// renamed to %%PageOrientation: and moved to the
432
os << "%%Pages: 1\n";
435
// The point of the following code is (1) to do the thing that's expected by users
436
// who have done File>New>A4_landscape or ...letter_landscape (i.e., rotate
437
// the output), while (2) not messing up users who simply want their output wider
438
// than it is tall (e.g., small figures for inclusion in LaTeX).
439
// The original patch by WQ only had the w>h condition.
441
double w = (d.x1 - d.x0); // width and height of bounding box, in pt
442
double h = (d.y1 - d.y0);
444
(w > 0. && h > 0.) // empty documents fail this sanity check, have w<0, h<0
445
&& (w > h) // implies, but does not prove, the user wanted landscape
446
&& (w > 600) // approximate maximum printable width of an A4
447
&& (!epsexport) // eps should always be portrait
453
os << "%%Orientation: Landscape\n";
454
os << "%%BoundingBox: " << (int) (_height - d.y1) << " "
456
<< (int) ceil(_height - d.y0) << " "
457
<< (int) ceil(d.x1) << "\n";
458
// According to Mike Sweet (the author of CUPS)
459
// HiResBoundingBox is only appropriate
460
// for EPS files. This means that we should
461
// distinguish if we export to ps or eps here.
462
// FIXME: I couldn't find HiResBoundingBox in the PS
463
// reference manual, so I guess we should skip
465
os << "%%HiResBoundingBox: " << (_height - d.y1) << " "
467
<< (_height - d.y0) << " "
470
os << "%%DocumentMedia: plain "
471
<< (int) ceil(_height) << " "
472
<< (int) ceil(_width) << " "
476
os << "%%Orientation: Portrait\n";
477
os << "%%BoundingBox: " << (int) d.x0 << " "
479
<< (int) ceil(d.x1) << " "
480
<< (int) ceil(d.y1) << "\n";
481
os << "%%HiResBoundingBox: " << d.x0 << " "
486
os << "%%DocumentMedia: plain "
487
<< (int) ceil(_width) << " "
488
<< (int) ceil(_height) << " "
493
os << "%%EndComments\n";
494
/* If font embedding requested, begin document setup section where to include font resources */
495
if(font_embedded) os << "%%BeginSetup\n";/* Resume it later with Begin(End)Resource sections for font embedding. So, for now, we are done with the prolog/setup part. */
496
gint ret = fprintf(_begin_stream, "%s", os.str().c_str());
497
if(ret < 0) return ret;
499
/* Main Script part (after document setup) begins */
500
/* Empty os from all previous printing */
501
std::string clrstr = "";
503
// This will become problematic if we print multi paged documents:
504
os << "%%Page: 1 1\n";
509
os << "0 " << (int) -ceil(_height) << " translate\n";
513
os << "0 " << (int) ceil(_height) << " translate\n";
518
os << PT_PER_PX << " " << -PT_PER_PX << " scale\n";
519
// from now on we can output px, but they will be treated as pt
522
/* As a new PS document is created, _fontlist has to be reinitialized (unref fonts from possible former PS docs) */
523
_fonts = g_tree_new_full((GCompareDataFunc)strcmp, NULL, (GDestroyNotify)g_free, (GDestroyNotify)g_free);
526
os << "0 0 0 setrgbcolor\n"
528
<< "1 setlinewidth\n"
532
/* FIXME: This function is declared to return unsigned, whereas fprintf returns a signed int *
533
* that can be zero if the first fprintf failed (os is empty) or "negative" (i.e. very positive
534
* in unsigned int interpretation) if the first fprintf failed but this one succeeds, or
535
* positive if both succeed. */
536
return fprintf(_stream, "%s", os.str().c_str());
540
PrintPS::finish(Inkscape::Extension::Print *mod)
542
if (!_stream) return 0;
545
double const dots_per_pt = _dpi / PT_PER_IN;
547
double const x0 = 0.0;
548
double const y0 = 0.0;
549
double const x1 = x0 + _width;
550
double const y1 = y0 + _height;
552
/* Bitmap width/height in bitmap dots. */
553
int const width = (int) (_width * dots_per_pt + 0.5);
554
int const height = (int) (_height * dots_per_pt + 0.5);
557
affine[0] = width / ((x1 - x0) * PX_PER_PT);
560
affine[3] = height / ((y1 - y0) * PX_PER_PT);
561
affine[4] = -affine[0] * x0;
562
affine[5] = -affine[3] * y0;
564
nr_arena_item_set_transform(mod->root, &affine);
566
guchar *const px = g_new(guchar, 4 * width * 64);
568
for (int y = 0; y < height; y += 64) {
569
/* Set area of interest. */
574
bbox.y1 = MIN(height, y + 64);
576
/* Update to renderable state. */
578
gc.transform.setIdentity();
579
nr_arena_item_invoke_update(mod->root, &bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
581
/* This should take guchar* instead of unsigned char*) */
583
nr_pixblock_setup_extern(&pb, NR_PIXBLOCK_MODE_R8G8B8A8N,
584
bbox.x0, bbox.y0, bbox.x1, bbox.y1,
585
(guchar*)px, 4 * width, FALSE, FALSE);
586
memset(px, 0xff, 4 * width * 64);
587
nr_arena_item_invoke_render(NULL, mod->root, &bbox, &pb, 0);
588
/* Blitter goes here */
590
imgt[0] = (bbox.x1 - bbox.x0) / dots_per_pt;
593
imgt[3] = (bbox.y1 - bbox.y0) / dots_per_pt;
595
imgt[5] = _height - y / dots_per_pt - (bbox.y1 - bbox.y0) / dots_per_pt;
597
print_image(_stream, px, bbox.x1 - bbox.x0, bbox.y1 - bbox.y0, 4 * width, &imgt);
603
fprintf(_stream, "showpage\n");
604
int const res = fprintf(_stream, "%%%%EOF\n");
606
/* Flush stream to be sure. */
607
(void) fflush(_stream);
610
/* If font embedding... */
611
if(mod->get_param_bool("fontEmbedded"))
613
/* Close the document setup section that had been started (because all the needed resources are supposed to be included now) */
614
/*res = */fprintf(_begin_stream, "%s", "%%EndSetup\n");
615
/* If font embedding requested, the following PS script part was printed to a different file from the prolog/setup, so script part (current _stream) needs to be copied to prolog/setup file to get the complete (E)PS document */
616
if(fseek(_stream, 0, SEEK_SET) == 0)
618
while((c = fgetc(_stream))!=EOF) fputc(c, _begin_stream);
621
_stream = _begin_stream;
624
/* fixme: should really use pclose for popen'd streams */
628
_latin1_encoded_fonts.clear();
630
g_tree_destroy(_fonts);
636
PrintPS::bind(Inkscape::Extension::Print */*mod*/, Geom::Matrix const *transform, float /*opacity*/)
638
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
639
if (_bitmap) return 0;
641
Inkscape::SVGOStringStream os;
642
os << "gsave [" << (*transform)[0] << " "
643
<< (*transform)[1] << " "
644
<< (*transform)[2] << " "
645
<< (*transform)[3] << " "
646
<< (*transform)[4] << " "
647
<< (*transform)[5] << "] concat\n";
649
return fprintf(_stream, "%s", os.str().c_str());
653
PrintPS::release(Inkscape::Extension::Print */*mod*/)
655
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
656
if (_bitmap) return 0;
658
return fprintf(_stream, "grestore\n");
662
PrintPS::comment(Inkscape::Extension::Print */*mod*/, char const *comment)
664
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
665
if (_bitmap) return 0;
667
return fprintf(_stream, "%%! %s\n",comment);
671
PrintPS::print_fill_style(SVGOStringStream &os, SPStyle const *const style, NRRect const *pbox)
673
g_return_if_fail( style->fill.isColor()
674
|| ( style->fill.isPaintserver()
675
&& SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) ) );
677
if (style->fill.isColor()) {
679
sp_color_get_rgb_floatv(&style->fill.value.color, rgb);
681
os << rgb[0] << " " << rgb[1] << " " << rgb[2] << " setrgbcolor\n";
684
g_assert( style->fill.isPaintserver()
685
&& SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) );
687
if (SP_IS_LINEARGRADIENT(SP_STYLE_FILL_SERVER(style))) {
689
SPLinearGradient *lg = SP_LINEARGRADIENT(SP_STYLE_FILL_SERVER(style));
690
Geom::Point p1 (lg->x1.computed, lg->y1.computed);
691
Geom::Point p2 (lg->x2.computed, lg->y2.computed);
692
if (pbox && SP_GRADIENT(lg)->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
693
// convert to userspace
694
Geom::Matrix bbox2user(pbox->x1 - pbox->x0, 0, 0, pbox->y1 - pbox->y0, pbox->x0, pbox->y0);
699
os << "<<\n/ShadingType 2\n/ColorSpace /DeviceRGB\n";
700
os << "/Coords [" << p1[Geom::X] << " " << p1[Geom::Y] << " " << p2[Geom::X] << " " << p2[Geom::Y] <<"]\n";
701
os << "/Extend [true true]\n";
702
os << "/Domain [0 1]\n";
703
os << "/Function <<\n/FunctionType 3\n/Functions\n[\n";
705
sp_gradient_ensure_vector(SP_GRADIENT(lg)); // when exporting from commandline, vector is not built
706
for (unsigned i = 0; i + 1 < lg->vector.stops.size(); i++) {
708
sp_color_get_rgb_floatv(&lg->vector.stops[i].color, rgb);
709
os << "<<\n/FunctionType 2\n/Domain [0 1]\n";
710
os << "/C0 [" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "]\n";
711
sp_color_get_rgb_floatv(&lg->vector.stops[i+1].color, rgb);
712
os << "/C1 [" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "]\n";
715
os << "]\n/Domain [0 1]\n";
717
for (unsigned i = 0; i + 2 < lg->vector.stops.size(); i++) {
718
os << lg->vector.stops[i+1].offset <<" ";
722
for (unsigned i = 0; i + 1 < lg->vector.stops.size(); i++) {
728
} else if (SP_IS_RADIALGRADIENT(SP_STYLE_FILL_SERVER(style))) {
730
SPRadialGradient *rg = SP_RADIALGRADIENT(SP_STYLE_FILL_SERVER(style));
731
Geom::Point c(rg->cx.computed, rg->cy.computed);
732
Geom::Point f(rg->fx.computed, rg->fy.computed);
733
double r = rg->r.computed;
734
if (pbox && SP_GRADIENT(rg)->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
735
// convert to userspace
736
Geom::Matrix const bbox2user(pbox->x1 - pbox->x0, 0,
737
0, pbox->y1 - pbox->y0,
741
r *= bbox2user.descrim();
744
os << "<<\n/ShadingType 3\n/ColorSpace /DeviceRGB\n";
745
os << "/Coords ["<< f[Geom::X] <<" "<< f[Geom::Y] <<" 0 "<< c[Geom::X] <<" "<< c[Geom::Y] <<" "<< r <<"]\n";
746
os << "/Extend [true true]\n";
747
os << "/Domain [0 1]\n";
748
os << "/Function <<\n/FunctionType 3\n/Functions\n[\n";
750
sp_gradient_ensure_vector(SP_GRADIENT(rg)); // when exporting from commandline, vector is not built
751
for (unsigned i = 0; i + 1 < rg->vector.stops.size(); i++) {
753
sp_color_get_rgb_floatv(&rg->vector.stops[i].color, rgb);
754
os << "<<\n/FunctionType 2\n/Domain [0 1]\n";
755
os << "/C0 [" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "]\n";
756
sp_color_get_rgb_floatv(&rg->vector.stops[i+1].color, rgb);
757
os << "/C1 [" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "]\n";
760
os << "]\n/Domain [0 1]\n";
762
for (unsigned i = 0; i + 2 < rg->vector.stops.size(); i++) {
763
os << rg->vector.stops[i+1].offset << " ";
767
for (unsigned i = 0; i + 1 < rg->vector.stops.size(); i++) {
777
PrintPS::print_stroke_style(SVGOStringStream &os, SPStyle const *style)
780
sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
782
os << rgb[0] << " " << rgb[1] << " " << rgb[2] << " setrgbcolor\n";
784
// There are rare cases in which for a solid line stroke_dasharray_set is true. To avoid
785
// invalid PS-lines such as "[0.0000000 0.0000000] 0.0000000 setdash", which should be "[] 0 setdash",
786
// we first check if all components of stroke_dash.dash are 0.
787
bool LineSolid = true;
788
if (style->stroke_dash.n_dash &&
789
style->stroke_dash.dash )
792
while (LineSolid && (i < style->stroke_dash.n_dash)) {
793
if (style->stroke_dash.dash[i] > 0.00000001)
799
for (i = 0; i < style->stroke_dash.n_dash; i++) {
803
os << style->stroke_dash.dash[i];
805
os << "] " << style->stroke_dash.offset << " setdash\n";
807
os << "[] 0 setdash\n";
810
os << "[] 0 setdash\n";
813
os << style->stroke_width.computed << " setlinewidth\n";
814
os << style->stroke_linejoin.computed << " setlinejoin\n";
815
os << style->stroke_linecap.computed << " setlinecap\n";
820
PrintPS::fill(Inkscape::Extension::Print *mod, Geom::PathVector const &pathv, Geom::Matrix const *ctm, SPStyle const *const style,
821
NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
823
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
824
if (_bitmap) return 0;
826
if ( style->fill.isColor()
827
|| ( style->fill.isPaintserver()
828
&& SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) ) )
830
Inkscape::SVGOStringStream os;
834
print_fill_style(os, style, pbox);
836
print_pathvector(os, pathv);
838
if (style->fill_rule.computed == SP_WIND_RULE_EVENODD) {
839
if (style->fill.isColor()) {
842
g_assert( style->fill.isPaintserver()
843
&& SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) );
844
SPGradient const *g = SP_GRADIENT(SP_STYLE_FILL_SERVER(style));
846
if (g->gradientTransform_set) {
847
os << "gsave [" << g->gradientTransform[0] << " " << g->gradientTransform[1]
848
<< " " << g->gradientTransform[2] << " " << g->gradientTransform[3]
849
<< " " << g->gradientTransform[4] << " " << g->gradientTransform[5] << "] concat\n";
852
if (g->gradientTransform_set) {
857
if (style->fill.isColor()) {
860
g_assert( style->fill.isPaintserver()
861
&& SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) );
862
SPGradient const *g = SP_GRADIENT(SP_STYLE_FILL_SERVER(style));
864
if (g->gradientTransform_set) {
865
os << "gsave [" << g->gradientTransform[0] << " " << g->gradientTransform[1]
866
<< " " << g->gradientTransform[2] << " " << g->gradientTransform[3]
867
<< " " << g->gradientTransform[4] << " " << g->gradientTransform[5] << "] concat\n";
870
if (g->gradientTransform_set) {
878
fprintf(_stream, "%s", os.str().c_str());
886
PrintPS::stroke(Inkscape::Extension::Print *mod, Geom::PathVector const &pathv, Geom::Matrix const *ctm, SPStyle const *style,
887
NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
889
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
890
if (_bitmap) return 0;
892
if (style->stroke.isColor()) {
893
Inkscape::SVGOStringStream os;
895
print_stroke_style(os, style);
897
print_pathvector(os, pathv);
901
fprintf(_stream, "%s", os.str().c_str());
908
PrintPS::image(Inkscape::Extension::Print *mod, guchar *px, unsigned int w, unsigned int h, unsigned int rs,
909
Geom::Matrix const *transform, SPStyle const *style)
911
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
912
if (_bitmap) return 0;
914
return print_image(_stream, px, w, h, rs, transform);
917
/* PSFontName is now useless (cf. text() method code) */
919
PrintPS::PSFontName(SPStyle const *style)
921
font_instance *tf = font_factory::Default()->FaceFromStyle(style);
926
// PS does not like spaces in fontnames, replace them with the usual dashes.
929
tf->PSName(name_buf, sizeof(name_buf));
930
n = g_strdelimit(name_buf, " ", '-');
933
// 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
934
bool i = (style->font_style.value == SP_CSS_FONT_STYLE_ITALIC);
935
bool o = (style->font_style.value == SP_CSS_FONT_STYLE_OBLIQUE);
936
bool b = (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) ||
937
(style->font_weight.value >= SP_CSS_FONT_WEIGHT_500 && style->font_weight.value <= SP_CSS_FONT_WEIGHT_900);
939
n = g_strdup_printf("%s%s%s%s",
940
g_strdelimit(style->text->font_family.value, " ", '-'),
941
(b || i || o) ? "-" : "",
943
(i) ? "Italic" : ((o) ? "Oblique" : "") );
949
//LSB = Least Significant Byte
950
//converts 4-byte array to "LSB first" to "LSB last"
952
* (Used by PrintPS::embed_t1 (from libgnomeprint/gnome-font-face.c),
953
* to get the length of data segment (bytes 3-6 in IBM PC (PostScript) font file format.
954
* Reference: Adobe technical note 5040, "Supporting Downloadable PostScript
955
* Language Fonts", page 9)
958
#define INT32_LSB_2_5(q) ((q)[2] + ((q)[3] << 8) + ((q)[4] << 16) + ((q)[5] << 24))
961
* \brief For "Type 1" font only, print font data in output stream, to embed font data in PS output.
962
* \param os Stream of output.
963
* \param font Font whose data to embed.
964
* \return FALSE if font embedding canceled (due to error or not supported font type), TRUE otherwise
965
* TODO: enable font embedding for True Type
967
//adapted more/less from libgnomeprint/gnome_font_face_ps_embed_t1()
969
PrintPS::embed_t1 (SVGOStringStream &os, font_instance* font)
972
FT_Face font_face = pango_ft2_font_get_face(font->pFont);
973
const FT_String* font_type = FT_Get_X11_Font_Format(font_face);
974
g_return_val_if_fail (_fontTypesMap[font_type] == FONT_TYPE1, false);
975
//get font filename, stream to font file and size
976
FT_Stream font_stream = font_face->stream;
977
const char* font_filename = (char*) font_stream->pathname.pointer;
978
unsigned long font_stream_size = font_stream->size;
979
//first detect if font file is in IBM PC format
981
* if first byte is 0x80, font file is pfb, do the same as a pfb to pfa converter
982
* Reference: Adobe technical note 5040, "Supporting Downloadable PostScript
983
* Language Fonts", page 9
984
* else: include all the ASCII data in the font pfa file
986
char* buf = new char[7];
987
unsigned char* buffer = new unsigned char[7];//for the 6 header bytes (data segment length is unknown at this point) and final '\0'
988
std::string ascii_data;//for data segment "type 1" in IBM PC Format
989
//read the 6 header bytes
990
//for debug: g_warning("Reading from font file %s...", font_filename);
991
font_stream->close(font_stream);
992
ifstream font_file (font_filename, ios::in|ios::binary);
993
if (!font_file.is_open()) {
994
g_warning ("file %s: line %d: Cannot open font file %s", __FILE__, __LINE__, font_filename);
997
font_file.read(buf, 6);
998
buffer = (unsigned char*) buf;
1000
//If font file is pfb, do the same as pfb to pfa converter
1002
if (buffer[0] == 0x80) {
1003
const char hextab[17] = "0123456789abcdef";
1004
unsigned long offset = 0;
1006
while (offset < font_stream_size) {
1008
if (buffer[0] != 0x80) {
1009
g_warning ("file %s: line %d: Corrupt %s", __FILE__, __LINE__, font_filename);
1010
//TODO: print some default font data anyway like libgnomeprint/gnome_font_face_ps_embed_empty
1013
switch (buffer[1]) {
1015
//get data segment length from bytes 3-6
1016
//(Note: byte 1 is first byte in comments to match Adobe technical note 5040, but index 0 in code)
1017
length = INT32_LSB_2_5 (buffer);
1019
//resize the buffer to fit the data segment length
1021
buf = new char[length + 1];
1022
buffer = new unsigned char[length + 1];
1023
//read and print all the data segment length
1024
font_file.read(buf, length);
1025
buffer = (unsigned char*) buf;
1027
* Assigning a part from the buffer of length "length" ensures
1028
* that no incorrect extra character will be printed and make the PS output invalid
1029
* That was the case with the code:
1031
* (A substring method could have been used as well.)
1033
ascii_data.assign(buf, 0, length);
1036
//read next 6 header bytes
1037
font_file.read(buf, 6);
1040
length = INT32_LSB_2_5 (buffer);
1042
//resize the buffer to fit the data segment length
1044
buf = new char[length + 1];
1045
buffer = new unsigned char[length + 1];
1046
//read and print all the data segment length
1047
font_file.read(buf, length);
1048
buffer = (unsigned char*) buf;
1049
for (i = 0; i < length; i++) {
1050
os << hextab[buffer[i] >> 4];
1051
os << hextab[buffer[i] & 15];
1053
if ((i & 31) == 31 || i == length - 1)
1056
//read next 6 header bytes
1057
font_file.read(buf, 6);
1062
offset = font_stream_size;
1065
os << "%%%ERROR: Font file corrupted at byte " << offset << "\n";
1066
//TODO: print some default font data anyway like libgnomeprint/gnome_font_face_ps_embed_empty
1071
//else: font file is pfa, include all directly
1073
//font is not in IBM PC format, all the file content can be directly printed
1076
buf = new char[font_stream_size + 1];
1078
font_file.seekg (0, ios::beg);
1079
font_file.read(buf, font_stream_size);
1081
* Assigning a part from the buffer of length "length" ensures
1082
* that no incorrect extra character will be printed and make the PS output invalid
1083
* That was the case with the code:
1085
* (A substring method could have been used as well.)
1087
ascii_data.assign(buf, 0, font_stream_size);
1093
buffer = NULL;// Clear buffer to prevent using invalid memory reference.
1095
char font_psname[256];
1096
font->PSName(font_psname, sizeof(font_psname));
1097
FT_Long font_num_glyphs = font_face->num_glyphs;
1098
if (font_num_glyphs < 256) {
1101
os << "(" << font_psname << ") cvn findfont dup length dict begin\n";
1102
os << "{1 index /FID ne {def} {pop pop} ifelse} forall\n";
1103
os << "/Encoding [\n";
1104
for (glyph = 0; glyph < 256; glyph++) {
1108
g = (glyph < font_num_glyphs) ? glyph : 0;
1109
status = FT_Get_Glyph_Name (font_face, g, c, 256);
1111
if (status != FT_Err_Ok) {
1112
g_warning ("file %s: line %d: Glyph %d has no name in %s", __FILE__, __LINE__, g, font_filename);
1113
g_snprintf (c, 256, ".notdef");
1116
os << "/" << c << ( ((glyph & 0xf) == 0xf)?"\n":" " );
1118
os << "] def currentdict end\n";
1119
//TODO: manage several font instances for same ps name like in libgnomeprint/gnome_print_ps2_set_font_real()
1120
//gf_pso_sprintf (pso, "(%s) cvn exch definefont pop\n", pso->encodedname);
1121
os << "(" << font_psname << ") cvn exch definefont pop\n";
1125
nfonts = (font_num_glyphs + 255) >> 8;
1127
os << "32 dict begin\n";
1128
/* Common entries */
1129
os << "/FontType 0 def\n";
1130
os << "/FontMatrix [1 0 0 1 0 0] def\n";
1131
os << "/FontName (" << font_psname << "-Glyph-Composite) cvn def\n";
1132
os << "/LanguageLevel 2 def\n";
1134
/* Type 0 entries */
1135
os << "/FMapType 2 def\n";
1137
/* Bitch 'o' bitches */
1138
os << "/FDepVector [\n";
1140
for (i = 0; i < nfonts; i++) {
1141
os << "(" << font_psname << ") cvn findfont dup length dict begin\n";
1142
os << "{1 index /FID ne {def} {pop pop} ifelse} forall\n";
1143
os << "/Encoding [\n";
1144
for (j = 0; j < 256; j++) {
1148
glyph = 256 * i + j;
1149
if (glyph >= font_num_glyphs)
1151
status = FT_Get_Glyph_Name (font_face, glyph, c, 256);
1152
if (status != FT_Err_Ok) {
1153
g_warning ("file %s: line %d: Glyph %d has no name in %s", __FILE__, __LINE__, glyph, font_filename);
1154
g_snprintf (c, 256, ".notdef");
1156
os << "/" << c << ( ((j & 0xf) == 0xf)?"\n":" " );
1159
os << "currentdict end (" << font_psname << "-Glyph-Page-";
1160
os << std::dec << i;
1161
os << ") cvn exch definefont\n";
1164
os << "/Encoding [\n";
1165
for (i = 0; i < 256; i++) {
1167
fn = (i < nfonts) ? i : 0;
1168
os << std::dec << fn;
1169
os << ( ((i & 0xf) == 0xf) ? "\n" : " " );
1172
os << "currentdict end\n";
1173
//TODO: manage several font instances for same ps name like in libgnomeprint/gnome_print_ps2_set_font_real()
1174
//gf_pso_sprintf (pso, "(%s) cvn exch definefont pop\n", pso->encodedname);
1175
os << "(" << font_psname << ") cvn exch definefont pop\n";
1177
//font embedding completed
1184
* \brief Print font data in output stream, to embed font data in PS output.
1185
* \param os Stream of output.
1186
* \param font Font whose data to embed.
1187
* \return FALSE if font embedding canceled (due to error or not supported font type), TRUE otherwise
1189
//adapted from libgnomeprint/gnome_font_face_ps_embed()
1190
bool PrintPS::embed_font(SVGOStringStream &os, font_instance* font)
1192
//Hinted at by a comment in libgnomeprint/fcpattern_to_gp_font_entry()
1193
//Determining the font type in the "Pango way"
1194
FT_Face font_face = pango_ft2_font_get_face(font->pFont);
1195
const FT_String* font_type = FT_Get_X11_Font_Format(font_face);
1198
* Possible values of "font_type": Type 1, TrueType, etc.
1199
* Embedding available only for Type 1 fonts so far.
1201
//TODO: provide support for other font types (TrueType is a priority)
1202
switch(_fontTypesMap[font_type])
1205
return embed_t1 (os, font);
1206
//TODO: implement TT font embedding
1207
/*case FONT_TRUETYPE:
1208
embed_tt (os, font);
1211
g_warning("Unknown (not supported) font type for embedding: %s", font_type);
1212
//TODO: embed something like in libgnomeprint/gnome_font_face_ps_embed_empty();
1219
* \brief Converts UTF-8 string to sequence of glyph numbers for PostScript string (cf. "show" commands.).
1220
* \param os Stream of output.
1221
* \param font Font used for unicode->glyph mapping.
1222
* \param unistring UTF-8 encoded string to convert.
1224
void PrintPS::print_glyphlist(SVGOStringStream &os, font_instance* font, Glib::ustring unistring)
1226
//iterate through unicode chars in unistring
1227
Glib::ustring::iterator unistring_iter;
1229
gint glyph_index, glyph_page;
1231
FT_Face font_face = pango_ft2_font_get_face(font->pFont);
1232
FT_Long font_num_glyphs = font_face->num_glyphs;
1233
//whether font has more than one glyph pages (16-bit encoding)
1234
bool two_bytes_encoded = (font_num_glyphs > 255);
1236
for (unistring_iter = unistring.begin(); unistring_iter!=unistring.end(); unistring_iter++)
1239
unichar = *unistring_iter;
1240
//get matching glyph index in current font for unicode char
1241
//default glyph index is 0 for undefined font character (glyph unavailable)
1242
//TODO: if glyph unavailable for current font, use a default font among the most Unicode-compliant - e.g. Bitstream Cyberbit - I guess
1243
glyph_index = font->MapUnicodeChar(unichar);
1244
//if more than one glyph pages for current font (16-bit encoding),
1245
if(two_bytes_encoded)
1247
//add glyph page before glyph index.
1248
glyph_page = (glyph_index >> 8) & 0xff;
1250
//convert in octal code before printing
1251
os << std::oct << glyph_page;
1253
//(If one page - 8-bit encoding -, nothing to add.)
1254
//TODO: explain the following line inspired from libgnomeprint/gnome_print_ps2_glyphlist()
1255
glyph_index = glyph_index & 0xff;
1256
//TODO: mark glyph as used for current font, if Inkscape has to embed minimal font data in PS
1258
//convert in octal code before printing
1259
os << std::oct << glyph_index;
1264
PrintPS::text(Inkscape::Extension::Print *mod, char const *text, Geom::Point p,
1265
SPStyle const *const style)
1267
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
1268
if (_bitmap) return 0;
1270
//check whether fonts have to be embedded in the PS output
1271
//if not, use the former way of Inkscape to print text
1272
gboolean font_embedded = mod->fontEmbedded();
1274
Inkscape::SVGOStringStream os;
1277
* A font_instance object is necessary for the next steps,
1278
* that's why using PSFontName() method just to get the PS fontname
1279
* is not enough and not appropriate
1281
font_instance *tf = font_factory::Default()->FaceFromStyle(style);
1283
const gchar *fn = NULL;
1286
//check whether font was found
1288
* This check is not strictly reliable
1289
* since Inkscape returns a default font if font not found.
1290
* This is just to be consistent with the method PSFontName().
1294
tf->PSName(name_buf, sizeof(name_buf));
1297
// this system does not have this font, so cancel font embedding...
1298
font_embedded = FALSE;
1299
//this case seems to never happen since Inkscape uses a default font instead (like BitstreamVeraSans on Windows)
1300
g_warning("Font %s not found.", fn);
1301
//...and just use the name from SVG in the hope that PS interpreter will make sense of it
1302
bool i = (style->font_style.value == SP_CSS_FONT_STYLE_ITALIC);
1303
bool o = (style->font_style.value == SP_CSS_FONT_STYLE_OBLIQUE);
1304
bool b = (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) ||
1305
(style->font_weight.value >= SP_CSS_FONT_WEIGHT_500 && style->font_weight.value <= SP_CSS_FONT_WEIGHT_900);
1307
fn = g_strdup_printf("%s%s%s%s",
1308
g_strdelimit(style->text->font_family.value, " ", '-'),
1309
(b || i || o) ? "-" : "",
1311
(i) ? "Italic" : ((o) ? "Oblique" : "") );
1315
* If font embedding is requested, tempt to embed the font the first time it is used, once and for all.
1316
* There is no selection of the glyph descriptions to embed, based on the characters used effectively in the document.
1318
* Else, back to the former way of printing.
1320
gpointer is_embedded;
1321
//if not first time the font is used and if font embedding requested, check whether the font has been embedded (successfully the first time).
1322
if(g_tree_lookup_extended(_fonts, fn, NULL, &is_embedded)) font_embedded = font_embedded && (strcmp((char *)is_embedded, "TRUE") == 0);
1325
//first time the font is used
1328
//embed font in PS output
1329
//adapted from libgnomeprint/gnome_print_ps2_close()
1330
os << "%%BeginResource: font " << fn << "\n";
1331
font_embedded = embed_font(os, tf);
1332
os << "%%EndResource: font " << fn << "\n";
1333
if(!font_embedded) g_warning("Font embedding canceled for font: %s", fn);
1334
else fprintf(_begin_stream, "%s", os.str().c_str());
1335
//empty os before resume printing to the script stream
1336
std::string clrstr = "";
1341
g_tree_insert(_fonts, g_strdup(fn), g_strdup((font_embedded)?"TRUE":"FALSE"));
1346
Inkscape::SVGOStringStream escaped_text;
1347
//if font embedding, all characters will be converted to glyph indices (cf. PrintPS::print_glyphlist()),
1348
//so no need to escape characters
1349
//else back to the old way, i.e. escape chars: '\',')','(' and UTF-8 ones
1350
if(font_embedded) s = text;
1352
escaped_text << std::oct;
1353
for (gchar const *p_text = text ; *p_text ; p_text = g_utf8_next_char(p_text)) {
1354
gunichar const c = g_utf8_get_char(p_text);
1355
if (c == '\\' || c == ')' || c == '(')
1356
escaped_text << '\\' << static_cast<char>(c);
1358
escaped_text << '\\' << c;
1360
escaped_text << static_cast<char>(c);
1367
if(font_embedded) os << "/" << fn << " findfont\n";
1369
if (_latin1_encoded_fonts.find(fn) == _latin1_encoded_fonts.end()) {
1370
if (!_newlatin1font_proc_defined) {
1371
// input: newfontname, existingfontname
1372
// output: new font object, also defined to newfontname
1373
os << "/newlatin1font " // name of the proc
1374
"{findfont dup length dict copy " // load the font and create a copy of it
1375
"dup /Encoding ISOLatin1Encoding put " // change the encoding in the copy
1376
"definefont} def\n"; // create the new font and leave it on the stack, define the proc
1377
_newlatin1font_proc_defined = true;
1379
if(strchr(fn, ' ') == NULL)
1380
os << "/" << fn << "-ISOLatin1 /" << fn << " newlatin1font\n";
1382
os << "(/" << fn << "-ISOLatin1) (/" << fn << ") newlatin1font\n";
1383
_latin1_encoded_fonts.insert(fn);
1385
if(strchr(fn, ' ') == NULL)
1386
os << "/" << fn << "-ISOLatin1 findfont\n";
1388
os << "(/" << fn << "-ISOLatin1) findfont\n";
1390
os << style->font_size.computed << " scalefont\n";
1392
//The commented line beneath causes Inkscape to crash under Linux but not under Windows
1393
//g_free((void*) fn);
1395
if ( style->fill.isColor()
1396
|| ( style->fill.isPaintserver()
1397
&& SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) ) )
1400
print_fill_style(os, style, NULL);
1401
// FIXME: we don't know the pbox of text, so have to pass NULL. This means gradients with
1402
// bbox units won't work with text. However userspace gradients don't work with text either
1403
// (text is black) for some reason.
1406
os << p[Geom::X] << " " << p[Geom::Y] << " moveto\n";
1408
if(font_embedded) print_glyphlist(os, tf, s);
1409
else os << escaped_text.str();
1413
if (style->stroke.isColor()) {
1416
print_stroke_style(os, style);
1420
os << p[Geom::X] << " " << p[Geom::Y] << " moveto\n";
1422
if(font_embedded) print_glyphlist(os, tf, s);
1423
else os << escaped_text.str();
1424
os << ") false charpath stroke\n";
1431
fprintf(_stream, "%s", os.str().c_str());
1438
/* PostScript helpers */
1441
PrintPS::print_pathvector(SVGOStringStream &os, Geom::PathVector const &pathv)
1448
for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
1450
os << it->initialPoint()[Geom::X] << " " << it->initialPoint()[Geom::Y] << " moveto\n";
1452
for(Geom::Path::const_iterator cit = it->begin(); cit != it->end_open(); ++cit) {
1453
print_2geomcurve(os, *cit);
1457
os << "closepath\n";
1464
PrintPS::print_2geomcurve(SVGOStringStream &os, Geom::Curve const & c )
1469
if( is_straight_curve(c) )
1471
os << c.finalPoint()[X] << " " << c.finalPoint()[Y] << " lineto\n";
1473
else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
1474
std::vector<Geom::Point> points = cubic_bezier->points();
1475
os << points[1][X] << " " << points[1][Y] << " "
1476
<< points[2][X] << " " << points[2][Y] << " "
1477
<< points[3][X] << " " << points[3][Y] << " curveto\n";
1480
//this case handles sbasis as well as all other curve types
1481
Geom::Path sbasis_path = Geom::cubicbezierpath_from_sbasis(c.toSBasis(), 0.1);
1483
for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
1484
print_2geomcurve(os, *iter);
1490
/* The following code is licensed under GNU GPL.
1491
** The packbits, ascii85 and imaging printing code
1492
** is from the gimp's postscript.c.
1496
* \param nin Number of bytes of source data.
1497
* \param src Source data.
1498
* \param nout Number of output bytes.
1499
* \param dst Buffer for output.
1502
PrintPS::compress_packbits(int nin,
1509
int nrepeat, nliteral;
1511
guchar *start_dst = dst;
1512
guchar *last_literal = NULL;
1515
if (nin <= 0) break;
1520
/* Search repeat bytes */
1521
if ((nin > 1) && (c == src[1])) {
1525
while ((nin > 0) && (c == *src)) {
1529
if (nrepeat == 127) break; /* Maximum repeat */
1532
/* Add two-byte repeat to last literal run ? */
1534
&& (last_literal != NULL) && (((*last_literal)+1)+2 <= 128) )
1542
/* Add repeat run */
1543
*(dst++) = (guchar)((-nrepeat) & 0xff);
1545
last_literal = NULL;
1548
/* Search literal bytes */
1554
if (nin <= 0) break;
1556
if ((nin >= 2) && (src[0] == src[1])) /* A two byte repeat ? */
1562
if (nliteral == 128) break; /* Maximum literal run */
1565
/* Could be added to last literal run ? */
1566
if ((last_literal != NULL) && (((*last_literal)+1)+nliteral <= 128)) {
1567
*last_literal += nliteral;
1570
*(dst++) = (guchar)(nliteral-1);
1572
while (nliteral-- > 0) *(dst++) = *(run_start++);
1574
*nout = dst - start_dst;
1578
PrintPS::ascii85_init(void)
1581
ascii85_linewidth = 0;
1585
PrintPS::ascii85_flush(SVGOStringStream &os)
1588
bool const zero_case = (ascii85_buf == 0);
1589
static int const max_linewidth = 75;
1591
for (int i = 4; i >= 0; i--) {
1592
c[i] = (ascii85_buf % 85) + '!';
1595
/* check for special case: "!!!!!" becomes "z", but only if not
1596
* at end of data. */
1597
if (zero_case && (ascii85_len == 4)) {
1598
if (ascii85_linewidth >= max_linewidth) {
1600
ascii85_linewidth = 0;
1603
ascii85_linewidth++;
1605
for (int i = 0; i < ascii85_len+1; i++) {
1606
if ((ascii85_linewidth >= max_linewidth) && (c[i] != '%')) {
1608
ascii85_linewidth = 0;
1611
ascii85_linewidth++;
1620
PrintPS::ascii85_out(guchar byte, SVGOStringStream &os)
1622
if (ascii85_len == 4)
1626
ascii85_buf |= byte;
1631
PrintPS::ascii85_nout(int n, guchar *uptr, SVGOStringStream &os)
1634
ascii85_out(*uptr, os);
1640
PrintPS::ascii85_done(SVGOStringStream &os)
1643
/* zero any unfilled buffer portion, then flush */
1644
ascii85_buf <<= (8 * (4-ascii85_len));
1652
PrintPS::print_image(FILE *ofp, guchar *px, unsigned int width, unsigned int height, unsigned int rs,
1653
Geom::Matrix const *transform)
1655
Inkscape::SVGOStringStream os;
1659
os << "[" << (*transform)[0] << " "
1660
<< (*transform)[1] << " "
1661
<< (*transform)[2] << " "
1662
<< (*transform)[3] << " "
1663
<< (*transform)[4] << " "
1664
<< (*transform)[5] << "] concat\n";
1666
/* Write read image procedure */
1668
os << " /ImageType 3\n";
1669
os << " /InterleaveType 1\n";
1671
os << " /MaskDict\n";
1673
os << " /ImageType 1\n";
1674
os << " /Width " << width << "\n";
1675
os << " /Height " << height << "\n";
1676
os << " /ImageMatrix "
1677
<< "[" << width << " "
1680
<< -((long) height) << " "
1683
os << " /BitsPerComponent 8\n";
1684
os << " /Decode [1 0]\n";
1687
os << " /DataDict\n";
1689
os << " /ImageType 1\n";
1690
os << " /Width " << width << "\n";
1691
os << " /Height " << height << "\n";
1692
os << " /ImageMatrix "
1693
<< "[" << width << " "
1696
<< -((long )height) << " "
1699
os << " /DataSource currentfile /ASCII85Decode filter\n";
1700
os << " /BitsPerComponent 8\n";
1701
os << " /Decode [0 1 0 1 0 1]\n";
1706
/* Allocate buffer for packbits data. Worst case: Less than 1% increase */
1707
guchar *const packb = (guchar *)g_malloc((4*width * 105)/100+2);
1708
guchar *const plane = (guchar *)g_malloc(4*width);
1714
for (unsigned i = 0; i < height; i++) {
1715
guchar const *const src = px + i * rs;
1717
guchar const *src_ptr = src;
1718
guchar *plane_ptr = plane;
1719
for (unsigned j = 0; j < width; j++) {
1720
*(plane_ptr++) = *(src_ptr+3);
1721
*(plane_ptr++) = *(src_ptr+0);
1722
*(plane_ptr++) = *(src_ptr+1);
1723
*(plane_ptr++) = *(src_ptr+2);
1727
ascii85_nout(4*width, plane, os);
1736
fprintf(ofp, "%s", os.str().c_str());
1742
PrintPS::textToPath(Inkscape::Extension::Print * ext)
1744
return ext->get_param_bool("textToPath");
1748
* \brief Get "fontEmbedded" param
1749
* \retval TRUE Fonts have to be embedded in the output so that the user might not need to install fonts to have the interpreter read the document correctly
1750
* \retval FALSE No font embedding
1752
* Only available for Adobe Type 1 fonts in EPS output till now
1755
PrintPS::fontEmbedded(Inkscape::Extension::Print * ext)
1757
return ext->get_param_bool("fontEmbedded");
1760
#include "clear-n_.h"
1766
(void) Inkscape::Extension::build_from_mem(
1767
"<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
1768
"<name>" N_("Postscript Print") "</name>\n"
1769
"<id>" SP_MODULE_KEY_PRINT_PS "</id>\n"
1770
"<param name=\"bitmap\" type=\"boolean\">false</param>\n"
1771
"<param name=\"resolution\" type=\"string\">72</param>\n"
1772
"<param name=\"destination\" type=\"string\">| lp</param>\n"
1773
"<param name=\"pageBoundingBox\" type=\"boolean\">true</param>\n"
1774
"<param name=\"textToPath\" type=\"boolean\">true</param>\n"
1775
"<param name=\"fontEmbedded\" type=\"boolean\">false</param>\n"
1777
"</inkscape-extension>", new PrintPS());
1781
} /* namespace Internal */
1782
} /* namespace Extension */
1783
} /* namespace Inkscape */
1785
/* End of GNU GPL code */
1791
c-file-style:"stroustrup"
1792
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1793
indent-tabs-mode:nil
1797
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :