Skip to content

Commit

Permalink
Store canvas data of different pages separately
Browse files Browse the repository at this point in the history
This is only the proper way to ensure that the used canvas data matches
the context the text is being rendered to. Also, we need to clear the
canvas data (and cached styles) only if there *is* a forced redraw.
  • Loading branch information
shivaprsd committed Oct 14, 2022
1 parent 02f7253 commit 56a349e
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions addon/doq.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const DOQReader = {
preferences: {},
colorSchemes: [],
readerTone: {},
canvasData: null,
canvasData: new WeakMap(),
styleCache: new Map(),
options: { autoReader: true, dynamicTheme: true },
flags: { readerOn: false, isPrinting: false },
Expand Down Expand Up @@ -140,7 +140,7 @@ const DOQReader = {
saveCanvas(ctx, method) {
const cvs = ctx.canvas;
if (cvs.isConnected && cvs.closest(".canvasWrapper"))
this.canvasData = ctx.getImageData(0, 0, cvs.width, cvs.height);
this.canvasData.set(ctx, ctx.getImageData(0, 0, cvs.width, cvs.height));
},

/* Return fill and stroke styles */
Expand All @@ -164,14 +164,15 @@ const DOQReader = {
return newStyle;
},
getCanvasColor(ctx, tx, ty) {
if (!this.canvasData)
if (!this.canvasData.has(ctx))
return null;
const tfm = ctx.getTransform();
let {x, y} = tfm.transformPoint({x: tx, y: ty});
[x, y] = [x, y].map(Math.round);
const i = (y * this.canvasData.width + x) * 4;
const data = Array.from(this.canvasData.data.slice(i, i + 3));
return newColor(data.map(e => e / 255));
const canvasData = this.canvasData.get(ctx);
const i = (y * canvasData.width + x) * 4;
const rgb = Array.from(canvasData.data.slice(i, i + 3));
return newColor(rgb.map(e => e / 255));
},

/* Calculate a new style for given colorscheme and tone */
Expand Down Expand Up @@ -322,8 +323,6 @@ const DOQReader = {
this.enableReader(e?.isTrusted);
}
this.updatePreference("tone", pick);
this.styleCache.clear();
this.canvasData = null;
},
forceRedraw() {
const {pdfViewer, pdfThumbnailViewer} = window.PDFViewerApplication;
Expand All @@ -332,6 +331,8 @@ const DOQReader = {
Object.values(store || {}).forEach(e => e.rebuild());
return page;
};
this.styleCache.clear();
this.canvasData = new WeakMap();
pdfViewer._pages.filter(e => e.renderingState).map(rebuildAnnotations)
.forEach(e => e.reset());
pdfThumbnailViewer._thumbnails.filter(e => e.renderingState)
Expand Down

0 comments on commit 56a349e

Please sign in to comment.