rototor / pdfbox-graphics2d

Graphics2D Bridge for pdfbox
59 stars 22 forks source link

Support custom strokes #59

Open AlexGeller1 opened 1 month ago

AlexGeller1 commented 1 month ago

It seems that currently only java.awt.BasicStroke is supported. Something like this could perhaps be added to the code:

diff --git a/dev/lib-rototor-graphics2d/pdfbox-graphics2d/graphics2d/src/main/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2D.java b/dev/lib-rototor-graphics2d/pdfbox-graphics2d/graphics2d/src/main/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2D.java
index 4484b19a3c..12b9c57c16 100644
--- a/dev/lib-rototor-graphics2d/pdfbox-graphics2d/graphics2d/src/main/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2D.java
+++ b/dev/lib-rototor-graphics2d/pdfbox-graphics2d/graphics2d/src/main/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2D.java
@@ -588,6 +588,18 @@ public class PdfBoxGraphics2D extends Graphics2D
     };

     public void draw(Shape s)
+    {
+        if(stroke!=null&&!(stroke instanceof BasicStroke))
+        {
+            fill(stroke.createStrokedShape(s));
+        }
+        else
+        {
+            drawIntern(s);
+        }
+    }
+
+    public void drawIntern(Shape s)
     {
         checkNoCopyActive();
         /*
rototor commented 1 month ago

What library are you using, which defines a stroke other than the BasicStroke? I would at least like to have a possibility to test this.

AlexGeller1 commented 1 month ago

Thanks for the quick response. It is not due to a library, it is my own code that has that issue. I derived strokes from the interface java.awt.Stroke because I need the stroke to be serializable and BasicStroke isn't (https://bugs.openjdk.org/browse/JDK-4305099). I can provide a test.

rototor commented 1 month ago

Hmm, why are you not just deriving from BasicStroke and let that subclass implement Serializable?

If you could just provide a very simple test, this would be helpful.

AlexGeller1 commented 1 month ago

Regarding a test case, I modified the test PdfBoxGraphics2dTest.testLineWithRotation() as follows:

diff --git a/dev/lib-rototor-graphics2d/pdfbox-graphics2d/graphics2d/src/test/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2dTest.java b/dev/lib-rototor-graphics2d/pdfbox-graphics2d/graphics2d/src/test/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2dTest.java
index b60f8b1679..a72bee6fb3 100644
--- a/dev/lib-rototor-graphics2d/pdfbox-graphics2d/graphics2d/src/test/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2dTest.java
+++ b/dev/lib-rototor-graphics2d/pdfbox-graphics2d/graphics2d/src/test/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2dTest.java
@@ -470,7 +470,7 @@ public class PdfBoxGraphics2dTest extends PdfBoxGraphics2DTestBase
             @Override
             public void draw(Graphics2D gfx)
             {
-                gfx.setStroke(new BasicStroke(5f));
+                gfx.setStroke(new MyBasicStroke(5f));

                 float centerX = 200;
                 float centerY = 200;
@@ -485,5 +485,17 @@ public class PdfBoxGraphics2dTest extends PdfBoxGraphics2DTestBase
             }
         });
     }
+    static class MyBasicStroke implements java.awt.Stroke
+    {
+        final BasicStroke stroke;
+        MyBasicStroke(float width)
+        {
+            stroke=new BasicStroke(width);
+        }
+        public java.awt.Shape createStrokedShape(java.awt.Shape p)
+        {
+            return stroke.createStrokedShape(p);
+        }
+    }

 } 

Regarding implementing Serializable, yes it is possible but the class is immutable making it clunky. But yes, that is definitely an option.