pdfkit/lib/mixins/images.js

131 lines
3.0 KiB
JavaScript

import PDFImage from '../image';
export default {
initImages() {
this._imageRegistry = {};
return (this._imageCount = 0);
},
image(src, x, y, options = {}) {
let bh, bp, bw, image, ip, left, left1;
if (typeof x === 'object') {
options = x;
x = null;
}
x = (left = x != null ? x : options.x) != null ? left : this.x;
y = (left1 = y != null ? y : options.y) != null ? left1 : this.y;
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);
}
if (this.page.xobjects[image.label] == null) {
this.page.xobjects[image.label] = image.obj;
}
let w = options.width || image.width;
let h = options.height || image.height;
if (options.width && !options.height) {
const wp = w / image.width;
w = image.width * wp;
h = image.height * wp;
} else if (options.height && !options.width) {
const hp = h / image.height;
w = image.width * hp;
h = image.height * hp;
} else if (options.scale) {
w = image.width * options.scale;
h = image.height * options.scale;
} else if (options.fit) {
[bw, bh] = options.fit;
bp = bw / bh;
ip = image.width / image.height;
if (ip > bp) {
w = bw;
h = bw / ip;
} else {
h = bh;
w = bh * ip;
}
} else if (options.cover) {
[bw, bh] = options.cover;
bp = bw / bh;
ip = image.width / image.height;
if (ip > bp) {
h = bh;
w = bh * ip;
} else {
w = bw;
h = bw / ip;
}
}
if (options.fit || options.cover) {
if (options.align === 'center') {
x = x + bw / 2 - w / 2;
} else if (options.align === 'right') {
x = x + bw - w;
}
if (options.valign === 'center') {
y = y + bh / 2 - h / 2;
} else if (options.valign === 'bottom') {
y = y + bh - h;
}
}
// create link annotations if the link option is given
if (options.link != null) {
this.link(x, y, w, h, options.link);
}
if (options.goTo != null) {
this.goTo(x, y, w, h, options.goTo);
}
if (options.destination != null) {
this.addNamedDestination(options.destination, 'XYZ', x, y, null);
}
// Set the current y position to below the image if it is in the document flow
if (this.y === y) {
this.y += h;
}
this.save();
this.transform(w, 0, 0, -h, x, y + h);
this.addContent(`/${image.label} Do`);
this.restore();
return this;
},
openImage(src) {
let image;
if (typeof src === 'string') {
image = this._imageRegistry[src];
}
if (!image) {
image = PDFImage.open(src, `I${++this._imageCount}`);
if (typeof src === 'string') {
this._imageRegistry[src] = image;
}
}
return image;
}
};