Files
docx-js/src/file/drawing/anchor/anchor.ts

107 lines
5.2 KiB
TypeScript
Raw Normal View History

// http://officeopenxml.com/drwPicFloating.php
import { IMediaData, IMediaDataTransformation } from "@file/media";
import { XmlComponent } from "@file/xml-components";
2018-06-09 23:49:01 +01:00
import { IDrawingOptions } from "../drawing";
import { HorizontalPosition, IFloating, SimplePos, VerticalPosition } from "../floating";
2018-06-09 23:49:01 +01:00
import { Graphic } from "../inline/graphic";
2019-01-10 02:10:20 +00:00
import { TextWrappingType, WrapNone, WrapSquare, WrapTight, WrapTopAndBottom } from "../text-wrap";
2018-06-09 23:49:01 +01:00
import { DocProperties } from "./../doc-properties/doc-properties";
import { createEffectExtent } from "./../effect-extent/effect-extent";
2018-06-09 23:49:01 +01:00
import { Extent } from "./../extent/extent";
import { GraphicFrameProperties } from "./../graphic-frame/graphic-frame-properties";
import { AnchorAttributes } from "./anchor-attributes";
2021-05-26 09:28:31 +03:00
// <xsd:complexType name="CT_Anchor">
// <xsd:sequence>
// <xsd:element name="simplePos" type="a:CT_Point2D"/>
// <xsd:element name="positionH" type="CT_PosH"/>
// <xsd:element name="positionV" type="CT_PosV"/>
// <xsd:element name="extent" type="a:CT_PositiveSize2D"/>
// <xsd:element name="effectExtent" type="CT_EffectExtent" minOccurs="0"/>
// <xsd:group ref="EG_WrapType"/>
// <xsd:element name="docPr" type="a:CT_NonVisualDrawingProps" minOccurs="1" maxOccurs="1"/>
// <xsd:element name="cNvGraphicFramePr" type="a:CT_NonVisualGraphicFrameProperties"
// minOccurs="0" maxOccurs="1"/>
// <xsd:element ref="a:graphic" minOccurs="1" maxOccurs="1"/>
// </xsd:sequence>
// <xsd:attribute name="distT" type="ST_WrapDistance" use="optional"/>
// <xsd:attribute name="distB" type="ST_WrapDistance" use="optional"/>
// <xsd:attribute name="distL" type="ST_WrapDistance" use="optional"/>
// <xsd:attribute name="distR" type="ST_WrapDistance" use="optional"/>
// <xsd:attribute name="simplePos" type="xsd:boolean"/>
// <xsd:attribute name="relativeHeight" type="xsd:unsignedInt" use="required"/>
// <xsd:attribute name="behindDoc" type="xsd:boolean" use="required"/>
// <xsd:attribute name="locked" type="xsd:boolean" use="required"/>
// <xsd:attribute name="layoutInCell" type="xsd:boolean" use="required"/>
// <xsd:attribute name="hidden" type="xsd:boolean" use="optional"/>
// <xsd:attribute name="allowOverlap" type="xsd:boolean" use="required"/>
// </xsd:complexType>
export class Anchor extends XmlComponent {
public constructor({
mediaData,
transform,
drawingOptions,
}: {
readonly mediaData: IMediaData;
readonly transform: IMediaDataTransformation;
readonly drawingOptions: IDrawingOptions;
}) {
super("wp:anchor");
2020-12-25 00:07:57 +00:00
const floating: IFloating = {
allowOverlap: true,
behindDocument: false,
lockAnchor: false,
layoutInCell: true,
verticalPosition: {},
horizontalPosition: {},
...drawingOptions.floating,
};
2019-01-10 02:10:20 +00:00
this.root.push(
new AnchorAttributes({
2020-12-25 00:07:57 +00:00
distT: floating.margins ? floating.margins.top || 0 : 0,
distB: floating.margins ? floating.margins.bottom || 0 : 0,
distL: floating.margins ? floating.margins.left || 0 : 0,
distR: floating.margins ? floating.margins.right || 0 : 0,
simplePos: "0", // note: word doesn't fully support - so we use 0
allowOverlap: floating.allowOverlap === true ? "1" : "0",
behindDoc: floating.behindDocument === true ? "1" : "0",
locked: floating.lockAnchor === true ? "1" : "0",
layoutInCell: floating.layoutInCell === true ? "1" : "0",
2021-03-15 02:41:37 +00:00
relativeHeight: floating.zIndex ? floating.zIndex : transform.emus.y,
}),
);
this.root.push(new SimplePos());
this.root.push(new HorizontalPosition(floating.horizontalPosition));
this.root.push(new VerticalPosition(floating.verticalPosition));
2021-03-15 02:41:37 +00:00
this.root.push(new Extent(transform.emus.x, transform.emus.y));
this.root.push(createEffectExtent({ top: 0, right: 0, bottom: 0, left: 0 }));
2019-01-10 02:10:20 +00:00
if (drawingOptions.floating !== undefined && drawingOptions.floating.wrap !== undefined) {
switch (drawingOptions.floating.wrap.type) {
case TextWrappingType.SQUARE:
this.root.push(new WrapSquare(drawingOptions.floating.wrap, drawingOptions.floating.margins));
break;
2019-01-10 02:10:20 +00:00
case TextWrappingType.TIGHT:
this.root.push(new WrapTight(drawingOptions.floating.margins));
break;
2019-01-10 02:10:20 +00:00
case TextWrappingType.TOP_AND_BOTTOM:
this.root.push(new WrapTopAndBottom(drawingOptions.floating.margins));
break;
2019-01-10 02:10:20 +00:00
case TextWrappingType.NONE:
default:
this.root.push(new WrapNone());
}
} else {
this.root.push(new WrapNone());
}
2022-10-26 23:09:36 +01:00
this.root.push(new DocProperties(drawingOptions.docProperties));
this.root.push(new GraphicFrameProperties());
this.root.push(new Graphic({ mediaData, transform, outline: drawingOptions.outline }));
}
}