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

Show warning when text in header/footer does not fit within the provided space #43

Merged
merged 1 commit into from
Jun 1, 2022
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
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