Skip to content

Commit

Permalink
#364 Failing test for blocks in footnotes.
Browse files Browse the repository at this point in the history
Plus:
+ Overhauled BlockBoxing while trying to fix issue. Now better performance and cleaner code.
+ Added benchmarks for BlockBoxing.
  • Loading branch information
danfickle committed Jun 10, 2021
1 parent b923763 commit 541545e
Show file tree
Hide file tree
Showing 10 changed files with 517 additions and 272 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,9 @@ private static InlineLayoutBox addFirstLetterBox(LayoutContext c, LineBox curren

lbContext.setStart(lbContext.getEnd());

c.getFirstLettersTracker().clearStyles();
c.setFirstLettersTracker(
StyleTracker.withNoStyles());

currentIB.setStyle(previous);

return iB;
Expand Down Expand Up @@ -1066,7 +1068,8 @@ private static void saveLine(LineBox current, LayoutContext c,
}

if (hasFirstLinePCs && current.isFirstLine()) {
c.getFirstLinesTracker().clearStyles();
c.setFirstLinesTracker(
StyleTracker.withNoStyles());
block.styleText(c);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public class LayoutContext implements CssContext {

private boolean _isInFloatBottom;

private LayoutState _savedLayoutState;

private int _footnoteIndex;
private FootnoteManager _footnoteManager;

Expand Down Expand Up @@ -175,13 +177,13 @@ public void setDefaultTextDirection(byte direction) {
_bfcs = new LinkedList<>();
_layers = new LinkedList<>();

_firstLines = new StyleTracker();
_firstLetters = new StyleTracker();
_firstLines = StyleTracker.withNoStyles();
_firstLetters = StyleTracker.withNoStyles();
}

public void reInit(boolean keepLayers) {
_firstLines = new StyleTracker();
_firstLetters = new StyleTracker();
_firstLines = StyleTracker.withNoStyles();
_firstLetters = StyleTracker.withNoStyles();
_currentMarkerData = null;

_bfcs = new LinkedList<>();
Expand All @@ -196,22 +198,23 @@ public void reInit(boolean keepLayers) {
}

public LayoutState captureLayoutState() {
LayoutState result = new LayoutState();

result.setFirstLines(_firstLines);
result.setFirstLetters(_firstLetters);
result.setCurrentMarkerData(_currentMarkerData);

result.setBFCs(_bfcs);

if (isPrint()) {
result.setPageName(getPageName());
result.setExtraSpaceBottom(getExtraSpaceBottom());
result.setExtraSpaceTop(getExtraSpaceTop());
result.setNoPageBreak(getNoPageBreak());
if (!isPrint()) {
return new LayoutState(
_bfcs,
_currentMarkerData,
_firstLetters,
_firstLines);
} else {
return new LayoutState(
_bfcs,
_currentMarkerData,
_firstLetters,
_firstLines,
getPageName(),
getExtraSpaceTop(),
getExtraSpaceBottom(),
getNoPageBreak());
}

return result;
}

public void restoreLayoutState(LayoutState layoutState) {
Expand All @@ -231,17 +234,24 @@ public void restoreLayoutState(LayoutState layoutState) {
}

public LayoutState copyStateForRelayout() {
LayoutState result = new LayoutState();

result.setFirstLetters(_firstLetters.copyOf());
result.setFirstLines(_firstLines.copyOf());
result.setCurrentMarkerData(_currentMarkerData);

if (isPrint()) {
result.setPageName(getPageName());
if (_savedLayoutState != null &&
_savedLayoutState.equal(
_currentMarkerData,
_firstLetters,
_firstLines,
isPrint() ? getPageName() : null)) {

return _savedLayoutState;
}

return result;
_savedLayoutState =
new LayoutState(
_firstLetters,
_firstLines,
_currentMarkerData,
isPrint() ? getPageName() : null);

return _savedLayoutState;
}

public void restoreStateForRelayout(LayoutState layoutState) {
Expand Down Expand Up @@ -559,4 +569,13 @@ public FootnoteManager getFootnoteManager() {

return _footnoteManager;
}

public void setFirstLettersTracker(StyleTracker firstLetters) {
_firstLetters = firstLetters;
}

public void setFirstLinesTracker(StyleTracker firstLines) {
_firstLines = firstLines;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,89 +20,129 @@
package com.openhtmltopdf.layout;

import java.util.LinkedList;
import java.util.Objects;

import com.openhtmltopdf.render.MarkerData;

/**
* A bean which captures all state necessary to lay out an arbitrary box.
* A bean which captures all state necessary to lay out an arbitrary box.
* Mutable objects must be copied when provided to this class. It is far too
* expensive to maintain a bean of this class for each box.
* expensive to maintain a bean of this class for each box.
* It is only created as needed.
* <br><br>
* IMPORTANT: Immutable after construction.
*/
public class LayoutState {
private StyleTracker _firstLines;
private StyleTracker _firstLetters;

private MarkerData _currentMarkerData;

private LinkedList<BlockFormattingContext> _BFCs;

private String _pageName;
private int _extraSpaceTop;
private int _extraSpaceBottom;
private int _noPageBreak;

public LinkedList<BlockFormattingContext> getBFCs() {
return _BFCs;
private final StyleTracker _firstLines;
private final StyleTracker _firstLetters;

private final MarkerData _currentMarkerData;

private final LinkedList<BlockFormattingContext> _BFCs;

private final String _pageName;
private final int _extraSpaceTop;
private final int _extraSpaceBottom;
private final int _noPageBreak;

public LayoutState(
LinkedList<BlockFormattingContext> bfcs,
MarkerData markerData,
StyleTracker firstLetters,
StyleTracker firstLines,
String pageName,
int extraSpaceTop,
int extraSpaceBottom,
int noPageBreak) {
this._BFCs = bfcs;
this._currentMarkerData = markerData;
this._firstLetters = firstLetters;
this._firstLines = firstLines;
this._pageName = pageName;
this._extraSpaceTop = extraSpaceTop;
this._extraSpaceBottom = extraSpaceBottom;
this._noPageBreak = noPageBreak;
}

public LayoutState(
LinkedList<BlockFormattingContext> bfcs,
MarkerData currentMarkerData,
StyleTracker firstLetters,
StyleTracker firstLines) {
this(bfcs, currentMarkerData, firstLetters, firstLines, null, 0, 0, 0);
}

public LayoutState(
StyleTracker firstLetters,
StyleTracker firstLines,
MarkerData currentMarkerData,
String pageName) {
this(null, currentMarkerData, firstLetters, firstLines, pageName, 0, 0, 0);
}

public boolean equal(
LinkedList<BlockFormattingContext> bfcs,
MarkerData markerData,
StyleTracker firstLetters,
StyleTracker firstLines,
String pageName,
int extraSpaceTop,
int extraSpaceBottom,
int noPageBreak) {

return bfcs == _BFCs &&
markerData == _currentMarkerData &&
Objects.equals(firstLetters, _firstLetters) &&
Objects.equals(firstLines, _firstLines) &&
Objects.equals(pageName, _pageName) &&
extraSpaceTop == _extraSpaceTop &&
extraSpaceBottom == _extraSpaceBottom &&
noPageBreak == _noPageBreak;
}

public boolean equal(
MarkerData currentMarkerData,
StyleTracker firstLetters,
StyleTracker firstLines,
String pageName) {

return currentMarkerData == _currentMarkerData &&
Objects.equals(firstLetters, _firstLetters) &&
Objects.equals(firstLines, _firstLines) &&
Objects.equals(pageName, _pageName);
}

public void setBFCs(LinkedList<BlockFormattingContext> s) {
_BFCs = s;

public LinkedList<BlockFormattingContext> getBFCs() {
return _BFCs;
}

public MarkerData getCurrentMarkerData() {
return _currentMarkerData;
}

public void setCurrentMarkerData(MarkerData currentMarkerData) {
_currentMarkerData = currentMarkerData;
}

public StyleTracker getFirstLetters() {
return _firstLetters;
}

public void setFirstLetters(StyleTracker firstLetters) {
_firstLetters = firstLetters;
}

public StyleTracker getFirstLines() {
return _firstLines;
}

public void setFirstLines(StyleTracker firstLines) {
_firstLines = firstLines;
}

public String getPageName() {
return _pageName;
}

public void setPageName(String pageName) {
_pageName = pageName;
}

public int getExtraSpaceTop() {
return _extraSpaceTop;
}

public void setExtraSpaceTop(int extraSpaceTop) {
_extraSpaceTop = extraSpaceTop;
}

public int getExtraSpaceBottom() {
return _extraSpaceBottom;
}

public void setExtraSpaceBottom(int extraSpaceBottom) {
_extraSpaceBottom = extraSpaceBottom;
}

public int getNoPageBreak() {
return _noPageBreak;
}

public void setNoPageBreak(int noPageBreak) {
_noPageBreak = noPageBreak;
}
}
Loading

0 comments on commit 541545e

Please sign in to comment.