Add image dimensions

This commit is contained in:
Dolan
2018-01-22 20:42:57 +00:00
parent d3bc784248
commit ef05024f2f
12 changed files with 58 additions and 21 deletions

View File

@ -14,6 +14,16 @@ describe("Drawing", () => {
referenceId: 1, referenceId: 1,
stream: fs.createReadStream(path), stream: fs.createReadStream(path),
path: path, path: path,
dimensions: {
pixels: {
x: 100,
y: 100,
},
emus: {
x: 100 * 9525,
y: 100 * 9525,
},
},
}); });
}); });

View File

@ -11,6 +11,6 @@ export class Drawing extends XmlComponent {
throw new Error("imageData cannot be undefined"); throw new Error("imageData cannot be undefined");
} }
this.root.push(new Inline(imageData.referenceId)); this.root.push(new Inline(imageData.referenceId, imageData.dimensions));
} }
} }

View File

@ -3,12 +3,12 @@ import { ExtentAttributes } from "./extent-attributes";
export class Extent extends XmlComponent { export class Extent extends XmlComponent {
constructor() { constructor(x: number, y: number) {
super("wp:extent"); super("wp:extent");
this.root.push(new ExtentAttributes({ this.root.push(new ExtentAttributes({
cx: 3162300, cx: x,
cy: 2857500, cy: y,
})); }));
} }
} }

View File

@ -4,13 +4,13 @@ import { Pic } from "./pic";
export class GraphicData extends XmlComponent { export class GraphicData extends XmlComponent {
constructor(referenceId: number) { constructor(referenceId: number, x: number, y: number) {
super("a:graphicData"); super("a:graphicData");
this.root.push(new GraphicDataAttributes({ this.root.push(new GraphicDataAttributes({
uri: "http://schemas.openxmlformats.org/drawingml/2006/picture", uri: "http://schemas.openxmlformats.org/drawingml/2006/picture",
})); }));
this.root.push(new Pic(referenceId)); this.root.push(new Pic(referenceId, x, y));
} }
} }

View File

