Skip to content

Commit

Permalink
Show warning when text in header/footer does not fit within the provi…
Browse files Browse the repository at this point in the history
…ded space (#43)

Related to #23
  • Loading branch information
bertfrees authored Jun 1, 2022
1 parent 8a36a04 commit 9a8980d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/org/daisy/dotify/formatter/impl/page/PageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,12 @@ private MarginProperties getMarginRegionValue(
throw new PaginatorException("Failed to translate: " + ret, e);
}
}
boolean spaceOnly = ret.length() == 0;
if (ret.length() < w) {
int len = ret.codePointCount(0, ret.length());
boolean spaceOnly = len == 0;
if (len < w) {
StringBuilder sb = new StringBuilder();
if (rightSide) {
while (sb.length() < w - ret.length()) {
while (sb.length() < w - len) {
sb.append(fcontext.getSpaceCharacter());
}
sb.append(ret);
Expand All @@ -241,9 +242,9 @@ private MarginProperties getMarginRegionValue(
}
}
ret = sb.toString();
} else if (ret.length() > w) {
} else if (len > w) {
if (fcontext.getConfiguration().isAllowingTextOverflowTrimming()) {
String trimmed = ret.substring(0, mr.getWidth());
String trimmed = ret.substring(0, ret.offsetByCodePoints(0, mr.getWidth()));
logger.log(Level.WARNING, "Cannot fit \"" + ret + "\" into a margin-region of size " + mr.getWidth()
+ ", trimming to \"" + trimmed + "\"");
ret = trimmed;
Expand Down
13 changes: 11 additions & 2 deletions src/org/daisy/dotify/formatter/impl/page/PaginatorTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Does positioning of header/footer fields, and layout of tables.
*/
class PaginatorTools {

private static final Logger logger = Logger.getLogger(PaginatorTools.class.getCanonicalName());

/**
* Distribution modes.
*/
Expand Down Expand Up @@ -50,7 +55,7 @@ private static List<String> distributeEqualSpacing(
unit = truncate(unit, width);
} else {
throw new PaginatorToolsException(
"Text does not fit within provided space of " + width + ": " + units.get(0)
"Text does not fit within provided space of " + width + ": " + unit
);
}
}
Expand Down Expand Up @@ -138,7 +143,11 @@ private static String distributeTable(

private static String truncate(String s, int lengthInCodePoints) {
if (s.codePointCount(0, s.length()) > lengthInCodePoints) {
return s.substring(0, s.offsetByCodePoints(0, lengthInCodePoints));
String truncated = s.substring(0, s.offsetByCodePoints(0, lengthInCodePoints));
logger.log(Level.WARNING,
"Text does not fit within provided space of " + lengthInCodePoints + ": \"" + s + "\""
+ ", trimming to \"" + truncated + "\"");
return truncated;
} else {
return s;
}
Expand Down

0 comments on commit 9a8980d

Please sign in to comment.