Add configurable annotation styles
This commit is contained in:
@@ -297,49 +297,71 @@ class PdfDocument:
|
||||
page.insert_image(fitz.Rect(*rect), filename=str(image_path), keep_proportion=True)
|
||||
self.mark_changed()
|
||||
|
||||
def add_rectangle(self, page_index: int) -> None:
|
||||
def add_rectangle(
|
||||
self,
|
||||
page_index: int,
|
||||
stroke_color: tuple[float, float, float] = (0.9, 0.1, 0.1),
|
||||
border_width: float = 2,
|
||||
opacity: float = 1.0,
|
||||
) -> None:
|
||||
self.snapshot()
|
||||
page = self._page(page_index)
|
||||
rect = self._default_rect(page)
|
||||
annot = page.add_rect_annot(rect)
|
||||
annot.set_colors(stroke=(0.9, 0.1, 0.1))
|
||||
annot.set_border(width=2)
|
||||
self._apply_annotation_style(annot, stroke_color, border_width, opacity)
|
||||
annot.update()
|
||||
self.mark_changed()
|
||||
|
||||
def add_circle(self, page_index: int) -> None:
|
||||
def add_circle(
|
||||
self,
|
||||
page_index: int,
|
||||
stroke_color: tuple[float, float, float] = (0.1, 0.3, 0.9),
|
||||
border_width: float = 2,
|
||||
opacity: float = 1.0,
|
||||
) -> None:
|
||||
self.snapshot()
|
||||
page = self._page(page_index)
|
||||
rect = self._default_rect(page)
|
||||
annot = page.add_circle_annot(rect)
|
||||
annot.set_colors(stroke=(0.1, 0.3, 0.9))
|
||||
annot.set_border(width=2)
|
||||
self._apply_annotation_style(annot, stroke_color, border_width, opacity)
|
||||
annot.update()
|
||||
self.mark_changed()
|
||||
|
||||
def add_line(self, page_index: int, arrow: bool = False) -> None:
|
||||
def add_line(
|
||||
self,
|
||||
page_index: int,
|
||||
arrow: bool = False,
|
||||
stroke_color: tuple[float, float, float] = (0.1, 0.1, 0.1),
|
||||
border_width: float = 2,
|
||||
opacity: float = 1.0,
|
||||
) -> None:
|
||||
self.snapshot()
|
||||
page = self._page(page_index)
|
||||
rect = self._default_rect(page)
|
||||
start = fitz.Point(rect.x0, rect.y0)
|
||||
end = fitz.Point(rect.x1, rect.y1)
|
||||
annot = page.add_line_annot(start, end)
|
||||
annot.set_colors(stroke=(0.1, 0.1, 0.1))
|
||||
annot.set_border(width=2)
|
||||
self._apply_annotation_style(annot, stroke_color, border_width, opacity)
|
||||
if arrow:
|
||||
annot.set_line_ends(fitz.PDF_ANNOT_LE_NONE, fitz.PDF_ANNOT_LE_CLOSED_ARROW)
|
||||
annot.update()
|
||||
self.mark_changed()
|
||||
|
||||
def add_ink(self, page_index: int, points: list[tuple[float, float]]) -> None:
|
||||
def add_ink(
|
||||
self,
|
||||
page_index: int,
|
||||
points: list[tuple[float, float]],
|
||||
stroke_color: tuple[float, float, float] = (0.05, 0.05, 0.05),
|
||||
border_width: float = 2,
|
||||
opacity: float = 1.0,
|
||||
) -> None:
|
||||
if len(points) < 2:
|
||||
return
|
||||
self.snapshot()
|
||||
page = self._page(page_index)
|
||||
stroke = [(float(x), float(y)) for x, y in points]
|
||||
annot = page.add_ink_annot([stroke])
|
||||
annot.set_colors(stroke=(0.05, 0.05, 0.05))
|
||||
annot.set_border(width=2)
|
||||
stroke_points = [(float(x), float(y)) for x, y in points]
|
||||
annot = page.add_ink_annot([stroke_points])
|
||||
self._apply_annotation_style(annot, stroke_color, border_width, opacity)
|
||||
annot.update()
|
||||
self.mark_changed()
|
||||
|
||||
@@ -754,6 +776,17 @@ class PdfDocument:
|
||||
annot = annot.next
|
||||
return None
|
||||
|
||||
def _apply_annotation_style(
|
||||
self,
|
||||
annot,
|
||||
stroke_color: tuple[float, float, float],
|
||||
border_width: float,
|
||||
opacity: float,
|
||||
) -> None:
|
||||
annot.set_colors(stroke=stroke_color)
|
||||
annot.set_border(width=max(1, float(border_width)))
|
||||
annot.set_opacity(max(0.1, min(1.0, float(opacity))))
|
||||
|
||||
def mark_changed(self) -> None:
|
||||
self._revision += 1
|
||||
self._dirty = True
|
||||
|
||||
Reference in New Issue
Block a user