@ -6,7 +6,7 @@ import { PicAttributes } from "./pic-attributes";
import { ShapeProperties } from "./shape-properties/shape-properties"; import { ShapeProperties } from "./shape-properties/shape-properties";
export class Pic extends XmlComponent { export class Pic extends XmlComponent {
constructor(referenceId: number) { constructor(referenceId: number, x: number, y: number) {
super("pic:pic"); super("pic:pic");
this.root.push(new PicAttributes({ this.root.push(new PicAttributes({
@ -14,6 +14,6 @@ export class Pic extends XmlComponent {
})); }));
this.root.push(new NonVisualPicProperties()); this.root.push(new NonVisualPicProperties());
this.root.push(new BlipFill(referenceId)); this.root.push(new BlipFill(referenceId));
this.root.push(new ShapeProperties()); this.root.push(new ShapeProperties(x, y));
} }
} }

View File

@ -4,12 +4,12 @@ import { ExtentsAttributes } from "./extents-attributes";
export class Extents extends XmlComponent { export class Extents extends XmlComponent {
constructor() { constructor(x: number, y: number) {
super("a:ext"); super("a:ext");
this.root.push(new ExtentsAttributes({ this.root.push(new ExtentsAttributes({
cx: 3162300, cx: x,
cy: 2857500, cy: y,
})); }));
} }
} }

View File

@ -5,10 +5,10 @@ import { Offset } from "./offset/off";
export class Form extends XmlComponent { export class Form extends XmlComponent {
constructor() { constructor(x: number, y: number) {
super("a:xfrm"); super("a:xfrm");
this.root.push(new Extents()); this.root.push(new Extents(x, y));
this.root.push(new Offset()); this.root.push(new Offset());
} }
} }

View File

@ -8,14 +8,14 @@ import { ShapePropertiesAttributes } from "./shape-properties-attributes";
export class ShapeProperties extends XmlComponent { export class ShapeProperties extends XmlComponent {
constructor() { constructor(x: number, y: number) {
super("pic:spPr"); super("pic:spPr");
this.root.push(new ShapePropertiesAttributes({ this.root.push(new ShapePropertiesAttributes({
bwMode: "auto", bwMode: "auto",
})); }));
this.root.push(new Form()); this.root.push(new Form(x, y));
this.root.push(new PresetGeometry()); this.root.push(new PresetGeometry());
// this.root.push(new NoFill()); // this.root.push(new NoFill());
// this.root.push(new Outline()); // this.root.push(new Outline());

View File

@ -13,11 +13,11 @@ class GraphicAttributes extends XmlAttributeComponent<IGraphicProperties> {
export class Graphic extends XmlComponent { export class Graphic extends XmlComponent {
constructor(referenceId: number) { constructor(referenceId: number, x: number, y: number) {
super("a:graphic"); super("a:graphic");
this.root.push(new GraphicAttributes({ this.root.push(new GraphicAttributes({
a: "http://schemas.openxmlformats.org/drawingml/2006/main", a: "http://schemas.openxmlformats.org/drawingml/2006/main",
})); }));
this.root.push(new GraphicData(referenceId)); this.root.push(new GraphicData(referenceId, x, y));
} }
} }

View File

@ -1,4 +1,5 @@
// http://officeopenxml.com/drwPicInline.php // http://officeopenxml.com/drwPicInline.php
import { IMediaDataDimensions } from "file/media";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { DocProperties } from "./doc-properties/doc-properties"; import { DocProperties } from "./doc-properties/doc-properties";
import { EffectExtent } from "./effect-extent/effect-extent"; import { EffectExtent } from "./effect-extent/effect-extent";
@ -9,7 +10,7 @@ import { InlineAttributes } from "./inline-attributes";
export class Inline extends XmlComponent { export class Inline extends XmlComponent {
constructor(referenceId: number) { constructor(referenceId: number, dimensions: IMediaDataDimensions) {
super("wp:inline"); super("wp:inline");
this.root.push(new InlineAttributes({ this.root.push(new InlineAttributes({
@ -19,10 +20,10 @@ export class Inline extends XmlComponent {
distR: 0, 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 EffectExtent());
this.root.push(new DocProperties()); this.root.push(new DocProperties());
this.root.push(new GraphicFrameProperties()); this.root.push(new GraphicFrameProperties());
this.root.push(new Graphic(referenceId)); this.root.push(new Graphic(referenceId, dimensions.emus.x, dimensions.emus.y));
} }
} }

View File

@ -1,8 +1,20 @@
import * as fs from "fs"; import * as fs from "fs";
export interface IMediaDataDimensions {
pixels: {
x: number;
y: number;
};
emus: {
x: number;
y: number;
};
}
export interface IMediaData { export interface IMediaData {
referenceId: number; referenceId: number;
stream: fs.ReadStream; stream: fs.ReadStream;
path: string; path: string;
fileName: string; fileName: string;
dimensions: IMediaDataDimensions;
} }

View File

@ -1,6 +1,8 @@
import * as fs from "fs"; import * as fs from "fs";
import * as sizeOf from "image-size";
import * as path from "path"; import * as path from "path";
import { IMediaData } from "./data";
import {IMediaData} from "./data";
export class Media { export class Media {
private map: Map<string, IMediaData>; private map: Map<string, IMediaData>;
@ -21,11 +23,23 @@ export class Media {
public addMedia(filePath: string): IMediaData { public addMedia(filePath: string): IMediaData {
const key = path.basename(filePath); const key = path.basename(filePath);
const dimensions = sizeOf(filePath);
const imageData = { const imageData = {
referenceId: this.map.values.length + 3, referenceId: this.map.values.length + 3,
stream: fs.createReadStream(filePath), stream: fs.createReadStream(filePath),
path: filePath, path: filePath,
fileName: key, fileName: key,
dimensions: {
pixels: {
x: dimensions.width,
y: dimensions.height,
},
emus: {
x: dimensions.width * 9525,
y: dimensions.height * 9525,
},
},
}; };
this.map.set(key, imageData); this.map.set(key, imageData);