JaroslavTulach commented on code in PR #909:
URL: https://github.com/apache/poi/pull/909#discussion_r2389871491
##########
poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java:
##########
@@ -238,56 +233,87 @@ public static double getCellWidth(Cell cell, float
defaultCharWidth, DataFormatt
if(sval != null) {
String txt = sval + defaultChar;
AttributedString str = new AttributedString(txt);
- copyAttributes(font, str, 0, txt.length());
+ WithJavaDesktop.copyAttributes(font, str, 0, txt.length());
- width = getCellWidth(defaultCharWidth, colspan, style, width,
str);
+ width = WithJavaDesktop.getCellWidth(defaultCharWidth,
colspan, style, width, str);
}
}
return width;
}
- /**
- * Calculate the best-fit width for a cell
- * If a merged cell spans multiple columns, evenly distribute the column
width among those columns
- *
- * @param defaultCharWidth the width of a character using the default font
in a workbook
- * @param colspan the number of columns that is spanned by the cell (1 if
the cell is not part of a merged region)
- * @param style the cell style, which contains text rotation and indention
information needed to compute the cell width
- * @param minWidth the minimum best-fit width. This algorithm will only
return values greater than or equal to the minimum width.
- * @param str the text contained in the cell
- * @return the best fit cell width
- */
- 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 FAILOVER_FUNCTION.apply(defaultCharWidth, colspan,
style, minWidth, str);
+ private static final class WithJavaDesktop {
+ /**
+ * drawing context to measure text
+ */
+ private static FontRenderContext fontRenderContext = new
FontRenderContext(null, true, true);
+
+ /**
+ * Calculate the best-fit width for a cell
+ * If a merged cell spans multiple columns, evenly distribute the
column width among those columns
+ *
+ * @param defaultCharWidth the width of a character using the default
font in a workbook
+ * @param colspan the number of columns that is spanned by the cell (1
if the cell is not part of a merged region)
+ * @param style the cell style, which contains text rotation and
indention information needed to compute the cell width
+ * @param minWidth the minimum best-fit width. This algorithm will
only return values greater than or equal to the minimum width.
+ * @param str the text contained in the cell
+ * @return the best fit cell width
+ */
+ 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 FAILOVER_FUNCTION.apply(defaultCharWidth, colspan,
style, minWidth, str);
+ }
+ throw t;
}
- throw t;
+ final Rectangle2D bounds;
+ if (style.getRotation() != 0) {
+ /*
+ * Transform the text using a scale so that its height is
increased by a multiple of the leading,
+ * and then rotate the text before computing the bounds. The
scale results in some whitespace around
+ * the unrotated top and bottom of the text that normally
wouldn't be present if unscaled, but
+ * is added by the standard Excel autosize.
+ */
+ AffineTransform trans = new AffineTransform();
+
trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0));
+ trans.concatenate(
+ AffineTransform.getScaleInstance(1, fontHeightMultiple)
+ );
+ bounds = layout.getOutline(trans).getBounds();
+ } else {
+ bounds = layout.getBounds();
+ }
+ // frameWidth accounts for leading spaces which is excluded from
bounds.getWidth()
+ final double frameWidth = bounds.getX() + bounds.getWidth();
+ return Math.max(minWidth, ((frameWidth / colspan) /
defaultCharWidth) + style.getIndention());
}
- final Rectangle2D bounds;
- if (style.getRotation() != 0) {
- /*
- * Transform the text using a scale so that its height is
increased by a multiple of the leading,
- * and then rotate the text before computing the bounds. The scale
results in some whitespace around
- * the unrotated top and bottom of the text that normally wouldn't
be present if unscaled, but
- * is added by the standard Excel autosize.
- */
- AffineTransform trans = new AffineTransform();
-
trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0));
- trans.concatenate(
- AffineTransform.getScaleInstance(1, fontHeightMultiple)
- );
- bounds = layout.getOutline(trans).getBounds();
- } else {
- bounds = layout.getBounds();
+
+ private static float getDefaultCharWidthAsFloat(AttributedString str) {
+ TextLayout layout = new TextLayout(str.getIterator(),
fontRenderContext);
+ return layout.getAdvance();
+ }
+
+ /**
+ * Copy text attributes from the supplied Font to Java2D
AttributedString
+ */
+ private static void copyAttributes(Font font, AttributedString str,
@SuppressWarnings("SameParameterValue") int startIdx, int endIdx) {
Review Comment:
- It is a [copy of an
existing](https://github.com/apache/poi/pull/909/files#r2389874483) POI method.
- I just moved it to the `WithJavaDesktop` inner class
- it can be renamed to whatever you wish, if that guarantees integration of
this PR
- PS: I don't like the method either - because it is referencing `java.awt`
classes!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]