Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / pdf / AGENTS.md
| 1 | # AGENTS.md — pdf/ (':pdf') |
| 2 | |
| 3 | > Scoped supplement. Global rules in root `AGENTS.md` apply. Stay inside `pdf/` + allowed shared modules. Do not root-scan. |
| 4 | |
| 5 | _Install: ./install pdf (dev by default)._ |
| 6 | - Gradle: :pdf / dir: pdf/ |
| 7 | - Package roots present: ui, data, platform, util |
| 8 | - Entry files: MainActivity.kt, Route.kt, Navigation.kt (Route host; see Navigation.kt) |
| 9 | - Deps: :library:image, :library:ocr |
| 10 | - Metadata: metadata_data/pdf.md (present) |
| 11 | - Rust: yes / screenshotTest: yes |
| 12 | |
| 13 | ## Overview |
| 14 | **Purpose:** A simple open-source PDF viewer with capture and cut-and-glue assembly. No network permission at all — 100% offline. |
| 15 | **Key features:** |
| 16 | - Full viewer: text search, text selection, form fields, outlines, zoom state, print via share menu; several documents open as separate Recents entries |
| 17 | - Capture mode: photograph pages with auto-framing, crop, and compose them into a PDF; cut-and-glue screen for assembling pages |
| 18 | - Native Rust PDF engine (`PdfNative`) for parsing, rendering, and export |
| 19 | **Architecture / data-flow:** `MainActivity` hosts `Navigation.kt` (`Route.Home/Viewer/Capture/CapturePage/Crop/CutGlue`) over a single `platform/PdfViewModel` + `platform/PdfStateStore`, with `SafePdf*` platform wrappers around the Rust engine for document access, page rendering, and edits; capture flows through `data/CapturedImage` → `AutoFrameDetector` → `ComposePdfDocument` → `PdfExporter`. `onCreateBeforeContent` only captures the launch intent's data URI; `Content()` picks viewer vs capture vs cut-glue vs picker. |
| 20 | **Entry points + reading order:** `MainActivity.kt` + `Route.kt` + `Navigation.kt` (`InitialScreen` → viewer/capture/cut-glue) → `ui/SafePdfViewerScreen.kt` → `platform/PdfViewModel.kt` → `platform/SafePdfDocument.kt` → `platform/SafePdfParser.kt`. |
| 21 | **Gotchas:** |
| 22 | - Started-with-intent launches `finish()` on back instead of returning to the picker; in-app launches reset to the picker |
| 23 | - Document, frame, and edit code (`FrameGeometry`, `FrameImageOps`, `SafePdfDocumentEdits`, `SafePdfPrimitives`, the `SafePdf*` wrappers) is in `platform/`; `ui/` is `SafePdf*` + capture/crop/cut-glue screens; there is no `domain/` root |
| 24 | - `util/` holds only `PdfNative` (the Rust engine bridge) — don't add more files there; the capture model is in `data/` |
| 25 | - Search-index prewarm at open is page-count gated (`SafePdfDocument.MAX_PREWARM_PAGES`, 256): opening a document never eagerly interprets the whole book; larger documents build the same index lazily on first search |
| 26 | - Adobe YCCK (APP14 transform 2) CMYK JPEGs reach the Rust decoder with all four samples in inverted-ink polarity; the CMYK32 arm inverts all four before converting (transform 0 samples are already true ink) |
| 27 | - Wire format 2 carries raw rasters as little-endian ARGB *words* (`0xAARRGGBB`); Kotlin must read them as words (int-based Bitmap APIs), never `Bitmap.copyPixelsFromBuffer` — on a real device that reads ARGB_8888 memory as R,G,B,A bytes and swaps R/B (captured scans are FlateDecode, so they all hit this arm; JPEG pages pass through untouched), while Robolectric reads the buffer the other way and cannot catch the swap. The device round-trip is pinned in `PdfExporterDeviceTest.capturedPdfReopensWithFaithfulColours` |
| 28 | - Wire format is v11: the Image arm carries a per-image `/Interpolate` byte decided in Rust (`image_should_interpolate` — bilevel art is never smoothed, contone and explicit `/Interpolate true` are), and Kotlin applies it as `isFilterBitmap`; bumping the wire version without writing the byte desyncs every later primitive |
| 29 | - Outline drawer intent is owned by `ui/PdfOutlineState` (saveable, explicit user action only) and drives the Material drawer state; width changes are layout only and never open or close it, and the wide layout's side pane is derived from the width class, not from drawer state |
| 30 | - Viewer text search is declared through `SafePdfViewerChrome`'s `AppScaffold(search = ScaffoldSearch(...))` while `state.searching`; the case-sensitive (`Aa`) toggle and match navigation stay in the scaffold actions. Every `Route` leaf has a deterministic capture in `src/screenshotTest` (`<n>-<route-kebab>` in `MetadataPreviews`) |
| 31 | - Opening hands native a file descriptor (`SafePdfDocument.acquireDocumentFd` -> `PdfNative.openDocumentFd`): Rust dups and mmaps it, so no whole-file copy exists in the Kotlin heap or across JNI and file size alone never refuses an open; that one attempt classifies Opened / NeedsPassword / Failed (incl. not-a-PDF). Providers without a descriptor fall back to spooling the stream to a cache file in chunks (O(chunk) memory) and opening its descriptor. Cut-and-glue appends take the same descriptor path (`appendPdfFd`). The byte[] native API remains for small in-memory documents and keeps its own native size guard. Redaction presence is probed lazily once editing starts, not at open |
| 32 | |
| 33 | ## Key files |
| 34 | ### Root — entry + navigation |
| 35 | - com/vayunmathur/pdf/MainActivity.kt — host, intent → Route.Viewer routing |
| 36 | - com/vayunmathur/pdf/Route.kt — Home/Viewer(uri)/Capture/CapturePage(index)/Crop(index)/CutGlue |
| 37 | - com/vayunmathur/pdf/Navigation.kt — MainNavigation host |
| 38 | ### data/ — capture model |
| 39 | - com/vayunmathur/pdf/data/CapturedImage.kt |
| 40 | ### ui/ — viewer chrome + state |
| 41 | - com/vayunmathur/pdf/ui/SafePdfViewerScreen.kt |
| 42 | - com/vayunmathur/pdf/ui/SafePdfViewerState.kt |
| 43 | - com/vayunmathur/pdf/ui/SafePdfViewerModels.kt |
| 44 | - com/vayunmathur/pdf/ui/SafePdfViewerChrome.kt |
| 45 | - com/vayunmathur/pdf/ui/SafePdfViewerLoad.kt |
| 46 | - com/vayunmathur/pdf/ui/SafePdfViewerDialogs.kt |
| 47 | - com/vayunmathur/pdf/ui/SafePdfPageViewer.kt |
| 48 | - com/vayunmathur/pdf/ui/SafePdfPageCanvas.kt |
| 49 | - com/vayunmathur/pdf/ui/SafePdfPageItem.kt |
| 50 | - com/vayunmathur/pdf/ui/PdfViewerWideLayout.kt |
| 51 | - com/vayunmathur/pdf/ui/PdfOutlineDrawer.kt |
| 52 | - com/vayunmathur/pdf/ui/PdfOutlineState.kt |
| 53 | - com/vayunmathur/pdf/ui/SafePdfTextSelection.kt |
| 54 | - com/vayunmathur/pdf/ui/SafePdfFormFields.kt |
| 55 | ### ui/ — safe-draw rendering kit |
| 56 | - com/vayunmathur/pdf/ui/SafePdfDrawContext.kt |
| 57 | - com/vayunmathur/pdf/ui/SafePdfDrawState.kt |
| 58 | - com/vayunmathur/pdf/ui/SafePdfDrawStrokes.kt |
| 59 | - com/vayunmathur/pdf/ui/SafePdfDrawFills.kt |
| 60 | - com/vayunmathur/pdf/ui/SafePdfDrawText.kt |
| 61 | - com/vayunmathur/pdf/ui/SafePdfDrawImages.kt |
| 62 | - com/vayunmathur/pdf/ui/SafePdfBlendModes.kt |
| 63 | - com/vayunmathur/pdf/ui/SafePdfSelectionGlyphs.kt |
| 64 | - com/vayunmathur/pdf/ui/SafePdfZoomState.kt |
| 65 | ### ui/ — edit overlays + tooling |
| 66 | - com/vayunmathur/pdf/ui/SafePdfEditOverlay.kt |
| 67 | - com/vayunmathur/pdf/ui/SafePdfEditPreview.kt |
| 68 | - com/vayunmathur/pdf/ui/SafePdfEditToolbar.kt |
| 69 | - com/vayunmathur/pdf/ui/SafePdfNonEditOverlay.kt |
| 70 | - com/vayunmathur/pdf/ui/SafePdfToolMenus.kt |
| 71 | - com/vayunmathur/pdf/ui/SafePdfStyleDialog.kt |
| 72 | - com/vayunmathur/pdf/ui/SafePdfLaunchers.kt |
| 73 | ### ui/ — capture + assembly screens |
| 74 | - com/vayunmathur/pdf/ui/CapturePdfScreen.kt |
| 75 | - com/vayunmathur/pdf/ui/CaptureContent.kt — stateless no-camera-permission capture landing (screenshot slice) |
| 76 | - com/vayunmathur/pdf/ui/CropScreen.kt |
| 77 | - com/vayunmathur/pdf/ui/CropScreenSection.kt |
| 78 | - com/vayunmathur/pdf/ui/CutGlueScreen.kt |
| 79 | - com/vayunmathur/pdf/ui/components/CameraPreview.kt |
| 80 | - com/vayunmathur/pdf/ui/components/SubcroppedImage.kt |
| 81 | ### platform/ — ViewModel, engine wrappers, capture pipeline |
| 82 | - com/vayunmathur/pdf/platform/PdfViewModel.kt |
| 83 | - com/vayunmathur/pdf/platform/PdfStateStore.kt |
| 84 | - com/vayunmathur/pdf/platform/SafePdfDocument.kt |
| 85 | - com/vayunmathur/pdf/platform/SafePdfDocumentEdits.kt |
| 86 | - com/vayunmathur/pdf/platform/SafePdfParser.kt |
| 87 | - com/vayunmathur/pdf/platform/SafePdfModels.kt |
| 88 | - com/vayunmathur/pdf/platform/SafePdfPrimitives.kt |
| 89 | - com/vayunmathur/pdf/platform/SafePdfImages.kt |
| 90 | - com/vayunmathur/pdf/platform/SafePdfListings.kt |
| 91 | - com/vayunmathur/pdf/platform/AutoFrameDetector.kt |
| 92 | - com/vayunmathur/pdf/platform/FrameGeometry.kt |
| 93 | - com/vayunmathur/pdf/platform/FrameImageOps.kt |
| 94 | - com/vayunmathur/pdf/platform/ComposePdfDocument.kt |
| 95 | - com/vayunmathur/pdf/platform/PdfExporter.kt |
| 96 | ### util/ — Rust engine bridge |
| 97 | - com/vayunmathur/pdf/util/PdfNative.kt — Rust engine bridge |
| 98 | |
| 99 | ## Verify (this module only) |
| 100 | ``` |
| 101 | ./gradlew :pdf:compileDevKotlin |
| 102 | ./gradlew :pdf:lint |
| 103 | ./gradlew :pdf:checkMetadata |
| 104 | ``` |