From 851b52b032e9cccbd992766394cf870863570995 Mon Sep 17 00:00:00 2001
From: "Egli Adrian (IT-SCI-API-PFI)" <adrian.egli@sbb.ch>
Date: Wed, 29 May 2019 13:56:12 +0200
Subject: [PATCH] Windows support cairo solved: workaround: auto install (see
 setup.py)

---
 flatland/flatland.py             |  1 +
 flatland/utils/graphics_layer.py | 11 +++++++
 flatland/utils/graphics_pil.py   |  7 +++++
 requirements_dev.txt             |  6 ++--
 setup.py                         | 54 ++++++++++++++++++++++++++++++++
 5 files changed, 76 insertions(+), 3 deletions(-)

diff --git a/flatland/flatland.py b/flatland/flatland.py
index 7fbbae4f..6d61ba81 100644
--- a/flatland/flatland.py
+++ b/flatland/flatland.py
@@ -1,3 +1,4 @@
 # -*- coding: utf-8 -*-
 
 """Main module."""
+
diff --git a/flatland/utils/graphics_layer.py b/flatland/utils/graphics_layer.py
index 7fd49af8..4dd5179e 100644
--- a/flatland/utils/graphics_layer.py
+++ b/flatland/utils/graphics_layer.py
@@ -2,6 +2,17 @@
 import matplotlib.pyplot as plt
 from numpy import array
 
+import os
+if os.name=='nt':
+    import site
+    import ctypes.util
+    default_os_path = os.environ['PATH']
+    os.environ['PATH'] = ''
+    for s in site.getsitepackages():
+        os.environ['PATH'] = os.environ['PATH'] + ';' + s + '\\cairo'
+    os.environ['PATH'] = os.environ['PATH'] + ';' + default_os_path
+    if ctypes.util.find_library('cairo') is not None:
+        print("cairo installed: OK")
 
 class GraphicsLayer(object):
     def __init__(self):
diff --git a/flatland/utils/graphics_pil.py b/flatland/utils/graphics_pil.py
index b64d2e25..96951c83 100644
--- a/flatland/utils/graphics_pil.py
+++ b/flatland/utils/graphics_pil.py
@@ -6,7 +6,12 @@ import numpy as np
 # from flatland.utils.svg import Track, Zug
 import time
 import io
+
+
 from cairosvg import svg2png
+
+from IPython.display import SVG
+
 from flatland.core.transitions import RailEnvTransitions
 # from copy import copy
 
@@ -234,6 +239,8 @@ class PILSVG(PILGL):
     def pilFromSvgFile(self, sfPath):
         with open(sfPath, "r") as fIn:
             bytesPNG = svg2png(file_obj=fIn, output_height=self.nPixCell, output_width=self.nPixCell)
+
+            image = SVG(url=sfPath)
         
         with io.BytesIO(bytesPNG) as fIn:
             pil_img = Image.open(fIn)
diff --git a/requirements_dev.txt b/requirements_dev.txt
index db80383f..a3558c4f 100644
--- a/requirements_dev.txt
+++ b/requirements_dev.txt
@@ -20,10 +20,10 @@ xarray==0.11.3
 matplotlib==3.0.2
 PyQt5==5.12
 Pillow==5.4.1
-# CairoSVG==2.3.1
-# pycairo==1.18.1
+# see setup.py pycairo==1.18.1
+CairoSVG==2.3.1
 
 msgpack==0.6.1
 svgutils==0.3.1
 
-screeninfo==0.3.1
\ No newline at end of file
+screeninfo==0.3.1
diff --git a/setup.py b/setup.py
index d1407035..e8fe5f5e 100644
--- a/setup.py
+++ b/setup.py
@@ -4,6 +4,12 @@
 """The setup script."""
 import os
 from setuptools import setup, find_packages
+import sys
+import os
+
+import platform
+
+
 
 with open('README.rst') as readme_file:
     readme = readme_file.read()
@@ -11,6 +17,54 @@ with open('README.rst') as readme_file:
 with open('HISTORY.rst') as history_file:
     history = history_file.read()
 
+
+
+
+
+# install pycairo
+if os.name == 'nt':
+    p = platform.architecture()
+    is64bit = p[0] == '64bit'
+    if sys.version[0:3] == '3.5':
+        if is64bit:
+            url = 'https://download.lfd.uci.edu/pythonlibs/q5gtlas7/pycairo-1.18.0-cp35-cp35m-win_amd64.whl'
+        else:
+            url = 'https://download.lfd.uci.edu/pythonlibs/q5gtlas7/pycairo-1.18.0-cp35-cp35m-win32.whl'
+
+    if sys.version[0:3] == '3.6':
+        if is64bit:
+            url = 'https://download.lfd.uci.edu/pythonlibs/q5gtlas7/pycairo-1.18.0-cp36-cp36m-win_amd64.whl'
+        else:
+            url = 'https://download.lfd.uci.edu/pythonlibs/q5gtlas7/pycairo-1.18.0-cp36-cp36m-win32.whl'
+
+    if sys.version[0:3] == '3.7':
+        if is64bit:
+            url = 'https://download.lfd.uci.edu/pythonlibs/q5gtlas7/pycairo-1.18.0-cp37-cp37m-win_amd64.whl'
+        else:
+            url = 'https://download.lfd.uci.edu/pythonlibs/q5gtlas7/pycairo-1.18.0-cp37-cp37m-win32.whl'
+
+    try:
+        import pycairo
+    except:
+        call_cmd = "pip install " + url
+        os.system(call_cmd)
+
+        import site
+        import ctypes.util
+        default_os_path = os.environ['PATH']
+        os.environ['PATH'] = ''
+        for s in site.getsitepackages():
+            os.environ['PATH'] = os.environ['PATH']+';' + s+'\\cairo'
+        os.environ['PATH'] = os.environ['PATH']+';' + default_os_path
+        print(os.environ['PATH'])
+        if ctypes.util.find_library('cairo')is not None:
+            print("cairo installed: OK")
+else:
+    try:
+        import pycairo
+    except:
+        os.system("pip install pycairo==1.18.1")
+
 # Gather requirements from requirements_dev.txt
 # TODO : We could potentially split up the test/dev dependencies later
 install_reqs = []
-- 
GitLab