Skip to content

Commit 9335413

Browse files
src/__init__.py tests/: improve exceptions for non-rich text border_color.
Annot.update() now raises exception of border_color specified if not rich text, matching Annot.__init__(). Also test exceptions are raised as expected. Addresses #4447.
1 parent 2ee586c commit 9335413

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,12 @@ def update(self,
15671567
cross_out: draw diagonal lines, 'Redact' only.
15681568
rotate: set rotation, 'FreeText' and some others.
15691569
"""
1570+
annot_obj = mupdf.pdf_annot_obj(self.this)
1571+
1572+
if border_color:
1573+
is_rich_text = mupdf.pdf_dict_get(annot_obj, PDF_NAME("RC"))
1574+
if not is_rich_text:
1575+
raise ValueError("cannot set border_color if rich_text is False")
15701576
Annot.update_timing_test()
15711577
CheckParent(self)
15721578
def color_string(cs, code):

tests/test_annots.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,25 @@ def test_4254():
544544
annot = page.add_freetext_annot(rect, "Test Annotation from minimal example")
545545
annot.set_border(width=1, dashes=(3, 3))
546546
annot.set_opacity(0.5)
547+
try:
548+
annot.set_colors(stroke=(1, 0, 0))
549+
except ValueError as e:
550+
assert 'cannot be used for FreeText annotations' in str(e), f'{e}'
551+
else:
552+
assert 0
547553
annot.update()
548554

549555
rect = pymupdf.Rect(200, 200, 400, 400)
550556
annot2 = page.add_freetext_annot(rect, "Test Annotation from minimal example pt 2")
551557
annot2.set_border(width=1, dashes=(3, 3))
552558
annot2.set_opacity(0.5)
559+
try:
560+
annot2.set_colors(stroke=(1, 0, 0))
561+
except ValueError as e:
562+
assert 'cannot be used for FreeText annotations' in str(e), f'{e}'
563+
else:
564+
assert 0
565+
annot.update()
553566
annot2.update()
554567

555568
# stores top color for each pixmap
@@ -611,3 +624,66 @@ def test_richtext():
611624
annot.update(fill_color=gold, opacity=0.5, rotate=90)
612625
pix2 = page.get_pixmap()
613626
assert pix1.samples == pix2.samples
627+
628+
629+
def test_4447():
630+
document = pymupdf.open()
631+
632+
page = document.new_page()
633+
634+
text_color = (1, 0, 0)
635+
fill_color = (0, 1, 0)
636+
border_color = (0, 0, 1)
637+
638+
annot_rect = pymupdf.Rect(90.1, 486.73, 139.26, 499.46)
639+
640+
try:
641+
annot = page.add_freetext_annot(
642+
annot_rect,
643+
"AETERM",
644+
fontname="Arial",
645+
fontsize=10,
646+
text_color=text_color,
647+
fill_color=fill_color,
648+
border_color=border_color,
649+
border_width=1,
650+
)
651+
except ValueError as e:
652+
assert 'cannot set border_color if rich_text is False' in str(e), str(e)
653+
else:
654+
assert 0
655+
656+
try:
657+
annot = page.add_freetext_annot(
658+
(30, 400, 100, 450),
659+
"Two",
660+
fontname="Arial",
661+
fontsize=10,
662+
text_color=text_color,
663+
fill_color=fill_color,
664+
border_color=border_color,
665+
border_width=1,
666+
)
667+
except ValueError as e:
668+
assert 'cannot set border_color if rich_text is False' in str(e), str(e)
669+
else:
670+
assert 0
671+
672+
annot = page.add_freetext_annot(
673+
(30, 500, 100, 550),
674+
"Three",
675+
fontname="Arial",
676+
fontsize=10,
677+
text_color=text_color,
678+
border_width=1,
679+
)
680+
annot.update(text_color=text_color, fill_color=fill_color)
681+
try:
682+
annot.update(border_color=border_color)
683+
except ValueError as e:
684+
assert 'cannot set border_color if rich_text is False' in str(e), str(e)
685+
else:
686+
assert 0
687+
688+
path_out = os.path.normpath(f'{__file__}/../../tests/test_4447.pdf')
689+
document.save(path_out)

0 commit comments

Comments
 (0)