@@ -173,6 +173,8 @@ public class PdfCanvas implements Serializable {
173
173
private static final PdfSpecialCs .Pattern pattern = new PdfSpecialCs .Pattern ();
174
174
private static final long serialVersionUID = -4706222391732334562L ;
175
175
176
+ private static final float IDENTITY_MATRIX_EPS = 1e-4f ;
177
+
176
178
/**
177
179
* a LIFO stack of graphics state saved states.
178
180
*/
@@ -1995,6 +1997,8 @@ public PdfXObject addImage(ImageData image, Rectangle rect, boolean asInline) {
1995
1997
* @param asInline true if to add image as in-line
1996
1998
* @return the created imageXObject or null in case of in-line image (asInline = true)
1997
1999
* @see #concatMatrix(double, double, double, double, double, double)
2000
+ * @see PdfXObject#calculateProportionallyFitRectangleWithWidth(PdfXObject, float, float, float)
2001
+ * @see PdfXObject#calculateProportionallyFitRectangleWithHeight(PdfXObject, float, float, float)
1998
2002
*/
1999
2003
public PdfXObject addImageFittedIntoRectangle (ImageData image , Rectangle rect , boolean asInline ) {
2000
2004
return addImageWithTransformationMatrix (image , rect .getWidth (), 0 , 0 , rect .getHeight (),
@@ -2124,7 +2128,7 @@ public PdfXObject addImage(ImageData image, float x, float y, float height, bool
2124
2128
*/
2125
2129
public PdfCanvas addXObjectWithTransformationMatrix (PdfXObject xObject , float a , float b , float c , float d , float e , float f ) {
2126
2130
if (xObject instanceof PdfFormXObject ) {
2127
- return addFormWithTransformationMatrix ((PdfFormXObject ) xObject , a , b , c , d , e , f );
2131
+ return addFormWithTransformationMatrix ((PdfFormXObject ) xObject , a , b , c , d , e , f , true );
2128
2132
} else if (xObject instanceof PdfImageXObject ) {
2129
2133
return addImageWithTransformationMatrix (xObject , a , b , c , d , e , f );
2130
2134
} else {
@@ -2200,6 +2204,8 @@ public PdfCanvas addXObject(PdfXObject xObject, float x, float y) {
2200
2204
* @param xObject the xObject to add
2201
2205
* @param rect the rectangle in which the xObject will be fitted
2202
2206
* @return the current canvas
2207
+ * @see PdfXObject#calculateProportionallyFitRectangleWithWidth(PdfXObject, float, float, float)
2208
+ * @see PdfXObject#calculateProportionallyFitRectangleWithHeight(PdfXObject, float, float, float)
2203
2209
*/
2204
2210
public PdfCanvas addXObjectFittedIntoRectangle (PdfXObject xObject , Rectangle rect ) {
2205
2211
if (xObject instanceof PdfFormXObject ) {
@@ -2308,7 +2314,7 @@ public PdfCanvas addXObject(PdfXObject xObject, float x, float y, float height,
2308
2314
*/
2309
2315
public PdfCanvas addXObject (PdfXObject xObject ) {
2310
2316
if (xObject instanceof PdfFormXObject ) {
2311
- return addFormWithTransformationMatrix ((PdfFormXObject ) xObject , 1 , 0 , 0 , 1 , 0 , 0 );
2317
+ return addFormWithTransformationMatrix ((PdfFormXObject ) xObject , 1 , 0 , 0 , 1 , 0 , 0 , false );
2312
2318
} else if (xObject instanceof PdfImageXObject ) {
2313
2319
return addImageAt ((PdfImageXObject ) xObject , 0 , 0 );
2314
2320
} else {
@@ -2506,17 +2512,21 @@ protected void addInlineImage(PdfImageXObject imageXObject, float a, float b, fl
2506
2512
* Adds {@link PdfFormXObject} to canvas.
2507
2513
*
2508
2514
* @param form the formXObject to add
2509
- * @param a an element of the transformation matrix
2510
- * @param b an element of the transformation matrix
2511
- * @param c an element of the transformation matrix
2512
- * @param d an element of the transformation matrix
2513
- * @param e an element of the transformation matrix
2514
- * @param f an element of the transformation matrix
2515
+ * @param a an element of the transformation matrix
2516
+ * @param b an element of the transformation matrix
2517
+ * @param c an element of the transformation matrix
2518
+ * @param d an element of the transformation matrix
2519
+ * @param e an element of the transformation matrix
2520
+ * @param f an element of the transformation matrix
2521
+ * @param writeIdentityMatrix true if the matrix is written in any case, otherwise if the
2522
+ * {@link #isIdentityMatrix(float, float, float, float, float, float)} method indicates
2523
+ * that the matrix is identity, the matrix will not be written
2515
2524
* @return current canvas
2516
2525
*/
2517
- private PdfCanvas addFormWithTransformationMatrix (PdfFormXObject form , float a , float b , float c , float d , float e , float f ) {
2526
+ private PdfCanvas addFormWithTransformationMatrix (PdfFormXObject form , float a , float b , float c ,
2527
+ float d , float e , float f , boolean writeIdentityMatrix ) {
2518
2528
saveState ();
2519
- if (!PdfCanvas .isIdentityMatrix (a , b , c , d , e , f )) {
2529
+ if (writeIdentityMatrix || !PdfCanvas .isIdentityMatrix (a , b , c , d , e , f )) {
2520
2530
concatMatrix (a , b , c , d , e , f );
2521
2531
}
2522
2532
PdfName name = resources .addForm (form );
@@ -2537,7 +2547,7 @@ private PdfCanvas addFormWithTransformationMatrix(PdfFormXObject form, float a,
2537
2547
* @param f an element of the transformation matrix
2538
2548
* @return current canvas
2539
2549
* @deprecated will be removed in 7.2, use
2540
- * {@link #addFormWithTransformationMatrix(PdfFormXObject, float, float, float, float, float, float)} instead
2550
+ * {@link #addFormWithTransformationMatrix(PdfFormXObject, float, float, float, float, float, float, boolean )} instead
2541
2551
*/
2542
2552
@ Deprecated
2543
2553
private PdfCanvas addForm (PdfFormXObject form , float a , float b , float c , float d , float e , float f ) {
@@ -2566,7 +2576,7 @@ private PdfCanvas addFormAt(PdfFormXObject form, float x, float y) {
2566
2576
y + bBoxMax .get (Vector .I2 ) - bBoxMin .get (Vector .I2 ), 1 );
2567
2577
2568
2578
float [] result = PdfCanvas .calculateTransformationMatrix (rectMin , rectMax , bBoxMin , bBoxMax );
2569
- return addFormWithTransformationMatrix (form , result [0 ], result [1 ], result [2 ], result [3 ], result [4 ], result [5 ]);
2579
+ return addFormWithTransformationMatrix (form , result [0 ], result [1 ], result [2 ], result [3 ], result [4 ], result [5 ], false );
2570
2580
}
2571
2581
2572
2582
/**
@@ -2640,7 +2650,7 @@ private PdfCanvas addFormFittedIntoRectangle(PdfFormXObject form, Rectangle rect
2640
2650
Vector rectMax = new Vector (rect .getRight (), rect .getTop (), 1 );
2641
2651
2642
2652
float [] result = PdfCanvas .calculateTransformationMatrix (rectMin , rectMax , bBoxMin , bBoxMax );
2643
- return addFormWithTransformationMatrix (form , result [0 ], result [1 ], result [2 ], result [3 ], result [4 ], result [5 ]);
2653
+ return addFormWithTransformationMatrix (form , result [0 ], result [1 ], result [2 ], result [3 ], result [4 ], result [5 ], false );
2644
2654
}
2645
2655
2646
2656
/**
@@ -2805,7 +2815,7 @@ private static float[] calculateTransformationMatrix(Vector expectedMin, Vector
2805
2815
}
2806
2816
2807
2817
private static boolean isIdentityMatrix (float a , float b , float c , float d , float e , float f ) {
2808
- return Float . compare ( a , 1 ) == 0 && Float . compare ( b , 0 ) == 0 && Float . compare ( c , 0 ) == 0 &&
2809
- Float . compare ( d , 1 ) == 0 && Float . compare ( e , 0 ) == 0 && Float . compare ( f , 0 ) == 0 ;
2818
+ return Math . abs ( 1 - a ) < IDENTITY_MATRIX_EPS && Math . abs ( b ) < IDENTITY_MATRIX_EPS && Math . abs ( c ) < IDENTITY_MATRIX_EPS &&
2819
+ Math . abs ( 1 - d ) < IDENTITY_MATRIX_EPS && Math . abs ( e ) < IDENTITY_MATRIX_EPS && Math . abs ( f ) < IDENTITY_MATRIX_EPS ;
2810
2820
}
2811
2821
}
0 commit comments