DeepTutor icon indicating copy to clipboard operation
DeepTutor copied to clipboard

[Feature Request]: Select an area of a PDF page (incl. scanned pages and figures) and ask the reading assistant about it

Open BenjiAvH opened this issue 2 weeks ago • 0 comments

Do you need to file a feature request?

  • [x] I have searched the existing feature request and this feature request is not already filed.
  • [x] I believe this is a legitimate feature request, not just a question or bug.

Feature Request Description

Let the reader draw a rectangle on a PDF page in immersive reading and ask the reading assistant about exactly that area, whether it contains text, a figure, a formula, a table or handwriting.

Today the only way to point the assistant at part of a page is a text selection (viewport.selection). That fails whenever the page has no usable text layer:

  • Scanned PDFs (old exams, photographed worksheets, handwritten solutions): each page is a single raster image, so nothing can be selected. The picture hotspot from #2499 covers the whole page, which only opens the zoom preview.
  • Figures, charts and formulas in slide or textbook PDFs: the assistant receives the whole page (embedded images or the full-page render from page_render.py), but the user cannot say which sub-figure, cell or line the question is about.
  • Small print or handwriting: the full-page render at PAGE_RENDER_DPI = 110 is often too coarse for the vision model to read a single line reliably, while a crop of the same area at higher resolution is legible.

Related Module

Frontend/Web

Use Case

A student revises with a scanned past exam. They want to ask "How do I solve part (b) of this task?" or "What does this table say?". With a scanned PDF they currently have to describe the location in words, take a screenshot outside DeepTutor and upload it in a separate chat, or rely on a separately OCR'd copy. All three lose the reading position and the link to the source page.

Proposed design

Frontend (web/components/reading/)

  • An "area select" mode in the PDF reader (toolbar toggle, so a normal drag or a touch scroll on mobile is not hijacked). In this mode a pointer drag on a PdfPage draws a rectangle. Pointer Events, so mouse, pen and touch work.
  • On release, the same action affordance as a text selection: Ask, Explain. The composer shows a removable chip such as "Area on page 3".
  • The turn sends the area in the existing viewport payload, e.g. viewport.region = {locator, box: [x0, y0, x1, y1]} in page-normalised coordinates (same convention as NormalisedBox in pdf-page-geometry.ts).

Backend

  • _reading_viewport normalises and validates region (locator within unit_count, box clamped to [0,1], minimum size, finite numbers).
  • A region_render_record(material_id, locator, box) next to page_render_record renders only that clip with PyMuPDF (page.get_pixmap(clip=..., dpi=...)), with a DPI chosen so the crop stays within a pixel/byte budget. It is rendered server-side from the stored raw PDF, so the client never uploads pixels and the crop always matches the stored revision.
  • _reading_viewport_image_attachments puts the region crop first and keeps the total under READING_VIEWPORT_MAX_IMAGES.
  • pre_loop_seed tells the model that the user marked an area on locator N and that a crop of it is attached, analogous to the existing full-page render sentence.

Out of scope: OCR of the area into text, EPUB/text-view selection, and persisting regions as annotations. These could follow later.

Acceptance

  • Area select works with mouse and touch, and leaves normal scrolling, text selection and the #2499 picture hotspots unchanged when the mode is off.
  • A turn with a region attaches exactly one crop of that region (correct page, correct rectangle, legible) and the model is told about it. A turn without a region behaves as today.
  • Invalid regions (out of range locator, NaN, zero area, non-PDF material) are dropped server-side without failing the turn.
  • Unit tests for normalisation, the clip render and the attachment order. Frontend tests for the selection geometry.

Additional Context

Related: #2499 (picture hotspots and preview in PDFs), #431 (scanned PDFs in the knowledge base). I'm happy to open a PR for this.

BenjiAvH avatar Nov 16 '26 15:11 BenjiAvH