IDE: Fix printing
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Mon, 19 Aug 2024 23:58:01 +0200
changeset 3966 8cc6f56c3710
parent 3965 6c3469588c12
child 3967 8d82aa6e9dd9
IDE: Fix printing

More side effects of WxPy4 et Py3 + workaround WxWidget bug.
IDEFrame.py
graphics/LD_Objects.py
--- a/IDEFrame.py	Sun Aug 18 23:36:56 2024 +0200
+++ b/IDEFrame.py	Mon Aug 19 23:58:01 2024 +0200
@@ -1116,7 +1116,7 @@
             printout2 = GraphicPrintout(window, page_size, margins, True)
             preview = wx.PrintPreview(printout, printout2, data)
 
-            if preview.Ok():
+            if preview.IsOk():
                 preview_frame = wx.PreviewFrame(preview, self, _("Print preview"), style=wx.DEFAULT_FRAME_STYLE | wx.FRAME_FLOAT_ON_PARENT)
 
                 preview_frame.Initialize()
@@ -2599,7 +2599,7 @@
         self.PageSize = page_size
         if self.PageSize[0] == 0 or self.PageSize[1] == 0:
             self.PageSize = (1050, 1485)
-        self.Preview = preview
+        self.IsPreview = lambda *_x : preview
         self.Margins = margins
         self.FontSize = 5
         self.TextMargin = 3
@@ -2620,9 +2620,9 @@
 
     def OnBeginDocument(self, startPage, endPage):
         dc = self.GetDC()
-        if not self.Preview and isinstance(dc, wx.PostScriptDC):
+        if not self.IsPreview() and isinstance(dc, wx.PostScriptDC):
             dc.SetResolution(720)
-        super(GraphicPrintout, self).OnBeginDocument(startPage, endPage)
+        return super(GraphicPrintout, self).OnBeginDocument(startPage, endPage)
 
     def OnPrintPage(self, page):
         dc = self.GetDC()
@@ -2630,21 +2630,21 @@
         dc.Clear()
         dc.SetUserScale(1.0, 1.0)
         dc.SetDeviceOrigin(0, 0)
-        dc.printing = not self.Preview
+        dc.printing = not self.IsPreview()
 
         # Get the size of the DC in pixels
         ppiPrinterX, ppiPrinterY = self.GetPPIPrinter()
         pw, ph = self.GetPageSizePixels()
-        dw, dh = dc.GetSizeTuple()
+        dw, dh = dc.GetSize().Get()
         Xscale = (dw * ppiPrinterX) / (pw * 25.4)
         Yscale = (dh * ppiPrinterY) / (ph * 25.4)
 
-        fontsize = self.FontSize * Yscale
-
-        margin_left = self.Margins[0].x * Xscale
-        margin_top = self.Margins[0].y * Yscale
-        area_width = dw - self.Margins[1].x * Xscale - margin_left
-        area_height = dh - self.Margins[1].y * Yscale - margin_top
+        fontsize = round(self.FontSize * Yscale)
+
+        margin_left = round(self.Margins[0].x * Xscale)
+        margin_top = round(self.Margins[0].y * Yscale)
+        area_width = dw - round(self.Margins[1].x * Xscale) - margin_left
+        area_height = dh - round(self.Margins[1].y * Yscale) - margin_top
 
         dc.SetPen(MiterPen(wx.BLACK))
         dc.SetBrush(wx.TRANSPARENT_BRUSH)
@@ -2667,7 +2667,7 @@
 
         # Set the scale and origin
         dc.SetDeviceOrigin(-posX + margin_left, -posY + margin_top)
-        dc.SetClippingRegion(posX, posY, self.PageSize[0] * scale, self.PageSize[1] * scale)
+        dc.SetClippingRegion(posX, posY, round(self.PageSize[0] * scale), round(self.PageSize[1] * scale))
         dc.SetUserScale(scale, scale)
 
         self.Viewer.DoDrawing(dc, True)
--- a/graphics/LD_Objects.py	Sun Aug 18 23:36:56 2024 +0200
+++ b/graphics/LD_Objects.py	Mon Aug 19 23:58:01 2024 +0200
@@ -974,31 +974,24 @@
         elif self.Type == COIL_FALLING:
             typetext = "N"
 
-        if getattr(dc, "printing", False) and not isinstance(dc, wx.PostScriptDC):
-            # Draw an clipped ellipse for representing the coil
-            clipping_box = dc.GetClippingBox()
-            dc.SetClippingRegion(self.Pos.x - 1, self.Pos.y, self.Size[0] + 2, self.Size[1] + 1)
-            dc.DrawEllipse(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1)
-            dc.DestroyClippingRegion()
-            if clipping_box != (0, 0, 0, 0):
-                dc.SetClippingRegion(*clipping_box)
-            name_size = dc.GetTextExtent(self.Name)
-            if typetext != "":
-                type_size = dc.GetTextExtent(typetext)
-        else:
-            # Draw a two ellipse arcs for representing the coil
-            dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, 135, 225)
-            dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, -45, 45)
-            # Draw a point to avoid hole in left arc
-            if not getattr(dc, "printing", False):
-                if self.Value is not None and self.Value:
-                    dc.SetPen(MiterPen(wx.GREEN))
-                else:
-                    dc.SetPen(MiterPen(wx.BLACK))
-                dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] // 2 + 1)
-            name_size = self.NameSize
-            if typetext != "":
-                type_size = self.TypeSize
+        printing = getattr(dc, "printing", False)
+        # Draw a two ellipse arcs for representing the coil
+        pos = (self.Pos.x,  
+               self.Pos.y - round(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, 
+               self.Size[0], round(self.Size[1] * sqrt(2)) - 1)
+        
+        if printing:
+            # workaround for printing bug with DrawEllipticArc
+            # add an offset to the y position proportional to the height of the ellipse
+            # sqrt(2) ratio obtained heuristically
+            pos = (pos[0], pos[1] + round(sqrt(2)*pos[3]), pos[2], pos[3])
+            
+        dc.DrawEllipticArc(*pos, 135, 225)
+        dc.DrawEllipticArc(*pos, -45, 45)
+        
+        name_size = self.NameSize
+        if typetext != "":
+            type_size = self.TypeSize
 
         # Draw coil name
         name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) // 2,