Skip to content

Commit

Permalink
Merge branch 'fix-desync-charactorvisitors' into mc1.21.4
Browse files Browse the repository at this point in the history
  • Loading branch information
keve1227 committed Feb 6, 2025
2 parents 007cb65 + 8e71e47 commit a70f6fc
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 364 deletions.
21 changes: 15 additions & 6 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ ij_smart_tabs = false
ij_visual_guides = 80, 120
ij_wrap_on_typing = false

[*.yml]
indent_size = 2
tab_width = 2
ij_yaml_keep_indents_on_empty_lines = false
ij_yaml_keep_line_breaks = true
ij_yaml_space_before_colon = false
ij_yaml_spaces_within_braces = true
ij_yaml_spaces_within_brackets = false

[*.java]
ij_java_align_consecutive_assignments = false
ij_java_align_consecutive_variable_declarations = false
Expand Down Expand Up @@ -72,12 +81,12 @@ ij_java_case_statement_on_separate_line = true
ij_java_catch_on_new_line = false
ij_java_class_annotation_wrap = split_into_lines
ij_java_class_brace_style = end_of_line
ij_java_class_count_to_use_import_on_demand = 5
ij_java_class_count_to_use_import_on_demand = 3
ij_java_class_names_in_javadoc = 1
ij_java_deconstruction_list_wrap = on_every_item
ij_java_do_not_indent_top_level_class_members = false
ij_java_do_not_wrap_after_single_annotation = false
ij_java_do_not_wrap_after_single_annotation_in_parameter = false
ij_java_do_not_wrap_after_single_annotation = true
ij_java_do_not_wrap_after_single_annotation_in_parameter = true
ij_java_do_while_brace_force = if_multiline
ij_java_doc_add_blank_line_after_description = true
ij_java_doc_add_blank_line_after_param_comments = false
Expand All @@ -102,7 +111,7 @@ ij_java_enum_constants_wrap = on_every_item
ij_java_enum_field_annotation_wrap = off
ij_java_extends_keyword_wrap = normal
ij_java_extends_list_wrap = normal
ij_java_field_annotation_wrap = split_into_lines
ij_java_field_annotation_wrap = normal
ij_java_field_name_prefix =
ij_java_field_name_suffix =
ij_java_finally_on_new_line = false
Expand Down Expand Up @@ -154,7 +163,7 @@ ij_java_new_line_after_lparen_in_deconstruction_pattern = true
ij_java_new_line_after_lparen_in_record_header = true
ij_java_new_line_when_body_is_presented = false
ij_java_packages_to_use_import_on_demand = java.awt.*, javax.swing.*
ij_java_parameter_annotation_wrap = off
ij_java_parameter_annotation_wrap = normal
ij_java_parameter_name_prefix =
ij_java_parameter_name_suffix =
ij_java_parentheses_expression_new_line_after_left_paren = true
Expand Down Expand Up @@ -267,7 +276,7 @@ ij_java_use_external_annotations = false
ij_java_use_fq_class_names = false
ij_java_use_relative_indents = false
ij_java_use_single_class_imports = true
ij_java_variable_annotation_wrap = off
ij_java_variable_annotation_wrap = normal
ij_java_visibility = public
ij_java_while_brace_force = if_multiline
ij_java_while_on_new_line = false
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: Artifacts
path: build/libs/
path: build/libs/
14 changes: 8 additions & 6 deletions src/main/java/com/kevinsundqvistnorlen/rubi/IRubyStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import net.minecraft.text.Style;

