Jelajahi Sumber

- finished working in adding DPI settings for PNG export in Film Tool although there are some limitations due of Reportlab

Marius Stanciu 5 tahun lalu
induk
melakukan
8cb4b5abf8
3 mengubah file dengan 47 tambahan dan 31 penghapusan
  1. 1 0
      CHANGELOG.md
  2. 42 27
      appTools/ToolFilm.py
  3. 4 4
      camlib.py

+ 1 - 0
CHANGELOG.md

@@ -10,6 +10,7 @@ CHANGELOG for FlatCAM beta
 5.10.2020
 
 - working on adding DPI setting for PNG export in the Film Tool
+- finished working in adding DPI settings for PNG export in Film Tool although there are some limitations due of Reportlab
 
 26.09.2020
 

+ 42 - 27
appTools/ToolFilm.py

@@ -436,23 +436,31 @@ class Film(AppTool):
             self.app.inform.emit('[WARNING_NOTCL] %s: %s' % (_("No object Box. Using instead"), obj))
             box = obj
 
-        new_png_dpi = self.ui.png_dpi_spinner.get_value()
-        dpi_rate = new_png_dpi / 96
-        if dpi_rate != 1:
-            scale_factor_x += dpi_rate
-            scale_factor_y += dpi_rate
+        scale_factor_x = scale_factor_x
+        scale_factor_y = scale_factor_y
 
-        def make_negative_film():
-            exported_svg = obj.export_svg(scale_stroke_factor=scale_stroke_factor,
-                                          scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y,
-                                          skew_factor_x=skew_factor_x, skew_factor_y=skew_factor_y,
-                                          mirror=mirror
-                                          )
+        def make_negative_film(scale_factor_x, scale_factor_y):
+            log.debug("FilmTool.export_negative().make_negative_film()")
 
+            scale_reference = 'center'
             # Determine bounding area for svg export
             bounds = box.bounds()
             size = box.size()
 
+            default_dpi = 96
+            new_png_dpi = self.ui.png_dpi_spinner.get_value()
+            dpi_rate = new_png_dpi / default_dpi
+            if dpi_rate != 1:
+                scale_factor_x += dpi_rate
+                scale_factor_y += dpi_rate
+                scale_reference = (bounds[0], bounds[1])
+
+            exported_svg = obj.export_svg(scale_stroke_factor=scale_stroke_factor,
+                                          scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y,
+                                          skew_factor_x=skew_factor_x, skew_factor_y=skew_factor_y,
+                                          mirror=mirror, scale_reference=scale_reference
+                                          )
+
             uom = obj.units.lower()
 
             # Convert everything to strings for use in the xml doc
@@ -521,7 +529,7 @@ class Film(AppTool):
                 try:
                     doc_final = StringIO(doc_final)
                     drawing = svg2rlg(doc_final)
-                    if new_png_dpi == 96:
+                    if new_png_dpi == default_dpi:
                         renderPM.drawToFile(drawing, filename, 'PNG')
                     else:
                         renderPM.drawToFile(drawing, filename, 'PNG', dpi=new_png_dpi)
@@ -565,7 +573,7 @@ class Film(AppTool):
 
             def job_thread_film(app_obj):
                 try:
-                    make_negative_film()
+                    make_negative_film(scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y)
                 except Exception:
                     proc.done()
                     return
@@ -626,24 +634,34 @@ class Film(AppTool):
             self.inform.emit('[WARNING_NOTCL] %s: %s' % (_("No object Box. Using instead"), obj))
             box = obj
 
+        scale_factor_x = scale_factor_x
+        scale_factor_y = scale_factor_y
+
         p_size = pagesize_val
         orientation = orientation_val
         color = color_val
         transparency_level = opacity_val
 
-        new_png_dpi = self.ui.png_dpi_spinner.get_value()
-        dpi_rate = new_png_dpi / 96
-        if dpi_rate != 1:
-            scale_factor_x += dpi_rate
-            scale_factor_y += dpi_rate
-
-        def make_positive_film(p_size, orientation, color, transparency_level):
+        def make_positive_film(p_size, orientation, color, transparency_level, scale_factor_x, scale_factor_y):
             log.debug("FilmTool.export_positive().make_positive_film()")
 
+            scale_reference = 'center'
+            # Determine bounding area for svg export
+            bounds = box.bounds()
+            size = box.size()
+
+            default_dpi = 96
+            new_png_dpi = self.ui.png_dpi_spinner.get_value()
+            dpi_rate = new_png_dpi / default_dpi
+            if dpi_rate != 1:
+                scale_factor_x += dpi_rate
+                scale_factor_y += dpi_rate
+                scale_reference = (bounds[0], bounds[1])
+
             exported_svg = obj.export_svg(scale_stroke_factor=scale_stroke_factor,
                                           scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y,
                                           skew_factor_x=skew_factor_x, skew_factor_y=skew_factor_y,
-                                          mirror=mirror
+                                          mirror=mirror, scale_reference=scale_reference
                                           )
 
             # Change the attributes of the exported SVG
@@ -658,10 +676,6 @@ class Film(AppTool):
 
             exported_svg = ET.tostring(root)
 
-            # Determine bounding area for svg export
-            bounds = box.bounds()
-            size = box.size()
-
             # This contain the measure units
             uom = obj.units.lower()
 
@@ -709,7 +723,7 @@ class Film(AppTool):
                 try:
                     doc_final = StringIO(doc_final)
                     drawing = svg2rlg(doc_final)
-                    if new_png_dpi == 96:
+                    if new_png_dpi == default_dpi:
                         renderPM.drawToFile(drawing, filename, 'PNG')
                     else:
                         renderPM.drawToFile(drawing, filename, 'PNG', dpi=new_png_dpi)
@@ -753,7 +767,8 @@ class Film(AppTool):
             def job_thread_film():
                 try:
                     make_positive_film(p_size=p_size, orientation=orientation, color=color,
-                                       transparency_level=transparency_level)
+                                       transparency_level=transparency_level,
+                                       scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y)
                 except Exception:
                     proc.done()
                     return

+ 4 - 4
camlib.py

@@ -2221,7 +2221,7 @@ class Geometry(object):
     def export_svg(self, scale_stroke_factor=0.00,
                    scale_factor_x=None, scale_factor_y=None,
                    skew_factor_x=None, skew_factor_y=None,
-                   skew_reference='center',
+                   skew_reference='center', scale_reference='center',
                    mirror=None):
         """
         Exports the Geometry Object as a SVG Element
@@ -2256,11 +2256,11 @@ class Geometry(object):
         geom = geom_svg
 
         if scale_factor_x and not scale_factor_y:
-            geom = affinity.scale(geom_svg, scale_factor_x, 1.0)
+            geom = affinity.scale(geom_svg, scale_factor_x, 1.0, origin=scale_reference)
         elif not scale_factor_x and scale_factor_y:
-            geom = affinity.scale(geom_svg, 1.0, scale_factor_y)
+            geom = affinity.scale(geom_svg, 1.0, scale_factor_y, origin=scale_reference)
         elif scale_factor_x and scale_factor_y:
-            geom = affinity.scale(geom_svg, scale_factor_x, scale_factor_y)
+            geom = affinity.scale(geom_svg, scale_factor_x, scale_factor_y, origin=scale_reference)
 
         if skew_factor_x and not skew_factor_y:
             geom = affinity.skew(geom_svg, skew_factor_x, 0.0, origin=skew_ref)