Skip to content
Open
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
2 changes: 1 addition & 1 deletion benchmarks/scenarios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class RenderBudgetWorker {
});
return;
}
if (request.output === "png") {
if (request.output !== "bitmap") {
throw new Error("Render-budget contract requests bitmaps only");
}
const width = request.maxWidth ?? 1_920;
Expand Down
10 changes: 10 additions & 0 deletions esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ const inlineWorkerPlugin = {
target: "es2022",
minify: production,
metafile: production,
alias: {
// supernote-typescript's svg/vector-ink/conversion modules import
// image-js at module scope; the code paths used here never call
// it, and the lockfile must stay image-js-free (see
// tests/worker-ownership-lifecycle.test.ts).
"image-js": resolve("src/shared/image-js-stub.ts"),
},
loader: {
".ttf": "binary",
},
Expand Down Expand Up @@ -108,6 +115,9 @@ const context = await esbuild.context({
metafile: production,
sourcemap: production ? false : "inline",
treeShaking: true,
alias: {
"image-js": resolve("src/shared/image-js-stub.ts"),
},
outfile: "main.js",
loader: {
".ttf": "binary",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"pako": "1.0.11",
"pdf-lib": "1.17.1",
"spark-md5": "3.0.2",
"supernote-typescript": "0.3.0"
"supernote-typescript": "0.7.3"
},
"devDependencies": {
"@eslint/js": "9.32.0",
Expand All @@ -56,7 +56,7 @@
},
"pnpm": {
"overrides": {
"supernote-typescript@0.3.0>image-js": "-"
"supernote-typescript@0.7.3>image-js": "-"
}
}
}
80 changes: 45 additions & 35 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/note/notebook-rasterizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ interface Rgba {
const transparent: Rgba = { red: 0, green: 0, blue: 0, alpha: 0 };
const unknown: Rgba = { red: 255, green: 255, blue: 255, alpha: 0 };
const black: Rgba = { red: 0, green: 0, blue: 0, alpha: 255 };
const darkGray: Rgba = { red: 169, green: 169, blue: 169, alpha: 255 };
const gray: Rgba = { red: 128, green: 128, blue: 128, alpha: 255 };
// Gray levels follow supernote-typescript 0.7.x, which swapped the 0.3.0
// values so darkGray < gray in lightness (matching the device's rendering).
const darkGray: Rgba = { red: 128, green: 128, blue: 128, alpha: 255 };
const gray: Rgba = { red: 169, green: 169, blue: 169, alpha: 255 };
const white: Rgba = { red: 255, green: 255, blue: 255, alpha: 255 };

const palette = new Map<number, Rgba>([
Expand Down
15 changes: 0 additions & 15 deletions src/note/notebook-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1414,21 +1414,6 @@ export class NotebookService implements NotebookSessionProvider {
return false;
}

private retainedBytes(): number {
return (
[...this.sessionsById.values()].reduce(
(total, session) =>
total +
session.sourceBytes +
session.parsedBytes +
this.canvasBytes(session),
0,
) +
this.retainedBitmapBytes +
this.inFlightBytes
);
}

private renderError(
record: SessionRecord,
descriptor: NotebookDescriptor,
Expand Down
121 changes: 121 additions & 0 deletions src/note/notebook-vector-render.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import type { SupernoteX } from "supernote-typescript/lib/parsing";
import type { IStrokePoint } from "supernote-typescript/lib/strokes";
import {
buildVectorInkBackgroundNote,
buildVectorInkPrimitives,
prepareVectorInkPages,
type VectorInkPrimitive,
} from "supernote-typescript/lib/vector-ink";

import {
rasterizeNotebookPage,
type BitmapBackgroundDecoder,
type RasterizedNotebookPage,
} from "./notebook-rasterizer";

// Renders a notebook page to pixels with its pen strokes drawn as real
// vector paths on a canvas, over the raster background (templates/images,
// ink layers removed). The canvas anti-aliases the strokes, so they render
// smooth - unlike the device's 1-bit RLE ink raster, which reads as
// "dotted / no anti-aliasing" when scaled. Returns null when the page has
// no trustworthy vector ink, so the caller falls back to the plain raster.
//
// This is the same technique the other Supernote viewer uses (draw vector
// strokes into a raster at render time) rather than handing the browser an
// SVG <img> and depending on its rasterizer - which under Obsidian's GPU
// compositing came out aliased. Worker-safe: OffscreenCanvas + Path2D, no
// image-js, no obsidian.

const pointsToPath = (points: readonly IStrokePoint[]): string => {
const [first, ...rest] = points;
if (!first) return "";
const coord = (value: number): string => {
const rounded = Math.round(value * 100) / 100;
return Object.is(rounded, -0) ? "0" : String(rounded);
};
return (
`M${coord(first.x)} ${coord(first.y)}` +
rest.map((point) => `L${coord(point.x)} ${coord(point.y)}`).join("")
);
};

export const drawPrimitives = (
context: OffscreenCanvasRenderingContext2D,
primitives: readonly VectorInkPrimitive[],
): void => {
for (const primitive of primitives) {
if (primitive.kind === "rect") {
context.fillStyle = primitive.color;
// 'hatch' is a cross-hatch heading background; a reduced opacity
// approximates "visibly non-solid" without a pattern per page.
context.globalAlpha = primitive.fill === "hatch" ? 0.35 : 1;
context.fillRect(
primitive.x,
primitive.y,
primitive.width,
primitive.height,
);
context.globalAlpha = 1;
} else if (primitive.kind === "filledPath") {
context.fillStyle = primitive.color;
const d = primitive.rings
.map((ring) => `${pointsToPath(ring)}Z`)
.join(" ");
context.fill(new Path2D(d), "evenodd");
} else {
context.strokeStyle = primitive.color;
context.lineWidth = primitive.width;
context.lineCap = "round";
context.lineJoin = "round";
context.stroke(new Path2D(pointsToPath(primitive.points)));
}
}
};

export const renderVectorPage = async (
note: SupernoteX,
pageNumber: number,
decodeBitmapBackground: BitmapBackgroundDecoder,
): Promise<RasterizedNotebookPage | null> => {
const vectorPages = prepareVectorInkPages(note, [pageNumber], 1);
const vectorPage = vectorPages[0];
if (!vectorPage?.useVectorInk) {
return null;
}

const backgroundNote = buildVectorInkBackgroundNote(note, vectorPages);
const backgroundPage = backgroundNote.pages[pageNumber - 1]!;
const background = await rasterizeNotebookPage(
note.pageWidth,
note.pageHeight,
{
pageStyle: backgroundPage.PAGESTYLE,
layerSequence: backgroundPage.LAYERSEQ,
layers: Object.fromEntries(
backgroundPage.LAYERSEQ.map((name) => [name, backgroundPage[name]]),
),
},
decodeBitmapBackground,
);

const canvas = new OffscreenCanvas(note.pageWidth, note.pageHeight);
const context = canvas.getContext("2d", { willReadFrequently: true });
if (!context) {
throw new Error("Supernote worker cannot create a 2D canvas");
}
context.putImageData(
new ImageData(background.pixels, background.width, background.height),
0,
0,
);
drawPrimitives(
context,
buildVectorInkPrimitives(vectorPage.strokes, vectorPage.styles),
);

return {
width: note.pageWidth,
height: note.pageHeight,
pixels: context.getImageData(0, 0, note.pageWidth, note.pageHeight).data,
};
};
Loading