public interface IRubyStyle {
Style withRuby(String word, String ruby);
RubyText getRuby();
Style removeRuby();
import java.util.Optional;

static RubyText getRuby(Style style) {
return ((IRubyStyle) style).getRuby();
public interface IRubyStyle {
static Optional<RubyText> getRuby(Style style) {
return Optional.ofNullable(((IRubyStyle) style).rubi$getRuby());
}

Style rubi$withRuby(String word, String ruby);
RubyText rubi$getRuby();
Style rubi$removeRuby();
}
159 changes: 57 additions & 102 deletions src/main/java/com/kevinsundqvistnorlen/rubi/RubyText.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
package com.kevinsundqvistnorlen.rubi;

import com.kevinsundqvistnorlen.rubi.option.ITextHandler;
import com.kevinsundqvistnorlen.rubi.option.RubyRenderMode;
import net.minecraft.client.font.TextHandler;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Style;
import net.minecraft.text.TextVisitFactory;
import net.minecraft.text.*;
import org.apache.commons.lang3.mutable.MutableFloat;
import org.apache.commons.lang3.mutable.MutableObject;
import org.joml.Matrix4f;

import java.util.*;
import java.util.Objects;
import java.util.regex.Pattern;

public record RubyText(String text, String ruby) {

public record RubyText(String text, String ruby, Style style) {
public static final Pattern RUBY_PATTERN = Pattern.compile("\\^\\s*(.+?)\\s*\\(\\s*(.+?)\\s*\\)");
public static final Pattern RUBY_PATTERN_FOR_STRIPPING = Pattern.compile(\\^\\s*(.+?)\\s*\\(\\s*(.+?)\\s*\\)");

public static final char RUBY_MARKER = '\uFFFC';//'\ue9c0';
public static final Pattern RUBY_PATTERN_FOR_STRIPPING = Pattern.compile("§" + RUBY_PATTERN.pattern());

public static final float RUBY_SCALE = 0.5f;
public static final float RUBY_OVERLAP = 0.1f;
Expand All @@ -34,92 +27,66 @@ public static String strip(String returnValue) {
return sb.toString();
}

public float getWidth(ITextHandler textHandler, Style style) {
var mode = RubyRenderMode.getOption().getValue();
float baseWidth = 0f,
rubyWidth = 0f;
if (mode != RubyRenderMode.REPLACE) baseWidth = textHandler.getWidth(OrderedText.styledForwardsVisitedString(this.text(), style));
if (mode != RubyRenderMode.HIDDEN) rubyWidth = textHandler.getWidth(OrderedText.styledForwardsVisitedString(this.ruby(), style));
return switch (mode) {
case ABOVE, BELOW -> Math.max(
baseWidth * RubyText.TEXT_SCALE,
rubyWidth * RubyText.RUBY_SCALE
);
case HIDDEN -> baseWidth;
case REPLACE -> rubyWidth;
};
public float draw(
float x,
float y,
Matrix4f matrix,
TextHandler handler,
int fontHeight,
TextDrawer drawer
) {
float width = handler.getWidth(OrderedText.styled('', this.style));

switch (RubyRenderMode.getOption().getValue()) {
case ABOVE -> this.drawAbove(x, y, width, matrix, handler, fontHeight, drawer);
case BELOW -> this.drawBelow(x, y, width, matrix, handler, fontHeight, drawer);
case REPLACE -> this.drawReplace(x, y, matrix, drawer);
case HIDDEN -> this.drawHidden(x, y, matrix, drawer);
}

return width;
}

public float getWidth(TextHandler.WidthRetriever widthRetriever, Style style) {
var mode = RubyRenderMode.getOption().getValue();
MutableFloat baseWidth = new MutableFloat(),
rubyWidth = new MutableFloat();
if (mode != RubyRenderMode.REPLACE) TextVisitFactory.visitForwards(this.text(), style, (unused, s, codePoint) -> {
baseWidth.add(widthRetriever.getWidth(codePoint, s));
return true;
});
if (mode != RubyRenderMode.HIDDEN) TextVisitFactory.visitForwards(this.ruby(), style, (unused, s, codePoint) -> {
rubyWidth.add(widthRetriever.getWidth(codePoint, s));
return true;
});
public float getWidth(TextHandler.WidthRetriever widthRetriever) {
final var mode = RubyRenderMode.getOption().getValue();
final var style = ((IRubyStyle) this.style).rubi$removeRuby();
MutableFloat baseWidth = new MutableFloat(), rubyWidth = new MutableFloat();

if (mode != RubyRenderMode.REPLACE) {
TextVisitFactory.visitForwards(this.text, style, (unused, s, codePoint) -> {
baseWidth.add(widthRetriever.getWidth(codePoint, s));
return true;
});
}

if (mode != RubyRenderMode.HIDDEN) {
TextVisitFactory.visitForwards(this.ruby, style, (unused, s, codePoint) -> {
rubyWidth.add(widthRetriever.getWidth(codePoint, s));
return true;
});
}

return switch (mode) {
case ABOVE, BELOW -> Math.max(
baseWidth.getValue() * RubyText.TEXT_SCALE,
rubyWidth.getValue() * RubyText.RUBY_SCALE
);
case ABOVE, BELOW ->
Math.max(baseWidth.getValue() * RubyText.TEXT_SCALE, rubyWidth.getValue() * RubyText.RUBY_SCALE);
case HIDDEN -> baseWidth.getValue();
case REPLACE -> rubyWidth.getValue();
};
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
RubyText rubyText = (RubyText) o;
return Objects.equals(text, rubyText.text) && Objects.equals(ruby, rubyText.ruby);
if (o == null || this.getClass() != o.getClass()) return false;
RubyText other = (RubyText) o;
return Objects.equals(this.text, other.text()) && Objects.equals(this.ruby, other.ruby());
}

@Override
public int hashCode() {
return Objects.hash(text, ruby);
}

public static float draw(
OrderedText rubiCharacter,
float x,
float y,
Matrix4f matrix,
TextHandler handler,
int fontHeight,
TextDrawer drawer
) {
float width = handler.getWidth(rubiCharacter);

final MutableObject<RubyText> rubyTextWrapper = new MutableObject<>(null);
final MutableObject<Style> styleWrapper = new MutableObject<>(null);
rubiCharacter.accept((index, style, codePoint) -> {
if (rubyTextWrapper.getValue() != null) throw new IllegalStateException("Expected only one element in OrderedText rubiCharacter");
if (codePoint != RubyText.RUBY_MARKER || IRubyStyle.getRuby(style) == null)
throw new IllegalArgumentException("Expected rubi character");
rubyTextWrapper.setValue(IRubyStyle.getRuby(style));
styleWrapper.setValue(style);
return true;
});

final var rubyText = rubyTextWrapper.getValue();
final var style = ((IRubyStyle) styleWrapper.getValue()).removeRuby();
switch (RubyRenderMode.getOption().getValue()) {
case ABOVE -> rubyText.drawAbove(style, x, y, width, matrix, handler, fontHeight, drawer);
case BELOW -> rubyText.drawBelow(style, x, y, width, matrix, handler, fontHeight, drawer);
case REPLACE -> rubyText.drawReplace(style, x, y, matrix, drawer);
case HIDDEN -> rubyText.drawHidden(style, x, y, matrix, drawer);
}

return width;
return Objects.hash(this.text, this.ruby);
}

private void drawRubyPair(
Style style,
float x,
float yText,
float yRuby,
Expand All @@ -128,8 +95,10 @@ private void drawRubyPair(
TextHandler handler,
Matrix4f matrix
) {
final var style = ((IRubyStyle) this.style).rubi$removeRuby();

drawer.drawSpacedApart(
OrderedText.styledForwardsVisitedString(this.text(), style),
OrderedText.styledForwardsVisitedString(this.text, style),
x,
yText,
RubyText.TEXT_SCALE,
Expand All @@ -139,7 +108,7 @@ private void drawRubyPair(
);

drawer.drawSpacedApart(
OrderedText.styledForwardsVisitedString(this.ruby(), style.withUnderline(false).withStrikethrough(false)),
OrderedText.styledForwardsVisitedString(this.ruby, style.withUnderline(false).withStrikethrough(false)),
x,
yRuby,
RubyText.RUBY_SCALE,
Expand All @@ -150,7 +119,6 @@ private void drawRubyPair(
}

public void drawAbove(
Style style,
float x,
float y,
float width,
Expand All @@ -165,11 +133,10 @@ public void drawAbove(
float yBody = y + (fontHeight - textHeight);
float yAbove = yBody - rubyHeight + fontHeight * RubyText.RUBY_OVERLAP;

this.drawRubyPair(style, x, yBody, yAbove, width, drawer, handler, matrix);
this.drawRubyPair(x, yBody, yAbove, width, drawer, handler, matrix);
}

public void drawBelow(
Style style,
float x,
float y,
float width,
Expand All @@ -181,26 +148,14 @@ public void drawBelow(
float textHeight = fontHeight * RubyText.TEXT_SCALE;
float yBelow = y + textHeight - fontHeight * RubyText.RUBY_OVERLAP;

this.drawRubyPair(style, x, y, yBelow, width, drawer, handler, matrix);
this.drawRubyPair(x, y, yBelow, width, drawer, handler, matrix);
}

public void drawReplace(
Style style,
float x,
float y,
Matrix4f matrix,
TextDrawer drawer
) {
drawer.draw(OrderedText.styledForwardsVisitedString(this.ruby(), style), x, y, matrix);
public void drawReplace(float x, float y, Matrix4f matrix, TextDrawer drawer) {
drawer.draw(OrderedText.styledForwardsVisitedString(this.ruby, this.style), x, y, matrix);
}

public void drawHidden(
Style style,
float x,
float y,
Matrix4f matrix,
TextDrawer drawer
) {
drawer.draw(OrderedText.styledForwardsVisitedString(this.text(), style), x, y, matrix);
public void drawHidden(float x, float y, Matrix4f matrix, TextDrawer drawer) {
drawer.draw(OrderedText.styledForwardsVisitedString(this.text, this.style), x, y, matrix);
}
}
Loading

0 comments on commit a70f6fc

Please sign in to comment.