Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
*/
package com.openhtmltopdf.pdfboxout;

import org.w3c.dom.Element;

import com.openhtmltopdf.css.constants.CSSName;
import com.openhtmltopdf.extend.FSImage;
import com.openhtmltopdf.extend.ReplacedElement;
import com.openhtmltopdf.extend.ReplacedElementFactory;
Expand All @@ -29,6 +28,7 @@
import com.openhtmltopdf.layout.LayoutContext;
import com.openhtmltopdf.render.BlockBox;
import com.openhtmltopdf.simple.extend.FormSubmissionListener;
import org.w3c.dom.Element;

public class PdfBoxReplacedElementFactory implements ReplacedElementFactory {
private final PdfBoxOutputDevice _outputDevice;
Expand All @@ -40,25 +40,48 @@ public PdfBoxReplacedElementFactory(PdfBoxOutputDevice outputDevice, SVGDrawer s
}

public ReplacedElement createReplacedElement(LayoutContext c, BlockBox box,
UserAgentCallback uac, int cssWidth, int cssHeight) {
UserAgentCallback uac, int cssWidth, int cssHeight) {
Element e = box.getElement();
if (e == null) {
return null;
}

String nodeName = e.getNodeName();

if (nodeName.equals("svg") &&
_svgImpl != null) {
if (nodeName.equals("svg") && _svgImpl != null) {
return new PdfBoxSVGReplacedElement(e, _svgImpl, cssWidth, cssHeight, c.getSharedContext().getDotsPerPixel());
}
else if (nodeName.equals("img")) {
} else if (nodeName.equals("img")) {
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) {
fsImage.scale(cssWidth, cssHeight);
boolean hasMaxProperty = !box.getStyle().isMaxWidthNone() || !box.getStyle().isMaxHeightNone();
if (cssWidth == -1 && cssHeight == -1) {
if (hasMaxProperty) {
long maxWidth = box.getStyle().asLength(c, CSSName.MAX_WIDTH).value();
long maxHeight = box.getStyle().asLength(c, CSSName.MAX_HEIGHT).value();
int intrinsicHeight = fsImage.getHeight();
int intrinsicWidth = fsImage.getWidth();
if (intrinsicHeight > maxHeight && intrinsicHeight >= intrinsicWidth) {
fsImage.scale(-1, (int) maxHeight);
} else if (intrinsicWidth > maxWidth) {
fsImage.scale((int) maxWidth, -1);
}
}
} else {
if (hasMaxProperty) {
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);
}
} else {
fsImage.scale(cssWidth, cssHeight);
}
}
return new PdfBoxImageElement(fsImage);
}
Expand Down