void sp_print_document_to_file(SPDocument *doc, gchar const *filename) { doc->ensureUpToDate(); Inkscape::Extension::Print *mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_PS); SPPrintContext context; gchar const *oldconst = mod->get_param_string("destination"); gchar *oldoutput = g_strdup(oldconst); mod->set_param_string("destination", (gchar *)filename); /* Start */ context.module = mod; /* fixme: This has to go into module constructor somehow */ /* Create new drawing */ mod->base = doc->getRoot(); Inkscape::Drawing drawing; mod->dkey = SPItem::display_key_new(1); mod->root = (mod->base)->invoke_show(drawing, mod->dkey, SP_ITEM_SHOW_DISPLAY); drawing.setRoot(mod->root); /* Print document */ mod->begin(doc); (mod->base)->invoke_print(&context); mod->finish(); /* Release drawing items */ (mod->base)->invoke_hide(mod->dkey); mod->base = NULL; mod->root = NULL; // should be deleted by invoke_hide /* end */ mod->set_param_string("destination", oldoutput); g_free(oldoutput); }
ExportResult sp_export_png_file(SPDocument *doc, gchar const *filename, Geom::Rect const &area, unsigned long width, unsigned long height, double xdpi, double ydpi, unsigned long bgcolor, unsigned (*status)(float, void *), void *data, bool force_overwrite, GSList *items_only) { g_return_val_if_fail(doc != NULL, EXPORT_ERROR); g_return_val_if_fail(filename != NULL, EXPORT_ERROR); g_return_val_if_fail(width >= 1, EXPORT_ERROR); g_return_val_if_fail(height >= 1, EXPORT_ERROR); g_return_val_if_fail(!area.hasZeroArea(), EXPORT_ERROR); if (!force_overwrite && !sp_ui_overwrite_file(filename)) { // aborted overwrite return EXPORT_ABORTED; } doc->ensureUpToDate(); /* Calculate translation by transforming to document coordinates (flipping Y)*/ Geom::Point translation = Geom::Point(-area[Geom::X][0], area[Geom::Y][1] - doc->getHeight().value("px")); /* This calculation is only valid when assumed that (x0,y0)= area.corner(0) and (x1,y1) = area.corner(2) * 1) a[0] * x0 + a[2] * y1 + a[4] = 0.0 * 2) a[1] * x0 + a[3] * y1 + a[5] = 0.0 * 3) a[0] * x1 + a[2] * y1 + a[4] = width * 4) a[1] * x0 + a[3] * y0 + a[5] = height * 5) a[1] = 0.0; * 6) a[2] = 0.0; * * (1,3) a[0] * x1 - a[0] * x0 = width * a[0] = width / (x1 - x0) * (2,4) a[3] * y0 - a[3] * y1 = height * a[3] = height / (y0 - y1) * (1) a[4] = -a[0] * x0 * (2) a[5] = -a[3] * y1 */ Geom::Affine const affine(Geom::Translate(translation) * Geom::Scale(width / area.width(), height / area.height())); //SP_PRINT_MATRIX("SVG2PNG", &affine); struct SPEBP ebp; ebp.width = width; ebp.height = height; ebp.background = bgcolor; /* Create new drawing */ Inkscape::Drawing drawing; drawing.setExact(true); // export with maximum blur rendering quality unsigned const dkey = SPItem::display_key_new(1); // Create ArenaItems and set transform drawing.setRoot(doc->getRoot()->invoke_show(drawing, dkey, SP_ITEM_SHOW_DISPLAY)); drawing.root()->setTransform(affine); ebp.drawing = &drawing; // We show all and then hide all items we don't want, instead of showing only requested items, // because that would not work if the shown item references something in defs if (items_only) { hide_other_items_recursively(doc->getRoot(), items_only, dkey); } ebp.status = status; ebp.data = data; bool write_status = false;; ebp.sheight = 64; ebp.px = g_try_new(guchar, 4 * ebp.sheight * width); if (ebp.px) { write_status = sp_png_write_rgba_striped(doc, filename, width, height, xdpi, ydpi, sp_export_get_rows, &ebp); g_free(ebp.px); } // Hide items, this releases arenaitem doc->getRoot()->invoke_hide(dkey); return write_status ? EXPORT_OK : EXPORT_ERROR; }