|
| 1 | +package com.itextpdf.html2pdf.utils; |
| 2 | + |
| 3 | +import com.itextpdf.kernel.geom.AffineTransform; |
| 4 | +import com.itextpdf.kernel.geom.Matrix; |
| 5 | +import com.itextpdf.kernel.geom.NoninvertibleTransformException; |
| 6 | +import com.itextpdf.kernel.geom.Point; |
| 7 | +import com.itextpdf.kernel.geom.Rectangle; |
| 8 | +import com.itextpdf.kernel.pdf.canvas.parser.EventType; |
| 9 | +import com.itextpdf.kernel.pdf.canvas.parser.data.IEventData; |
| 10 | +import com.itextpdf.kernel.pdf.canvas.parser.data.ImageRenderInfo; |
| 11 | +import com.itextpdf.kernel.pdf.canvas.parser.listener.IEventListener; |
| 12 | + |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Set; |
| 17 | + |
| 18 | +public class ImageSizeMeasuringListener implements IEventListener { |
| 19 | + |
| 20 | + Rectangle cropbox; |
| 21 | + public Rectangle bbox; |
| 22 | + int page; |
| 23 | + |
| 24 | + public ImageSizeMeasuringListener(int page) { |
| 25 | + |
| 26 | + if (cropbox == null) { |
| 27 | + cropbox = new Rectangle(Float.MIN_VALUE, Float.MIN_VALUE, Float.MAX_VALUE, Float.MAX_VALUE); |
| 28 | + } |
| 29 | + this.page = page; |
| 30 | + } |
| 31 | + |
| 32 | + public void eventOccurred(IEventData data, EventType type) { |
| 33 | + switch (type) { |
| 34 | + case RENDER_IMAGE: |
| 35 | + ImageRenderInfo renderInfo = (ImageRenderInfo) data; |
| 36 | + final Matrix imageCtm = renderInfo.getImageCtm(); |
| 37 | + bbox = calcImageRect(imageCtm); |
| 38 | + default: |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public Set<EventType> getSupportedEvents() { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + private double getWidth(Matrix m) { |
| 48 | + return Math.sqrt(Math.pow(m.get(Matrix.I11), 2) + Math.pow(m.get(Matrix.I21), 2)); |
| 49 | + } |
| 50 | + |
| 51 | + private double getHeight(Matrix m) { |
| 52 | + return Math.sqrt(Math.pow(m.get(Matrix.I12), 2) + Math.pow(m.get(Matrix.I22), 2)); |
| 53 | + } |
| 54 | + |
| 55 | + private Rectangle calcImageRect(Matrix ctm) { |
| 56 | + if (ctm == null) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + Point[] points = transformPoints(ctm, false, |
| 61 | + new Point(0, 0), new Point(0, 1), |
| 62 | + new Point(1, 0), new Point(1, 1)); |
| 63 | + |
| 64 | + return getAsRectangle(points[0], points[1], points[2], points[3]); |
| 65 | + } |
| 66 | + |
| 67 | + private Rectangle getAsRectangle(Point p1, Point p2, Point p3, Point p4) { |
| 68 | + List<Double> xs = Arrays.asList(p1.getX(), p2.getX(), p3.getX(), p4.getX()); |
| 69 | + List<Double> ys = Arrays.asList(p1.getY(), p2.getY(), p3.getY(), p4.getY()); |
| 70 | + |
| 71 | + double left = Collections.min(xs); |
| 72 | + double bottom = Collections.min(ys); |
| 73 | + double right = Collections.max(xs); |
| 74 | + double top = Collections.max(ys); |
| 75 | + |
| 76 | + return new Rectangle((float) left, (float) bottom, (float) right, (float) top); |
| 77 | + } |
| 78 | + |
| 79 | + private Point[] transformPoints(Matrix transformationMatrix, boolean inverse, Point... points) { |
| 80 | + AffineTransform t = new AffineTransform(transformationMatrix.get(Matrix.I11), |
| 81 | + transformationMatrix.get(Matrix.I12), |
| 82 | + transformationMatrix.get(Matrix.I21), transformationMatrix.get(Matrix.I22), |
| 83 | + transformationMatrix.get(Matrix.I31), transformationMatrix.get(Matrix.I32)); |
| 84 | + Point[] transformed = new Point[points.length]; |
| 85 | + |
| 86 | + if (inverse) { |
| 87 | + try { |
| 88 | + t = t.createInverse(); |
| 89 | + } catch (NoninvertibleTransformException e) { |
| 90 | + throw new RuntimeException("Exception during effective image rectangle calculation", e); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + t.transform(points, 0, transformed, 0, points.length); |
| 95 | + |
| 96 | + return transformed; |
| 97 | + } |
| 98 | +} |
0 commit comments