xxyxyz / flat

Generative infrastructure for Python
MIT License
125 stars 14 forks source link

Exporting Shapes to SVG #9

Closed michielkauwatjoe closed 5 years ago

michielkauwatjoe commented 5 years ago

Hi again, when I try to export a shape to SVG, I get this traceback:

Traceback (most recent call last):
  File "./DrawQuadraticGlyph.py", line 284, in draw
    context.saveImage(path)
  File "/Users/michiel/Code/PageBot/Lib/pagebot/contexts/flat/flatcontext.py", line 165, in saveDocument
    im = self.pages[0].image(kind=RGB)
  File "/Users/michiel/Code/Flat/flat/document.py", line 47, in image
    item.rasterize(r, k, 0.0, 0.0)
  File "/Users/michiel/Code/Flat/flat/shape.py", line 498, in rasterize
    style.stroke.rasterize(rasterizer)
  File "/Users/michiel/Code/Flat/flat/color.py", line 28, in rasterize
    raise ValueError('Invalid color kind.')
ValueError: Invalid color kind.

Traceback (most recent call last):
  File "./DrawQuadraticGlyph.py", line 296, in draw
    context.saveImage(path)
  File "/Users/michiel/Code/PageBot/Lib/pagebot/contexts/flat/flatcontext.py", line 181, in saveDocument
    self.pages[0].svg(path)
  File "/Users/michiel/Code/Flat/flat/document.py", line 39, in svg
    data = svgserialize(self, compress)
  File "/Users/michiel/Code/Flat/flat/svg.py", line 163, in serialize
    defs, b'\n'.join(item.svg() for item in page.items))
  File "/Users/michiel/Code/Flat/flat/svg.py", line 163, in <genexpr>
    defs, b'\n'.join(item.svg() for item in page.items))
  File "/Users/michiel/Code/Flat/flat/shape.py", line 464, in svg
    return self.item.svg(self.k, self.x, self.y)
  File "/Users/michiel/Code/Flat/flat/shape.py", line 351, in svg
    self.style.svg())
  File "/Users/michiel/Code/Flat/flat/shape.py", line 83, in svg
    attributes.append(b'stroke="%s"' % self.stroke.svg())
  File "/Users/michiel/Code/Flat/flat/color.py", line 24, in svg
    raise NotImplementedError('SVG does not support grayscale.')
NotImplementedError: SVG does not support grayscale.

which seems to be caused by the default stroke value in style, set to gray(0). Not sure if there's a proper way to implement grayscale for SVG, however setting the default stroke to a RGB value, for example rgb(100, 100, 100), seems to circumvent the problem. Should I create another pull request?

M.

sukop commented 5 years ago

Hi!

The problem is that the default color space in PDF is DeviceGray [1] and SVG does not yet support anything like it. As SVG does not have a notion of a graphic state, I think it is preferred to do what PDF does, namely to default to grayscale. The proper fix would be to use device-gray in all svg methods of color.py but the spec is not yet final.

That said, a much easier fix in case of SVG is to explicitly ask for RGB color space for every shape() and not relying on the defaults. Would that work for you?

[1] see e.g. PDF Referece 1.3, 4.3 Graphics State [2] https://www.w3.org/TR/2013/WD-SVG2-20130618/color.html#Unmanaged

michielkauwatjoe commented 5 years ago

I see now that our code should already initialize RGB color after a new shape is created, but I guess something is resetting it somehow. I'll look into it, thanks again!

michielkauwatjoe commented 5 years ago

Fixed it, that helped :) I'll close this issue.