Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement to add excel formula options (#1643) #1644

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ public abstract class ExcelEmitter implements IContentEmitter {
/** property: ExcelEmitter.DisplayZeros */
public static final String DISPLAYZEROS_PROP = "ExcelEmitter.DisplayZeros";

/** property: ExcelEmitter.ValueAsFormula */
public static final String VALUE_AS_FORMULA = "ExcelEmitter.ValueAsFormula";

/** property: ExcelEmitter.Formula */
public static final String FORMULA = "ExcelEmitter.Formula";

/** property: ExcelEmitter.TemplateFile */
public static final String TEMPLATE_FILE = "ExcelEmitter.TemplateFile";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.font.TextMeasurer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -786,11 +787,12 @@ protected int getRichTextRunIndexForStart(List<RichTextRun> richTextRuns, int st
* @param defaultFont The font to be used prior to the first RichTextRun.
* @param widthMM The width of the output.
* @param richTextRuns The list of RichTextRuns to be applied to the string
* @return The heigh, in points, of a box big enough to contain the formatted
* @param wrap The text use wrapped style
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment for wrap is not clear to me (and grammar errors)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change it.

* @return The height, in points, of a box big enough to contain the formatted
* sourceText.
*/
public float calculateTextHeightPoints(String sourceText, Font defaultFont, double widthMM,
List<RichTextRun> richTextRuns) {
List<RichTextRun> richTextRuns, boolean wrap) {
log.debug("Calculating height for ", sourceText);

final float widthPt = (float) (72 * Math.max(0, widthMM - 6) / 25.4);
Expand Down Expand Up @@ -843,21 +845,37 @@ public float calculateTextHeightPoints(String sourceText, Font defaultFont, doub
}
}

LineBreakMeasurer measurer = new LineBreakMeasurer(attrString.getIterator(), frc);

float heightAdjustment = 0.0F;
int lineLength = textLine.isEmpty() ? 1 : textLine.length();
while (measurer.getPosition() < lineLength) {
TextLayout layout = measurer.nextLayout(widthPt);
float lineHeight = layout.getAscent() + layout.getDescent() + layout.getLeading();
if (layout.getDescent() + layout.getLeading() > heightAdjustment) {
heightAdjustment = layout.getDescent() + layout.getLeading();
try {
if (wrap) {
LineBreakMeasurer measurer = new LineBreakMeasurer(attrString.getIterator(), frc);

float heightAdjustment = 0.0F;
int lineLength = textLine.isEmpty() ? 1 : textLine.length();
while (measurer.getPosition() < lineLength) {
TextLayout layout = measurer.nextLayout(widthPt);
float lineHeight = layout.getAscent() + layout.getDescent() + layout.getLeading();
if (layout.getDescent() + layout.getLeading() > heightAdjustment) {
heightAdjustment = layout.getDescent() + layout.getLeading();
}
log.debug("Line: ", textLine, " gives height ", lineHeight, "(", layout.getAscent(), "/",
layout.getDescent(), "/", layout.getLeading(), ")");
totalHeight += lineHeight;
}
totalHeight += heightAdjustment;
} else {
if (textLine.length() > 0) {
TextMeasurer measurer = new TextMeasurer(attrString.getIterator(), frc);
TextLayout layout = measurer.getLayout(0, textLine.length());
float lineHeight = layout.getAscent() + 2 * (layout.getDescent() + layout.getLeading());
log.debug("Line: ", textLine, " gives height ", lineHeight, "(", layout.getAscent(), "/",
layout.getDescent(), "/", layout.getLeading(), ")");
totalHeight += lineHeight;
}
}
log.debug("Line: ", textLine, " gives height ", lineHeight, "(", layout.getAscent(), "/",
layout.getDescent(), "/", layout.getLeading(), ")");
totalHeight += lineHeight;
} catch (Throwable ex) {
log.error(0, "Calculating height of line \"" + textLine + "\" threw: ", ex);
}
totalHeight += heightAdjustment;

}
log.debug("Height calculated as ", totalHeight);
Expand Down
Loading
Loading