Skip to content

Commit

Permalink
allow users supply a SheetUtil failover function
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923466 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
pjfanning committed Jan 30, 2025
1 parent 43aded3 commit 0f56037
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;
Expand Down Expand Up @@ -257,14 +258,14 @@ public static double getCellWidth(Cell cell, float defaultCharWidth, DataFormatt
* @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;
}
Expand Down Expand Up @@ -368,6 +369,21 @@ public static float getDefaultCharWidthAsFloat(final Workbook wb) {
}
}

@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
Expand Down

0 comments on commit 0f56037

Please sign in to comment.