Skip to content

Commit

Permalink
PCVL-397 Fix SVG rendering with drawsvg 2 (#195)
Browse files Browse the repository at this point in the history
Fix SVG rendering with drawsvg 2
  • Loading branch information
ericbrts authored Mar 21, 2023
1 parent 6d4fef5 commit a26b0bd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
6 changes: 6 additions & 0 deletions perceval/rendering/canvas/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def position(self, v):
self._maxy = y
self._position = (x, y)

def height(self):
return self._maxy - self._miny

def width(self):
return self._maxx - self._minx

def add_mline(self,
points: List[float],
stroke: str = "black",
Expand Down
9 changes: 4 additions & 5 deletions perceval/rendering/canvas/svg_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ class SvgCanvas(Canvas):
With it, it is possible to create dynamic svg graphics.
"""
def __init__(self, **opts):
super().__init__(**opts, inverse_Y=True)
super().__init__(**opts)
self._draws = []
self._render_width = (opts["total_width"]+3)*50
self._render_height = (opts["total_height"]+1)*50
self._render_height = (opts["total_height"]+.5)*50
self._pixel_size = 1.25
if 'render_size' in opts:
self._pixel_size *= opts['render_size']
Expand Down Expand Up @@ -117,8 +116,8 @@ def draw(self):
if hasattr(self, "_group"):
d = draw.Group(x=self._group[0], y=self._group[1])
else:
d = draw.Drawing(self._render_width, self._render_height,
origin=(self._minx-25, -self._maxy))
d = draw.Drawing(self.width()+50, self._render_height,
origin=(self._minx-25, 0))
for dr in self._draws:
d.append(dr)
return d.set_pixel_scale(self._pixel_size)
4 changes: 1 addition & 3 deletions perceval/rendering/circuit/abstract_skin.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ def get_size(self, p: AProcessor, recursive: bool = False) -> Tuple[int, int]:
comp_width = self.get_width(comp)
end_w = start_w + comp_width
w[r] = [end_w] * comp.m
# For now, return the whole circuit height as processor height, even if some heralded modes are not shown.
# TODO fix this
return max(w), p.circuit_size # min(p.circuit_size, height+2)
return max(w), min(p.circuit_size, height+2)

def measure(self, c: AComponent) -> Tuple[int, int]:
"""
Expand Down
4 changes: 2 additions & 2 deletions perceval/rendering/pdisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ def pdisplay_to_file(o, path: str, output_format: Format = None, **opts):
_, output_ext = os.path.splitext(path)
try:
if output_ext == ".png":
res.savePng(path) # May fail when rasterization is not available (i.e. on Windows)
res.save_png(path) # May fail when rasterization is not available (i.e. on Windows)
else:
res.saveSvg(path)
res.save_svg(path)
return
except:
pass
Expand Down

0 comments on commit a26b0bd

Please sign in to comment.