Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
|
@@ -59,32 +59,47 @@ class PDFBrowser:
|
|
| 59 |
self.current_page -= 1
|
| 60 |
return self.render_page()
|
| 61 |
|
| 62 |
-
def handle_click(self,
|
| 63 |
-
# Extract display coordinates
|
| 64 |
x_click, y_click = evt.index
|
| 65 |
-
|
| 66 |
-
canvas_w = 1024
|
| 67 |
-
page = self.doc[self.current_page]
|
| 68 |
pdf_w, pdf_h = page.rect.width, page.rect.height
|
| 69 |
-
scale =
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
if not
|
| 75 |
-
print("⚠️ Click outside image bounds.")
|
| 76 |
return None
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
pt = fitz.Point(x_pdf, y_pdf)
|
| 81 |
-
# Find matching mp4 link
|
| 82 |
-
for rect, full in self.link_map.get(self.current_page, []):
|
| 83 |
if rect.contains(pt):
|
| 84 |
-
print(f"▶️ Selected video: {full}")
|
| 85 |
-
# Return path for Gradio Video component to display
|
| 86 |
return full
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
return None
|
| 89 |
|
| 90 |
# -------- Gradio Interface --------
|
|
|
|
| 59 |
self.current_page -= 1
|
| 60 |
return self.render_page()
|
| 61 |
|
| 62 |
+
def handle_click(self, evt: SelectData):
|
|
|
|
| 63 |
x_click, y_click = evt.index
|
| 64 |
+
page = self.doc[self.current_page]
|
|
|
|
|
|
|
| 65 |
pdf_w, pdf_h = page.rect.width, page.rect.height
|
| 66 |
+
scale = 1024 / pdf_w
|
| 67 |
+
x_pdf = x_click/scale
|
| 68 |
+
y_pdf = pdf_h - (y_click/scale)
|
| 69 |
+
pt = fitz.Point(x_pdf, y_pdf)
|
| 70 |
+
links = self.link_map.get(self.current_page, [])
|
| 71 |
+
if not links:
|
|
|
|
| 72 |
return None
|
| 73 |
+
|
| 74 |
+
# 1) try exact hit
|
| 75 |
+
for rect, full in links:
|
|
|
|
|
|
|
|
|
|
| 76 |
if rect.contains(pt):
|
|
|
|
|
|
|
| 77 |
return full
|
| 78 |
+
|
| 79 |
+
# 2) fallback: pick the nearest link‐center
|
| 80 |
+
# compute (distance, video_path) pairs
|
| 81 |
+
distances = []
|
| 82 |
+
for rect, full in links:
|
| 83 |
+
cx = (rect.x0 + rect.x1) / 2
|
| 84 |
+
cy = (rect.y0 + rect.y1) / 2
|
| 85 |
+
center = fitz.Point(cx, cy)
|
| 86 |
+
distances.append((pt.distance(center), full))
|
| 87 |
+
|
| 88 |
+
# sort by increasing distance
|
| 89 |
+
distances.sort(key=lambda x: x[0])
|
| 90 |
+
best_dist, best_full = distances[0]
|
| 91 |
+
|
| 92 |
+
# set a PDF‐unit threshold: e.g. half the diagonal of the link rect,
|
| 93 |
+
# or a fixed value like 50 pts.
|
| 94 |
+
# Here we take half the diagonal of the first rect as a guide:
|
| 95 |
+
sample_rect = links[0][0]
|
| 96 |
+
diag = ((sample_rect.x1 - sample_rect.x0)**2 + (sample_rect.y1 - sample_rect.y0)**2)**0.5
|
| 97 |
+
max_dist = diag * 0.5
|
| 98 |
+
|
| 99 |
+
if best_dist <= max_dist:
|
| 100 |
+
return best_full
|
| 101 |
+
|
| 102 |
+
# still nothing close enough
|
| 103 |
return None
|
| 104 |
|
| 105 |
# -------- Gradio Interface --------
|