From d309159aabd3ea3a8d85282db509cbdf6fb0be65 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Tue, 27 Aug 2024 22:19:34 +0200 Subject: [PATCH] Add minimum set of classes so that SXSSFSheet can be constructed This avoids exceptions when trying to initialize the Java Font System This fixes the test-item for issue 89 --- poishadow/build.gradle | 7 +++++-- .../poi/java/awt/font/FontRenderContext.java | 11 +++++++++++ .../org/apache/poi/java/awt/font/TextLayout.java | 14 ++++++++++++++ .../apache/poi/java/awt/geom/AffineTransform.java | 9 +++++++++ .../poiandroidtest/poitest/MainActivity.java | 4 +++- 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 poishadow/src/main/java/org/apache/poi/java/awt/font/FontRenderContext.java create mode 100644 poishadow/src/main/java/org/apache/poi/java/awt/font/TextLayout.java create mode 100644 poishadow/src/main/java/org/apache/poi/java/awt/geom/AffineTransform.java diff --git a/poishadow/build.gradle b/poishadow/build.gradle index ae603445..a6afdd12 100644 --- a/poishadow/build.gradle +++ b/poishadow/build.gradle @@ -57,10 +57,13 @@ shadowJar { // java.awt is not available, but class Color is used in APIs in POI, therefore // relocate this class to another one where we can include a rewrite - relocate 'java.awt.Color', 'org.apache.poi.java.awt.Color' + relocate 'java.awt.font.FontRenderContext', 'org.apache.poi.java.awt.font.FontRenderContext' + relocate 'java.awt.font.TextLayout', 'org.apache.poi.java.awt.font.TextLayout' + relocate 'java.awt.geom.AffineTransform', 'org.apache.poi.java.awt.geom.AffineTransform' relocate 'java.awt.geom.Dimension2D', 'org.apache.poi.java.awt.geom.Dimension2D' - relocate 'java.awt.Dimension', 'org.apache.poi.java.awt.Dimension' relocate 'java.awt.image.BufferedImage', 'org.apache.poi.java.awt.image.BufferedImage' + relocate 'java.awt.Color', 'org.apache.poi.java.awt.Color' + relocate 'java.awt.Dimension', 'org.apache.poi.java.awt.Dimension' relocate 'javax.imageio.ImageIO', 'org.apache.poi.javax.imageio.ImageIO' relocate 'javax.imageio.ImageReader', 'org.apache.poi.javax.imageio.ImageReader' diff --git a/poishadow/src/main/java/org/apache/poi/java/awt/font/FontRenderContext.java b/poishadow/src/main/java/org/apache/poi/java/awt/font/FontRenderContext.java new file mode 100644 index 00000000..92115e99 --- /dev/null +++ b/poishadow/src/main/java/org/apache/poi/java/awt/font/FontRenderContext.java @@ -0,0 +1,11 @@ +package org.apache.poi.java.awt.font; + +import java.awt.geom.AffineTransform; + +public class FontRenderContext { + public FontRenderContext(AffineTransform o, boolean a, boolean b) { + //You can't crash here yet! If you do, a static field in SheetUtil will fail to load. + //Then next time SheetUtil will have to be accessed, a NoClassDefFoundError("SheetUtil") + //will be thrown! + } +} \ No newline at end of file diff --git a/poishadow/src/main/java/org/apache/poi/java/awt/font/TextLayout.java b/poishadow/src/main/java/org/apache/poi/java/awt/font/TextLayout.java new file mode 100644 index 00000000..e0574ecb --- /dev/null +++ b/poishadow/src/main/java/org/apache/poi/java/awt/font/TextLayout.java @@ -0,0 +1,14 @@ +package org.apache.poi.java.awt.font; + +import java.text.AttributedCharacterIterator; + +public class TextLayout { + public TextLayout(AttributedCharacterIterator iterator, FontRenderContext fontRenderContext) { + //This is called in: + //SXSSFSheet.java:106 + //AutoSizeColumnTracker.java:117 + //SheetUtil.java:353 + //This tricks SXSSFSheet constructor into not creating AutoSizeColumnTracker. + throw new NoClassDefFoundError("X11FontManager"); + } +} diff --git a/poishadow/src/main/java/org/apache/poi/java/awt/geom/AffineTransform.java b/poishadow/src/main/java/org/apache/poi/java/awt/geom/AffineTransform.java new file mode 100644 index 00000000..63a7a4a7 --- /dev/null +++ b/poishadow/src/main/java/org/apache/poi/java/awt/geom/AffineTransform.java @@ -0,0 +1,9 @@ +package org.apache.poi.java.awt.geom; + +import java.awt.font.FontRenderContext; + +/** + * Just an empty class, so we can have a correct {@link FontRenderContext} constructor + */ +public class AffineTransform { +} diff --git a/poitest/src/main/java/org/dstadler/poiandroidtest/poitest/MainActivity.java b/poitest/src/main/java/org/dstadler/poiandroidtest/poitest/MainActivity.java index 00426ede..d7d44d01 100644 --- a/poitest/src/main/java/org/dstadler/poiandroidtest/poitest/MainActivity.java +++ b/poitest/src/main/java/org/dstadler/poiandroidtest/poitest/MainActivity.java @@ -68,6 +68,8 @@ protected void onCreate(Bundle savedInstanceState) { System.setProperty("org.apache.poi.javax.xml.stream.XMLOutputFactory", "com.fasterxml.aalto.stax.OutputFactoryImpl"); System.setProperty("org.apache.poi.javax.xml.stream.XMLEventFactory", "com.fasterxml.aalto.stax.EventFactoryImpl"); + System.setProperty("org.apache.poi.ss.ignoreMissingFontSystem", "true"); + try { // create all the list items setupContent(); @@ -269,7 +271,7 @@ private void setupContent() throws IOException { })); // reproducer for https://github.com/centic9/poi-on-android/issues/89 - DummyContent.addItem(new DummyItemWithCode("c" + (idCount++), "Test Issue 89 - Crashes!!", () -> { + DummyContent.addItem(new DummyItemWithCode("c" + (idCount++), "Test Issue 89", () -> { try (OutputStream outputStream = openFileOutput("issue89.xlsx", Context.MODE_PRIVATE)) { TestIssue89.saveExcelFile(outputStream); }