Repository: btel/svg_utils

Issue:
[0.3.3] Started producing SVG without width, height and viewBox
Hi @btel ,

due to my [rendering regression tests in xiangqi-setup](https://github.com/hartwork/xiangqi-setup/blob/master/test-for-rendering-regressions.sh) I noticed that things broke when [trying to upgrade to svgutils 0.3.3](https://github.com/hartwork/xiangqi-setup/pull/37). What happens is that the *width, height, and viewBox parameters are no longer included in the written SVG file*. Here's what that looks like in Inkscape: The dotted line is where the viewbox was with svgutils 0.3.2, the solid frame is where the new defacto viewbox is:

![svgutils_0_3_3_viewbox](https://user-images.githubusercontent.com/1577132/107497839-5677fa00-6b93-11eb-8609-1177d061ff10.png)

Here's a diff of before and after, with regard to the SVG output.
```diff
--- tests/actual-board-a4_blank_2cm_margin_BEFORE.svg   2021-02-10 11:22:04.300605915 +0100
+++ tests/actual-board-a4_blank_2cm_margin_AFTER.svg    2021-02-10 11:21:49.446494230 +0100
@@ -1,5 +1,5 @@
 <?xml version='1.0' encoding='ASCII' standalone='yes'?>
-<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="248.03149580999997px" viewBox="0 0 248.03149580999997 350.78740120838086" height="350.78740120838086px">
+<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
   <g>
     <g transform="translate(0, 0) scale(0.33333333299287476 0.33333333299287476) "><defs id="defs4"/>
   <sodipodi:namedview xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" id="base" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="0.7" inkscape:cx="359.38222" inkscape:cy="512.51453" inkscape:document-units="px" inkscape:current-layer="layer1" showgrid="false" inkscape:window-width="1920" inkscape:window-height="1024" inkscape:window-x="0" inkscape:window-y="0" inkscape:window-maximized="1"/>
```

I have a feeling that #58 is causing this: `.width` and `.height` (with no underscores) are no longer set?
Is that a bug or a feature? How can I make svgutils 0.3.3 put width, height and viewBox into SVG output again?

Thanks and best, Sebastian

Discussion:

Patch:
diff --git a/src/svgutils/transform.py b/src/svgutils/transform.py
index 384379e..ef15f9e 100644
--- a/src/svgutils/transform.py
+++ b/src/svgutils/transform.py
@@ -240,14 +240,14 @@ class SVGFigure(object):
 
         if width:
             try:
-                # width is an instance of Unit
-                self._width = width.value
+                self.width = width  # this goes to @width.setter a few lines down
             except AttributeError:
                 # int or str
                 self._width = width
+
         if height:
             try:
-                self._height = height.value
+                self.height = height  # this goes to @height.setter a few lines down
             except AttributeError:
                 self._height = height
 


Test patch:
diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml
new file mode 100644
index 0000000..32e1d0c
--- /dev/null
+++ b/.github/workflows/run-tests.yml
@@ -0,0 +1,28 @@
+# Copyright (C) 2021 Sebastian Pipping <sebastian@pipping.org>
+# Licensed under the MIT License
+
+name: Run the test suite
+
+on:
+- pull_request
+- push
+
+jobs:
+  run-tests:
+    name: Run the test suite
+    strategy:
+      matrix:
+        python-version: [2.7, 3.5, 3.9]  # no explicit need for 3.6, 3.7, 3.8
+        runs-on: [macos-latest, ubuntu-latest]
+    runs-on: ${{ matrix.runs-on }}
+    steps:
+    - uses: actions/checkout@v2.3.4
+    - uses: actions/setup-python@v2.1.4
+      with:
+        python-version: ${{ matrix.python-version }}
+    - name: Run the test suite
+      run: |
+        python --version
+        pip install nose
+        pip install -e .
+        python -m nose -v
diff --git a/tests/test_transform.py b/tests/test_transform.py
index f17b4bb..710c3be 100644
--- a/tests/test_transform.py
+++ b/tests/test_transform.py
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 # coding=utf-8
+from tempfile import NamedTemporaryFile
 
 from svgutils import transform
 from svgutils.compose import Unit
@@ -61,3 +62,16 @@ def test_create_svg_figure():
 
     svg_fig = transform.SVGFigure("2", "3")
     assert "svg" in svg_fig.to_str().decode("ascii")
+
+
+def test_svg_figure_writes_width_height_and_view_box():
+    svg_fig = transform.SVGFigure(Unit("400mm"), Unit("300mm"))
+
+    with NamedTemporaryFile() as f1:
+        svg_fig.save(f1.name)
+        with open(f1.name) as f2:
+            written_content = f2.read()
+
+    assert 'width="400.0mm"' in written_content
+    assert 'height="300.0mm"' in written_content
+    assert 'viewBox="0 0 400.0 300.0"' in written_content