import { xml, XmlGeneralNode, XmlNode } from "src/xml"; import { ImageContent } from "./imageContent"; import { nameFromId, pixelsToEmu, transparencyPercentToAlpha } from "./imageUtils"; export function createImage(imageId: number, relId: string, content: ImageContent): XmlNode { // http://officeopenxml.com/drwPicInline.php // // Performance note: // // I've tried to improve the markup generation performance by parsing // the string once and caching the result (and of course customizing it // per image) but it made no change whatsoever (in both cases 1000 items // loop takes around 8 seconds on my machine) so I'm sticking with this // approach which I find to be more readable. // const name = nameFromId(imageId); const markupText = ` ${docProperties(imageId, name, content)} ${pictureMarkup(imageId, relId, name, content)} `; const markupXml = xml.parser.parse(markupText) as XmlGeneralNode; xml.modify.removeEmptyTextNodes(markupXml); // remove whitespace return markupXml; } function docProperties(imageId: number, name: string, content: ImageContent): string { if (content.altText) { return ``; } return ` `; } function pictureMarkup(imageId: number, relId: string, name: string, content: ImageContent) { // http://officeopenxml.com/drwPic.php // Legend: // nvPicPr - non-visual picture properties - id, name, etc. // blipFill - binary large image (or) picture fill - image size, image fill, etc. // spPr - shape properties - frame size, frame fill, etc. return ` ${transparencyMarkup(content.transparencyPercent)} `; } function transparencyMarkup(transparencyPercent: number): string { if (transparencyPercent === null || transparencyPercent === undefined) { return ''; } const alpha = transparencyPercentToAlpha(transparencyPercent); return ``; }