Skip to content

Commit

Permalink
Simplify conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
eumiro committed Aug 5, 2023
1 parent 9503a46 commit 486aebd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
16 changes: 6 additions & 10 deletions cairosvg/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def bounding_box_elliptical_arc(x1, y1, rx, ry, phi, large, sweep, x, y):
cx = cxprime * cos(phi) - cyprime * sin(phi) + (x1 + x) / 2
cy = cxprime * sin(phi) + cyprime * cos(phi) + (y1 + y) / 2

if phi == 0 or phi == pi:
if phi in (0, pi):
minx = cx - rx
tminx = angle(-rx, 0)
maxx = cx + rx
Expand All @@ -269,7 +269,7 @@ def bounding_box_elliptical_arc(x1, y1, rx, ry, phi, large, sweep, x, y):
tminy = angle(0, -ry)
maxy = cy + ry
tmaxy = angle(0, ry)
elif phi == pi / 2 or phi == 3 * pi / 2:
elif phi in (pi / 2, 3 * pi / 2):
minx = cx - ry
tminx = angle(-ry, 0)
maxx = cx + ry
Expand Down Expand Up @@ -314,17 +314,13 @@ def bounding_box_elliptical_arc(x1, y1, rx, ry, phi, large, sweep, x, y):
angle1, angle2 = angle2, angle1
other_arc = True

if ((not other_arc and (angle1 > tminx or angle2 < tminx)) or
(other_arc and not (angle1 > tminx or angle2 < tminx))):
if other_arc == (angle1 <= tminx <= angle2):
minx = min(x, x1)
if ((not other_arc and (angle1 > tmaxx or angle2 < tmaxx)) or
(other_arc and not (angle1 > tmaxx or angle2 < tmaxx))):
if other_arc == (angle1 <= tmaxx <= angle2):
maxx = max(x, x1)
if ((not other_arc and (angle1 > tminy or angle2 < tminy)) or
(other_arc and not (angle1 > tminy or angle2 < tminy))):
if other_arc == (angle1 <= tminy <= angle2):
miny = min(y, y1)
if ((not other_arc and (angle1 > tmaxy or angle2 < tmaxy)) or
(other_arc and not (angle1 > tmaxy or angle2 < tmaxy))):
if other_arc == (angle1 <= tmaxy <= angle2):
maxy = max(y, y1)

return minx, miny, maxx - minx, maxy - miny
Expand Down
8 changes: 5 additions & 3 deletions cairosvg/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ def image(surface, node):
width = size(surface, node.get('width'), 'x')
height = size(surface, node.get('height'), 'y')

if image_bytes[:4] == b'\x89PNG' and not surface.map_image:
if image_bytes.startswith(b'\x89PNG') and not surface.map_image:
png_file = BytesIO(image_bytes)
elif (image_bytes[:5] in (b'<svg ', b'<?xml', b'<!DOC') or
image_bytes[:2] == b'\x1f\x8b') or b'<svg' in image_bytes:
elif (
image_bytes.startswith((b'<svg ', b'<?xml', b'<!DOC', b'\x1f\x8b'))
or b'<svg' in image_bytes
):
if 'x' in node:
del node['x']
if 'y' in node:
Expand Down
2 changes: 1 addition & 1 deletion cairosvg/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def __init__(self, **kwargs):
if not bytestring:
bytestring = self.fetch_url(
parse_url(self.url), 'image/svg+xml')
if len(bytestring) >= 2 and bytestring[:2] == b'\x1f\x8b':
if bytestring.startswith(b'\x1f\x8b'):
bytestring = gzip.decompress(bytestring)
tree = ElementTree.fromstring(
bytestring, forbid_entities=not unsafe,
Expand Down
6 changes: 2 additions & 4 deletions cairosvg/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,8 @@ def rect(surface, node):
if rx == 0 or ry == 0:
surface.context.rectangle(x, y, width, height)
else:
if rx > width / 2:
rx = width / 2
if ry > height / 2:
ry = height / 2
rx = min(rx, width / 2)
ry = min(ry, height / 2)

# Inspired by Cairo Cookbook
# http://cairographics.org/cookbook/roundedrectangles/
Expand Down

0 comments on commit 486aebd

Please sign in to comment.