Skip to content

Commit

Permalink
Adds a split option to disregard the last unit size
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Håkansson committed Feb 13, 2018
1 parent 8859613 commit f09c7e1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ repositoryRevision=N/A
repositoryURL=https://github.com/brailleapps/dotify.common
repositorySCM=scm:git:https://github.com/brailleapps/dotify.common.git
moduleName=org.daisy.dotify.common
version=4.0.1-SNAPSHOT
version=4.1.0-SNAPSHOT
8 changes: 5 additions & 3 deletions src/org/daisy/dotify/common/splitter/SizeStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class SizeStep<T extends SplitPointUnit> implements StepForward<T> {
private final Supplements<T> map;
private final Set<String> ids;
private final float breakPoint;
private final boolean useLastUnitSize;
private T lastUnit;
private boolean hasSupplements;

SizeStep(float breakPoint, Supplements<T> map) {
SizeStep(float breakPoint, Supplements<T> map, boolean useLastUnitSize) {
this.breakPoint = breakPoint;
this.useLastUnitSize = useLastUnitSize;
this.map = map;
this.ids = new HashSet<>();
this.hasSupplements = false;
Expand Down Expand Up @@ -48,7 +50,7 @@ public boolean overflows(T buffer) {
return size+(
buffer!=null?
lastUnitSize(buffer) + (lastUnit!=null?lastUnit.getUnitSize():0):
lastUnit!=null?lastUnit.getLastUnitSize():0
lastUnit!=null?(useLastUnitSize?lastUnit.getLastUnitSize():lastUnit.getUnitSize()):0
)>breakPoint;
}

Expand All @@ -70,7 +72,7 @@ private float lastUnitSize(T b) {
}
}
}
ret += b.getLastUnitSize();
ret += useLastUnitSize?b.getLastUnitSize():b.getUnitSize();
return ret;
}

Expand Down
15 changes: 9 additions & 6 deletions src/org/daisy/dotify/common/splitter/SplitPointHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ public SplitPointSpecification find(float breakPoint, U data, SplitPointCost<T>
return SplitPointSpecification.empty();
} else if (breakPoint<=0) {
return SplitPointSpecification.none();
} else if (fits(data, breakPoint)) {
} else if (fits(data, breakPoint, opts.useLastUnitSize)) {
return SplitPointSpecification.all();
} else {
int startPos = findCollapse(data, new SizeStep<>(breakPoint, data.getSupplements()));
int startPos = findCollapse(data, new SizeStep<>(breakPoint, data.getSupplements(), opts.useLastUnitSize));
// If no units are returned here it's because even the first unit doesn't fit.
// Therefore, force will not help.
if (startPos<0) {
Expand All @@ -189,13 +189,16 @@ public SplitPointSpecification find(float breakPoint, U data, SplitPointCost<T>
private static class SplitOptions {
boolean useForce = false;
boolean trimTrailing = true;
boolean useLastUnitSize = true;
static SplitOptions parse(SplitOption ... opts) {
SplitOptions result = new SplitOptions();
for (SplitOption option : opts) {
if (option==StandardSplitOption.ALLOW_FORCE) {
result.useForce = true;
} else if (option==StandardSplitOption.RETAIN_TRAILING) {
result.trimTrailing = false;
} else if (option==StandardSplitOption.NO_LAST_UNIT_SIZE) {
result.useLastUnitSize = false;
} else if (option == null) {
//no-op
} else {
Expand Down Expand Up @@ -445,8 +448,8 @@ private static class BreakPointScannerResult {
* @param limit the maximum width that is relevant to calculate
* @return returns the size
*/
static <T extends SplitPointUnit, U extends SplitPointDataSource<T, U>> boolean fits(U data, float limit) {
return totalSize(data, limit)<=limit;
static <T extends SplitPointUnit, U extends SplitPointDataSource<T, U>> boolean fits(U data, float limit, boolean useLastUnitSize) {
return totalSize(data, limit, useLastUnitSize)<=limit;
}
/**
* If the total size is less than the limit, the size is returned, otherwise a value greater
Expand All @@ -456,7 +459,7 @@ static <T extends SplitPointUnit, U extends SplitPointDataSource<T, U>> boolean
* @param limit the maximum width that is relevant to calculate
* @return returns the size
*/
static <T extends SplitPointUnit, U extends SplitPointDataSource<T, U>> float totalSize(U data, float limit) {
static <T extends SplitPointUnit, U extends SplitPointDataSource<T, U>> float totalSize(U data, float limit, boolean useLastUnitSize) {
float ret = 0;
Set<String> ids = new HashSet<>();
Supplements<T> map = data.getSupplements();
Expand All @@ -480,7 +483,7 @@ static <T extends SplitPointUnit, U extends SplitPointDataSource<T, U>> float to
}
}
//last unit?
if (!data.hasElementAt(i+1)) {
if (useLastUnitSize && !data.hasElementAt(i+1)) {
ret += unit.getLastUnitSize();
} else {
ret += unit.getUnitSize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ public enum StandardSplitOption implements SplitOption {
/**
* Retain trailing skippable units
*/
RETAIN_TRAILING;
RETAIN_TRAILING,
/**
* Treats the last unit as a regular unit.
*/
NO_LAST_UNIT_SIZE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public void testTotalSize_01() {
final DummySplitPoint s1 = new DummySplitPoint.Builder().breakable(true).skippable(false).size(1).minSize(0.5f).build();
final DummySplitPoint s2 = new DummySplitPoint.Builder().breakable(true).skippable(false).size(1).minSize(0.5f).build();
final DummySplitPoint s3 = new DummySplitPoint.Builder().breakable(true).skippable(false).size(1).minSize(0.5f).build();
float res = SplitPointHandler.totalSize(new SplitPointDataList<DummySplitPoint>(Arrays.asList(s1, s2, s3)), 3);
float res = SplitPointHandler.totalSize(new SplitPointDataList<DummySplitPoint>(Arrays.asList(s1, s2, s3)), 3, true);
assertEquals(2.5, res, 0);
}

Expand Down

0 comments on commit f09c7e1

Please sign in to comment.