Skip to content

Commit

Permalink
Fix not handle \n correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
kappa-maintainer committed Apr 3, 2024
1 parent 09519f6 commit 8773b10
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ apply plugin: "com.github.johnrengelman.shadow"
apply plugin: 'org.spongepowered.mixin'


version "1.3.2-beta"
version "1.3.3-beta"
group "gkappa.wrapfix"
archivesBaseName = "WrapFix"

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gkappa/wrapfix/WrapFix.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class WrapFix {
public static final String MODID = "wrapfix";
public static final String NAME = "WrapFix";
public static final String VERSION = "1.3.2-beta";
public static final String VERSION = "1.3.3-beta";

public static final BreakIterator BREAK_ITERATOR = BreakIterator.getLineInstance();
public static Logger logger;
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/gkappa/wrapfix/mixin/MixinFontRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
@Mixin({FontRenderer.class})
public abstract class MixinFontRenderer {

@Shadow
private static boolean isFormatSpecial(char formatChar) {
return false;
}

@Inject(method = "renderStringAtPos", at = @At(value = "INVOKE", target = "Ljava/lang/String;length()I", ordinal = 1))
private void captureLocal(String text, boolean shadow, CallbackInfo ci, @Share("i") LocalIntRef intRef, @Local(ordinal = 0) int index) {
Expand Down Expand Up @@ -62,11 +58,11 @@ private void wrapStringToWidthICU4J(String str, int wrapWidth, CallbackInfoRetur
switch (current) {
case '\n':
list.add(line.toString());
fed++;
fed = i + 1;
line.delete(0, line.length()).append(format);
lineWidth = 0;
widths[i - fed] = lineWidth;
formats[i - fed] = format.toString();
widths[0] = lineWidth;
formats[0] = format.toString();
continue;
case '§':
if (i + 1 < chars.length) { // Prevent out of bound
Expand Down Expand Up @@ -109,7 +105,7 @@ private void wrapStringToWidthICU4J(String str, int wrapWidth, CallbackInfoRetur
} else {
icui = WrapFix.BREAK_ITERATOR.preceding(i);
}
if (icui <= fed || i == icui) {
if (icui <= fed + 1 || i == icui) {
list.add(line.substring(0,line.length() - 1));
fed = i;
line.delete(0, line.length()).append(format).append(current);
Expand Down Expand Up @@ -140,4 +136,10 @@ public int getCharWidth(char c0) {
private static boolean isFormatColor(char c1) {
return false;
}


@Shadow
private static boolean isFormatSpecial(char formatChar) {
return false;
}
}
52 changes: 26 additions & 26 deletions src/main/java/gkappa/wrapfix/mixin/MixinRenderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.commons.lang3.tuple.Pair;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
Expand Down Expand Up @@ -46,42 +47,34 @@ private static void splitString(String str, int wrapWidth, FontRenderer font, Ca
switch (current) {
case '\n':
list.add(line.toString());
fed = i;
fed = i + 1;
line.delete(0, line.length()).append(format);
lineWidth = 0;
widths[i - fed] = lineWidth;
formats[i - fed] = format.toString();
widths[0] = lineWidth;
formats[0] = format.toString();
continue;
case '§':
if (i + 1 < chars.length) { // Prevent out of bound
f = chars[i + 1];
if (f != 'l' && f != 'L') { // Check start of bold style
if (f == 'r' || f == 'R' || isFormatColor(f)) { // Not Bold, check end of style
bold = false;
if (f == 'r' || f == 'R') {
boolean isC = isFormatColor(f);
if (isC || isFormatSpecial(f)) {
if (f != 'l' && f != 'L') { // Check start of bold style
if (f == 'r' || f == 'R') { // Not Bold, check end of style
bold = false;
format.delete(0, format.length()); // Clear the format
} else {
format.append('§').append(f); // Add to current format code
} else if (isC) {
bold = false;
}
line.append('§').append(f);
widths[i - fed] = lineWidth;
formats[i - fed] = format.toString();
i++;
continue;
}
if (f >= 'k' && f <= 'o' || f >= 'K' && f <= 'O') {
format.append('§').append(f); // Add to current format code
line.append('§').append(f);
widths[i - fed] = lineWidth;
formats[i - fed] = format.toString();
continue;
} else {
bold = true;
}
} else {
bold = true;
format.append('§').append(f); // Add to current format code
line.append('§').append(f);
widths[i - fed] = lineWidth;
widths[i - fed + 1] = lineWidth;
formats[i - fed] = format.toString();
formats[i - fed + 1] = format.toString();
i++;
continue;
}
}
Expand All @@ -101,17 +94,18 @@ private static void splitString(String str, int wrapWidth, FontRenderer font, Ca
} else {
icui = WrapFix.BREAK_ITERATOR.preceding(i);
}
if (icui <= fed || i == icui) {
if (icui <= fed + 1 || i == icui) {
list.add(line.substring(0,line.length() - 1));
fed = i - 1;
fed = i;
line.delete(0, line.length()).append(format).append(current);
prevFormat = format.length();
lineWidth = font.getCharWidth(current);
} else {
d = icui - fed;
if (line.charAt(d + prevFormat - 1) == '§') d++;
list.add(line.substring(0, d + prevFormat));
temp = line.substring(d + prevFormat);
fed = icui;
fed += d;
line.delete(0, line.length()).append(formats[d]).append(temp);
prevFormat = formats[d].length();
lineWidth = lineWidth - widths[d - 1];
Expand All @@ -132,5 +126,11 @@ private static boolean isFormatColor(char c1) {
WrapFix.logger.error("MIXIN Failed");
return false;
}

@Unique
private static boolean isFormatSpecial(char formatChar)
{
return formatChar >= 'k' && formatChar <= 'o' || formatChar >= 'K' && formatChar <= 'O' || formatChar == 'r' || formatChar == 'R';
}
}

2 changes: 1 addition & 1 deletion src/main/resources/mcmod.info
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"modid": "wrapfix",
"name": "WrapFix",
"description": "Fix line wrapping",
"version": "1.3.2-beta",
"version": "1.3.3-beta",
"mcversion": "1.12.2",
"url": "",
"updateUrl": "",
Expand Down

0 comments on commit 8773b10

Please sign in to comment.