Skip to content
Draft
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ nb-configuration.xml
.classpath
.project
.vscode
private.key
private.key
src/test/resources/fonts/*
7 changes: 7 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
248 changes: 248 additions & 0 deletions gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 21 additions & 15 deletions src/main/java/be/quodlibet/boxable/Paragraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,28 @@ public List<String> getLines() {
String word = token.getData();
float wordWidth = token.getWidth(currentFont);
if(wordWidth / 1000f * currentFontSize > width && width > font.getAverageFontWidth() / 1000f * currentFontSize) {
// you need to check if you have already something in your line
boolean alreadyTextInLine = false;
// If there's already content in the current line, flush it first
// before starting character-by-character splitting.
// This prevents mixing text from different wrap point segments
// on the same line (e.g. when a custom WrappingFunction is used).
if(textInLine.trimmedWidth()>0){
alreadyTextInLine = true;
textInLine.push(sinceLastWrapPoint);
textInLineMaxFontHeight = Math.max(textInLineMaxFontHeight, sinceLastWrapPointMaxFontHeight);
textInLineMaxFontSize = Math.max(textInLineMaxFontSize, sinceLastWrapPointMaxFontSize);
sinceLastWrapPointMaxFontHeight = 0f;
sinceLastWrapPointMaxFontSize = 0f;
result.add(textInLine.trimmedText());
lineWidths.put(lineCounter, textInLine.trimmedWidth());
mapLineTokens.put(lineCounter, textInLine.tokens());
lineHeights.put(lineCounter, textInLineMaxFontHeight == 0f
? FontUtils.getHeight(font, fontSize)
: textInLineMaxFontHeight);
lineFontSizes.put(lineCounter, textInLineMaxFontSize == 0f ? fontSize : textInLineMaxFontSize);
maxLineWidth = Math.max(maxLineWidth, textInLine.trimmedWidth());
textInLine.reset();
textInLineMaxFontHeight = 0f;
textInLineMaxFontSize = 0f;
lineCounter++;
}
while (wordWidth / 1000f * currentFontSize > width) {
float width = 0;
Expand All @@ -597,15 +615,6 @@ public List<String> getLines() {
} catch (IOException e) {
e.printStackTrace();
}
if(alreadyTextInLine){
if (width < this.width - textInLine.trimmedWidth()) {
firstPartOfWord.append(c);
firstPartWordWidth = Math.max(width, firstPartWordWidth);
} else {
restOfTheWord.append(c);
restOfTheWordWidth = Math.max(width, restOfTheWordWidth);
}
} else {
if (width < this.width) {
firstPartOfWord.append(c);
firstPartWordWidth = Math.max(width, firstPartWordWidth);
Expand All @@ -622,10 +631,7 @@ public List<String> getLines() {

}
}
}
}
// reset
alreadyTextInLine = false;
sinceLastWrapPoint.push(currentFont, currentFontSize,
Token.text(TokenType.TEXT, firstPartOfWord.toString()));
sinceLastWrapPointMaxFontHeight = Math.max(sinceLastWrapPointMaxFontHeight,
Expand Down
45 changes: 39 additions & 6 deletions src/main/java/be/quodlibet/boxable/text/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,47 @@ private static Stack<Integer> findWrapPoints(String text) {

private static Stack<Integer> findWrapPointsWithFunction(String text, WrappingFunction wrappingFunction) {
final String[] split = wrappingFunction.getLines(text);
int textIndex = text.length();
final Stack<Integer> possibleWrapPoints = new Stack<>();
possibleWrapPoints.push(textIndex);
for (int i = split.length - 1; i > 0; i--) {
final int splitLength = split[i].length();
possibleWrapPoints.push(textIndex - splitLength);
textIndex -= splitLength;
possibleWrapPoints.push(text.length());

if (split.length == 0) {
return possibleWrapPoints;
}

// Find the actual position of each segment in the original text
// Wrap points should be placed AFTER any delimiter characters between segments
final List<Integer> wrapPointsList = new ArrayList<>();
int searchStartIndex = 0;
for (int i = 0; i < split.length - 1; i++) {
final String segment = split[i];
// Search for this segment starting from where the previous segment ended
// This handles cases where segments might contain repeated substrings
final int segmentIndex = text.indexOf(segment, searchStartIndex);
if (segmentIndex >= 0) {
// The wrap point is at the start of the next segment
// (any characters between this segment and the next are delimiters)
final int endOfSegment = segmentIndex + segment.length();
// Find the next segment to determine where delimiters end
final String nextSegment = split[i + 1];
// Search for next segment starting after this segment ends
final int nextSegmentIndex = text.indexOf(nextSegment, endOfSegment);
if (nextSegmentIndex >= 0) {
// Wrap point is at the start of the next segment
// This means delimiters between segments will be included in the previous TEXT token
wrapPointsList.add(nextSegmentIndex);
searchStartIndex = nextSegmentIndex;
} else {
// Couldn't find next segment, move past current segment
searchStartIndex = endOfSegment;
}
}
}

// Push wrap points in reverse order onto the stack (required by the tokenizer)
for (int i = wrapPointsList.size() - 1; i >= 0; i--) {
possibleWrapPoints.push(wrapPointsList.get(i));
}

return possibleWrapPoints;
}

Expand Down
Loading