Skip to content

Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.

A text field draws its value as typed, § codes included

MSedgeMCd02c07cde6dc
diff --git a/crates/horizon-client-render/src/font.rs b/crates/horizon-client-render/src/font.rs
index 01ede34bb..916945660 100644
--- a/crates/horizon-client-render/src/font.rs
+++ b/crates/horizon-client-render/src/font.rs
@@ -947,6 +947,16 @@ impl Font {
.sum::<f32>()
.ceil()
}
+ /// `Font.width(FormattedCharSequence)` of a sequence a component built
+ /// with `FormattedCharSequence.forward`: every character is a glyph, a
+ /// `§` included (`StringDecomposer.iterate`, not `iterateFormatted`).
+ pub fn width_drawn(&self, t: &Text) -> f32 {
+ crate::bidi::visual(t)
+ .chars()
+ .map(|(c, s)| self.advance(c, s))
+ .sum::<f32>()
+ .ceil()
+ }
/// `StringSplitter.splitLines`: each line and whether it continues a
/// wrapped one. A line breaks at a `\n`, or once a character overflows
/// `max_width` (never the first visible one) at the last space so far, or
diff --git a/crates/horizon-client-render/src/screen_draw.rs b/crates/horizon-client-render/src/screen_draw.rs
index 697b0e6a7..f2acb0e08 100644
--- a/crates/horizon-client-render/src/screen_draw.rs
+++ b/crates/horizon-client-render/src/screen_draw.rs
@@ -133,6 +133,21 @@ pub fn text(out: &mut Vec<Draw>, text: Text, x: i32, y: i32, color: i32, shadow:
});
}
}
+/// `graphics.text` on text the caller already styled, drawn as it is: a `§`
+/// in it stays a character, as `FormattedCharSequence.forward`
+/// (`StringDecomposer.iterate`, not `iterateFormatted`) draws it. An edit box
+/// builds its own sequence that way, so a `§` in its value shows.
+pub fn text_as_typed(out: &mut Vec<Draw>, text: Text, x: i32, y: i32, color: i32, shadow: bool) {
+ if (color as u32) >> 24 != 0 {
+ out.push(Draw::Text {
+ text,
+ x,
+ y,
+ color,
+ shadow,
+ });
+ }
+}
/// `StringDecomposer.iterateFormatted`: the drawn characters of `contents`,
/// with their index in it and their style; legacy `§` codes restyle what
@@ -186,6 +201,11 @@ impl Measure<'_> {
pub fn width(&self, t: &Text) -> i32 {
self.0.width_text(&t.resolve_legacy()) as i32
}
+ /// `Font.width(FormattedCharSequence)` of a sequence drawn as it is: a
+ /// `§` in the text counts as the glyph it is drawn as.
+ pub fn width_drawn(&self, t: &Text) -> i32 {
+ self.0.width_drawn(t) as i32
+ }
pub fn width_str(&self, s: &str) -> i32 {
self.0.width(s) as i32
}
@@ -864,8 +884,8 @@ impl TextField {
displayed.clone()
};
let t = styled(half, self.display);
- let w = m.width(&t);
- text(out, t, draw_x, ty, color, self.text_shadow);
+ let w = m.width_drawn(&t);
+ text_as_typed(out, t, draw_x, ty, color, self.text_shadow);
draw_x += w + 1;
}
let insert = self.cursor < len || len >= self.max_length;
@@ -878,7 +898,7 @@ impl TextField {
}
if !displayed.is_empty() && on_screen && (rel_cursor as usize) < shown_len {
let t = styled(sub(&displayed, rel_cursor as usize, shown_len), self.cursor);
- text(out, t, draw_x, ty, color, self.text_shadow);
+ text_as_typed(out, t, draw_x, ty, color, self.text_shadow);
}
if let Some(hint) = &self.hint
&& displayed.is_empty()
diff --git a/tools/test_chat_screen_parity.py b/tools/test_chat_screen_parity.py
index fead1b5cb..57d044788 100644
--- a/tools/test_chat_screen_parity.py
+++ b/tools/test_chat_screen_parity.py
@@ -79,6 +79,41 @@ def cases(count):
'chatOnly': True, 'opacity': 0.5, 'mouse': [0, 0], 'blink': 0,
'actions': [{'type': 'type', 'text': text}]
+ [{'type': 'key', 'key': key} for key in keys]})
+ # The box holds 256 characters: a longer paste is cut there, in the middle
+ # of a selection the room left by the selection is kept, and once full no
+ # key adds anything.
+ for text, keys in [('a' * 300, ['end', 'backspace']),
+ ('b' * 255 + 'cdefghij', ['left', 'left', 'ctrl+a', 'delete', 'down']),
+ ('c' * 256, ['home', 'delete', 'right', 'end', 'backspace']),
+ ('/' + 'd' * 300, ['end', 'ctrl+backspace', 'tab'])]:
+ out.append({'width': 320, 'height': 240, 'initial': '', 'history': [], 'autoSuggest': True,
+ 'chatOnly': True, 'opacity': 0.5, 'mouse': [0, 0], 'blink': 0,
+ 'actions': [{'type': 'type', 'text': text}]
+ + [{'type': 'key', 'key': key} for key in keys]})
+ # Characters the box never takes (the section sign, DEL, control codes)
+ # beside ones it does, and a value that already holds a `§`: the box draws
+ # its value as the literal component vanilla builds from it, so the code
+ # is a character on the screen, not a format.
+ for text in ['ab\u00a7cd\u007f' + 'e\u0001f\u001fg\u00c9h', '\u00a7', 'x' * 250 + '\u00a7y' * 20]:
+ out.append({'width': 427, 'height': 240, 'initial': '\u00a7z', 'history': [], 'autoSuggest': False,
+ 'chatOnly': False, 'opacity': 1.0, 'mouse': [10, 30], 'blink': 400,
+ 'actions': [{'type': 'type', 'text': text}, {'type': 'key', 'key': 'left'}]})
+ # A command typed with a colour code in it, as a command block's editor
+ # opens with: the code is drawn, the text after it keeps its own colour.
+ for initial in ['/say \u00a7chello', '/tellraw @a {"text":"\u00a7lbold"}', '\u00a7']:
+ out.append({'width': 640, 'height': 360, 'initial': initial, 'history': [], 'autoSuggest': True,
+ 'chatOnly': True, 'opacity': 0.5, 'mouse': [0, 0], 'blink': 0,
+ 'actions': [{'type': 'key', 'key': 'left'}, {'type': 'key', 'key': 'left'},
+ {'type': 'key', 'key': 'end'}]})
+ # A history entry longer than the box is recalled cut to its length, and
+ # the draft typed before going back through the history is not lost when
+ # the history is walked forward again.
+ out.append({'width': 640, 'height': 360, 'initial': '', 'history': ['h' * 300, '/time set day'],
+ 'autoSuggest': True, 'chatOnly': True, 'opacity': 0.5, 'blink': 0,
+ 'mouse': [0, 0],
+ 'actions': [{'type': 'type', 'text': 'draft'}, {'type': 'key', 'key': 'up'},
+ {'type': 'key', 'key': 'up'}, {'type': 'key', 'key': 'home'},
+ {'type': 'key', 'key': 'down'}, {'type': 'key', 'key': 'down'}]})
return out