Skip to content

Commit

Permalink
Attempt to fix edge cases in columns not lining up in generated indexes
Browse files Browse the repository at this point in the history
Reported issue is SlimeKnights/TinkersConstruct#4720
Mantle text drawing logic for list elements includes a prefix element which is drawn before the text ignoring the split. In some other languages, the recommend split is after the prefix element causing the estimate to be too many lines, while the renderer never splits after the prefix.
To fix this issue, just ask Mantle directly for the number of lines needed to draw the text considering the prefix instead of using the Minecraft font renderer
  • Loading branch information
KnightMiner committed Feb 13, 2024
1 parent 40bf198 commit 1f18116
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slimeknights.mantle.client.book.data.content;

import lombok.Getter;
import net.minecraft.ChatFormatting;
import net.minecraft.Util;
import slimeknights.mantle.Mantle;
import slimeknights.mantle.client.book.data.BookData;
Expand All @@ -9,6 +10,7 @@
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.book.transformer.ContentListingSectionTransformer;
import slimeknights.mantle.client.screen.book.BookScreen;
import slimeknights.mantle.client.screen.book.TextDataRenderer;
import slimeknights.mantle.client.screen.book.element.BookElement;
import slimeknights.mantle.client.screen.book.element.ListingLeftElement;

Expand Down Expand Up @@ -142,8 +144,14 @@ public void build(BookData book, ArrayList<BookElement> list, boolean rightSide)
if (text.isEmpty()) {
y += LINE_HEIGHT;
} else {
if (!data.bold) text = "- " + text;
int height = this.parent.parent.parent.fontRenderer.wordWrapHeight(text, width) * LINE_HEIGHT / 9;
// if (!data.bold) text = "- " + text;
// int height = this.parent.parent.parent.fontRenderer.wordWrapHeight(text, width) * LINE_HEIGHT / 9;
int height;
if (data.bold) {
height = TextDataRenderer.getLinesForString(text, ChatFormatting.BOLD.toString(), width, "", parent.parent.parent.fontRenderer) * LINE_HEIGHT;
} else {
height = TextDataRenderer.getLinesForString(text, "", width, "- ", parent.parent.parent.fontRenderer) * LINE_HEIGHT;
}
list.add(new ListingLeftElement(x, y + yOff, width, height, data.bold, data));
y += height;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.Lists;
import lombok.Getter;
import net.minecraft.ChatFormatting;
import net.minecraft.resources.ResourceLocation;
import slimeknights.mantle.Mantle;
import slimeknights.mantle.client.book.data.BookData;
Expand All @@ -10,6 +11,7 @@
import slimeknights.mantle.client.book.data.content.ContentListing;
import slimeknights.mantle.client.book.data.content.ContentPadding.ContentRightPadding;
import slimeknights.mantle.client.screen.book.BookScreen;
import slimeknights.mantle.client.screen.book.TextDataRenderer;

import javax.annotation.Nullable;
import java.util.List;
Expand Down Expand Up @@ -121,8 +123,13 @@ public boolean hasEntries() {
}

/** Increases the number of entries in the column */
private void incrementColumns(String text) {
entriesInColumn += section.parent.fontRenderer.wordWrapHeight(text, COLUMN_WIDTH) / 9;
private void incrementColumns(String text, boolean bold) {
// entriesInColumn += section.parent.fontRenderer.wordWrapHeight(text, COLUMN_WIDTH) / 9;
if (bold) {
entriesInColumn += TextDataRenderer.getLinesForString(text, ChatFormatting.BOLD.toString(), COLUMN_WIDTH, "", section.parent.fontRenderer);
} else {
entriesInColumn += TextDataRenderer.getLinesForString(text, "", COLUMN_WIDTH, "- ", section.parent.fontRenderer);
}
}

/** Starts a new column */
Expand Down Expand Up @@ -155,7 +162,7 @@ public void addGroup(String name, @Nullable PageData data) {
}

// add the title entry to the column
incrementColumns(name);
incrementColumns(name, true);
currentListing.addEntry(name, data, true);
}

Expand All @@ -164,7 +171,7 @@ public void addPage(String name, PageData data) {
if (entriesInColumn == maxInColumn) {
startNewColumn(false);
}
incrementColumns("- " + name);
incrementColumns(name, false);
currentListing.addEntry(name, data, false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public static String[] cropStringBySize(String s, String modifiers, int width, i
for (int i = 0; i < s.length(); i++) {
curWidth += fr.width(modifiers + s.charAt(i)) * scale;

if (s.charAt(i) == '\n' || (curHeight == (int) (fr.lineHeight * scale) && curWidth > firstWidth) || (curHeight != (int) (fr.lineHeight * scale) && curWidth > width)) {
if (s.charAt(i) == '\n' || (curHeight == (int) (fr.lineHeight * scale) ? curWidth > firstWidth : curWidth > width)) {
int oldI = i;
if (s.charAt(i) != '\n') {
while (i >= 0 && s.charAt(i) != ' ') {
Expand Down Expand Up @@ -234,6 +234,11 @@ public static String[] cropStringBySize(String s, String modifiers, int width, i
return s.split("\r");
}

/** Gets the number of lines needed to render the given text */
public static int getLinesForString(String s, String modifiers, int width, String prefix, Font fr) {
return cropStringBySize(s, modifiers, width, Short.MAX_VALUE, width - fr.width(prefix), fr, 1.0f).length;
}

//BEGIN METHODS FROM GUI
public static void drawScaledString(PoseStack matrixStack, Font font, String text, float x, float y, int color, boolean dropShadow, float scale) {
matrixStack.pushPose();
Expand Down

0 comments on commit 1f18116

Please sign in to comment.