Author: fanningpj
Date: Thu Jan 30 22:15:32 2025
New Revision: 1923466

URL: http://svn.apache.org/viewvc?rev=1923466&view=rev
Log:
allow users supply a SheetUtil failover function

Modified:
    poi/trunk/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java

Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java?rev=1923466&r1=1923465&r2=1923466&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java Thu Jan 
30 22:15:32 2025
@@ -26,6 +26,7 @@ import java.text.AttributedString;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.function.BiFunction;
 
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellStyle;
@@ -257,14 +258,14 @@ public class SheetUtil {
      * @param str the text contained in the cell
      * @return the best fit cell width
      */
-    private static double getCellWidth(float defaultCharWidth, int colspan,
-            CellStyle style, double minWidth, AttributedString str) {
+    private static double getCellWidth(float defaultCharWidth, final int 
colspan,
+            final CellStyle style, final double minWidth, final 
AttributedString str) {
         TextLayout layout;
         try {
             layout = new TextLayout(str.getIterator(), fontRenderContext);
         } catch (Throwable t) {
             if (shouldIgnoreMissingFontSystem(t)) {
-                return defaultCharWidth;
+                return FAILOVER_FUNCTION.apply(defaultCharWidth, colspan, 
style, minWidth, str);
             }
             throw t;
         }
@@ -368,6 +369,21 @@ public class SheetUtil {
         }
     }
 
+    @FunctionalInterface
+    public interface Function5Arity<A, B, C, D, E, R> {
+        R apply(A a, B b, C c, D d, E e);
+    }
+
+    private static final Function5Arity<Float, Integer, CellStyle, Double, 
AttributedString, Float> DEFAULT_FAILOVER_FUNCTION =
+            (defaultCharWidth, colspan, style, minWidth, string) -> 
defaultCharWidth;
+
+    private static Function5Arity<Float, Integer, CellStyle, Double, 
AttributedString, Float> FAILOVER_FUNCTION =
+            DEFAULT_FAILOVER_FUNCTION;
+
+    public static void setFailoverFunction(Function5Arity<Float, Integer, 
CellStyle, Double, AttributedString, Float> failoverFunction) {
+        FAILOVER_FUNCTION = failoverFunction == null ? 
DEFAULT_FAILOVER_FUNCTION : failoverFunction;
+    }
+
     private static boolean shouldIgnoreMissingFontSystem(final Throwable t) {
         return ignoreMissingFontSystem && (
                 // the three types of exception usually indicate here that the 
font



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to