From 0f560377be2fee0f2d668b6b3fe7a7cfc2f50768 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 30 Jan 2025 22:15:32 +0000 Subject: [PATCH] allow users supply a SheetUtil failover function git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923466 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/ss/util/SheetUtil.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java b/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java index b7380672de..a6cabf543b 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java +++ b/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java @@ -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 { + R apply(A a, B b, C c, D d, E e); + } + + private static final Function5Arity DEFAULT_FAILOVER_FUNCTION = + (defaultCharWidth, colspan, style, minWidth, string) -> defaultCharWidth; + + private static Function5Arity FAILOVER_FUNCTION = + DEFAULT_FAILOVER_FUNCTION; + + public static void setFailoverFunction(Function5Arity 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