public static Rectangle computeReplacedObjectSize(ReplacedContent obj, ElementBox box)
    {
        int boxw; //resulting size
        int boxh;
        
        int intw; //intrinsic sizes
        int inth;
        float intr;
        if (obj != null)
        {
            intw = obj.getIntrinsicWidth();
            inth = obj.getIntrinsicHeight();
            if (intw == 0 || inth == 0)
            {
                log.warn("Obtained a zero intrinsic width or height for " + obj.toString());
                intw = inth = 1; //a fallback for avoiding zeros in ratios
            }
            intr = (float) intw / inth;
            boxw = intw;
            boxh = inth;
        }
        else
        {
            boxw = intw = 20; //some reasonable default values
            boxh = inth = 20;
            intr = 1.0f;
        }
        
        Rectangle cb = box.getContainingBlock();
        
        //decode the width/height attributes
        Element el = box.getElement();
        int atrw = -1;
        int atrh = -1;
        try {
            if (!HTMLNorm.getAttribute(el, "width").equals(""))
            {
                atrw = HTMLNorm.computeAttributeLength(HTMLNorm.getAttribute(el, "width"), cb.width);
            }
        } catch (NumberFormatException e) {
            log.info("Invalid width value: " + HTMLNorm.getAttribute(el, "width"));
        }
        try {
            if (!HTMLNorm.getAttribute(el, "height").equals(""))
            {
                //Total heights used for percentages -- find the nearest containing block with a fixed height
                //This is a quirks mode behavior (percentages in attributes should not be used in standards mode).
                Box cbox = box.getContainingBlockBox();
                while (cbox != null && !cbox.hasFixedHeight() && !(cbox instanceof Viewport))
                    cbox = cbox.getContainingBlockBox();
                atrh = HTMLNorm.computeAttributeLength(HTMLNorm.getAttribute(el, "height"), cbox.getContentHeight());
            }
        } catch (NumberFormatException e) {
            log.info("Invalid height value: " + HTMLNorm.getAttribute(el, "width"));
        }

        //use standard CSS2.1 computation of the containing block height
        final int twidth = cb.width;
        final int theight = cb.height;
        //compute dimensions from styles (styles should override the attributes)
        CSSDecoder dec = new CSSDecoder(box.getVisualContext());
        CSSProperty.Width width = box.getStyle().getProperty("width");
        TermLengthOrPercent vWidth = null;
        if (width == CSSProperty.Width.AUTO)
            width = null; //auto and null are equal for width
        if (width != null)
            vWidth = box.getLengthValue("width");
        
        CSSProperty.Height height = box.getStyle().getProperty("height");
        TermLengthOrPercent vHeight = null;
        if (height == CSSProperty.Height.AUTO)
            height = null; //auto and null are equal for height
        if (height != null)
        {
            vHeight = box.getLengthValue("height");
            if (vHeight.isPercentage() && !box.getContainingBlockBox().hasFixedHeight())
                height = null; //the percentage heights are treated as 'auto' when the containing block height is not set
        }
        
        if (width == null && height == null && atrw == -1 && atrh == -1)
        {
            final int[] result = applyCombinedLimits(boxw, boxh, box, dec, twidth, theight);
            boxw = result[0];
            boxh = result[1];
        }
        else if (width == null && atrw == -1 && (height != null || atrh != -1))
        {
            //compute boxh, boxw is intrinsic
            boxh = dec.getLength(vHeight, false, atrh, 0, theight);
            boxh = applyHeightLimits(boxh, box, dec, theight);
            //boxw intrinsic value
            boxw = Math.round(intr * boxh);
            boxw = applyWidthLimits(boxw, box, dec, twidth);
        }
        else if ((width != null || atrw != -1) && height == null && atrh == -1)
        {
            //compute boxw, boxh is intrinsic
            boxw = dec.getLength(vWidth, false, atrw, 0, twidth);
            boxw = applyWidthLimits(boxw, box, dec, twidth);
            //boxh intrinsic value
            boxh = Math.round(boxw / intr);
            boxh = applyHeightLimits(boxh, box, dec, theight);
        }
        else
        {
            boxw = dec.getLength(vWidth, false, atrw, 0, twidth);
            boxw = applyWidthLimits(boxw, box, dec, twidth);
            boxh = dec.getLength(vHeight, false, atrh, 0, theight);
            boxh = applyHeightLimits(boxh, box, dec, theight);
        }
        
        return new Rectangle(boxw, boxh);
    }
