vkapoor commited on
Commit
409ffbd
·
1 Parent(s): afe5647
Files changed (1) hide show
  1. app.py +36 -21
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, evt: SelectData):
63
- # Extract display coordinates
64
  x_click, y_click = evt.index
65
- # Image is scaled to fixed width of 1024px
66
- canvas_w = 1024
67
- page = self.doc[self.current_page]
68
  pdf_w, pdf_h = page.rect.width, page.rect.height
69
- scale = canvas_w / pdf_w
70
- img_w, img_h = canvas_w, int(pdf_h * scale)
71
- # No centering offsets (full-width image)
72
- x_adj, y_adj = x_click, y_click
73
- # Check bounds
74
- if not (0 <= x_adj <= img_w and 0 <= y_adj <= img_h):
75
- print("⚠️ Click outside image bounds.")
76
  return None
77
- # Convert to PDF coords
78
- x_pdf = x_adj / scale
79
- y_pdf = pdf_h - (y_adj / scale)
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
- print("⚠️ No clickable link matched the location.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 --------