Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions app/note/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
type PickedImageResult,
} from '../../src/services/imageInsertStorage';
import { exportNotebookAsPdf } from '../../src/services/exportService';
import { resolvePdfRenderUri } from '../../src/services/pdfUtilsModule';
import { recordSuccessfulNoteSave } from '../../src/services/lifecycleService';
import { textBoxId, insertedElementId } from '../../src/utils/id';
import { spacing } from '../../src/theme/spacing';
Expand Down Expand Up @@ -147,6 +148,7 @@ export default function NoteScreen() {
const lastPenToolRef = useRef<'pen' | 'highlighter' | 'crayon' | 'calligraphy'>('pen');

const [metadata, setMetadata] = useState<NoteMetadata | null>(null);
const [pdfRenderUri, setPdfRenderUri] = useState<string>();
const [enginePages, setEnginePages] = useState<NotebookPage[]>([]);
const [overlayMap, setOverlayMap] = useState<Map<string, PageOverlayState>>(
() => new Map(),
Expand Down Expand Up @@ -309,7 +311,11 @@ export default function NoteScreen() {
try {
const [meta, body] = await Promise.all([getNote(id), readNoteBody(id)]);
if (cancelled) return;
if (meta) setMetadata(meta);
const sourceUri = meta?.pdfUri ?? undefined;
const renderUri = await resolvePdfRenderUri(sourceUri).catch(() => sourceUri);
if (cancelled) return;
setPdfRenderUri(renderUri);
setMetadata(meta);
if (body.kind === 'unreadable') {
// A body file exists but cannot be read. Never fall back to an empty
// notebook here: autosave would overwrite the file and destroy
Expand Down Expand Up @@ -905,7 +911,7 @@ export default function NoteScreen() {
pageHeight={PAGE_HEIGHT}
toolState={toolState}
backgroundType={metadata?.backgroundType ?? 'plain'}
pdfBackgroundBaseUri={metadata?.pdfUri ?? undefined}
pdfBackgroundBaseUri={pdfRenderUri}
fingerDrawingEnabled={fingerDrawingEnabled}
onReady={handleCanvasReady}
onDrawingChange={handleDrawingChange}
Expand Down
4 changes: 4 additions & 0 deletions ios/OpenNotes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
7D3A801A2E1A4CC89B1B09C1 /* ICloudBackupModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D3A80182E1A4CC89B1B09C1 /* ICloudBackupModule.swift */; };
7D3A801B2E1A4CC89B1B09C1 /* ICloudBackupModule.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D3A80192E1A4CC89B1B09C1 /* ICloudBackupModule.m */; };
7D3A801D2E1A4CC89B1B09C1 /* ImageThumbnailEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D3A801C2E1A4CC89B1B09C1 /* ImageThumbnailEncoder.swift */; };
7D3A80322E1A4CC89B1B09C1 /* PDFRenderCopy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D3A80312E1A4CC89B1B09C1 /* PDFRenderCopy.swift */; };
9C2FD7C620026BB779139CCD /* libPods-OpenNotes.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A7A85B485F4321855B927960 /* libPods-OpenNotes.a */; };
BB2F792D24A3F905000567C9 /* Expo.plist in Resources */ = {isa = PBXBuildFile; fileRef = BB2F792C24A3F905000567C9 /* Expo.plist */; };
F11748422D0307B40044C1D9 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F11748412D0307B40044C1D9 /* AppDelegate.swift */; };
Expand All @@ -31,6 +32,7 @@
7D3A80182E1A4CC89B1B09C1 /* ICloudBackupModule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ICloudBackupModule.swift; path = OpenNotes/ICloudBackupModule.swift; sourceTree = "<group>"; };
7D3A80192E1A4CC89B1B09C1 /* ICloudBackupModule.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = ICloudBackupModule.m; path = OpenNotes/ICloudBackupModule.m; sourceTree = "<group>"; };
7D3A801C2E1A4CC89B1B09C1 /* ImageThumbnailEncoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ImageThumbnailEncoder.swift; path = OpenNotes/ImageThumbnailEncoder.swift; sourceTree = "<group>"; };
7D3A80312E1A4CC89B1B09C1 /* PDFRenderCopy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = PDFRenderCopy.swift; path = OpenNotes/PDFRenderCopy.swift; sourceTree = "<group>"; };
9B82DFB67047998EABD93C92 /* ExpoModulesProvider.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ExpoModulesProvider.swift; path = "Pods/Target Support Files/Pods-OpenNotes/ExpoModulesProvider.swift"; sourceTree = "<group>"; };
A7A85B485F4321855B927960 /* libPods-OpenNotes.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-OpenNotes.a"; sourceTree = BUILT_PRODUCTS_DIR; };
AA286B85B6C04FC6940260E9 /* SplashScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = SplashScreen.storyboard; path = OpenNotes/SplashScreen.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -63,6 +65,7 @@
7D3A80182E1A4CC89B1B09C1 /* ICloudBackupModule.swift */,
7D3A80192E1A4CC89B1B09C1 /* ICloudBackupModule.m */,
7D3A801C2E1A4CC89B1B09C1 /* ImageThumbnailEncoder.swift */,
7D3A80312E1A4CC89B1B09C1 /* PDFRenderCopy.swift */,
F11748442D0722820044C1D9 /* OpenNotes-Bridging-Header.h */,
BB2F792B24A3F905000567C9 /* Supporting */,
13B07FB51A68108700A75B9A /* Images.xcassets */,
Expand Down Expand Up @@ -352,6 +355,7 @@
7D3A801A2E1A4CC89B1B09C1 /* ICloudBackupModule.swift in Sources */,
7D3A801B2E1A4CC89B1B09C1 /* ICloudBackupModule.m in Sources */,
7D3A801D2E1A4CC89B1B09C1 /* ImageThumbnailEncoder.swift in Sources */,
7D3A80322E1A4CC89B1B09C1 /* PDFRenderCopy.swift in Sources */,
4788858903E15B3CAEB129E6 /* ExpoModulesProvider.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
49 changes: 49 additions & 0 deletions ios/OpenNotes/PDFRenderCopy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import CryptoKit
import Foundation
import PDFKit

enum PDFRenderCopy {
private static let unannotated: NSCache<NSString, NSNumber> = {
let cache = NSCache<NSString, NSNumber>()
cache.countLimit = 128
return cache
}()

static func url(for source: URL, cacheDirectory: URL) throws -> URL {
guard #available(iOS 16.0, macOS 13.0, *) else { return source }
let files = FileManager.default
let attributes = try files.attributesOfItem(atPath: source.path)
let modified = (attributes[.modificationDate] as? Date)?.timeIntervalSinceReferenceDate ?? 0
let key = "\(source.path)\n\(attributes[.systemFileNumber] ?? 0)\n\(attributes[.size] ?? 0)\n\(modified)"
let digest = SHA256.hash(data: Data(key.utf8)).map { String(format: "%02x", $0) }.joined()
let destination = cacheDirectory.appendingPathComponent(digest).appendingPathExtension("pdf")
if files.fileExists(atPath: destination.path) { return destination }
if unannotated.object(forKey: key as NSString) != nil { return source }

guard let document = PDFDocument(url: source), !document.isLocked else { return source }
var hasAnnotations = false
for index in 0..<document.pageCount {
guard let page = document.page(at: index) else { continue }
for annotation in page.annotations {
if annotation.shouldDisplay {
hasAnnotations = true
} else {
// PDFKit also burns in print-only annotations unless they are removed.
page.removeAnnotation(annotation)
}
}
}
guard hasAnnotations else {
unannotated.setObject(true, forKey: key as NSString)
return source
}
try files.createDirectory(at: cacheDirectory, withIntermediateDirectories: true)
let temporary = cacheDirectory.appendingPathComponent(UUID().uuidString).appendingPathExtension("pdf")
defer { try? files.removeItem(at: temporary) }
guard document.write(to: temporary, withOptions: [.burnInAnnotationsOption: true]) else {
throw CocoaError(.fileWriteUnknown)
}
try files.moveItem(at: temporary, to: destination)
return destination
}
}
4 changes: 4 additions & 0 deletions ios/OpenNotes/PDFUtilsModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

@interface RCT_EXTERN_MODULE(PDFUtilsModule, NSObject)

RCT_EXTERN_METHOD(preparePdfForRendering:(NSString *)fileUri
resolver:(RCTPromiseResolveBlock)resolver
rejecter:(RCTPromiseRejectBlock)rejecter)

RCT_EXTERN_METHOD(readImageForThumbnail:(NSString *)fileUri
resolver:(RCTPromiseResolveBlock)resolver
rejecter:(RCTPromiseRejectBlock)rejecter)
Expand Down
25 changes: 25 additions & 0 deletions ios/OpenNotes/PDFUtilsModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,35 @@ import UIKit

@objc(PDFUtilsModule)
class PDFUtilsModule: NSObject {
private static let pdfRenderQueue = DispatchQueue(label: "OpenNotes.pdf-render-copy", qos: .userInitiated)

@objc static func requiresMainQueueSetup() -> Bool {
return false
}

@objc
func preparePdfForRendering(_ fileUri: String,
resolver: @escaping RCTPromiseResolveBlock,
rejecter: @escaping RCTPromiseRejectBlock) {
Self.pdfRenderQueue.async {
autoreleasepool {
guard let source = Self.localFileURL(fileUri) else {
rejecter("E_INVALID_PATH", "Invalid PDF file URL", nil)
return
}
do {
let caches = try FileManager.default.url(for: .cachesDirectory, in: .userDomainMask,
appropriateFor: nil, create: true)
let directory = caches.appendingPathComponent("pdf-render-v1", isDirectory: true)
let rendered = try PDFRenderCopy.url(for: source, cacheDirectory: directory)
resolver(rendered == source ? fileUri : rendered.absoluteString)
} catch {
rejecter("E_PDF_RENDER_COPY", "Could not prepare the PDF annotations for rendering", error)
}
}
}
}

@objc
func readImageForThumbnail(_ fileUri: String,
resolver: @escaping RCTPromiseResolveBlock,
Expand Down
7 changes: 4 additions & 3 deletions scripts/catalogStore.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ test('catalog reload preserves composed thumbnail versions and still accepts old
const catalog = parseCatalog(JSON.stringify({
version: 1, folders: [], notes: [
{ id: 'old', title: 'Old note', thumbnailUri: 'old-ink-preview' },
{ id: 'new', title: 'Photo note', thumbnailUri: 'composed-cover', thumbnailVersion: 1 },
{ id: 'v1', title: 'Old cover', thumbnailUri: 'old-cover', thumbnailVersion: 1 },
{ id: 'new', title: 'PDF note', thumbnailUri: 'composed-cover', thumbnailVersion: 2 },
],
}));
assert.equal(catalog.notes[0].thumbnailUri, 'old-ink-preview');
assert.equal(catalog.notes[0].thumbnailVersion, undefined);
assert.equal(catalog.notes[1].thumbnailVersion, 1);
const restored = parseCatalog(JSON.stringify(catalog));
assert.equal(restored.notes[1].thumbnailUri, 'composed-cover');
assert.equal(restored.notes[1].thumbnailVersion, 1);
assert.equal(restored.notes[2].thumbnailUri, 'composed-cover');
assert.equal(restored.notes[2].thumbnailVersion, 2);
});

function makeKv(initial = {}) {
Expand Down
70 changes: 61 additions & 9 deletions scripts/exportService.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function notebook(pages) {
};
}

function harness(files = {}, { mergePdfFiles } = {}) {
function harness(files = {}, { mergePdfFiles, preparePdfForRendering } = {}) {
const printCalls = [];
const batchCalls = [];
const fileReads = [];
Expand Down Expand Up @@ -66,14 +66,14 @@ function harness(files = {}, { mergePdfFiles } = {}) {
if (name === 'react-native') return { PixelRatio: { get: () => 2 } };
if (name === './pdfUtilsModule') {
return {
PDFUtilsModule: mergePdfFiles === null
? {}
: {
mergePdfFiles: async (uris) => {
mergeCalls.push(uris);
return mergePdfFiles ? mergePdfFiles(uris) : 'file:///merged.pdf';
},
},
PDFUtilsModule: {
preparePdfForRendering,
mergePdfFiles: mergePdfFiles === null ? undefined : async (uris) => {
mergeCalls.push(uris);
return mergePdfFiles ? mergePdfFiles(uris) : 'file:///merged.pdf';
},
},
resolvePdfRenderUri: async uri => uri ? preparePdfForRendering?.(uri) ?? uri : undefined,
};
}
if (name === '@mathnotes/mobile-ink') return mobileInk;
Expand Down Expand Up @@ -256,3 +256,55 @@ test('base64 page rasters are embedded verbatim instead of escaped copies', () =
const unsafe = buildPdfHtml([{ rasterUri: 'file:///a"b.png', images: [], textBoxes: [] }]);
assert.ok(unsafe.includes('file:///a&quot;b.png'));
});

test('PDF export uses the prepared copy and preserves the source page index', async () => {
const source = 'file:///imported.pdf';
const prepared = 'file:///cache/annotated.pdf';
const h = harness({}, { preparePdfForRendering: async uri => {
assert.equal(uri, source);
return prepared;
} });

const result = await h.exportNotebookAsPdf({
data: notebook([{ pageType: 'pdf', pdfPageNumber: 3 }]),
pdfBackgroundUri: source,
filename: 'marked-up',
});

assert.equal(result.ok, true);
assert.equal(h.batchCalls[0][5], prepared);
assert.equal(h.batchCalls[0][6][0], 2);
});

test('large exports regenerate annotated PDF pages while reusing other saved previews', async () => {
const oldPreview = 'file:///stale-preview.png';
const plainPreview = 'data:image/png;base64,UEdF';
const prepared = 'file:///cache/annotated.pdf';
const h = harness({ [oldPreview]: 'T0xE' }, { preparePdfForRendering: async () => prepared });
const data = notebook([
{ pageType: undefined, pdfPageNumber: 5, previewUri: oldPreview },
...Array.from({ length: 149 }, () => ({ previewUri: plainPreview })),
]);

const result = await h.exportNotebookAsPdf({ data, pdfBackgroundUri: 'file:///imported.pdf', filename: 'large' });

assert.equal(result.ok, true);
assert.equal(h.batchCalls.length, 1);
assert.equal(h.batchCalls[0][5], prepared);
assert.equal(h.batchCalls[0][6][0], 4);
assert.deepEqual(h.fileReads, []);
assert.doesNotMatch(h.printCalls[0].html, /T0xE/);
assert.match(h.printCalls[0].html, /UEdF/);
});

test('large unannotated PDF exports can still use saved previews', async () => {
const h = harness({}, { preparePdfForRendering: async uri => uri });
const previewUri = 'data:image/png;base64,UERG';
const data = notebook(Array.from({ length: 150 }, () => ({ pageType: 'pdf', previewUri })));

const result = await h.exportNotebookAsPdf({ data, pdfBackgroundUri: 'file:///imported.pdf', filename: 'large' });

assert.equal(result.ok, true);
assert.equal(h.batchCalls.length, 0);
assert.match(h.printCalls[0].html, /UERG/);
});
48 changes: 39 additions & 9 deletions scripts/noteThumbnails.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const source = ts.transpileModule(
{ compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } },
).outputText;

const importSource = ts.transpileModule(
readFileSync(new URL('../src/services/pdfImportService.ts', import.meta.url), 'utf8'),
{ compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } },
).outputText;

const ink = 'data:image/png;base64,aW5r';
const composite = 'data:image/png;base64,cGhvdG8=';
const photo = { id: 'photo', type: 'image', sourceUri: 'file:///photo.jpg', x: 20, y: 30, width: 100, height: 80 };
Expand All @@ -31,14 +36,14 @@ function harness({ writeOk = true, renderFails = false, render = async () => com
readBody: async () => { reads += 1; return { kind: readKind, data: structuredClone(stored) }; },
};
if (name === './thumbnailService') return {
THUMBNAIL_VERSION: 1,
THUMBNAIL_VERSION: 2,
createPageThumbnail: async (...args) => {
if (renderFails) throw new Error('Image decoder unavailable');
return render(...args);
},
};
if (name === '../i18n') return { t: { common: { untitled: 'Untitled' } } };
if (name === '../utils/id') return {};
if (name === '../utils/id') return { noteId: () => 'imported' };
if (name === './catalogStore') return {};
if (name === './pdfStorage' || name === './imageInsertStorage') return {};
throw new Error('Unexpected import: ' + name);
Expand All @@ -54,7 +59,7 @@ test('saving uses a composed cover without flattening overlays into the note bod
const result = await h.saveNoteBody('note', original);
assert.equal(result.ok, true);
assert.equal(h.note().thumbnailUri, composite);
assert.equal(h.note().thumbnailVersion, 1);
assert.equal(h.note().thumbnailVersion, 2);
assert.deepEqual(h.stored(), original);
assert.equal(h.stored().pages[0].previewUri, ink);
});
Expand All @@ -71,22 +76,23 @@ test('thumbnail failure cannot fail a note save or discard its images', async ()
assert.equal((await h.saveNoteBody('note', body())).ok, true);
assert.equal(h.stored().pages[0].insertedElements[0].sourceUri, photo.sourceUri);
assert.equal(h.note().thumbnailUri, ink);
assert.notEqual(h.note().thumbnailVersion, 1);
assert.notEqual(h.note().thumbnailVersion, 2);
});

test('legacy thumbnail repair leaves the note body and modification date untouched', async () => {
test('repairing a version-one cover leaves the note body and modification date untouched', async () => {
const h = harness();
await h.updateMetadata('note', { thumbnailVersion: 1, updatedAt: '2026-01-02' });
const before = structuredClone(h.stored());
assert.equal(await h.getNoteThumbnail(h.note()), composite);
assert.equal(h.note().updatedAt, '2026-01-02');
assert.equal(h.note().thumbnailVersion, 1);
assert.equal(h.note().thumbnailVersion, 2);
assert.deepEqual(h.stored(), before);
});

test('failed legacy thumbnail repair keeps the existing cover eligible for retry', async () => {
const h = harness({ renderFails: true });
assert.equal(await h.getNoteThumbnail(h.note()), ink);
assert.notEqual(h.note().thumbnailVersion, 1);
assert.notEqual(h.note().thumbnailVersion, 2);
});

test('image-only saves compose the first page even when no ink preview exists', async () => {
Expand Down Expand Up @@ -126,7 +132,7 @@ for (const readKind of ['unreadable', 'missing']) {

test('current thumbnail versions do not re-read or rewrite legacy bodies', async () => {
const h = harness();
await h.updateMetadata('note', { thumbnailVersion: 1, thumbnailUri: composite });
await h.updateMetadata('note', { thumbnailVersion: 2, thumbnailUri: composite });
assert.equal(await h.getNoteThumbnail(h.note()), composite);
assert.equal(h.reads(), 0);
});
Expand All @@ -138,7 +144,7 @@ test('an old thumbnail repair cannot overwrite a newer note save', async () => {
const h = harness({ render: () => new Promise(resolve => { finishRender = resolve; startRender(); }) });
const repairing = h.getNoteThumbnail(structuredClone(h.note()));
await started;
await h.updateMetadata('note', { updatedAt: '2026-10-22', thumbnailUri: 'new-cover', thumbnailVersion: 1 });
await h.updateMetadata('note', { updatedAt: '2026-10-22', thumbnailUri: 'new-cover', thumbnailVersion: 2 });
finishRender(composite);
assert.equal(await repairing, 'new-cover');
assert.equal(h.note().thumbnailUri, 'new-cover');
Expand All @@ -165,3 +171,27 @@ test('duplicate card requests share a single repair of the same legacy cover', a
assert.equal(await second, composite);
assert.equal(h.reads(), 1);
});

test('importing a PDF generates its first cover with the copied PDF already available', async () => {
const pdfUri = 'file:///notes/pdfs/imported.pdf';
const h = harness({ render: async (_page, background, uri) =>
background === 'pdf' && uri === pdfUri ? composite : null });
const exports = {};
vm.runInNewContext(importSource, { exports, require(name) {
if (name === './notesRepo') return h;
if (name === './lifecycleService') return { recordSuccessfulNoteSave: async () => {} };
if (name === './pdfStorage') return {
importPdfFromUri: async () => ({ uri: pdfUri, name: 'Lecture.pdf', pageCount: 1 }),
createPdfNotebookData: () => ({
version: '1.0', pages: [{ id: 'page-1', data: '', pageType: 'pdf', pdfPageNumber: 1 }],
}),
};
throw new Error('Unexpected import: ' + name);
} });

const imported = await exports.createPdfNoteFromUri('file:///incoming/lecture.pdf');

assert.equal(imported.pdfUri, pdfUri);
assert.equal(imported.thumbnailUri, composite);
assert.equal(imported.thumbnailVersion, 2);
});
Loading
Loading