Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support max-height for img tag #47

Closed
achuinard opened this issue Nov 1, 2016 · 1 comment
Closed

Support max-height for img tag #47

achuinard opened this issue Nov 1, 2016 · 1 comment

Comments

@achuinard
Copy link
Contributor

I don't believe the library is supporting a CSS max-height property. I am new to the library but found the same problem in Flying Saucer when I was using that. I came up with this to fix it:


    private static final class MaxWidthHeightSupportingRenderer extends ITextReplacedElementFactory {
        MaxWidthHeightSupportingRenderer(ITextOutputDevice outputDevice) {
            super(outputDevice);
        }

        @Override
        public ReplacedElement createReplacedElement(LayoutContext c, BlockBox box, UserAgentCallback uac, int cssWidth, int cssHeight) {
            Element e = box.getElement();
            if (e != null && "img".equals(e.getNodeName())) {
                String srcAttr = e.getAttribute("src");

                if (srcAttr != null && srcAttr.length() > 0) {
                    FSImage fsImage = uac.getImageResource(srcAttr).getImage();
                    if (fsImage != null) {
                        if (cssWidth != -1 || cssHeight != -1) {
                            long maxWidth = box.getStyle().asLength(c, CSSName.MAX_WIDTH).value();
                            long maxHeight = box.getStyle().asLength(c, CSSName.MAX_HEIGHT).value();
                            if (cssHeight > maxHeight && cssHeight >= cssWidth) {
                                fsImage.scale(-1, (int) maxHeight);
                            } else if (cssWidth > maxWidth) {
                                fsImage.scale((int) maxWidth, -1);
                            } else {
                                fsImage.scale(cssWidth, cssHeight);
                            }
                        }
                        return new ITextImageElement(fsImage);
                    }
                }
            }
            return super.createReplacedElement(c, box, uac, cssWidth, cssHeight);
        }
    }

I don't know how relevant that is to this project, as I just started using the library and need to learn more about how it works. Excited to see this though!

@achuinard
Copy link
Contributor Author

I've created pull request #48 which I believe fixes this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant