#!/usr/bin/env python3
# (C) 2026 Cadence Design Systems, Inc. (Cadence)
# All rights reserved.
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of Cadence products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. Cadence claims no rights to Customer's
# modifications. Modification of Sample Code is at Customer's sole and
# exclusive risk. Sample Code may require Customer to have a then
# current license or subscription to the applicable Cadence offering.
# THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED.  OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall Cadence be
# liable for any damages or liability in connection with the Sample Code
# or its use.


"""Generates image of Jiji from the Miyazaki's film Kiki's Delivery Service."""

import os
import sys

from openeye import oechem, oedepict


def main() -> int:
    """Depict Jiji."""
    image_width, image_height = (
        200,
        200,
    )
    image = oedepict.OEImage(image_width, image_height, oechem.OEWhite)

    black_pen = oedepict.OEPen(oechem.OEBlack, oechem.OEBlack, oedepict.OEFill_On, 1.0)
    white_pen = oedepict.OEPen(oechem.OEWhite, oechem.OEWhite, oedepict.OEFill_On, 1.0)
    pink_pen = oedepict.OEPen(
        oechem.OEColor(250, 190, 200),
        oechem.OEColor(250, 190, 200),
        oedepict.OEFill_On,
        1.0,
    )

    center = oedepict.OE2DPoint(100, 120)

    # ears

    to = center + oedepict.OE2DPoint(-60, -110)
    rt = center + oedepict.OE2DPoint(-15, -55)
    lf = center + oedepict.OE2DPoint(-60, -25)

    image.DrawCubicBezier(
        to,
        to + oedepict.OE2DPoint(+5, -15),
        rt + oedepict.OE2DPoint(0, -25),
        rt,
        black_pen,
    )
    image.DrawCubicBezier(
        to,
        to + oedepict.OE2DPoint(-15, +25),
        lf + oedepict.OE2DPoint(-15, -25),
        lf,
        black_pen,
    )
    image.DrawTriangle(to, rt, lf, black_pen)

    to += oedepict.OE2DPoint(0, 5)
    rt += oedepict.OE2DPoint(-15, 5)
    lf += oedepict.OE2DPoint(+10, 5)
    image.DrawCubicBezier(
        to,
        to + oedepict.OE2DPoint(5, -15),
        rt + oedepict.OE2DPoint(0, -25),
        rt,
        pink_pen,
    )
    image.DrawCubicBezier(
        to,
        to + oedepict.OE2DPoint(-15, +25),
        lf + oedepict.OE2DPoint(-15, -25),
        lf,
        pink_pen,
    )
    image.DrawTriangle(to, rt, lf, pink_pen)

    to = center + oedepict.OE2DPoint(+60, -110)
    lf = center + oedepict.OE2DPoint(+15, -55)
    rt = center + oedepict.OE2DPoint(+60, -25)

    image.DrawCubicBezier(
        to,
        to + oedepict.OE2DPoint(-5, -15),
        lf + oedepict.OE2DPoint(0, -25),
        lf,
        black_pen,
    )
    image.DrawCubicBezier(
        to,
        to + oedepict.OE2DPoint(15, +25),
        rt + oedepict.OE2DPoint(+15, -25),
        rt,
        black_pen,
    )
    image.DrawTriangle(to, lf, rt, black_pen)

    to += oedepict.OE2DPoint(0, 5)
    lf += oedepict.OE2DPoint(+15, 5)
    rt += oedepict.OE2DPoint(-10, 5)

    image.DrawCubicBezier(
        to,
        to + oedepict.OE2DPoint(-5, -15),
        lf + oedepict.OE2DPoint(0, -25),
        lf,
        pink_pen,
    )
    image.DrawCubicBezier(
        to,
        to + oedepict.OE2DPoint(+15, +25),
        rt + oedepict.OE2DPoint(+15, -25),
        rt,
        pink_pen,
    )
    image.DrawTriangle(to, lf, rt, pink_pen)

    # head

    _draw_ellipse(image, center, 180, 110, black_pen)

    # eyes

    _draw_ellipse(image, center + oedepict.OE2DPoint(+30, 0), 50, 40, white_pen)
    _draw_ellipse(image, center + oedepict.OE2DPoint(+20, 0), 10, 10, black_pen)
    _draw_ellipse(image, center + oedepict.OE2DPoint(-30, 0), 50, 40, white_pen)
    _draw_ellipse(image, center + oedepict.OE2DPoint(-20, 0), 10, 10, black_pen)

    # nose

    _draw_ellipse(image, center + oedepict.OE2DPoint(0, 20), 20, 10, pink_pen)

    # add OEDepict TK watermark

    font = oedepict.OEFont(
        oedepict.OEFontFamily_Default,
        oedepict.OEFontStyle_Default,
        10,
        oedepict.OEAlignment_Right,
        oechem.OELightGrey,
    )
    text = "Generated by OEDepict TK"
    image.DrawText(
        oedepict.OE2DPoint(image_width - 5.0, image_height - 5.0), text, font
    )

    # write image

    oedepict.OEWriteImage("jiji.svg", image)
    return os.EX_OK


def _draw_ellipse(
    image: oedepict.OEImageBase,
    center: oedepict.OE2DPoint,
    width: float,
    height: float,
    pen: oedepict.OEPen,
) -> None:
    image.DrawCubicBezier(
        center + oedepict.OE2DPoint(0.0, -height / 2.0),
        center + oedepict.OE2DPoint(+width / 2.0, -height / 2.0),
        center + oedepict.OE2DPoint(+width / 2.0, +height / 2.0),
        center + oedepict.OE2DPoint(0.0, +height / 2.0),
        pen,
    )

    image.DrawCubicBezier(
        center + oedepict.OE2DPoint(0.0, +height / 2.0),
        center + oedepict.OE2DPoint(-width / 2.0, +height / 2.0),
        center + oedepict.OE2DPoint(-width / 2.0, -height / 2.0),
        center + oedepict.OE2DPoint(0.0, -height / 2.0),
        pen,
    )


if __name__ == "__main__":
    sys.exit(main())
