From: Hartmut Goebel <h.goebel@crazy-compilers.com>
Date: Sat, 14 Oct 2023 18:46:01 +0200
Subject: Tests: Make tests independent of fonts.

Origin: https://gitlab.com/pdftools/python-ghostscript/-/commit/f2eaf4c9ae0907042b9a47e1e093f0414f50bdc2
Forwarded: not-needed

Depending on the platform ghostscript might use different fonts or
different versions of the font. Thus switch to test images being
independent of fonts, using just some geometric forms.

Thanks to Elena ``of Valhalla'' for reporting.

/closes #27
---
 MANIFEST.in            |   2 +-
 test/hello_world.bmp   | Bin 598 -> 0 bytes
 test/test_highlevel.py |  28 +++++++++++-----------------
 test/test_lowlevel.py  |  30 +++++++++++++-----------------
 test/testimage.bmp     | Bin 0 -> 918 bytes
 test/testimage.ps      |  21 +++++++++++++++++++++
 6 files changed, 46 insertions(+), 35 deletions(-)
 delete mode 100644 test/hello_world.bmp
 create mode 100644 test/testimage.bmp
 create mode 100644 test/testimage.ps

diff --git a/MANIFEST.in b/MANIFEST.in
index aea078c..3a1d60a 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1,5 +1,5 @@
 include COPYING
 include README.txt
 exclude .gitgnore
-recursive-include test *.xml *.py *.bmp
+recursive-include test *.xml *.py *.bmp *.ps
 #recursive-include doc *.html *.1
diff --git a/test/hello_world.bmp b/test/hello_world.bmp
deleted file mode 100644
index cb933aa..0000000
--- a/test/hello_world.bmp
+++ /dev/null
@@ -1 +0,0 @@
-BMV      v   (   P            à                  ÿ    ÿ  ÿÿ    ÿ ÿ ÿ  ÿÿ ÿÿÿ     ÿ    ÿ  ÿÿ    ÿ ÿ ÿ  ÿÿ ÿÿÿ ðÿÿðÿ  ÿðÿ  ÿÿÿðÿðÿð ÿÿ  ÿÿÿÿÿÿÿÿÿðÿÿðÿÿðÿÿÿÿðÿ ÿðÿðÿÿÿÿÿÿÿÿÿÿÿÿðÿÿðÿÿÿððÿÿÿðÿ  ÿðÿÿ ÿÿÿÿÿÿÿÿÿÿÿðÿÿðÿ  ððÿÿÿÿððÿÿðÿÿ ÿÿÿÿÿÿÿÿÿÿÿð   ÿÿðÿÿÿÿððÿðÿðÿÿÿÿÿÿÿÿÿÿÿÿðÿÿðÿ  ÿðÿ  ÿÿÿððÿð ÿ   ÿÿÿÿÿÿÿÿÿðÿÿðÿÿÿÿðÿÿÿÿÿÿððÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðÿÿðÿÿÿÿðÿÿÿÿÿÿÿ ÿðÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðÿÿðÿÿÿÿðÿÿÿÿÿðÿÿðÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
\ No newline at end of file
diff --git a/test/test_highlevel.py b/test/test_highlevel.py
index 62f8292..8d90069 100644
--- a/test/test_highlevel.py
+++ b/test/test_highlevel.py
@@ -22,31 +22,25 @@ __copyright__ = "Copyright 2010-2021 by Hartmut Goebel <h.goebel@crazy-compilers
 __licence__ = "GNU General Public License version 3 (GPL v3)"
 
 import io
-import sys, os
-import locale # required to encode file paths
 import binascii
 import warnings
+import pathlib
 
 import py, pytest
 
 import ghostscript as gslib
 
 
-postscript_img = b"""
-    /Helvetica findfont 12 scalefont setfont
-    0 0 moveto
-    (Hello World) show
-    showpage
-    """
-
 HELLO_WORLD = ''.join(('%x' % ord(c) for c in 'Hello World'))
 postscript_doc = ('<%s> = flush' % HELLO_WORLD).encode('ascii')
 
 STDARGS = ['test.py', '-dNOPAUSE', '-dBATCH', '-dSAFER', '-q',
-           '-sDEVICE=bmp16', '-g80x12']
+           '-sDEVICE=bmp16', '-g80x20']
 
