<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"""Custom element classes for shape-related elements like `&lt;w:inline&gt;`."""

from __future__ import annotations

from typing import TYPE_CHECKING

from docx.oxml.ns import nsdecls
from docx.oxml.parser import parse_xml
from docx.oxml.simpletypes import (
    ST_Coordinate,
    ST_DrawingElementId,
    ST_PositiveCoordinate,
    ST_RelationshipId,
    XsdString,
    XsdToken,
)
from docx.oxml.xmlchemy import (
    BaseOxmlElement,
    OneAndOnlyOne,
    OptionalAttribute,
    RequiredAttribute,
    ZeroOrOne,
)

if TYPE_CHECKING:
    from docx.shared import Length


class CT_Anchor(BaseOxmlElement):
    """`&lt;wp:anchor&gt;` element, container for a "floating" shape."""


class CT_Blip(BaseOxmlElement):
    """``&lt;a:blip&gt;`` element, specifies image source and adjustments such as alpha and
    tint."""

    embed = OptionalAttribute("r:embed", ST_RelationshipId)
    link = OptionalAttribute("r:link", ST_RelationshipId)


class CT_BlipFillProperties(BaseOxmlElement):
    """``&lt;pic:blipFill&gt;`` element, specifies picture properties."""

    blip = ZeroOrOne("a:blip", successors=("a:srcRect", "a:tile", "a:stretch"))


class CT_GraphicalObject(BaseOxmlElement):
    """``&lt;a:graphic&gt;`` element, container for a DrawingML object."""

    graphicData = OneAndOnlyOne("a:graphicData")


class CT_GraphicalObjectData(BaseOxmlElement):
    """``&lt;a:graphicData&gt;`` element, container for the XML of a DrawingML object."""

    pic = ZeroOrOne("pic:pic")
    uri = RequiredAttribute("uri", XsdToken)


class CT_Inline(BaseOxmlElement):
    """`&lt;wp:inline&gt;` element, container for an inline shape."""

    extent = OneAndOnlyOne("wp:extent")
    docPr = OneAndOnlyOne("wp:docPr")
    graphic = OneAndOnlyOne("a:graphic")

    @classmethod
    def new(cls, cx: Length, cy: Length, shape_id: int, pic: CT_Picture) -&gt; CT_Inline:
        """Return a new ``&lt;wp:inline&gt;`` element populated with the values passed as
        parameters."""
        inline = parse_xml(cls._inline_xml())
        inline.extent.cx = cx
        inline.extent.cy = cy
        inline.docPr.id = shape_id
        inline.docPr.name = "Picture %d" % shape_id
        inline.graphic.graphicData.uri = (
            "http://schemas.openxmlformats.org/drawingml/2006/picture"
        )
        inline.graphic.graphicData._insert_pic(pic)
        return inline

    @classmethod
    def new_pic_inline(
        cls, shape_id: int, rId: str, filename: str, cx: Length, cy: Length
    ) -&gt; CT_Inline:
        """Create `wp:inline` element containing a `pic:pic` element.

        The contents of the `pic:pic` element is taken from the argument values.
        """
        pic_id = 0  # Word doesn't seem to use this, but does not omit it
        pic = CT_Picture.new(pic_id, filename, rId, cx, cy)
        inline = cls.new(cx, cy, shape_id, pic)
        inline.graphic.graphicData._insert_pic(pic)
        return inline

    @classmethod
    def _inline_xml(cls):
        return (
            "&lt;wp:inline %s&gt;\n"
            '  &lt;wp:extent cx="914400" cy="914400"/&gt;\n'
            '  &lt;wp:docPr id="666" name="unnamed"/&gt;\n'
            "  &lt;wp:cNvGraphicFramePr&gt;\n"
            '    &lt;a:graphicFrameLocks noChangeAspect="1"/&gt;\n'
            "  &lt;/wp:cNvGraphicFramePr&gt;\n"
            "  &lt;a:graphic&gt;\n"
            '    &lt;a:graphicData uri="URI not set"/&gt;\n'
            "  &lt;/a:graphic&gt;\n"
            "&lt;/wp:inline&gt;" % nsdecls("wp", "a", "pic", "r")
        )


class CT_NonVisualDrawingProps(BaseOxmlElement):
    """Used for ``&lt;wp:docPr&gt;`` element, and perhaps others.

    Specifies the id and name of a DrawingML drawing.
    """

    id = RequiredAttribute("id", ST_DrawingElementId)
    name = RequiredAttribute("name", XsdString)


class CT_NonVisualPictureProperties(BaseOxmlElement):
    """``&lt;pic:cNvPicPr&gt;`` element, specifies picture locking and resize behaviors."""


