Ascidia icon indicating copy to clipboard operation
Ascidia copied to clipboard

Win32 line endings

Open Grueslayer opened this issue 9 years ago • 0 comments

Under Windows the created PNG files are corrupted because the generated content is written to a stream opened with "w". Files opened this way are treated as TEXT streams. For Win32 systems this leads to a newline conversion of CRLF. PNG is a binary format, so the output stream must be opened with "wb" (on all systems).

I did not find any os independent solution for changing the existing STDOUT, so it is only done for WIn32 systems.

diff -urN Ascidia-master/ascidia Ascidia/ascidia
--- Ascidia-master/ascidia  2015-05-19 21:04:57.000000000 +0200
+++ Ascidia/ascidia 2015-11-20 11:36:29.263006600 +0100
@@ -9,12 +9,14 @@
    filename = None
    stream = None
    last_report_len = 0
+   binary = False

-   def __init__(self,filename):
+   def __init__(self,filename,binary):
        self.filename = filename
+       self.binary = binary

    def __enter__(self):
-       self.stream = open(self.filename,"w")
+       self.stream = open(self.filename, "wb" if self.binary else "w")
        return self.stream

    def __exit__(self,extype,exvalue,traceback):
@@ -28,8 +30,15 @@


 class StdOutContext(object):
+   binary = False

+   def __init__(self,binary):
+       self.binary = binary
+       
    def __enter__(self):
+       if self.binary and sys.platform == "win32":
+           import os, msvcrt
+           msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
        return sys.stdout

    def __exit__(self,extype,exvalue,traceback): 
@@ -109,17 +118,17 @@
        format = fmtdefault

    if args.outfile == "-":
-       outctx = StdOutContext()
+       outctx = StdOutContext(format.BINARYOUTPUT)
    elif args.outfile is not None:
-       outctx = FileOutContext(args.outfile)
+       outctx = FileOutContext(args.outfile,format.BINARYOUTPUT)
    elif args.infile == "-":
-       outctx = StdOutContext()
+       outctx = StdOutContext(format.BINARYOUTPUT)
    else:
        name = args.infile
        extpos = name.rfind(".")
        if extpos != -1: name = name[:extpos]
        name += "." + format.EXTS[0]
-       outctx = FileOutContext(name)
+       outctx = FileOutContext(name,format.BINARYOUTPUT)

    prefs = OutputPrefs(args.foreground,args.background,args.charheight)

diff -urN Ascidia-master/main.py Ascidia/main.py
--- Ascidia-master/main.py  2015-05-19 21:04:57.000000000 +0200
+++ Ascidia/main.py 2015-11-20 11:09:50.350856600 +0100
@@ -114,6 +114,8 @@
     DASH_PATTERN = 8,8
     TEXT_BASELINE = 0.75

+    BINARYOUTPUT = True
+
     diagram = None
     stream = None
     prefs = None
@@ -261,6 +263,8 @@
     FONT_SIZE = 0.667 # of character height
     DASH_PATTERN = 8,8

+    BINARYOUTPUT = False
+
     diagram = None
     stream = None
     prefs = None

Grueslayer avatar Nov 20 '15 11:11 Grueslayer