-TEST_PIC_FILENAME = os.path.join(os.path.dirname(__file__), 'hello_world.bmp')
-TEST_PIC_DATA = py.path.local(TEST_PIC_FILENAME).read('rb')
+POSTSCRIPT_FILE = pathlib.Path(__file__).with_name('testimage.ps')
+POSTSCRIPT_DATA = POSTSCRIPT_FILE.read_bytes()
+TEST_PIC = POSTSCRIPT_FILE.with_suffix('.bmp')
+TEST_PIC_DATA = TEST_PIC.read_bytes()
 
 
 # Ensure the instanse is removed after all high-level tests have been
@@ -80,7 +74,7 @@ def test_revision_instance(instance):
 def test_simple(instance, tmpdir):
     """Let ghostscript read from a file and write to a file"""
     infile = tmpdir.join('in.ps')
-    infile.write(postscript_img)
+    infile.write(POSTSCRIPT_DATA)
     outfile = tmpdir.join('out.bmp')
 
     # Using a context with an empty body looks not like good code, So
@@ -95,7 +89,7 @@ def test_simple(instance, tmpdir):
 def test_unicode_arguments(instance, tmpdir):
     """Let ghostscript read from a file and write to a file"""
     infile = tmpdir.join('in-Ã¤Ã¶Ã¼.ps')
-    infile.write(postscript_img)
+    infile.write(POSTSCRIPT_DATA)
     outfile = tmpdir.join('outÃ¤Ã¶Ã¼.bmp')
 
     gs = gslib.Ghostscript(*STDARGS, '-sOutputFile=%s' % outfile, str(infile))
@@ -108,7 +102,7 @@ def test_unicode_arguments(instance, tmpdir):
 def test_run_string_empty(instance, tmpdir):
     """Let ghostscript read from a file and write to a file"""
     infile = tmpdir.join('in.ps')
-    infile.write(postscript_img)
+    infile.write(POSTSCRIPT_DATA)
     outfile = tmpdir.join('out.bmp')
 
     with gslib.Ghostscript(*STDARGS, '-sOutputFile=%s' % outfile) as gs:
@@ -128,7 +122,7 @@ def test_run_string(instance, tmpdir):
     outfile = tmpdir.join('out.bmp')
 
     with gslib.Ghostscript(*STDARGS, '-sOutputFile=%s' % outfile) as gs:
-        gs.run_string(postscript_img)
+        gs.run_string(POSTSCRIPT_DATA)
 
     data = outfile.read('rb')
     assert data == TEST_PIC_DATA
@@ -138,7 +132,7 @@ def test_stdin(tmpdir):
     """Let ghostscript read from stdin and write to a file"""
     outfile = tmpdir.join('out.bmp')
     gs = gslib.Ghostscript(*STDARGS, '-sOutputFile=%s' % outfile, '-',
-                           stdin=io.BytesIO(postscript_img))
+                           stdin=io.BytesIO(POSTSCRIPT_DATA))
     gs.exit()
 
     data = outfile.read('rb')
diff --git a/test/test_lowlevel.py b/test/test_lowlevel.py
index 5711cdb..7e9e6a4 100644
--- a/test/test_lowlevel.py
+++ b/test/test_lowlevel.py
@@ -22,30 +22,26 @@ __copyright__ = "Copyright 2010-2021 by Hartmut Goebel <h.goebel@crazy-compilers
 __licence__ = "GNU General Public License version 3 (GPL v3)"
 
 import io
-import sys, os
-import locale # required to encode file paths
+import locale # required to encode arguments
 import binascii
+import pathlib
 
 import py
 
 import ghostscript._gsprint as gs
 
-postscript_img = b"""
-    /Helvetica findfont 12 scalefont setfont
-    0 0 moveto
-    (Hello World) show
-    showpage
-    """
 
 HELLO_WORLD = ''.join(('%x' % ord(c) for c in 'Hello World'))
 #HELLO_WORLD = binascii.hexlify('Hello World')
 postscript_doc = ('<%s> = flush' % HELLO_WORLD).encode('ascii')
 
 STDARGS = [b'test.py', b'-dNOPAUSE', b'-dBATCH', b'-dSAFER', b'-q',
-           b'-sDEVICE=bmp16', b'-g80x12']
+           b'-sDEVICE=bmp16', b'-g80x20']
 
