diff --git a/src/file/drawing/drawing.spec.ts b/src/file/drawing/drawing.spec.ts index 8bda6243e7..8dd6d48a91 100644 --- a/src/file/drawing/drawing.spec.ts +++ b/src/file/drawing/drawing.spec.ts @@ -14,6 +14,16 @@ describe("Drawing", () => { referenceId: 1, stream: fs.createReadStream(path), path: path, + dimensions: { + pixels: { + x: 100, + y: 100, + }, + emus: { + x: 100 * 9525, + y: 100 * 9525, + }, + }, }); }); diff --git a/src/file/drawing/drawing.ts b/src/file/drawing/drawing.ts index a514442fb1..fcaaa960c0 100644 --- a/src/file/drawing/drawing.ts +++ b/src/file/drawing/drawing.ts @@ -11,6 +11,6 @@ export class Drawing extends XmlComponent { throw new Error("imageData cannot be undefined"); } - this.root.push(new Inline(imageData.referenceId)); + this.root.push(new Inline(imageData.referenceId, imageData.dimensions)); } } diff --git a/src/file/drawing/inline/extent/extent.ts b/src/file/drawing/inline/extent/extent.ts index 13670d947e..3b787e0e66 100644 --- a/src/file/drawing/inline/extent/extent.ts +++ b/src/file/drawing/inline/extent/extent.ts @@ -3,12 +3,12 @@ import { ExtentAttributes } from "./extent-attributes"; export class Extent extends XmlComponent { - constructor() { + constructor(x: number, y: number) { super("wp:extent"); this.root.push(new ExtentAttributes({ - cx: 3162300, - cy: 2857500, + cx: x, + cy: y, })); } } diff --git a/src/file/drawing/inline/graphic/graphic-data/graphic-data.ts b/src/file/drawing/inline/graphic/graphic-data/graphic-data.ts index 12406c5e17..4b32b25cc4 100644 --- a/src/file/drawing/inline/graphic/graphic-data/graphic-data.ts +++ b/src/file/drawing/inline/graphic/graphic-data/graphic-data.ts @@ -4,13 +4,13 @@ import { Pic } from "./pic"; export class GraphicData extends XmlComponent { - constructor(referenceId: number) { + constructor(referenceId: number, x: number, y: number) { super("a:graphicData"); this.root.push(new GraphicDataAttributes({ uri: "http://schemas.openxmlformats.org/drawingml/2006/picture", })); - this.root.push(new Pic(referenceId)); + this.root.push(new Pic(referenceId, x, y)); } } diff --git a/src/file/drawing/inline/graphic/graphic-data/pic/pic.ts b/src/file/drawing/inline/graphic/graphic-data/pic/pic.ts index ac848a71f3..31bee69136 100644 --- a/src/file/drawing/inline/graphic/graphic-data/pic/pic.ts +++ b/src/file/drawing/inline/graphic/graphic-data/pic/pic.ts @@ -6,7 +6,7 @@ import { PicAttributes } from "./pic-attributes"; import { ShapeProperties } from "./shape-properties/shape-properties"; export class Pic extends XmlComponent { - constructor(referenceId: number) { + constructor(referenceId: number, x: number, y: number) { super("pic:pic"); this.root.push(new PicAttributes({ @@ -14,6 +14,6 @@ export class Pic extends XmlComponent { })); this.root.push(new NonVisualPicProperties()); this.root.push(new BlipFill(referenceId)); - this.root.push(new ShapeProperties()); + this.root.push(new ShapeProperties(x, y)); } } diff --git a/src/file/drawing/inline/graphic/graphic-data/pic/shape-properties/form/extents/extents.ts b/src/file/drawing/inline/graphic/graphic-data/pic/shape-properties/form/extents/extents.ts index a10789f0d2..de38caab18 100644 --- a/src/file/drawing/inline/graphic/graphic-data/pic/shape-properties/form/extents/extents.ts +++ b/src/file/drawing/inline/graphic/graphic-data/pic/shape-properties/form/extents/extents.ts @@ -4,12 +4,12 @@ import { ExtentsAttributes } from "./extents-attributes"; export class Extents extends XmlComponent { - constructor() { + constructor(x: number, y: number) { super("a:ext"); this.root.push(new ExtentsAttributes({ - cx: 3162300, - cy: 2857500, + cx: x, + cy: y, })); } } diff --git a/src/file/drawing/inline/graphic/graphic-data/pic/shape-properties/form/form.ts b/src/file/drawing/inline/graphic/graphic-data/pic/shape-properties/form/form.ts index 9f74c9aa1f..94ed24e1eb 100644 --- a/src/file/drawing/inline/graphic/graphic-data/pic/shape-properties/form/form.ts +++ b/src/file/drawing/inline/graphic/graphic-data/pic/shape-properties/form/form.ts @@ -5,10 +5,10 @@ import { Offset } from "./offset/off"; export class Form extends XmlComponent { - constructor() { + constructor(x: number, y: number) { super("a:xfrm"); - this.root.push(new Extents()); + this.root.push(new Extents(x, y)); this.root.push(new Offset()); } } diff --git a/src/file/drawing/inline/graphic/graphic-data/pic/shape-properties/shape-properties.ts b/src/file/drawing/inline/graphic/graphic-data/pic/shape-properties/shape-properties.ts index 38f9f2cbee..41b914a0fd 100644 --- a/src/file/drawing/inline/graphic/graphic-data/pic/shape-properties/shape-properties.ts +++ b/src/file/drawing/inline/graphic/graphic-data/pic/shape-properties/shape-properties.ts @@ -8,14 +8,14 @@ import { ShapePropertiesAttributes } from "./shape-properties-attributes"; export class ShapeProperties extends XmlComponent { - constructor() { + constructor(x: number, y: number) { super("pic:spPr"); this.root.push(new ShapePropertiesAttributes({ bwMode: "auto", })); - this.root.push(new Form()); + this.root.push(new Form(x, y)); this.root.push(new PresetGeometry()); // this.root.push(new NoFill()); // this.root.push(new Outline()); diff --git a/src/file/drawing/inline/graphic/index.ts b/src/file/drawing/inline/graphic/index.ts index 81251fd41c..60b336f6ba 100644 --- a/src/file/drawing/inline/graphic/index.ts +++ b/src/file/drawing/inline/graphic/index.ts @@ -13,11 +13,11 @@ class GraphicAttributes extends XmlAttributeComponent { export class Graphic extends XmlComponent { - constructor(referenceId: number) { + constructor(referenceId: number, x: number, y: number) { super("a:graphic"); this.root.push(new GraphicAttributes({ a: "http://schemas.openxmlformats.org/drawingml/2006/main", })); - this.root.push(new GraphicData(referenceId)); + this.root.push(new GraphicData(referenceId, x, y)); } } diff --git a/src/file/drawing/inline/inline.ts b/src/file/drawing/inline/inline.ts index bdf657ebc9..15facb5681 100644 --- a/src/file/drawing/inline/inline.ts +++ b/src/file/drawing/inline/inline.ts @@ -1,4 +1,5 @@ // http://officeopenxml.com/drwPicInline.php +import { IMediaDataDimensions } from "file/media"; import { XmlComponent } from "file/xml-components"; import { DocProperties } from "./doc-properties/doc-properties"; import { EffectExtent } from "./effect-extent/effect-extent"; @@ -9,7 +10,7 @@ import { InlineAttributes } from "./inline-attributes"; export class Inline extends XmlComponent { - constructor(referenceId: number) { + constructor(referenceId: number, dimensions: IMediaDataDimensions) { super("wp:inline"); this.root.push(new InlineAttributes({ @@ -19,10 +20,10 @@ export class Inline extends XmlComponent { distR: 0, })); - this.root.push(new Extent()); + this.root.push(new Extent(dimensions.emus.x, dimensions.emus.y)); this.root.push(new EffectExtent()); this.root.push(new DocProperties()); this.root.push(new GraphicFrameProperties()); - this.root.push(new Graphic(referenceId)); + this.root.push(new Graphic(referenceId, dimensions.emus.x, dimensions.emus.y)); } } diff --git a/src/file/media/data.ts b/src/file/media/data.ts index 6d90e260a2..ea75493f79 100644 --- a/src/file/media/data.ts +++ b/src/file/media/data.ts @@ -1,8 +1,20 @@ import * as fs from "fs"; +export interface IMediaDataDimensions { + pixels: { + x: number; + y: number; + }; + emus: { + x: number; + y: number; + }; +} + export interface IMediaData { referenceId: number; stream: fs.ReadStream; path: string; fileName: string; + dimensions: IMediaDataDimensions; } diff --git a/src/file/media/media.ts b/src/file/media/media.ts index 71406d7e1c..b77ebf76fd 100644 --- a/src/file/media/media.ts +++ b/src/file/media/media.ts @@ -1,6 +1,8 @@ import * as fs from "fs"; +import * as sizeOf from "image-size"; import * as path from "path"; -import { IMediaData } from "./data"; + +import {IMediaData} from "./data"; export class Media { private map: Map; @@ -21,11 +23,23 @@ export class Media { public addMedia(filePath: string): IMediaData { const key = path.basename(filePath); + const dimensions = sizeOf(filePath); + const imageData = { referenceId: this.map.values.length + 3, stream: fs.createReadStream(filePath), path: filePath, fileName: key, + dimensions: { + pixels: { + x: dimensions.width, + y: dimensions.height, + }, + emus: { + x: dimensions.width * 9525, + y: dimensions.height * 9525, + }, + }, }; this.map.set(key, imageData);