Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
srush committed Aug 18, 2024
1 parent 225ff85 commit 75a0620
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 8 deletions.
13 changes: 7 additions & 6 deletions chalk/backend/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,25 @@ def write_style(d: Dict[str, Any]) -> str:
return out


def render_svg_patches(patches: List[Patch], dwg: str) -> None:
def render_svg_patches(patches: List[Patch]) -> str:
out = ""
for ind, patch, style_new in chalk.backend.patch.order_patches(patches):
inner = to_svg(patch, ind)

dwg += f"""
out += f"""
<g style="{write_style(style_new)}">
{inner}
</g>
"""
return out

def patches_to_file(
patches: List[Patch], path: str, height: tx.IntLike, width: tx.IntLike
) -> None:
dwg = f"""
<?xml version="1.0" encoding="utf-8" ?>
<svg baseProfile="full" height="{int(height)}" version="1.1" width="{int(width)}" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink">
dwg = f"""<?xml version="1.0" encoding="utf-8" ?>
<svg baseProfile="full" height="{int(height)}" version="1.1" width="{int(width)}" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink">
"""
render_svg_patches(patches, dwg)
dwg += render_svg_patches(patches)
dwg += "</svg>"
with open(path, "w") as f:
f.write(dwg)
Expand Down
2 changes: 2 additions & 0 deletions chalk/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
tree_map,
vectorize,
vmap,
onp
)
from jaxtyping import Float
from typing_extensions import Self
Expand Down Expand Up @@ -549,4 +550,5 @@ def arc_to_bezier(theta1: Array, theta2: Array, n: int = 5) -> Array:
"prefix_broadcast",
"BoolLike",
"JAX_MODE",
"onp"
]
2 changes: 1 addition & 1 deletion examples/bigben.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def fit_in(b: Diagram, s: Diagram, frame=0.1) -> Diagram:
# Find the inner radius
trace = b.get_trace()
p, m = trace(tx.origin, unit_x)
m = min([x for x, m1 in zip(p[0], m[0]) if m1 and x > 0])
m = min([x for x, m1 in zip(p, m) if m1 and x > 0])

# Scale the inner diagram to that size
return b + s.scale_uniform_to_x(2 * m - frame)
Expand Down
Binary file modified examples/output/squares.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
259 changes: 258 additions & 1 deletion examples/output/squares.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ imageio
jaxtyping
numpy
jax
chex
typing-extensions
importlib-metadata

0 comments on commit 75a0620

Please sign in to comment.