Skip to content

Commit

Permalink
Adds support for volume-keep-priority
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Håkansson committed Jun 17, 2016
1 parent 646155d commit e5aac2a
Show file tree
Hide file tree
Showing 23 changed files with 982 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repositories {
dependencies {
provided group: "biz.aQute.bnd", name: "annotation", version: "2.3.0"
compile "org.daisy.dotify:dotify.api:2.9.0"
compile "org.daisy.dotify:dotify.common:2.0.2"
compile "org.daisy.dotify:dotify.common:2.1.0"
compile (group: 'org.daisy.libs', name: 'saxon-he', version: '9.5.1.5') {
exclude module: 'Saxon-HE'
}
Expand Down
7 changes: 6 additions & 1 deletion src/org/daisy/dotify/formatter/impl/AncestorContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

class AncestorContext {
private final BlockProperties blockProperties;
private final Integer volumeKeepPriority;
private int listIterator;

AncestorContext(BlockProperties props) {
AncestorContext(BlockProperties props, Integer volumeKeep) {
this.blockProperties = props;
this.volumeKeepPriority = volumeKeep;
this.listIterator = 0;
}
public BlockProperties getBlockProperties() {
return blockProperties;
}

public Integer getVolumeKeepPriority() {
return volumeKeepPriority;
}
public int nextListNumber() {
listIterator++;
return listIterator;
Expand Down
29 changes: 29 additions & 0 deletions src/org/daisy/dotify/formatter/impl/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ abstract class Block implements Cloneable {
private int keepWithNext;
private int keepWithPreviousSheets;
private int keepWithNextSheets;
private Integer avoidVolumeBreakInsidePriority;
private Integer avoidVolumeBreakAfterPriority;
private String id;
protected final Stack<Segment> segments;
protected RowDataProperties rdp;
Expand All @@ -42,6 +44,8 @@ abstract class Block implements Cloneable {
this.keepWithNext = 0;
this.keepWithPreviousSheets = 0;
this.keepWithNextSheets = 0;
this.avoidVolumeBreakInsidePriority = null;
this.avoidVolumeBreakAfterPriority = null;
this.id = "";
this.blockId = blockId;
this.segments = new Stack<>();
Expand Down Expand Up @@ -75,6 +79,14 @@ public int getKeepWithNextSheets() {
return keepWithNextSheets;
}

public Integer getVolumeKeepInsidePriority() {
return avoidVolumeBreakInsidePriority;
}

public Integer getVolumeKeepAfterPriority() {
return avoidVolumeBreakAfterPriority;
}

public String getIdentifier() {
return id;
}
Expand Down Expand Up @@ -103,6 +115,14 @@ public void setKeepWithNextSheets(int keepWithNextSheets) {
this.keepWithNextSheets = keepWithNextSheets;
}

public void setVolumeKeepInsidePriority(Integer priority) {
this.avoidVolumeBreakInsidePriority = priority;
}

public void setVolumeKeepAfterPriority(Integer priority) {
this.avoidVolumeBreakAfterPriority = priority;
}

public void setIdentifier(String id) {
this.id = id;
}
Expand Down Expand Up @@ -152,6 +172,15 @@ public void setRowDataProperties(RowDataProperties value) {
RenderingScenario getRenderingScenario() {
return rs;
}

Integer getAvoidVolumeBreakAfterPriority() {
return avoidVolumeBreakAfterPriority;
}

Integer getAvoidVolumeBreakInsidePriority() {
return avoidVolumeBreakInsidePriority;
}


@Override
public Object clone() {
Expand Down
40 changes: 37 additions & 3 deletions src/org/daisy/dotify/formatter/impl/FormatterCoreImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class FormatterCoreImpl extends Stack<Block> implements FormatterCore, BlockGrou
private Table table;
protected final FormatterCoreContext fc;
private MarkerProcessor mp;
//The code where this variable is used is not very nice, but it will do to get the feature running
private Boolean endStart = null;
// TODO: fix recursive keep problem
// TODO: Implement floating elements
public FormatterCoreImpl(FormatterCoreContext fc) {
Expand Down Expand Up @@ -84,6 +86,10 @@ public void startBlock(BlockProperties p, String blockId) {
if (table!=null) {
throw new IllegalStateException("A table is open.");
}
if (endStart!=null && endStart == true) {
getCurrentBlock().setVolumeKeepAfterPriority(getCurrentVolumeKeepPriority());
}
endStart = null;
String lb = "";
String rb = "";
if (p.getTextBorderStyle()!=null) {
Expand Down Expand Up @@ -137,7 +143,11 @@ public void startBlock(BlockProperties p, String blockId) {
}
c.setKeepWithNextSheets(p.getKeepWithNextSheets());
c.setVerticalPosition(p.getVerticalPosition());
propsContext.push(new AncestorContext(p));
AncestorContext ac = new AncestorContext(p, inheritVolumeKeepPriority(p.getVolumeKeepPriority()));
// We don't get the volume keep priority from block properties, because it could have been inherited from an ancestor
c.setVolumeKeepInsidePriority(ac.getVolumeKeepPriority());
c.setVolumeKeepAfterPriority(ac.getVolumeKeepPriority());
propsContext.push(ac);
Block bi = getCurrentBlock();
RowDataProperties.Builder builder = new RowDataProperties.Builder(bi.getRowDataProperties());
if (p.getTextBorderStyle()!=null) {
Expand All @@ -150,6 +160,18 @@ public void startBlock(BlockProperties p, String blockId) {
bi.setRowDataProperties(builder.build());
//firstRow = true;
}

private Integer inheritVolumeKeepPriority(Integer value) {
return (value==null?getCurrentVolumeKeepPriority():value);
}

private Integer getCurrentVolumeKeepPriority() {
return propsContext.isEmpty()?null:propsContext.peek().getVolumeKeepPriority();
}

private Integer getParentVolumeKeepPriority() {
return propsContext.size()<2?null:propsContext.get(propsContext.size()-2).getVolumeKeepPriority();
}

@Override
public void endBlock() {
Expand All @@ -159,8 +181,14 @@ public void endBlock() {
if (listItem!=null) {
addChars("", new TextProperties.Builder(null).build());
}
if (endStart == null) {
endStart = true;
} else {
endStart = false;
}
{
BlockProperties p = propsContext.pop().getBlockProperties();
AncestorContext ac = propsContext.pop();
BlockProperties p = ac.getBlockProperties();
Block bi = getCurrentBlock();
RowDataProperties.Builder builder = new RowDataProperties.Builder(bi.getRowDataProperties());
if (p.getTextBorderStyle()!=null) {
Expand All @@ -173,11 +201,14 @@ public void endBlock() {
outerSpaceAfter(bi.getRowDataProperties().getOuterSpaceAfter()+p.getMargin().getBottomSpacing());
bi.setKeepWithPreviousSheets(p.getKeepWithPreviousSheets());
bi.setRowDataProperties(builder.build());
//set the volume keep after for the closing block to the parent priority
bi.setVolumeKeepAfterPriority(getCurrentVolumeKeepPriority());
}
leftMargin.pop();
rightMargin.pop();
if (propsContext.size()>0) {
BlockProperties p = propsContext.peek().getBlockProperties();
AncestorContext ac = propsContext.peek();
BlockProperties p = ac.getBlockProperties();
Keep keep = p.getKeepType();
int next = p.getKeepWithNext();
subtractFromBlockIndent(p.getBlockIndent());
Expand All @@ -198,6 +229,9 @@ public void endBlock() {
Block c = newBlock(null, rdp.build());
c.setKeepType(keep);
c.setKeepWithNext(next);
// We don't get the volume keep priority from the BlockProperties, as it could have been inherited from an ancestor
c.setVolumeKeepInsidePriority(getCurrentVolumeKeepPriority());
c.setVolumeKeepAfterPriority(getParentVolumeKeepPriority());
}
//firstRow = true;
}
Expand Down
10 changes: 10 additions & 0 deletions src/org/daisy/dotify/formatter/impl/PageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class PageImpl implements Page {
private boolean isVolBreak;
private boolean isVolBreakAllowed;
private int keepPreviousSheets;
private Integer volumeBreakAfterPriority;
private int volumeNumber;


Expand All @@ -74,6 +75,7 @@ public PageImpl(LayoutMaster master, FormatterContext fcontext, int pageIndex, L
this.isVolBreak = false;
this.isVolBreakAllowed = true;
this.keepPreviousSheets = 0;
this.volumeBreakAfterPriority = null;
this.volumeNumber = 0;
}

Expand Down Expand Up @@ -708,5 +710,13 @@ int getVolumeNumber() {
void setVolumeNumber(int volumeNumber) {
this.volumeNumber = volumeNumber;
}

Integer getAvoidVolumeBreakAfter() {
return volumeBreakAfterPriority;
}

void setAvoidVolumeBreakAfter(Integer value) {
this.volumeBreakAfterPriority = value;
}

}
17 changes: 16 additions & 1 deletion src/org/daisy/dotify/formatter/impl/PageSequenceBuilder2.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ PageImpl nextPage() throws PaginatorException, RestartPaginationException {
}
force = res.getHead().size()==0;
data = res.getTail();
for (RowGroup rg : res.getHead()) {
List<RowGroup> head = res.getHead();
for (RowGroup rg : head) {
addProperties(rg);
for (RowImpl r : rg.getRows()) {
if (r.shouldAdjustForMargin()) {
Expand All @@ -178,6 +179,12 @@ PageImpl nextPage() throws PaginatorException, RestartPaginationException {
currentPage().newRow(r);
}
}
Integer lastPriority = getLastPriority(head);
if (!res.getDiscarded().isEmpty()) {
//override if not empty
lastPriority = getLastPriority(res.getDiscarded());
}
currentPage().setAvoidVolumeBreakAfter(lastPriority);
for (RowGroup rg : res.getDiscarded()) {
addProperties(rg);
}
Expand All @@ -199,6 +206,14 @@ PageImpl nextPage() throws PaginatorException, RestartPaginationException {
return ret;
}

private static Integer getLastPriority(List<RowGroup> list) {
if (!list.isEmpty()) {
return list.get(list.size()-1).getAvoidVolumeBreakAfterPriority();
} else {
return null;
}
}

private boolean firstUnitHasSupplements(SplitPointData<RowGroup> spd) {
return !spd.getUnits().isEmpty() && !spd.getUnits().get(0).getSupplementaryIDs().isEmpty();
}
Expand Down
4 changes: 4 additions & 0 deletions src/org/daisy/dotify/formatter/impl/PageSequenceRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ boolean isDataEmpty() {
return data.isDataEmpty();
}

RowGroupSequence currentSequence() {
return data.dataGroups.peek();
}

void addRowGroup(RowGroup rg) {
data.addRowGroup(rg);
}
Expand Down
10 changes: 9 additions & 1 deletion src/org/daisy/dotify/formatter/impl/PageStructBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,21 @@ List<Sheet> paginate(DefaultContext rcontext) throws PaginatorException {
si = new SheetIdentity(rcontext.getSpace(), rcontext.getCurrentVolume()==null?0:rcontext.getCurrentVolume(), ret.size());
sheetIndex++;
}
s.avoidVolumeBreakAfterPriority(p.getAvoidVolumeBreakAfter());
if (sheetIndex==1 && bs.getSequenceProperties().getBreakBeforeType()==SequenceBreakBefore.VOLUME) {
s.startNewVolume(true);
}
if (pageIndex==pages.size()-1) {
s.avoidVolumeBreakAfterPriority(null);
//Don't get or store this value in crh as it is transient and not a property of the sheet context
s.breakable(true);
} else {
s.breakable(crh.getBreakable(si));
boolean br = crh.getBreakable(si);
//TODO: the following is a low effort way of giving existing uses of non-breakable units a high priority, but it probably shouldn't be done this way
if (!br) {
s.avoidVolumeBreakAfterPriority(1);
}
s.breakable(br);
}

setPreviousSheet(si.getSheetIndex()-1, Math.min(p.keepPreviousSheets(), sheetIndex-1), rcontext);
Expand All @@ -63,6 +70,7 @@ List<Sheet> paginate(DefaultContext rcontext) throws PaginatorException {
s.add(p);
}
if (s!=null) {
//Last page in the sequence doesn't need volume keep priority
ret.add(s.build());
}
} catch (RestartPaginationException e) {
Expand Down
10 changes: 10 additions & 0 deletions src/org/daisy/dotify/formatter/impl/RowGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class RowGroup implements SplitPointUnit {
private final List<String> ids;
private final String identifier;
private final int keepWithNextSheets, keepWithPreviousSheets;
private Integer avoidVolumeBreakAfterPriority = null;

static class Builder {
private final List<RowImpl> rows;
Expand Down Expand Up @@ -135,6 +136,7 @@ private RowGroup(Builder builder) {
this.identifier = template.identifier;
this.keepWithNextSheets = template.keepWithNextSheets;
this.keepWithPreviousSheets = template.keepWithPreviousSheets;
this.avoidVolumeBreakAfterPriority = template.avoidVolumeBreakAfterPriority;
}

private static float getRowSpacing(float rowDefault, RowImpl r) {
Expand Down Expand Up @@ -249,4 +251,12 @@ public int getKeepWithPreviousSheets() {
return keepWithPreviousSheets;
}

public Integer getAvoidVolumeBreakAfterPriority() {
return avoidVolumeBreakAfterPriority;
}

void setAvoidVolumeBreakAfterPriority(Integer value) {
this.avoidVolumeBreakAfterPriority = value;
}

}
Loading

0 comments on commit e5aac2a

Please sign in to comment.