diff --git a/src/main.py b/src/main.py index 0e5e7a0..863e8e3 100644 --- a/src/main.py +++ b/src/main.py @@ -7,29 +7,46 @@ from PIL import Image, ImageTk CURRENT_PAGE = 0 -def main(): + +def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("file") args = parser.parse_args() doc: pymupdf.Document with pymupdf.open(args.file) as doc: + toc = doc.get_toc() + + def toc_entry_for_page(page_num: int) -> str: + last_title = "" + for lvl, t, p in toc: + if (p - 1) > page_num: + return last_title + if (p - 1) == page_num: + return t + last_title = t + root = tkinter.Tk() frm = ttk.Frame(root, padding=10) frm.grid() - label = ttk.Label(frm) - label.grid(column=1, row=0) + page_title = ttk.Label(frm) + page_title.grid(column=1, row=0) - def load_page(i: int): - page = doc[i] + page_view = ttk.Label(frm) + page_view.grid(column=1, row=1) + + def load_page(page_num: int): + page = doc[page_num] pix = page.get_pixmap() mode = "RGBA" if pix.alpha else "RGB" img = Image.frombytes(mode, [pix.width, pix.height], pix.samples) img = ImageTk.PhotoImage(img) - label.configure(image=img) - label.image = img + page_view.configure(image=img) + page_view.image = img + + page_title.configure(text=toc_entry_for_page(page_num)) def next_page(): global CURRENT_PAGE @@ -41,8 +58,8 @@ def main(): CURRENT_PAGE -= 1 load_page(CURRENT_PAGE) - ttk.Button(frm, text="Previous", command=previous_page).grid(column=0, row=0) - ttk.Button(frm, text="Next", command=next_page).grid(column=2, row=0) + ttk.Button(frm, text="Previous", command=previous_page).grid(column=0, row=1) + ttk.Button(frm, text="Next", command=next_page).grid(column=2, row=1) load_page(CURRENT_PAGE)