-
Notifications
You must be signed in to change notification settings - Fork 154
/
edit.py
1188 lines (1053 loc) · 40.5 KB
/
edit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Edit images in a PDF document
-------------------------------------------------------------------------------
License: GNU AFFERO GPL 3.0
(c) 2020 Jorj X. McKie
Usage
-----
python edit.py
Dependencies
------------
PyMuPDF v1.17.5, wxPython Phoenix version
"""
import os
import sys
import fitz
import wx
print("Python:", sys.version)
print("wxPython:", wx.version())
print(fitz.__doc__)
try:
from PageFormat import FindFit
except ImportError:
def FindFit(*args):
return "not implemented"
try:
from icons import ico_pdf # PDF icon in upper left screen corner
do_icon = True
except ImportError:
do_icon = False
app = None
app = wx.App()
if wx.VERSION[:3] < (3, 0, 3):
raise AssertionError("need wxPython Phoenix version")
if fitz.VersionBind.split(".") < ["1", "17", "5"]:
raise AssertionError("need PyMuPDF 1.17.5 or later")
# compute maximum PDF page display dimensions
MAX_WIDTH, MAX_HEIGHT = wx.GetDisplaySize()
MAX_WIDTH -= 400
MAX_HEIGHT -= 130
cur_hand = wx.Cursor(wx.CURSOR_HAND)
cur_cross = wx.Cursor(wx.CURSOR_CROSS)
cur_norm = wx.Cursor(wx.CURSOR_DEFAULT)
degrees = ["0", "90", "180", "270"]
def getint(v):
"""Convert any string to a non-negative integer."""
try:
return int(v) # fastest
except:
pass
if not type(v) is str: # we need a string at least
return 0
return int("0" + "".join((d for d in v if d in "0123456789")))
def calc_matrix(fw, fh, tr, rotate=0):
"""Calculate transformation matrix for image insertion.
Notes:
The result is basically a multiplication of four matrices in this
sequence: number one moves the image rectangle (always a unit rect!) to (0,0), number two rotates as desired, number three
scales using the width-height-ratio, and number four moves to the
target rect.
Args:
fw, fh: width / height ratio factors 0 < f <= 1.
The longer one must be 1.
tr: target rect in PDF (!) coordinates
rotate: (degrees) rotation angle.
Returns:
Transformation matrix.
"""
# compute scale matrix parameters
small = min(fw, fh) # factor of the smaller side
if rotate not in (0, 180):
fw, fh = fh, fw # width / height exchange their roles
if fw < 1: # portrait
if tr.width / fw > tr.height / fh:
w = tr.height * small
h = tr.height
else:
w = tr.width
h = tr.width / small
elif fw != fh: # landscape
if tr.width / fw > tr.height / fh:
w = tr.height / small
h = tr.height
else:
w = tr.width
h = tr.width * small
else: # (treated as) equal sided
w = tr.width
h = tr.height
# center point of target rectangle
tmp = (tr.tl + tr.br) / 2.0
# move image center to (0, 0), then rotate
m = fitz.Matrix(1, 0, 0, 1, -0.5, -0.5) * fitz.Matrix(rotate)
m *= fitz.Matrix(w, h) # concat scale matrix
m *= fitz.Matrix(1, 0, 0, 1, tmp.x, tmp.y) # concat move to target center
return m
def find_image(page, img):
"""Locate image reference in page contents.
Args:
page: the PDF page
img: image reference name
Returns:
Dictionary with keys:
xref - containing /Contents object
start - position of 'a' in 'a b c d e f cm'
stop - position of 'cm' + 1
deg - one of {0, 90, 180, 270}
bbox - image rectangle
msg - None if successful
Notes:
For changing the image's position, replace bytes [start:stop]
replaced by the new matrix command.
"""
doc = page.parent
img_rect = page.get_image_bbox(img)
rc = {
"xref": 0,
"start": -1,
"stop": -1,
"begin": -1, # begin of '/name Do' command
"end": -1, # position after 'Do' command
"deg": 0,
"bbox": img_rect,
"msg": None,
}
if img_rect.is_infinite:
rc["msg"] = "cannot find image"
return rc
invoker = b"/%s Do" % img.encode()
pos1 = -1
cont = b""
mat_factors = []
xref = 0
for xref in page.get_contents():
cont = doc.xref_stream(xref)
pos1 = cont.find(invoker)
if pos1 > 0:
rc["begin"] = pos1
rc["end"] = pos1 + len(invoker)
break
if pos1 <= 0:
rc["msg"] = "cannot find image"
return rc
rc["xref"] = xref # found the image here
pos2 = cont.rfind(b"cm", 0, pos1)
if pos2 <= 0:
rc["msg"] = "cannot find 'cm'" # PDF command not found
return rc
n = pos2 - 1
for _ in range(6): # find the 6 matrix coefficients
while cont[n] in (32, 10): # space or line break
n -= 1
if n <= 0:
rc["msg"] = "cannot find matrix coeffs"
return rc
m = n - 1
while cont[m] not in (32, 10):
m -= 1
if m <= 0:
rc["msg"] = "cannot find matrix coeffs"
return rc
try: # convert coeff from string to float
m += 1
mat_factors.append(round(float(cont[m : n + 1]), 5))
except:
rc["msg"] = "invalid matrix coeff " + cont[m : n + 1].decode()
return rc
n = m - 1
start = n + 1 # matrix command starts here
stop = pos2 + 2 # first byte after matrix command
rc["start"] = start # fill return dictionay
rc["stop"] = stop
deg = -1
mat_factors = reversed(mat_factors)
# -------------------------------------------------------------------------
# This matrix must recreate the image rectangle. Only then we can be sure,
# that future changes to this matrix will have valid results.
# -------------------------------------------------------------------------
a, b, c, d, _, _ = mat = fitz.Matrix(mat_factors)
if b == c == 0: # extract the rotation angle
if max(a, d) < 0:
deg = 180
elif min(a, d) > 0:
deg = 0
elif a == d == 0:
if c < 0 < b:
deg = 90
elif b < 0 < c:
deg = 270
if deg < 0: # we only support rotations by 0, 90, 180, 270 deg
rc["msg"] = "cannot handle image rotation"
return rc
rc["deg"] = deg
mat_test = calc_matrix(1, 1, img_rect * ~page.transformation_matrix, rotate=deg)
delta = abs(mat - mat_test) # this must small enough
if delta > 1e-3:
rc["msg"] = "cannot re-compute image rect"
return rc
return rc
def get_images(page):
"""Loop through the page images.
We only consider images referenced by the page itself, not those shown
by e.g. Form XObjects.
"""
img_list = [img[-3] for img in page.get_images(True) if img[-1] == 0]
images = {} # the result
for img in img_list:
try:
d = find_image(page, img)
except:
d = {} # image not found, or some unknown problem
if d != {}:
images[img] = d
return images
# ----------------------------------------------------------------------------
# Our dialog is a subclass of wx.Dialog.
# Only special thing is, that we are being invoked with a filename ...
# ----------------------------------------------------------------------------
class PDFdisplay(wx.Dialog):
def __init__(self, parent, filename):
defPos = wx.DefaultPosition
defSiz = wx.DefaultSize
zoom = 1 # zoom factor of display
wx.Dialog.__init__(
self,
parent,
id=-1,
title="Image Maintenance of ",
pos=defPos,
size=defSiz,
style=wx.CAPTION | wx.CLOSE_BOX | wx.DEFAULT_DIALOG_STYLE,
)
# -------------------------------------------------------------
# display an icon top left of dialog, append filename to title
# -------------------------------------------------------------
if do_icon:
self.SetIcon(ico_pdf.img.GetIcon()) # set a screen icon
self.SetTitle(self.Title + os.path.basename(filename))
KHAKI = wx.Colour(240, 230, 140)
self.SetBackgroundColour(KHAKI)
# ---------------------------------------------------------------------
# open document when dialog gets created
# ---------------------------------------------------------------------
self.doc = fitz.open(filename) # create Document object
if self.doc.needs_pass: # check password protection
self.decrypt_doc()
if self.doc.is_encrypted: # quit if we cannot decrypt
self.Destroy()
return
self.last_pno = -1 # memorize last page displayed
self.last_image = 0 # position to this image
self.img_rect = wx.Rect() # store image rectangle here
self.img_bottom_rect = wx.Rect() # store bottom rectangle here
self.current_idx = -1 # store entry of found rectangle
self.page_height = 0 # page height in pixels
self.dragging_img = False
self.dragstart_x = -1 # for drags: original x
self.dragstart_y = -1 # for drags: original y
self.resize_rect = False # indicate resizing rect
self.sense = 10 # cursor tolerance, e.g. min. rectangle
# side is 2 * self.sense pixels
self.rotation = 0
self.page_images = {}
self.can_change_image = True
# forward button
self.btn_Next = wx.Button(self, -1, "forw", defPos, defSiz, wx.BU_EXACTFIT)
# backward button
self.btn_Prev = wx.Button(self, -1, "back", defPos, defSiz, wx.BU_EXACTFIT)
# -------------------------------------------------------------
# text field for entering a target page. wx.TE_PROCESS_ENTER is
# required to get data entry fired as events.
# -------------------------------------------------------------
self.TextToPage = wx.TextCtrl(
self, -1, "1", defPos, wx.Size(40, -1), wx.TE_RIGHT | wx.TE_PROCESS_ENTER
)
# displays total pages and page paper format
self.statPageMax = wx.StaticText(
self, -1, "of " + str(len(self.doc)) + " pages.", defPos, defSiz, 0
)
self.paperform = wx.StaticText(self, -1, "", defPos, defSiz, 0)
# ---------------------------------------------------------------------
# define zooming matrix for displaying PDF page images
# ---------------------------------------------------------------------
self.zoom = fitz.Matrix(zoom, zoom)
self.shrink = ~self.zoom # corresp. shrink matrix
self.bitmap = self.pdf_show(1)
self.PDFimage = wx.StaticBitmap(self, -1, self.bitmap, defPos, defSiz, style=0)
# ---------------------------------------------------------------------
# Fields defining an image
# ---------------------------------------------------------------------
self.t_Update = wx.StaticText(self, -1, "")
self.t_Save = wx.StaticText(self, -1, "")
self.imgRotation = wx.Choice(self, wx.ID_ANY, defPos, defSiz, degrees, 0)
self.imgName = wx.TextCtrl(self, -1, "", defPos, wx.Size(100, -1))
self.bboxLeft = wx.SpinCtrl(
self,
-1,
"",
defPos,
wx.Size(60, -1),
wx.TE_RIGHT | wx.SP_ARROW_KEYS | wx.TE_PROCESS_ENTER,
0,
9999,
0,
)
self.bboxTop = wx.SpinCtrl(
self,
-1,
"",
defPos,
wx.Size(60, -1),
wx.TE_RIGHT | wx.SP_ARROW_KEYS | wx.TE_PROCESS_ENTER,
0,
9999,
0,
)
self.bboxWidth = wx.SpinCtrl(
self,
-1,
"",
defPos,
wx.Size(60, -1),
wx.TE_RIGHT | wx.SP_ARROW_KEYS | wx.TE_PROCESS_ENTER,
0,
9999,
0,
)
self.bboxHeight = wx.SpinCtrl(
self,
-1,
"",
defPos,
wx.Size(60, -1),
wx.TE_RIGHT | wx.SP_ARROW_KEYS | wx.TE_PROCESS_ENTER,
0,
9999,
0,
)
self.img_count = wx.StaticText(self, -1, "")
self.btn_next_image = wx.Button(
self, -1, "Next img", defPos, defSiz, wx.BU_EXACTFIT
)
self.btn_Update = wx.Button(
self, -1, "Update img", defPos, defSiz, wx.BU_EXACTFIT
)
self.btn_Remove = wx.Button(
self,
id=wx.ID_ANY,
label="Remove img",
pos=defPos,
size=self.btn_Update.Size,
style=0,
validator=wx.DefaultValidator,
)
self.btn_Refresh = wx.Button(
self, -1, "Refresh img", defPos, self.btn_Update.Size, 0
)
self.btn_NewImg = wx.FilePickerCtrl(
self,
wx.ID_ANY,
wx.EmptyString,
"Select image file",
"*.*",
defPos,
defSiz,
wx.FLP_CHANGE_DIR | wx.FLP_FILE_MUST_EXIST | wx.FLP_SMALL,
)
self.btn_Save = wx.Button(
self, -1, "Save file", defPos, self.btn_Update.Size, 0
)
self.btn_Quit = wx.Button(
self, wx.ID_CANCEL, "Cancel", defPos, self.btn_Update.Size, 0
)
self.rectX0 = wx.StaticText(self, -1, "")
self.rectY0 = wx.StaticText(self, -1, "")
self.rectX1 = wx.StaticText(self, -1, "")
self.rectY1 = wx.StaticText(self, -1, "")
self.message = wx.StaticText(self, -1, "")
# ---------------------------------------------------------------------
# dialog sizers
# ---------------------------------------------------------------------
szr00 = wx.BoxSizer(wx.HORIZONTAL) # overall dialog window
szr10 = wx.BoxSizer(wx.VERTICAL) # right: page display
szr20 = wx.BoxSizer(wx.HORIZONTAL) # top right: page navigation
szr30 = wx.GridBagSizer(7, 7) # left: image handling
# fields for page navigation and info
szr20.Add(self.btn_Next, 0, wx.ALL, 5)
szr20.Add(self.btn_Prev, 0, wx.ALL, 5)
szr20.Add(self.TextToPage, 0, wx.ALL, 5)
szr20.Add(self.statPageMax, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5)
szr20.Add(self.paperform, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5)
# use GridBagSizer for image details
line_span = wx.GBSpan(1, 5) # use for item taking full line
# Image details header
t = wx.StaticText(self, -1, "Image Details", defPos, defSiz, wx.ALIGN_CENTER)
t.SetBackgroundColour("STEEL BLUE")
t.SetForegroundColour("WHITE")
szr30.Add(t, (0, 0), line_span, wx.EXPAND) # overall header
szr30.Add(
wx.StaticLine(self, -1, defPos, defSiz, wx.LI_HORIZONTAL),
(1, 0),
line_span,
wx.EXPAND,
)
# bbox header and fields
szr30.Add(self.btn_next_image, (2, 0), (1, 1))
szr30.Add(self.img_count, (2, 1), (1, 1), wx.ALIGN_LEFT)
szr30.Add(wx.StaticText(self, -1, "Name:"), (3, 0), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(self.imgName, (3, 1), (1, 1), wx.ALIGN_LEFT)
szr30.Add(wx.StaticText(self, -1, "Rotation:"), (4, 0), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(self.imgRotation, (4, 1), (1, 1), wx.ALIGN_LEFT)
szr30.Add(
wx.StaticText(self, -1, "[wx.Rect] Left:"),
(5, 0),
(1, 1),
wx.ALIGN_RIGHT,
)
szr30.Add(self.bboxLeft, (5, 1), (1, 1), wx.ALIGN_LEFT)
szr30.Add(wx.StaticText(self, -1, "Top:"), (5, 2), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(self.bboxTop, (5, 3), (1, 1), wx.ALIGN_LEFT)
szr30.Add(wx.StaticText(self, -1, "Width:"), (6, 0), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(self.bboxWidth, (6, 1), (1, 1), wx.ALIGN_LEFT)
szr30.Add(wx.StaticText(self, -1, "Height:"), (6, 2), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(self.bboxHeight, (6, 3), (1, 1), wx.ALIGN_LEFT)
szr30.Add(
wx.StaticLine(self, -1, defPos, defSiz, wx.LI_HORIZONTAL),
(7, 0),
line_span,
wx.EXPAND,
)
# display of fitz.Rect
szr30.Add(
wx.StaticText(self, -1, "[fitz.Rect] x0:"),
(8, 0),
(1, 1),
wx.ALIGN_RIGHT,
)
szr30.Add(self.rectX0, (8, 1), (1, 1), wx.ALIGN_LEFT)
szr30.Add(wx.StaticText(self, -1, "y0:"), (8, 2), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(self.rectY0, (8, 3), (1, 1), wx.ALIGN_LEFT)
szr30.Add(wx.StaticText(self, -1, "x1:"), (9, 0), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(self.rectX1, (9, 1), (1, 1), wx.ALIGN_LEFT)
szr30.Add(wx.StaticText(self, -1, "y1:"), (9, 2), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(self.rectY1, (9, 3), (1, 1), wx.ALIGN_LEFT)
szr30.Add(
wx.StaticLine(self, -1, defPos, defSiz, wx.LI_HORIZONTAL),
(10, 0),
line_span,
wx.EXPAND,
)
szr30.Add(self.message, (11, 0), line_span, wx.ALIGN_LEFT)
# buttons
szr30.Add(
wx.StaticLine(self, -1, defPos, defSiz, wx.LI_HORIZONTAL),
(14, 0),
line_span,
wx.EXPAND,
)
szr30.Add(self.btn_Update, (15, 0), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(self.t_Update, (15, 1), (1, 1))
szr30.Add(self.btn_Refresh, (16, 0), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(
wx.StaticText(self, -1, "Try to make modifyable"),
(16, 1),
line_span,
wx.EXPAND,
)
szr30.Add(self.btn_NewImg, (17, 0), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(
wx.StaticText(self, -1, "Insert image from file"),
(17, 1),
line_span,
wx.EXPAND,
)
szr30.Add(self.btn_Remove, (18, 0), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(
wx.StaticText(self, -1, ">> CAUTION - no undo <<"),
(18, 1),
line_span,
wx.EXPAND,
)
szr30.Add(self.btn_Save, (19, 0), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(self.t_Save, (19, 1), (1, 4))
szr30.Add(self.btn_Quit, (20, 0), (1, 1), wx.ALIGN_RIGHT)
szr30.Add(
wx.StaticLine(self, -1, defPos, defSiz, wx.LI_HORIZONTAL),
(21, 0),
line_span,
wx.EXPAND,
)
szr10.Add(szr20, 0, wx.EXPAND, 5)
szr10.Add(self.PDFimage, 0, wx.ALL, 5)
szr00.Add(szr30, 0, wx.ALL, 5)
szr00.Add(
wx.StaticLine(self, -1, defPos, defSiz, wx.LI_VERTICAL), 0, wx.EXPAND, 5
)
szr00.Add(szr10, 0, wx.EXPAND, 5)
szr00.Fit(self)
self.SetSizer(szr00)
self.Layout()
self.Centre(wx.BOTH)
# Bind dialog items to event handlers
self.btn_Save.Bind(wx.EVT_BUTTON, self.on_save_file)
self.btn_next_image.Bind(wx.EVT_BUTTON, self.on_next_image)
self.btn_Update.Bind(wx.EVT_BUTTON, self.on_update_image)
self.btn_Remove.Bind(wx.EVT_BUTTON, self.on_remove_image)
self.btn_Refresh.Bind(wx.EVT_BUTTON, self.on_refresh_image)
self.btn_Next.Bind(wx.EVT_BUTTON, self.on_next_page)
self.btn_Prev.Bind(wx.EVT_BUTTON, self.on_previous_page)
self.btn_NewImg.Bind(wx.EVT_FILEPICKER_CHANGED, self.on_new_image)
self.TextToPage.Bind(wx.EVT_TEXT_ENTER, self.on_goto_page)
self.PDFimage.Bind(wx.EVT_ENTER_WINDOW, self.on_enter_window)
self.PDFimage.Bind(wx.EVT_MOUSEWHEEL, self.on_mouse_wheel)
self.PDFimage.Bind(wx.EVT_MOTION, self.on_move_mouse)
self.PDFimage.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
self.PDFimage.Bind(wx.EVT_LEFT_UP, self.on_left_up)
self.bboxHeight.Bind(wx.EVT_SPINCTRL, self.on_image_changed)
self.bboxLeft.Bind(wx.EVT_SPINCTRL, self.on_image_changed)
self.bboxTop.Bind(wx.EVT_SPINCTRL, self.on_image_changed)
self.bboxWidth.Bind(wx.EVT_SPINCTRL, self.on_image_changed)
self.imgRotation.Bind(wx.EVT_CHOICE, self.on_rot_changed)
self.btn_Update.Disable()
self.btn_Remove.Disable()
self.btn_Refresh.Disable()
self.btn_Save.Disable()
self.imgName.Disable()
self.new_image(1)
def __del__(self):
pass
# -------------------------------------------------------------------------
# Button handlers and other functions
# -------------------------------------------------------------------------
def on_enter_window(self, evt):
evt.Skip()
return
def on_move_mouse(self, evt):
if not self.can_change_image:
evt.Skip()
return
pos = evt.GetPosition()
in_rect = self.img_rect.Contains(pos) # cursor in image rect
in_brect = self.img_bottom_rect.Contains(pos) # in bottom-right corner
self.PDFimage.SetCursor(cur_norm) # standard cursor
if in_brect: # cursor if in br corner
self.PDFimage.SetCursor(cur_cross)
elif in_rect: # cursor if in a rect
self.PDFimage.SetCursor(cur_hand)
if self.resize_rect: # resizing bbox?
self.PDFimage.SetCursor(cur_cross) # adjust cursor
if evt.LeftIsDown(): # mouse pressed? go!
r = self.img_rect # resizing rectangle
w = pos.x - r.x # new width
h = pos.y - r.y # new height
nr = wx.Rect(r.x, r.y, w, h) # new retangle
if 2 * self.sense <= min(w, h): # large enough?
self.bboxHeight.SetValue(h)
self.bboxWidth.SetValue(w)
self.redraw_bitmap()
self.draw_rect(nr.x, nr.y, w, h, "RED")
self.img_rect = nr
self.img_bottom_rect = self.bottom_rect_from_rect(nr)
self.refresh_fitz_labels(wx.Rect(nr.x, nr.y, w, h))
evt.Skip()
return
if in_rect: # still here and inside a rect?
self.PDFimage.SetCursor(cur_hand) # adjust cursor
if evt.LeftIsDown():
r = self.img_rect # this is the rectangle
x = pos.x - self.dragstart_x # new left ...
y = pos.y - self.dragstart_y # ... and top values
w = r.width # shape does ...
h = r.height # ... not change
nr = wx.Rect(x, y, w, h) # new rectangle
self.refresh_fitz_labels(nr) # update fitz.Rect display
self.bboxLeft.SetValue(x) # new screen value
self.bboxTop.SetValue(y) # new screen value
self.redraw_bitmap()
self.draw_rect(x, y, w, h, "RED")
self.img_rect = nr
self.img_bottom_rect = self.bottom_rect_from_rect(nr)
evt.Skip()
return
def bottom_rect_from_rect(self, wxrect):
p = wxrect.BottomRight
br = wx.Rect(p.x - self.sense, p.y - self.sense, 2 * self.sense, 2 * self.sense)
return br
def refresh_fitz_labels(self, wxrect):
"""Update screen fitz.Rect with wx.Rect values."""
nfr = self.wxRect_to_Rect(wxrect)
self.rectX0.Label = "%g" % nfr.x0
self.rectY0.Label = "%g" % nfr.y0
self.rectX1.Label = "%g" % nfr.x1
self.rectY1.Label = "%g" % nfr.y1
def on_rot_changed(self, evt):
idx = evt.GetInt()
self.rotation = int(degrees[idx])
self.btn_Update.Enable()
evt.Skip()
return
def on_mouse_wheel(self, evt):
# process wheel as paging operations
d = evt.GetWheelRotation() # int indicating direction
if d < 0:
self.on_next_page(evt)
elif d > 0:
self.on_previous_page(evt)
return
def on_next_page(self, event): # means: page forward
page = getint(self.TextToPage.Value) + 1 # current page + 1
page = min(page, self.doc.page_count) # cannot go beyond last page
self.TextToPage.ChangeValue(str(page)) # put target page# in screen
self.new_image(page) # refresh the layout
event.Skip()
def on_previous_page(self, event): # means: page back
page = getint(self.TextToPage.Value) - 1 # current page - 1
page = max(page, 1) # cannot go before page 1
self.TextToPage.ChangeValue(str(page)) # put target page# in screen
self.new_image(page)
event.Skip()
def on_goto_page(self, event): # means: go to page number
page = getint(self.TextToPage.Value) # get page# from screen
page = min(page, len(self.doc)) # cannot go beyond last page
page = max(page, 1) # cannot go before page 1
self.TextToPage.ChangeValue(str(page)) # make sure it's on the screen
self.new_image(page)
event.Skip()
def on_update_image(self, evt):
"""Perform PDF update of changed image position.
Compute new rectangle from displayed values, compute new matrix,
update resp. /Contents object with computed matrix, reload
page and refresh image.
"""
pno = getint(self.TextToPage.Value) - 1 # page number
page = self.doc[pno] # load page
r = wx.Rect( # get wx:Rect from screen
self.bboxLeft.Value,
self.bboxTop.Value,
self.bboxWidth.Value,
self.bboxHeight.Value,
)
new_rect = self.wxRect_to_Rect(r) # compute fitz.Rect
rot = self.rotation # rotation value
key = self.imgName.Value # image reference name
self.last_image = list(self.page_images.keys()).index(key)
d = self.page_images[key] # image parameters
xref = d["xref"]
start = d["start"]
stop = d["stop"]
rect = d["bbox"] # get width / height from previous rectangle
factor = max(rect.width, rect.height)
fw = rect.width / factor # calculate width / height ratio
fh = rect.height / factor
if d["deg"] in (90, 270): # exchange values
fw, fh = fh, fw
tar_rect = new_rect * ~page.transformation_matrix
matrix = calc_matrix(fw, fh, tar_rect, rot)
# we have the matrix, now read /Contents stream
cont = bytearray(self.doc.xref_stream(xref)) # modifyable version!
new_mat = b"%g %g %g %g %g %g cm" % tuple(matrix)
cont[start:stop] = new_mat # put in new PDF matrix
self.doc.update_stream(xref, bytes(cont)) # rewrite stream
page = self.doc.reload_page(page) # reload the page
self.last_pno = -1
self.new_image(pno + 1) # make new bitmap from page
self.btn_Save.Enable()
self.t_Save.Label = "Save changed file."
evt.Skip()
return
def on_refresh_image(self, evt):
"""Remove image invocation command and insert new one.
This should make the image position changeable in corner cases.
A valid image rectangle must however exist to be successful!
"""
pno = getint(self.TextToPage.Value) - 1
page = self.doc[pno]
key = self.imgName.Value # name of image
d = self.page_images[key]
xref = d["xref"]
begin = d["begin"]
end = d["end"]
rect = d["bbox"] # get width / height from previous rectangle
factor = max(rect.width, rect.height)
fw = rect.width / factor # calculate width / height ratio
fh = rect.height / factor
tar_rect = rect * ~page.transformation_matrix
matrix = calc_matrix(fw, fh, tar_rect, 0)
# we have the matrix, now read /Contents stream
cont = bytearray(self.doc.xref_stream(xref)) # modifyable version!
cont[begin:end] = b"" # remove old display command
self.doc.update_stream(xref, bytes(cont))
new_cmd = b"\nq\n%g %g %g %g %g %g cm " % tuple(matrix)
new_cmd += b"/%s Do\nQ\n" % key.encode()
if not page.is_wrapped:
page.wrap_contents()
fitz.TOOLS._insert_contents(page, new_cmd, 1)
page.clean_contents()
page = self.doc.reload_page(page)
self.last_pno = -1
self.new_image(pno + 1)
self.btn_Save.Enable()
self.t_Save.Label = "Save changed file."
evt.Skip()
return
def on_remove_image(self, evt):
"""Delete image from page completely.
This works by removing substring '/name Do' from
resp. /Contents object and refresh image.
"""
pno = getint(self.TextToPage.Value) - 1
page = self.doc[pno]
key = self.imgName.Value # name of image
d = self.page_images[key]
xref = d["xref"]
begin = d["begin"]
end = d["end"]
cont = bytearray(self.doc.xref_stream(xref)) # modifyable version!
cont[begin:end] = b""
self.doc.update_stream(xref, bytes(cont))
page.clean_contents()
page = self.doc.reload_page(page)
self.last_pno = -1
self.last_image = None
self.new_image(pno + 1)
self.btn_Save.Enable()
self.t_Save.Label = "Save changed file."
evt.Skip()
return
def on_image_changed(self, evt):
# change image rectangle
if not self.can_change_image:
evt.Skip()
return
r = wx.Rect(
self.bboxLeft.Value,
self.bboxTop.Value,
self.bboxWidth.Value,
self.bboxHeight.Value,
)
self.refresh_fitz_labels(r)
self.redraw_bitmap()
self.draw_rect(r.x, r.y, r.width, r.height, "RED")
self.btn_Update.Enable()
evt.Skip()
return
def on_save_file(self, evt):
indir, infile = os.path.split(self.doc.name)
odir = indir
ofile = infile
if self.doc.needs_pass or not self.doc.can_save_incrementally():
ofile = ""
sdlg = wx.FileDialog(
self, "Specify Output", odir, ofile, "PDF files (*.pdf)|*.pdf", wx.FD_SAVE
)
if sdlg.ShowModal() == wx.ID_CANCEL:
evt.Skip()
return
outfile = sdlg.GetPath()
if self.doc.needs_pass or not self.doc.can_save_incrementally():
title = "Repaired / decrypted PDF requires new output file"
while outfile == self.doc.name:
sdlg = wx.FileDialog(
self, title, odir, "", "PDF files (*.pdf)|*.pdf", wx.FD_SAVE
)
if sdlg.ShowModal() == wx.ID_CANCEL:
evt.Skip()
return
outfile = sdlg.GetPath()
if outfile == self.doc.name:
self.doc.saveIncr() # equal: update input file
else:
self.doc.save(outfile, garbage=3, deflate=True)
sdlg.Destroy()
self.btn_Save.Disable()
self.t_Save.Label = ""
self.btn_Update.Disable()
evt.Skip()
return
def on_new_image(self, evt):
path = evt.GetPath()
pno = getint(self.TextToPage.Value) - 1
page = self.doc[pno]
w = min(page.rect.width / 2, 200)
h = min(page.rect.height / 2, 200)
r = fitz.Rect(0, 0, w, h)
try:
page.insert_image(r, filename=path)
except Exception as exc:
self.message.Label = "Unsupported image file"
print(str(exc), r)
evt.Skip()
return
page = self.doc.reload_page(page)
self.last_image = -1
self.last_pno = -1
self.btn_Save.Enable()
self.new_image(pno + 1)
evt.Skip()
return
def on_left_up(self, evt):
self.dragging_img = False
self.dragstart_x = -1
self.dragstart_y = -1
self.resize_rect = False
self.PDFimage.SetCursor(cur_norm)
evt.Skip()
return
def on_left_down(self, evt):
if not self.can_change_image:
evt.Skip()
return
pos = evt.GetPosition()
in_rect = self.img_rect.Contains(pos)
in_brect = self.img_bottom_rect.Contains(pos)
if in_brect:
self.resize_rect = True
self.current_idx = in_brect
self.btn_Update.Enable()
evt.Skip()
return
if in_rect:
self.dragging_img = True # we are about to drag
r = self.img_rect # the wx.Rect we will be dragging
self.dragstart_x = pos.x - r.x # delta to left
self.dragstart_y = pos.y - r.y # delta to top
if not in_rect:
self.current_idx = False
evt.Skip()
return
self.current_idx = in_rect
r = self.img_rect
self.bboxLeft.SetValue(r.x)
self.bboxTop.SetValue(r.y)
self.bboxHeight.SetValue(r.Height)
self.bboxWidth.SetValue(r.Width)
self.refresh_fitz_labels(r)
self.draw_rect(r.x, r.y, r.width, r.height, "RED")
self.btn_Update.Enable()
evt.Skip()
return
def enable_update(self):
self.can_change_image = True
self.bboxLeft.Enable()
self.bboxTop.Enable()
self.bboxHeight.Enable()
self.bboxWidth.Enable()
self.imgRotation.Enable()
self.t_Update.Label = ""
self.btn_Refresh.Disable()
def disable_update(self):
self.can_change_image = False
self.bboxLeft.Disable()
self.bboxTop.Disable()
self.bboxHeight.Disable()
self.bboxWidth.Disable()
self.imgRotation.Disable()
self.btn_Update.Disable()
self.btn_Refresh.Enable()
def redraw_bitmap(self):
"""Refresh bitmap image."""
w = self.bitmap.Size[0]
h = self.bitmap.Size[1]
x = y = 0
rect = wx.Rect(x, y, w, h)
bm = self.bitmap.GetSubBitmap(rect)
dc = wx.ClientDC(self.PDFimage) # make a device control out of img
dc.DrawBitmap(bm, x, y) # refresh bitmap before draw
return
def draw_rect(self, x, y, w, h, c):
dc = wx.ClientDC(self.PDFimage)
dc.SetPen(wx.Pen(c, width=1))
dc.SetBrush(wx.Brush(c, style=wx.BRUSHSTYLE_TRANSPARENT))
dc.DrawRectangle(x, y, w, h)
return
def on_next_image(self, evt):
self.redraw_bitmap()
img = self.imgName.Value
keys = list(self.page_images.keys())
idx = keys.index(img) + 1
self.fill_img_details(idx % len(keys))
self.btn_Update.Disable()
evt.Skip()
return
def clear_img_details(self):
self.imgName.Value = ""
self.img_count.Label = ""