[mat-dev] Solution for issue #133

Nachricht löschen

Nachricht beantworten
Autor: Antonio Eugenio Burriel
Datum:  
To: mat-dev
Betreff: [mat-dev] Solution for issue #133
Hello,

I wanted to comment on ticket #133 - Cleaned PDFs wont print correctly
(on some printers at least), but for some reason 0xacab doesn't like
my email provider, so I'm unable to register.

pyCairo by default renders the PDF surfaces with a resolution of 72
dpi which is so low that the bitmap gets blurred compared to original.

Since pyCairo 1.12.0, a new method set_device_scale(x_scale, y_scale)
is added, which allows changing the canvas resolution.

I've attached a patch with addresses this problem if the proper
version of pyCairo is available.

Best regards,
--- a/libmat2/pdf.py    2021-03-19 17:54:21.000000000 +0100
+++ b/libmat2/pdf.py    2021-07-23 20:29:48.472494919 +0200
@@ -32,7 +32,7 @@
     def __init__(self, filename):
         super().__init__(filename)
         self.uri = 'file://' + os.path.abspath(self.filename)
-        self.__scale = 2  # how much precision do we want for the render
+        self.__scale = 200 / 72.0  # how much precision do we want for the render
         try:  # Check now that the file is valid, to avoid surprises later
             Poppler.Document.new_from_file(self.uri, None)
         except GLib.GError:  # Invalid PDF
@@ -90,8 +90,8 @@
             page_width, page_height = page.get_size()
             logging.info("Rendering page %d/%d", pagenum + 1, pages_count)


-            width = int(page_width) * self.__scale
-            height = int(page_height) * self.__scale
+            width = int(page_width * self.__scale)
+            height = int(page_height * self.__scale)
             img_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
             img_context = cairo.Context(img_surface)


@@ -105,7 +105,11 @@
             buf.seek(0)


             img = cairo.ImageSurface.create_from_png(buf)
-            pdf_surface.set_size(page_width*self.__scale, page_height*self.__scale)
+            if cairo.version_info < (1, 12, 0):
+                pdf_surface.set_size(width, height)
+            else:
+                pdf_surface.set_size(page_width, page_height)
+                pdf_surface.set_device_scale(1 / self.__scale, 1 / self.__scale)
             pdf_context.set_source_surface(img, 0, 0)
             pdf_context.paint()
             pdf_context.show_page()  # draw pdf_context on pdf_surface