-TEST_PIC_FILENAME = os.path.join(os.path.dirname(__file__), 'hello_world.bmp')
-TEST_PIC_DATA = py.path.local(TEST_PIC_FILENAME).read('rb')
+POSTSCRIPT_FILE = pathlib.Path(__file__).with_name('testimage.ps')
+POSTSCRIPT_DATA = POSTSCRIPT_FILE.read_bytes()
+TEST_PIC = POSTSCRIPT_FILE.with_suffix('.bmp')
+TEST_PIC_DATA = TEST_PIC.read_bytes()
 
 
 def _encode(*args):
@@ -71,7 +67,7 @@ def test_run_string(tmpdir):
 
     try:
         gs.init_with_args(instance, args)
-        gs.run_string(instance, postscript_img)
+        gs.run_string(instance, POSTSCRIPT_DATA)
     finally:
         gs.exit(instance)
         gs.delete_instance(instance)
@@ -83,7 +79,7 @@ def test_run_string(tmpdir):
 def test_simple(tmpdir):
     """Let ghostscript read from a file and write to a file"""
     infile = tmpdir.join('in.ps')
-    infile.write(postscript_img)
+    infile.write(POSTSCRIPT_DATA)
     outfile = tmpdir.join('out.bmp')
 
     args = STDARGS + _encode('-sOutputFile=%s' % outfile, str(infile))
@@ -122,7 +118,7 @@ def test_stdin(tmpdir):
 
     args = STDARGS + _encode('-sOutputFile=%s' % outfile, '-')
 
-    _gs_stdio(args, stdin=io.BytesIO(postscript_img))
+    _gs_stdio(args, stdin=io.BytesIO(POSTSCRIPT_DATA))
 
     data = outfile.read('rb')
     assert data == TEST_PIC_DATA
@@ -206,10 +202,10 @@ def generate_test_picture():
     Use command line ghostscript to generate the image used in testing
     """
     import subprocess
-    outfile = TEST_PIC_FILENAME
+    args = ['gs'] + STDARGS[1:] + _encode('-sOutputFile=%s' % TEST_PIC,
+                                          str(POSTSCRIPT_FILE))
+    subprocess.Popen(args).wait()
 
-    args = ['gs'] + STDARGS[1:] + _encode('-sOutputFile=%s' % outfile, '-')
-    subprocess.Popen(args).communicate(postscript_doc)    
 
 if __name__ ==  '__main__':
     generate_test_picture()
diff --git a/test/testimage.bmp b/test/testimage.bmp
new file mode 100644
index 0000000..a683d41
--- /dev/null
+++ b/test/testimage.bmp
@@ -0,0 +1 @@
+BM–      v   (   P                               ÿ    ÿ  ÿÿ    ÿ ÿ ÿ  ÿÿ ÿÿÿ     ÿ    ÿ  ÿÿ    ÿ ÿ ÿ  ÿÿ ÿÿÿ ðÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðððÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðððÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðððððÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðððððÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðððððððÿÿÿÿÿÿÿÿÿÿÿÿÿðððððððÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðððððððððÿÿÿÿÿÿÿÿÿÿÿðððððððððÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðððððððððððÿÿÿÿÿÿÿÿÿðððððððððððÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðððððððððððððÿÿÿÿÿÿÿðððððððððððððÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðððððððððððððððÿÿÿÿÿðððððððððððððððÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðððððððððððððððððÿÿÿðððððððððððððððððÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðððððððððððððððððððÿðððððððððððððððððððÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
\ No newline at end of file
diff --git a/test/testimage.ps b/test/testimage.ps
new file mode 100644
index 0000000..90947af
--- /dev/null
+++ b/test/testimage.ps
@@ -0,0 +1,21 @@
+%!PS-Adobe-3.0
+%%BoundingBox: 0 0 80 50
+newpath
+0 0 moveto
+0 20 rlineto
+40 0 rlineto
+closepath
+gsave
+0.5 setgray
+fill
+grestore
+newpath
+40 0 moveto
+0 20 rlineto
+40 0 rlineto
+closepath
+gsave
+0.7 setgray
+fill
+grestore
+showpage
