MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / draw_rect

Method draw_rect

src/__init__.py:15068–15117  ·  view source on GitHub ↗

Draw a rectangle. Args: radius: if not None, the rectangle will have rounded corners. This is the radius of the curvature, given as percentage of the rectangle width or height. Valid are values 0 < v <= 0.5. For a sequence of two v

(self, rect: rect_like, *, radius=None)

Source from the content-addressed store, hash-verified

15066 return self.last_point
15067
15068 def draw_rect(self, rect: rect_like, *, radius=None) -> Point:
15069 """Draw a rectangle.
15070
15071 Args:
15072 radius: if not None, the rectangle will have rounded corners.
15073 This is the radius of the curvature, given as percentage of
15074 the rectangle width or height. Valid are values 0 < v <= 0.5.
15075 For a sequence of two values, the corners will have different
15076 radii. Otherwise, the percentage will be computed from the
15077 shorter side. A value of (0.5, 0.5) will draw an ellipse.
15078 """
15079 r = Rect(rect)
15080 if radius is None: # standard rectangle
15081 self.draw_cont += _format_g(JM_TUPLE(
15082 list(r.bl * self.ipctm) + [r.width, r.height]
15083 )) + " re\n"
15084 self.updateRect(r)
15085 self.last_point = r.tl
15086 return self.last_point
15087 # rounded corners requested. This requires 1 or 2 values, each
15088 # with 0 < value <= 0.5
15089 if hasattr(radius, "__float__"):
15090 if radius <= 0 or radius > 0.5:
15091 raise ValueError(f"bad radius value {radius}.")
15092 d = min(r.width, r.height) * radius
15093 px = (d, 0)
15094 py = (0, d)
15095 elif hasattr(radius, "__len__") and len(radius) == 2:
15096 rx, ry = radius
15097 px = (rx * r.width, 0)
15098 py = (0, ry * r.height)
15099 if min(rx, ry) <= 0 or max(rx, ry) > 0.5:
15100 raise ValueError(f"bad radius value {radius}.")
15101 else:
15102 raise ValueError(f"bad radius value {radius}.")
15103
15104 lp = self.draw_line(r.tl + py, r.bl - py)
15105 lp = self.draw_curve(lp, r.bl, r.bl + px)
15106
15107 lp = self.draw_line(lp, r.br - px)
15108 lp = self.draw_curve(lp, r.br, r.br - py)
15109
15110 lp = self.draw_line(lp, r.tr + py)
15111 lp = self.draw_curve(lp, r.tr, r.tr - px)
15112
15113 lp = self.draw_line(lp, r.tl + px)
15114 self.last_point = self.draw_curve(lp, r.tl, r.tl + py)
15115
15116 self.updateRect(r)
15117 return self.last_point
15118
15119 def draw_quad(self, quad: quad_like) -> Point:
15120 """Draw a Quad."""

Callers

nothing calls this directly

Calls 6

updateRectMethod · 0.95
draw_lineMethod · 0.95
draw_curveMethod · 0.95
RectClass · 0.85
_format_gFunction · 0.85
JM_TUPLEFunction · 0.85

Tested by

no test coverage detected