public static Rectangle computeReplacedObjectSize ( ReplacedContent obj , ElementBox box ) { int boxw ; int boxh ; int intw ; int inth ; float intr ; if ( obj != null ) { intw = obj . getIntrinsicWidth ( ) ; inth = obj . getIntrinsicHeight ( ) ; if ( intw == 0 || inth == 0 ) { log . warn ( "Obtained a zero intrinsic width or height for " + obj . toString ( ) ) ; intw = inth = 1 ; } intr = ( float ) intw / inth ; boxw = intw ; boxh = inth ; } else { boxw = intw = 20 ; boxh = inth = 20 ; intr = 1.0f ; } Rectangle cb = box . getContainingBlock ( ) ; Element el = box . getElement ( ) ; int atrw = - 1 ; int atrh = - 1 ; try { if ( ! HTMLNorm . getAttribute ( el , "width" ) . equals ( "" ) ) { atrw = HTMLNorm . computeAttributeLength ( HTMLNorm . getAttribute ( el , "width" ) , cb . width ) ; } } catch ( NumberFormatException e ) { log . info ( "Invalid width value: " + HTMLNorm . getAttribute ( el , "width" ) ) ; } try { if ( ! HTMLNorm . getAttribute ( el , "height" ) . equals ( "" ) ) { Box cbox = box . getContainingBlockBox ( ) ; while ( cbox != null && ! cbox . hasFixedHeight ( ) && ! ( cbox instanceof Viewport ) ) cbox = cbox . getContainingBlockBox ( ) ; atrh = HTMLNorm . computeAttributeLength ( HTMLNorm . getAttribute ( el , "height" ) , cbox . getContentHeight ( ) ) ; } } catch ( NumberFormatException e ) { log . info ( "Invalid height value: " + HTMLNorm . getAttribute ( el , "width" ) ) ; } final int twidth = cb . width ; final int theight = cb . height ; CSSDecoder dec = new CSSDecoder ( box . getVisualContext ( ) ) ; CSSProperty . Width width = box . getStyle ( ) . getProperty ( "width" ) ; TermLengthOrPercent vWidth = null ; if ( width == CSSProperty . Width . AUTO ) width = null ; if ( width != null ) vWidth = box . getLengthValue ( "width" ) ; CSSProperty . Height height = box . getStyle ( ) . getProperty ( "height" ) ; TermLengthOrPercent vHeight = null ; if ( height == CSSProperty . Height . AUTO ) height = null ; if ( height != null ) { vHeight = box . getLengthValue ( "height" ) ; if ( vHeight . isPercentage ( ) && ! box . getContainingBlockBox ( ) . hasFixedHeight ( ) ) height = null ; } if ( width == null && height == null && atrw == - 1 && atrh == - 1 ) { final int [ ] result = applyCombinedLimits ( boxw , boxh , box , dec , twidth , theight ) ; boxw = result [ 0 ] ; boxh = result [ 1 ] ; } else if ( width == null && atrw == - 1 && ( height != null || atrh != - 1 ) ) { boxh = dec . getLength ( vHeight , false , atrh , 0 , theight ) ; boxh = applyHeightLimits ( boxh , box , dec , theight ) ; boxw = Math . round ( intr * boxh ) ; boxw = applyWidthLimits ( boxw , box , dec , twidth ) ; } else if ( ( width != null || atrw != - 1 ) && height == null && atrh == - 1 ) { boxw = dec . getLength ( vWidth , false , atrw , 0 , twidth ) ; boxw = applyWidthLimits ( boxw , box , dec , twidth ) ; boxh = Math . round ( boxw / intr ) ; boxh = applyHeightLimits ( boxh , box , dec , theight ) ; } else { boxw = dec . getLength ( vWidth , false , atrw , 0 , twidth ) ; boxw = applyWidthLimits ( boxw , box , dec , twidth ) ; boxh = dec . getLength ( vHeight , false , atrh , 0 , theight ) ; boxh = applyHeightLimits ( boxh , box , dec , theight ) ; } return new Rectangle ( boxw , boxh ) ; }
Computes the width and height of a replaced object based on the following properties:
<ul>
<li>Intrinsic width and height</li>
<li>The <code>width</code> and <code>height</code> attributes</li>
<li>Effective style</li>
</ul>
@param obj The replaced content object
@param box The element box whose size should be computed
@return A rectangle with the width and height set accordingly