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

Better flowing column support #380

Merged
merged 6 commits into from
Aug 19, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
*/
package com.openhtmltopdf.css.constants;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import java.util.Objects;
import com.openhtmltopdf.css.parser.CSSErrorHandler;
import com.openhtmltopdf.css.parser.CSSParser;
import com.openhtmltopdf.css.parser.PropertyValue;
Expand Down Expand Up @@ -55,7 +55,7 @@
*
* @author Patrick Wright
*/
public final class CSSName implements Comparable {
public final class CSSName implements Comparable<CSSName> {
/**
* marker var, used for initialization
*/
Expand Down Expand Up @@ -115,12 +115,12 @@ public final class CSSName implements Comparable {
/**
* Map of all CSS properties
*/
private static final Map ALL_PROPERTY_NAMES = new TreeMap();
private static final Map<String, CSSName> ALL_PROPERTY_NAMES = new HashMap<>();

/**
* Map of all non-shorthand CSS properties
*/
private static final Map ALL_PRIMITIVE_PROPERTY_NAMES = new TreeMap();
private static final Map<String, CSSName> ALL_PRIMITIVE_PROPERTY_NAMES = new HashMap<>();

/**
* Unique CSSName instance for CSS2 property.
Expand Down Expand Up @@ -915,6 +915,24 @@ public final class CSSName implements Comparable {
INHERITS,
new PrimitivePropertyBuilders.PageBreakInside()
);

public final static CSSName BREAK_AFTER =
addProperty(
"break-after",
PRIMITIVE,
"auto",
NOT_INHERITED,
new PrimitivePropertyBuilders.BreakAfter()
);

public final static CSSName BREAK_BEFORE =
addProperty(
"break-before",
PRIMITIVE,
"auto",
NOT_INHERITED,
new PrimitivePropertyBuilders.BreakBefore()
);

/**
* Unique CSSName instance for CSS2 property.
Expand Down Expand Up @@ -1815,24 +1833,6 @@ public static int countCSSPrimitiveNames() {
return ALL_PRIMITIVE_PROPERTY_NAMES.size();
}

/**
* Iterator of ALL CSS 2 visual property names.
*
* @return Returns
*/
public static Iterator allCSS2PropertyNames() {
return ALL_PROPERTY_NAMES.keySet().iterator();
}

/**
* Iterator of ALL primitive (non-shorthand) CSS 2 visual property names.
*
* @return Returns
*/
public static Iterator allCSS2PrimitivePropertyNames() {
return ALL_PRIMITIVE_PROPERTY_NAMES.keySet().iterator();
}

/**
* Returns true if the named property inherits by default, according to the
* CSS2 spec.
Expand Down Expand Up @@ -1877,8 +1877,7 @@ public static PropertyBuilder getPropertyBuilder(CSSName cssName) {
* @return The byPropertyName value
*/
public static CSSName getByPropertyName(String propName) {

return (CSSName) ALL_PROPERTY_NAMES.get(propName);
return ALL_PROPERTY_NAMES.get(propName);
}

public static CSSName getByID(int id) {
Expand Down Expand Up @@ -1927,10 +1926,10 @@ private static synchronized CSSName addProperty(
}

static {
Iterator iter = ALL_PROPERTY_NAMES.values().iterator();
Iterator<CSSName> iter = ALL_PROPERTY_NAMES.values().iterator();
ALL_PROPERTIES = new CSSName[ALL_PROPERTY_NAMES.size()];
while (iter.hasNext()) {
CSSName name = (CSSName) iter.next();
CSSName name = iter.next();
ALL_PROPERTIES[name.FS_ID] = name;
}
}
Expand All @@ -1941,8 +1940,8 @@ public void error(String uri, String message) {
XRLog.cssParse("(" + uri + ") " + message);
}
});
for (Iterator i = ALL_PRIMITIVE_PROPERTY_NAMES.values().iterator(); i.hasNext(); ) {
CSSName cssName = (CSSName)i.next();
for (Iterator<CSSName> i = ALL_PRIMITIVE_PROPERTY_NAMES.values().iterator(); i.hasNext(); ) {
CSSName cssName = i.next();
if (cssName.initialValue.charAt(0) != '=' && cssName.implemented) {
PropertyValue value = parser.parsePropertyValue(
cssName, StylesheetInfo.USER_AGENT, cssName.initialValue);
Expand All @@ -1959,14 +1958,14 @@ public void error(String uri, String message) {
}
}

//Assumed to be consistent with equals because CSSName is in essence an enum
public int compareTo(Object object) {
if (object == null) throw new NullPointerException();//required by Comparable
return FS_ID - ((CSSName) object).FS_ID;//will throw ClassCastException according to Comparable if not a CSSName
// Assumed to be consistent with equals because CSSName is in essence an enum
@Override
public int compareTo(CSSName object) {
Objects.requireNonNull(object);
return this.FS_ID - object.FS_ID;
}

// FIXME equals, hashcode

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CSSName)) return false;
Expand All @@ -1976,6 +1975,7 @@ public boolean equals(Object o) {
return FS_ID == cssName.FS_ID;
}

@Override
public int hashCode() {
return FS_ID;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@
*/
public class IdentValue implements FSDerivedValue {
private static int maxAssigned = 0;
private static final Map<String, IdentValue> ALL_IDENT_VALUES = new HashMap<>();

/**
* Description of the Field
*/
private final String ident;

public final int FS_ID;

public final static IdentValue ABSOLUTE = addValue("absolute");
Expand Down Expand Up @@ -254,11 +251,10 @@ public class IdentValue implements FSDerivedValue {
public static final IdentValue BORDER_BOX = addValue("border-box");
public static final IdentValue CONTENT_BOX = addValue("content-box");


/**
* Description of the Field
/*
* Column break.
*/
private static Map<String, IdentValue> ALL_IDENT_VALUES;
public static final IdentValue COLUMN = addValue("column");

/**
* Constructor for the IdentValue object
Expand All @@ -276,6 +272,7 @@ private IdentValue(String ident) {
*
* @return a string representation of the object.
*/
@Override
public String toString() {
return ident;
}
Expand All @@ -290,22 +287,19 @@ public String toString() {
* @return see desc.
*/
public static IdentValue getByIdentString(String ident) {
IdentValue val = (IdentValue) ALL_IDENT_VALUES.get(ident);
IdentValue val = ALL_IDENT_VALUES.get(ident);
if (val == null) {
throw new XRRuntimeException("Ident named " + ident + " has no IdentValue instance assigned to it.");
}
return val;
}

/**
* TODO: doc
*/
public static boolean looksLikeIdent(String ident) {
return (IdentValue) ALL_IDENT_VALUES.get(ident) != null;
return ALL_IDENT_VALUES.get(ident) != null;
}

public static IdentValue valueOf(String ident) {
return (IdentValue)ALL_IDENT_VALUES.get(ident);
return ALL_IDENT_VALUES.get(ident);
}

public static int getIdentCount() {
Expand All @@ -318,10 +312,7 @@ public static int getIdentCount() {
* @param ident The feature to be added to the Value attribute
* @return Returns
*/
private final static synchronized IdentValue addValue(String ident) {
if (ALL_IDENT_VALUES == null) {
ALL_IDENT_VALUES = new HashMap<String, IdentValue>();
}
private final static IdentValue addValue(String ident) {
IdentValue val = new IdentValue(ident);
ALL_IDENT_VALUES.put(ident, val);
return val;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,25 @@ protected BitSet getAllowed() {
return ALLOWED;
}
}

public static class BreakBefore extends SingleIdent {
private static final BitSet ALLOWED = setFor(
new IdentValue[] { IdentValue.AUTO, IdentValue.COLUMN });

protected BitSet getAllowed() {
return ALLOWED;
}
}

public static class BreakAfter extends SingleIdent {
private static final BitSet ALLOWED = setFor(
new IdentValue[] { IdentValue.AUTO, IdentValue.COLUMN });

protected BitSet getAllowed() {
return ALLOWED;
}
}

public static class Page extends AbstractPropertyBuilder {
public List buildDeclarations(
CSSName cssName, List values, int origin, boolean important, boolean inheritAllowed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,10 @@ public boolean isAutoZIndex() {
}

public boolean establishesBFC() {
if (hasColumns()) {
return true;
}

FSDerivedValue value = valueByName(CSSName.POSITION);

if (value instanceof FunctionValue) { // running(header)
Expand Down Expand Up @@ -1031,7 +1035,7 @@ public boolean isListItem() {
}

public boolean hasColumns() {
return !isIdent(CSSName.COLUMN_COUNT, IdentValue.AUTO) && asFloat(CSSName.COLUMN_COUNT) > 1;
return !isIdent(CSSName.COLUMN_COUNT, IdentValue.AUTO) && asFloat(CSSName.COLUMN_COUNT) > 1;
}

public int columnCount() {
Expand Down Expand Up @@ -1091,6 +1095,14 @@ public boolean isForcePageBreakAfter() {
return val == IdentValue.ALWAYS || val == IdentValue.LEFT
|| val == IdentValue.RIGHT;
}

public boolean isColumnBreakBefore() {
return isIdent(CSSName.BREAK_BEFORE, IdentValue.COLUMN);
}

public boolean isColumnBreakAfter() {
return isIdent(CSSName.BREAK_AFTER, IdentValue.COLUMN);
}

public boolean isAvoidPageBreakInside() {
return isIdent(CSSName.PAGE_BREAK_INSIDE, IdentValue.AVOID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ private static void createChildren(
child.setElement(element);

if (style.hasColumns() && c.isPrint()) {
FlowingColumnContainerBox cont = (FlowingColumnContainerBox) child;
FlowingColumnContainerBox cont = (FlowingColumnContainerBox) child;
cont.setOnlyChild(c, new FlowingColumnBox(cont));
cont.getChild().setStyle(style.createAnonymousStyle(IdentValue.BLOCK));
cont.getChild().setElement(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
* non-floated (block) boxes.
*/
public class FloatManager {
private static final int LEFT = 1;
private static final int RIGHT = 2;
public static final int LEFT = 1;
public static final int RIGHT = 2;

/* Lazily created for performance. */
private List<BoxOffset> _leftFloats = Collections.emptyList();
Expand Down Expand Up @@ -124,7 +124,7 @@ private void position(CssContext cssCtx, BlockFormattingContext bfc,
}
}

private List<BoxOffset> getFloats(int direction) {
public List<BoxOffset> getFloats(int direction) {
return direction == LEFT ? _leftFloats : _rightFloats;
}

Expand Down Expand Up @@ -435,10 +435,10 @@ public void performFloatOperation(FloatOperation op) {
performFloatOperation(op, getFloats(RIGHT));
}

private static class BoxOffset {
private BlockBox _box;
private int _x;
private int _y;
public static class BoxOffset {
private final BlockBox _box;
private final int _x;
private final int _y;

public BoxOffset(BlockBox box, int x, int y) {
_box = box;
Expand All @@ -460,8 +460,8 @@ public int getY() {
}

private static class BoxDistance {
private BlockBox _box;
private int _distance;
private final BlockBox _box;
private final int _distance;

public BoxDistance(BlockBox box, int distance) {
_box = box;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,8 @@ private int layoutRunningFooter(LayoutContext c) {
}

private boolean isNeedAnalyzePageBreaks() {
Box b = getParent();
while (b != null) {
if (b.getStyle().isTable() && b.getStyle().isPaginateTable()) {
return false;
}

b = b.getParent();
}

return true;
Box b = findAncestor(bx -> bx.getStyle().isTable() && bx.getStyle().isPaginateTable());
return b == null;
}

private void analyzePageBreaks(LayoutContext c) {
Expand Down
Loading