2022-06-26 23:26:42 +01:00
|
|
|
import { uniqueId } from "@util/convenience-functions";
|
2021-03-18 02:48:37 +00:00
|
|
|
|
2022-06-26 23:26:42 +01:00
|
|
|
import { IContext, IXmlableObject } from "@file/xml-components";
|
2022-10-26 23:09:36 +01:00
|
|
|
import { DocPropertiesOptions } from "@file/drawing/doc-properties/doc-properties";
|
2021-03-22 05:23:30 +00:00
|
|
|
|
2021-03-18 02:48:37 +00:00
|
|
|
import { Drawing, IFloating } from "../../drawing";
|
|
|
|
import { IMediaTransformation } from "../../media";
|
|
|
|
import { IMediaData } from "../../media/data";
|
|
|
|
import { Run } from "../run";
|
|
|
|
|
|
|
|
export interface IImageOptions {
|
|
|
|
readonly data: Buffer | string | Uint8Array | ArrayBuffer;
|
|
|
|
readonly transformation: IMediaTransformation;
|
|
|
|
readonly floating?: IFloating;
|
2022-10-26 23:09:36 +01:00
|
|
|
readonly altText?: DocPropertiesOptions;
|
2021-03-18 02:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ImageRun extends Run {
|
|
|
|
private readonly key = `${uniqueId()}.png`;
|
|
|
|
private readonly imageData: IMediaData;
|
|
|
|
|
2022-08-31 07:52:27 +01:00
|
|
|
public constructor(options: IImageOptions) {
|
2021-03-18 02:48:37 +00:00
|
|
|
super({});
|
|
|
|
const newData = typeof options.data === "string" ? this.convertDataURIToBinary(options.data) : options.data;
|
|
|
|
|
|
|
|
this.imageData = {
|
|
|
|
stream: newData,
|
|
|
|
fileName: this.key,
|
|
|
|
transformation: {
|
|
|
|
pixels: {
|
|
|
|
x: Math.round(options.transformation.width),
|
|
|
|
y: Math.round(options.transformation.height),
|
|
|
|
},
|
|
|
|
emus: {
|
|
|
|
x: Math.round(options.transformation.width * 9525),
|
|
|
|
y: Math.round(options.transformation.height * 9525),
|
|
|
|
},
|
|
|
|
flip: options.transformation.flip,
|
|
|
|
rotation: options.transformation.rotation ? options.transformation.rotation * 60000 : undefined,
|
|
|
|
},
|
|
|
|
};
|
2022-10-26 23:09:36 +01:00
|
|
|
const drawing = new Drawing(this.imageData, { floating: options.floating, docProperties: options.altText });
|
2021-03-18 02:48:37 +00:00
|
|
|
|
|
|
|
this.root.push(drawing);
|
|
|
|
}
|
|
|
|
|
|
|
|
public prepForXml(context: IContext): IXmlableObject | undefined {
|
|
|
|
context.file.Media.addImage(this.key, this.imageData);
|
|
|
|
|
|
|
|
return super.prepForXml(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
private convertDataURIToBinary(dataURI: string): Uint8Array {
|
2022-10-26 23:09:36 +01:00
|
|
|
if (typeof atob === "function") {
|
|
|
|
// https://gist.github.com/borismus/1032746
|
|
|
|
// https://github.com/mafintosh/base64-to-uint8array
|
|
|
|
const BASE64_MARKER = ";base64,";
|
|
|
|
const base64Index = dataURI.indexOf(BASE64_MARKER);
|
2021-03-18 02:48:37 +00:00
|
|
|
|
2022-10-26 23:09:36 +01:00
|
|
|
const base64IndexWithOffset = base64Index === -1 ? 0 : base64Index + BASE64_MARKER.length;
|
2021-03-18 02:48:37 +00:00
|
|
|
|
|
|
|
return new Uint8Array(
|
2022-10-26 23:09:36 +01:00
|
|
|
atob(dataURI.substring(base64IndexWithOffset))
|
2021-03-18 02:48:37 +00:00
|
|
|
.split("")
|
|
|
|
.map((c) => c.charCodeAt(0)),
|
|
|
|
);
|
|
|
|
} else {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
|
2021-03-18 02:48:37 +00:00
|
|
|
const b = require("buf" + "fer");
|
|
|
|
return new b.Buffer(dataURI, "base64");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|