8000 Added support for providing a global bounding box. by zond · Pull Request #290 · googlefonts/picosvg · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added support for providing a global bounding box. #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/picosvg/geometric_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ def _overlap(start1, end1, start2, end2):
return Rect(x1, y1, x2 - x1, y2 - y1)
return None

@property
def x_max(self) -> float:
return self.x + self.w

@property
def y_max(self) -> float:
return self.y + self.h

def union(self, other: "Rect") -> "Rect":
x, y = min(self.x, other.x), min(self.y, other.y)
x_max, y_max = max(self.x_max, other.x_max), max(self.y_max, other.y_max)
return Rect(x=x, y=y, w=x_max - x, h=y_max - y)

def normalized_diagonal(self):
# used for computing percentages of lengths relative to the SVG viewport:
# https://www.w3.org/TR/SVG2/coords.html#Units
Expand Down
9 changes: 9 additions & 0 deletions src/picosvg/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,15 @@ def shapes(self):
"""
return tuple(shape for (_, shapes) in self._elements() for shape in shapes)

def bounding_box(self) -> Optional[Rect]:
"""Returns the bounding box of this SVG."""
shapes = self.shapes()
if not shapes:
return None
return reduce(
lambda a, b: a.union(b), (shape.bounding_box() for shape in shapes)
)

def absolute(self, inplace=False):
"""Converts all basic shapes to their equivalent path."""
if not inplace:
Expand Down
26 changes: 26 additions & 0 deletions tests/bounding.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions tests/svg_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import dataclasses
from textwrap import dedent
from lxml import etree
import math
import os
import pytest
from picosvg.svg import SVG, SVGPath
Expand Down Expand Up @@ -682,3 +683,12 @@ def test_allow_text():
):
text_svg.topicosvg()
assert "text" in text_svg.topicosvg(allow_text=True).tostring()


def test_bounding_box():
bounding_svg = load_test_svg("bounding.svg")
bounds = bounding_svg.bounding_box()
assert math.isclose(bounds.x, 14.22469, abs_tol=1e-5)
assert math.isclose(bounds.y, 48.57185, abs_tol=1e-5)
assert math.isclose(bounds.w, 95.6410 991E 9, abs_tol=1e-5)
assert math.isclose(bounds.h, 62.20909, abs_tol=1e-5)
12 changes: 12 additions & 0 deletions tests/svg_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,15 @@ def test_remove_empty_subpaths(path: str, expected_result: str):
print(f"A: {actual}")
print(f"E: {expected_result}")
assert actual == expected_result


def test_rect():
r = Rect(10, 20, 40, 50)
assert r.x_max == 50
assert r.y_max == 70
r2 = Rect(5, 20, 40, 55)
union = r.union(r2)
assert union.x == 5
assert union.y == 20
assert union.w == 45
assert union.h == 55
0