Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ follow semantic versioning; release dates are ISO 8601.
registered again, after a registration of it that travelled, is likewise told only what its
last registration lacks.

### Performance

- **A large table's DOCX export no longer reads its rows quadratically, nor makes XMLBeans throw
for every colour.** The file it writes is the same bytes.
- POI's `XWPFTable.getRow(int)` and `getNumberOfRows()` count the table's rows on every call, and
the export read each row that way several times while writing it, so the reads grew with the
square of the table's rows. The rows are now read from POI's own list of them, in the passes
that write a table's rows.
- A Word colour attribute (`w:color`, `w:fill`) holds either the word `auto` or three hex bytes.
Given a colour as a hex string, XMLBeans tried it as the word first and threw and caught an
`XmlValueOutOfRangeException`, stack trace included, for every colour that is not `auto` —
four for each ruled cell. Every such attribute the export sets to a colour — on borders,
shading, a run, the Normal style, a timeline rail — is now given its bytes, which XMLBeans
writes as the same upper-case hex.
- The passes that count the lines written at an exact height and fill the shaded spaces closing a
paragraph asked each paragraph and run for a few of its children through an XPath of its own;
they walk the children now.
- JMH `DocxExportJmhBenchmark`, `-prof gc`, allocation per export, the mean of two runs:
`table1000` 233 → 159 MB, `report20` 10.1 → 9.5 MB, `small` 1.87 → 1.68 MB, `images20`
2.99 → 2.92 MB. Time was measured on a machine other builds were loading, so as pairs: run
alternately, one fork of each build a round, `table1000` was faster in three rounds of four
(median 1,496 → 476 ms/op), and its thread CPU time per export in the quietest round went
688 → 500 ms; the smaller shapes move within the noise.
- Measured byte-identical: the 105 documents of the DOCX fidelity corpus, every part of each, with
the same 1,718 report notes; and the benchmark's four documents, every part but the creation
time in `docProps/core.xml`.

### Internal

- **`PptxRenderEnvironment` no longer carries the PPTX backend's font embedding.** Embedding had
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ final class DocxClosingSpaces {

private static final String W = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

private static final String NAMESPACES = "declare namespace w='" + W + "' "
+ "declare namespace wp='http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing' ";

/** Every paragraph of a part, those in its tables, cells and text boxes included. */
private static final String PARAGRAPHS = NAMESPACES + ".//w:p";
private static final String PARAGRAPHS = "declare namespace w='" + W + "' .//w:p";

/** A paragraph's own runs in reading order, those inside its links and simple fields included. */
private static final String RUNS = NAMESPACES + "./w:r | ./w:hyperlink/w:r | ./w:fldSimple/w:r";
private static final QName RUN = new QName(W, "r");
private static final QName HYPERLINK = new QName(W, "hyperlink");
private static final QName SIMPLE_FIELD = new QName(W, "fldSimple");
private static final QName RUN_PROPERTIES = new QName(W, "rPr");
private static final QName SHADING = new QName(W, "shd");
private static final QName INLINE =
new QName("http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", "inline");

private static final char NO_BREAK_SPACE = (char) 0x00A0;

Expand Down Expand Up @@ -72,9 +74,9 @@ static List<XmlObject> fill(XWPFDocument document) {

private static void fill(XmlObject part, List<XmlObject> filled) {
for (XmlObject paragraph : part.selectPath(PARAGRAPHS)) {
XmlObject[] runs = paragraph.selectPath(RUNS);
for (int index = runs.length - 1; index >= 0; index--) {
XmlObject run = runs[index];
List<XmlObject> runs = runs(paragraph);
for (int index = runs.size() - 1; index >= 0; index--) {
XmlObject run = runs.get(index);
Closing closing = fillTheClosingSpaces(run);
if (closing == Closing.FILLED || closing == Closing.FILLED_AND_ENDED) {
filled.add(run);
Expand All @@ -86,6 +88,22 @@ private static void fill(XmlObject part, List<XmlObject> filled) {
}
}

/** A paragraph's own runs in reading order, those inside its links and simple fields included. */
private static List<XmlObject> runs(XmlObject paragraph) {
List<XmlObject> runs = new ArrayList<>();
try (XmlCursor cursor = paragraph.newCursor()) {
for (boolean more = cursor.toFirstChild(); more; more = cursor.toNextSibling()) {
QName name = cursor.getName();
if (RUN.equals(name)) {
runs.add(cursor.getObject());
} else if (HYPERLINK.equals(name) || SIMPLE_FIELD.equals(name)) {
runs.addAll(DocxXmlChildren.path(cursor.getObject(), RUN));
}
}
}
return runs;
}

/** What a run read from its end does to the walk back along its paragraph. */
private enum Closing {
/** Nothing but spaces or what takes no room: the walk goes on to the run before. */
Expand Down Expand Up @@ -119,7 +137,7 @@ private static Closing fillTheClosingSpaces(XmlObject run) {
if ("rPr".equals(name)) {
continue;
}
if ("drawing".equals(name) && child.selectPath(NAMESPACES + "./wp:inline").length == 0) {
if ("drawing".equals(name) && DocxXmlChildren.path(child, INLINE).isEmpty()) {
// Anchored, it takes no room in the line.
continue;
}
Expand Down Expand Up @@ -153,11 +171,11 @@ private static Closing fillTheClosingSpaces(XmlObject run) {
* which a text box's content holds untyped.
*/
private static boolean isShaded(XmlObject run) {
XmlObject[] shadings = run.selectPath(NAMESPACES + "./w:rPr/w:shd");
if (shadings.length == 0) {
XmlObject shading = DocxXmlChildren.first(run, RUN_PROPERTIES, SHADING);
if (shading == null) {
return false;
}
try (XmlCursor cursor = shadings[0].newCursor()) {
try (XmlCursor cursor = shading.newCursor()) {
String fill = cursor.getAttributeText(new QName(W, "fill"));
return fill != null && !"auto".equalsIgnoreCase(fill)
&& !"nil".equals(cursor.getAttributeText(new QName(W, "val")));
Expand Down
Loading