class CT_Picture(BaseOxmlElement):
    """``&lt;pic:pic&gt;`` element, a DrawingML picture."""

    nvPicPr = OneAndOnlyOne("pic:nvPicPr")
    blipFill = OneAndOnlyOne("pic:blipFill")
    spPr = OneAndOnlyOne("pic:spPr")

    @classmethod
    def new(cls, pic_id, filename, rId, cx, cy):
        """Return a new ``&lt;pic:pic&gt;`` element populated with the minimal contents
        required to define a viable picture element, based on the values passed as
        parameters."""
        pic = parse_xml(cls._pic_xml())
        pic.nvPicPr.cNvPr.id = pic_id
        pic.nvPicPr.cNvPr.name = filename
        pic.blipFill.blip.embed = rId
        pic.spPr.cx = cx
        pic.spPr.cy = cy
        return pic

    @classmethod
    def _pic_xml(cls):
        return (
            "&lt;pic:pic %s&gt;\n"
            "  &lt;pic:nvPicPr&gt;\n"
            '    &lt;pic:cNvPr id="666" name="unnamed"/&gt;\n'
            "    &lt;pic:cNvPicPr/&gt;\n"
            "  &lt;/pic:nvPicPr&gt;\n"
            "  &lt;pic:blipFill&gt;\n"
            "    &lt;a:blip/&gt;\n"
            "    &lt;a:stretch&gt;\n"
            "      &lt;a:fillRect/&gt;\n"
            "    &lt;/a:stretch&gt;\n"
            "  &lt;/pic:blipFill&gt;\n"
            "  &lt;pic:spPr&gt;\n"
            "    &lt;a:xfrm&gt;\n"
            '      &lt;a:off x="0" y="0"/&gt;\n'
            '      &lt;a:ext cx="914400" cy="914400"/&gt;\n'
            "    &lt;/a:xfrm&gt;\n"
            '    &lt;a:prstGeom prst="rect"/&gt;\n'
            "  &lt;/pic:spPr&gt;\n"
            "&lt;/pic:pic&gt;" % nsdecls("pic", "a", "r")
        )


class CT_PictureNonVisual(BaseOxmlElement):
    """``&lt;pic:nvPicPr&gt;`` element, non-visual picture properties."""

    cNvPr = OneAndOnlyOne("pic:cNvPr")


class CT_Point2D(BaseOxmlElement):
    """Used for ``&lt;a:off&gt;`` element, and perhaps others.

    Specifies an x, y coordinate (point).
    """

    x = RequiredAttribute("x", ST_Coordinate)
    y = RequiredAttribute("y", ST_Coordinate)


class CT_PositiveSize2D(BaseOxmlElement):
    """Used for ``&lt;wp:extent&gt;`` element, and perhaps others later.

    Specifies the size of a DrawingML drawing.
    """

    cx = RequiredAttribute("cx", ST_PositiveCoordinate)
    cy = RequiredAttribute("cy", ST_PositiveCoordinate)


class CT_PresetGeometry2D(BaseOxmlElement):
    """``&lt;a:prstGeom&gt;`` element, specifies an preset autoshape geometry, such as
    ``rect``."""


class CT_RelativeRect(BaseOxmlElement):
    """``&lt;a:fillRect&gt;`` element, specifying picture should fill containing rectangle
    shape."""


class CT_ShapeProperties(BaseOxmlElement):
    """``&lt;pic:spPr&gt;`` element, specifies size and shape of picture container."""

    xfrm = ZeroOrOne(
        "a:xfrm",
        successors=(
            "a:custGeom",
            "a:prstGeom",
            "a:ln",
            "a:effectLst",
            "a:effectDag",
            "a:scene3d",
            "a:sp3d",
            "a:extLst",
        ),
    )

    @property
    def cx(self):
        """Shape width as an instance of Emu, or None if not present."""
        xfrm = self.xfrm
        if xfrm is None:
            return None
        return xfrm.cx

    @cx.setter
    def cx(self, value):
        xfrm = self.get_or_add_xfrm()
        xfrm.cx = value

    @property
    def cy(self):
        """Shape height as an instance of Emu, or None if not present."""
        xfrm = self.xfrm
        if xfrm is None:
            return None
        return xfrm.cy

    @cy.setter
    def cy(self, value):
        xfrm = self.get_or_add_xfrm()
        xfrm.cy = value


class CT_StretchInfoProperties(BaseOxmlElement):
    """``&lt;a:stretch&gt;`` element, specifies how picture should fill its containing
    shape."""


class CT_Transform2D(BaseOxmlElement):
    """``&lt;a:xfrm&gt;`` element, specifies size and shape of picture container."""

    off = ZeroOrOne("a:off", successors=("a:ext",))
    ext = ZeroOrOne("a:ext", successors=())

    @property
    def cx(self):
        ext = self.ext
        if ext is None:
            return None
        return ext.cx

    @cx.setter
    def cx(self, value):
        ext = self.get_or_add_ext()
        ext.cx = value

    @property
    def cy(self):
        ext = self.ext
        if ext is None:
            return None
        return ext.cy

    @cy.setter
    def cy(self, value):
        ext = self.get_or_add_ext()
        ext.cy = value
</pre></body></html>