26#include <glibmm/i18n.h>
35 static Glib::RefPtr<Gtk::PrintSettings> printer_settings;
36 return printer_settings;
46 _printop = Gtk::PrintOperation::create();
50 Glib::ustring title = _(
"Print");
55 _printop->set_unit(Gtk::Unit::POINTS);
56 Glib::RefPtr<Gtk::PageSetup> page_setup = Gtk::PageSetup::create();
60 _printop->set_default_page_setup(page_setup);
68 _printop->set_n_pages(pm.getPageCount());
69 _printop->set_current_page(pm.getSelectedPageIndex());
82 _printop->set_custom_tab_label(_(
"Rendering"));
90 const Glib::RefPtr<Gtk::PageSetup> &setup)
93 if (
auto page = pm.getPage(page_nr)) {
106 auto p_size = Gtk::PaperSize(
"custom",
"custom", page_width, page_height, Gtk::Unit::POINTS);
107 Gtk::PageOrientation orientation = Gtk::PageOrientation::PORTRAIT;
117 Gtk::PageOrientation search_orientation = Gtk::PageOrientation::PORTRAIT;
118 if (page_width > page_height) {
119 search_orientation = Gtk::PageOrientation::LANDSCAPE;
120 std::swap(page_width, page_height);
124 std::vector<Gtk::PaperSize> known_sizes = Gtk::PaperSize::get_paper_sizes(
false);
125 for (
auto&
size : known_sizes) {
126 if (fabs(
size.get_width(Gtk::Unit::POINTS) - page_width) >= 1.0) {
130 if (fabs(
size.get_height(Gtk::Unit::POINTS) - page_height) >= 1.0) {
136 orientation = search_orientation;
139 page_setup->set_paper_size(p_size);
140 page_setup->set_orientation(orientation);
157 prefs->
setBool(
"/dialogs/printing/asbitmap",
true);
159 prefs->
setDouble(
"/dialogs/printing/dpi", dpi);
163 rect =
page->getDesktopRect();
168 std::string tmp_base =
"inkscape-print-png-XXXXXX";
171 if ( (tmp_fd = Glib::file_open_tmp(tmp_png, tmp_base)) >= 0) {
177 if (
auto c = Colors::Color::parse(nv->
attribute(
"pagecolor"))) {
179 bgcolor =
c->toRGBA();
182 if (nv && nv->
attribute(
"inkscape:pageopacity")){
190 dpi, dpi, bgcolor,
nullptr,
nullptr,
true, {});
202 auto png = Cairo::ImageSurface::create_from_png(tmp_png);
203 auto pattern = Cairo::SurfacePattern::create(png);
204 auto cr = context->get_cairo_context();
205 auto m = cr->get_matrix();
209 cr->set_source(pattern);
215 unlink (tmp_png.c_str());
218 g_warning(
"%s", _(
"Could not open temporary PNG for bitmap printing"));
222 prefs->
setBool(
"/dialogs/printing/asbitmap",
false);
227 ctx.setFilterToBitmap(
true);
228 ctx.setBitmapResolution(72);
230 auto cr = context->get_cairo_context();
231 auto surface = cr->get_target();
232 auto ctm = cr->get_matrix();
234 bool ret = ctx.setSurfaceTarget(
surface->cobj(),
true, &ctm);
238 if (
auto page = pm.getPage(page_nr)) {
245 g_warning(
"%s", _(
"Could not set up Document"));
248 g_warning(
"%s", _(
"Failed to set CairoRenderContext"));
263Gtk::PrintOperation::Result
Print::run(Gtk::PrintOperation::Action, Gtk::Window &parent_window)
269 Gtk::PrintOperation::Result res =
_printop->run(Gtk::PrintOperation::Action::PRINT_DIALOG, parent_window);
272 if (res == Gtk::PrintOperation::Result::APPLY) {
277 }
catch (
const Glib::Error &e) {
281 return Gtk::PrintOperation::Result::ERROR;
Declaration of CairoRenderContext, a class used for rendering with Cairo.
Declaration of CairoRenderer, a class used for rendering via a CairoRenderContext.
Cairo::RefPtr< Cairo::ImageSurface > surface
void setTextToPath(bool texttopath)
bool renderPage(CairoRenderContext *ctx, SPDocument *doc, SPPage const *page, bool stretch_to_fit)
void renderItem(CairoRenderContext *ctx, SPItem const *item, SPItem const *origin=nullptr, SPPage const *page=nullptr)
Traverses the object tree and invokes the render methods.
CairoRenderContext createContext()
bool setupDocument(CairoRenderContext *ctx, SPDocument *doc, SPItem const *base=nullptr)
Initializes the CairoRenderContext according to the specified SPDocument.
SPPage * getPage(int index) const
Get the page at the given position or return nullptr if out of range.
Geom::OptRect getDesktopRect() const
Returns the total area of all the pages in desktop units.
Preference storage class.
static Preferences * get()
Access the singleton Preferences object.
void setDouble(Glib::ustring const &pref_path, double value)
Set a floating point value.
void setBool(Glib::ustring const &pref_path, bool value)
Set a Boolean value.
void draw_page(const Glib::RefPtr< Gtk::PrintContext > &context, int page_nr)
Gtk::PrintOperation::Result run(Gtk::PrintOperation::Action, Gtk::Window &parent_window)
struct workaround_gtkmm _workaround
Inkscape::UI::Widget::RenderingOptions _tab
void setup_page(const Glib::RefPtr< Gtk::PrintContext > &context, int page_nr, const Glib::RefPtr< Gtk::PageSetup > &setup)
Return the required page setup, only connected for multi-page documents and only required where there...
Print(SPDocument *doc, SPItem *base)
Gtk::Widget * create_custom_widget()
void set_paper_size(const Glib::RefPtr< Gtk::PageSetup > &, double width, double height)
Set the paper size with correct orientation.
void begin_print(const Glib::RefPtr< Gtk::PrintContext > &)
Glib::RefPtr< Gtk::PrintOperation > _printop
double value(Unit const *u) const
Return the quantity's value in the specified unit.
static double convert(double from_dist, Unit const *from, Unit const *to)
Convert distances.
Interface for refcounted XML nodes.
double getAttributeDouble(Util::const_char_ptr key, double default_value=0.0) const
virtual char const * attribute(char const *key) const =0
Get the string representation of a node's attribute.
Typed SVG document implementation.
Inkscape::Util::Quantity getWidth() const
char const * getDocumentName() const
basename or other human-readable label for the document.
Geom::OptRect preferredBounds() const
Inkscape::PageManager & getPageManager()
Inkscape::Util::Quantity getHeight() const
Inkscape::XML::Node * getReprNamedView()
Base class for visual SVG elements.
Inkscape::XML::Document * _doc
Reference to the clipboard's Inkscape::XML::Document.
constexpr uint32_t SP_COLOR_F_TO_U(double v)
Glib::RefPtr< Gtk::PrintSettings > & get_printer_settings()
Helper class to stream background task notifications as a series of messages.
ExportResult sp_export_png_file(SPDocument *doc, gchar const *filename, double x0, double y0, double x1, double y1, unsigned long int width, unsigned long int height, double xdpi, double ydpi, unsigned long bgcolor, unsigned int(*status)(float, void *), void *data, bool force_overwrite, const std::vector< SPItem const * > &items_only, bool interlace, int color_type, int bit_depth, int zlib, int antialiasing)
Export the given document as a Portable Network Graphics (PNG) file.
Singleton class to access the preferences file in a convenient way.
Inkscape::UI::Widget::RenderingOptions * _tab