package com.dk.mdm.infrastructure.util;

import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.layout.LayoutArea;
import com.itextpdf.layout.layout.LayoutContext;
import com.itextpdf.layout.layout.LayoutResult;
import com.itextpdf.layout.property.Property;
import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.layout.renderer.DivRenderer;

/**
 * @desc   : 是文字溢出当前DIV
 * https://qa.1r1g.com/sf/ask/3472120991/
 * @author : 洪旭东
 * @date   : 2023-04-07 16:11
 */
public class OverflowHiddenDivRenderer extends DivRenderer {
    public OverflowHiddenDivRenderer(Div modelElement) {
        super(modelElement);
    }

    @Override
    public Rectangle getOccupiedAreaBBox() {
        Rectangle rectangle = super.getOccupiedAreaBBox();
        if (height != null) {
            if (rectangle.getHeight() > height.getValue()) {
                rectangle.moveUp(rectangle.getHeight() - height.getValue()).setHeight(height.getValue());
            }
        }
        return rectangle;
    }

    @Override
    public LayoutResult layout(LayoutContext layoutContext) {
        height = getPropertyAsUnitValue(Property.HEIGHT);
        deleteProperty(Property.HEIGHT);
        LayoutResult layoutResult = super.layout(layoutContext);
        LayoutArea layoutArea = layoutResult.getOccupiedArea();
        if (layoutArea != null) {
            layoutArea.setBBox(getOccupiedAreaBBox());
        }
        return layoutResult;
    }

    UnitValue height;
}