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 custom strokes #59

Open
AlexGeller1 opened this issue Jun 10, 2024 · 4 comments
Open

Support custom strokes #59

AlexGeller1 opened this issue Jun 10, 2024 · 4 comments

Comments

@AlexGeller1
Copy link

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
Copy link
Owner

rototor commented Jun 10, 2024

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
Copy link
Author

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
Copy link
Owner

rototor commented Jun 10, 2024

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
Copy link
Author

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.

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

2 participants