Skip to content

Commit

Permalink
fix: dont use global image cache (#1863)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomura authored Jun 5, 2022
1 parent 1411d16 commit eecddbd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-peaches-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@react-pdf/render': patch
---

fix: dont use global image cache
3 changes: 2 additions & 1 deletion packages/render/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import addBookmarks from './operations/addBookmarks';

const render = (ctx, doc) => {
const pages = doc.children || [];
const options = { imageCache: new Map() };

addMetadata(ctx, doc);

pages.forEach(page => renderNode(ctx, page));
pages.forEach(page => renderNode(ctx, page, options));

addBookmarks(ctx, doc);

Expand Down
14 changes: 6 additions & 8 deletions packages/render/src/primitives/renderImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { isNil } from '@react-pdf/fns';
import clipNode from '../operations/clipNode';
import resolveObjectFit from '../utils/resolveObjectFit';

const IMAGE_CACHE = new Map();

const drawImage = (ctx, node) => {
const drawImage = (ctx, node, options = {}) => {
const { left, top } = node.box;
const opacity = node.style?.opacity;
const objectFit = node.style?.objectFit;
Expand All @@ -15,6 +13,7 @@ const drawImage = (ctx, node) => {
const paddingRight = node.box.paddingRight || 0;
const paddingBottom = node.box.paddingBottom || 0;
const paddingLeft = node.box.paddingLeft || 0;
const imageCache = options.imageCache || new Map();

const { width, height, xOffset, yOffset } = resolveObjectFit(
objectFit,
Expand All @@ -30,10 +29,9 @@ const drawImage = (ctx, node) => {
if (width !== 0 && height !== 0) {
const cacheKey = node.image.key;

const image =
IMAGE_CACHE.get(cacheKey) || ctx.embedImage(node.image.data);
const image = imageCache.get(cacheKey) || ctx.embedImage(node.image.data);

if (cacheKey) IMAGE_CACHE.set(cacheKey, image);
if (cacheKey) imageCache.set(cacheKey, image);

const imageOpacity = isNil(opacity) ? 1 : opacity;

Expand All @@ -58,11 +56,11 @@ const drawImage = (ctx, node) => {
}
};

const renderImage = (ctx, node) => {
const renderImage = (ctx, node, options) => {
ctx.save();

clipNode(ctx, node);
drawImage(ctx, node);
drawImage(ctx, node, options);

ctx.restore();
};
Expand Down
10 changes: 5 additions & 5 deletions packages/render/src/primitives/renderNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import setDestination from '../operations/setDestination';

const isRecursiveNode = node => node.type !== P.Text && node.type !== P.Svg;

const renderChildren = (ctx, node) => {
const renderChildren = (ctx, node, options) => {
ctx.save();

if (node.box) {
ctx.translate(node.box.left, node.box.top);
}

const children = node.children || [];
const renderChild = child => renderNode(ctx, child);
const renderChild = child => renderNode(ctx, child, options);

children.forEach(renderChild);

Expand All @@ -39,7 +39,7 @@ const renderFns = {
[P.Link]: setLink,
};

const renderNode = (ctx, node) => {
const renderNode = (ctx, node, options) => {
const overflowHidden = node.style?.overflow === 'hidden';
const shouldRenderChildren = isRecursiveNode(node);

Expand All @@ -55,9 +55,9 @@ const renderNode = (ctx, node) => {

const renderFn = renderFns[node.type];

if (renderFn) renderFn(ctx, node);
if (renderFn) renderFn(ctx, node, options);

if (shouldRenderChildren) renderChildren(ctx, node);
if (shouldRenderChildren) renderChildren(ctx, node, options);

setDestination(ctx, node);
renderDebug(ctx, node);
Expand Down

0 comments on commit eecddbd

Please sign in to comment.