Skip to content

Commit

Permalink
refactor: unify pdfkit image mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomura committed Feb 5, 2025
1 parent 6d87505 commit 45f46fc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
22 changes: 0 additions & 22 deletions packages/pdfkit/src/mixins/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,6 @@ export default {
return (this._imageCount = 0);
},

embedImage(src) {
let image;

if (typeof src === 'string') {
image = this._imageRegistry[src];
}

if (!image) {
if (src.width && src.height) {
image = src;
} else {
image = this.openImage(src);
}
}

if (!image.obj) {
image.embed(this);
}

return image;
},

image(src, x, y, options = {}) {
let bh, bp, bw, image, ip, left, left1, rotateAngle, originX, originY;
if (typeof x === 'object') {
Expand Down
25 changes: 25 additions & 0 deletions packages/render/src/operations/embedImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const embedImage = (ctx, node) => {
const src = node.image.data;

let image;

if (typeof src === 'string') {
image = ctx._imageRegistry[src];
}

if (!image) {
if (src.width && src.height) {
image = src;
} else {
image = ctx.openImage(src);
}
}

if (!image.obj) {
image.embed(ctx);
}

return image;
};

export default embedImage;
3 changes: 2 additions & 1 deletion packages/render/src/primitives/renderImage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isNil } from '@react-pdf/fns';

import clipNode from '../operations/clipNode';
import embedImage from '../operations/embedImage';
import resolveObjectFit from '../utils/resolveObjectFit';

const drawImage = (ctx, node, options = {}) => {
Expand Down Expand Up @@ -29,7 +30,7 @@ const drawImage = (ctx, node, options = {}) => {
if (width !== 0 && height !== 0) {
const cacheKey = node.image.key;

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

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

Expand Down

0 comments on commit 45f46fc

Please sign in to comment.