Merge pull request #1879 from socksprox/fix/legacy-colour-codes
🤖 Agent: thank you! Merged after the contribution gate (checked against vanilla 26.3; one integration branch of main + its batch: clippy -D warnings, tests, the affected parity harnesses (111/112, all pass), in game on the server thread and in-frame, lan_e2e in both host modes) and this head's verify.
This commit is contained in:
@@ -891,8 +891,11 @@ impl Font {
|
||||
}
|
||||
s.chars().map(|c| self.glyph(c).advance).sum::<f32>().ceil()
|
||||
}
|
||||
/// Font.width of a component: `§` codes in the spans are formatting,
|
||||
/// not glyphs.
|
||||
pub fn width_text(&self, t: &Text) -> f32 {
|
||||
t.chars()
|
||||
t.resolved()
|
||||
.chars()
|
||||
.map(|(c, s)| self.advance(c, s))
|
||||
.sum::<f32>()
|
||||
.ceil()
|
||||
|
||||
@@ -151,10 +151,13 @@ impl Game<'_> {
|
||||
.clone()
|
||||
.unwrap_or_else(|| Text::literal(&name))
|
||||
};
|
||||
// StringDecomposer.iterateFormatted: `§` codes a server leaves
|
||||
// inside the team prefix or the name itself apply at draw time.
|
||||
let shown = match team {
|
||||
Some(t) => t.formatted_name(member),
|
||||
None => member,
|
||||
};
|
||||
}
|
||||
.resolve_legacy();
|
||||
let mut lines = Vec::new();
|
||||
// EntityRenderer.extractNameTags: the score shows within the
|
||||
// below-name distance (10), under the name.
|
||||
@@ -162,7 +165,7 @@ impl Game<'_> {
|
||||
if distance_sqr < 10.0 * 10.0
|
||||
&& let Some(score) = holder.and_then(|h| self.below_name_text(&h))
|
||||
{
|
||||
lines.push((score, 0.0));
|
||||
lines.push((score.resolve_legacy(), 0.0));
|
||||
lift = f64::from(9.0f32 * 1.15 * 0.025);
|
||||
}
|
||||
lines.push((shown, lift));
|
||||
@@ -213,14 +216,15 @@ impl Game<'_> {
|
||||
let shown = match team {
|
||||
Some(t) => t.formatted_name(name.clone()),
|
||||
None => name.clone(),
|
||||
};
|
||||
}
|
||||
.resolve_legacy();
|
||||
let mut lines = Vec::new();
|
||||
let mut lift = 0.0;
|
||||
let holder = self.persist.uuids.get(&a.id).map(|u| uuid_text(*u));
|
||||
if distance_sqr < 10.0 * 10.0
|
||||
&& let Some(score) = holder.and_then(|h| self.below_name_text(&h))
|
||||
{
|
||||
lines.push((score, 0.0));
|
||||
lines.push((score.resolve_legacy(), 0.0));
|
||||
lift = f64::from(9.0f32 * 1.15 * 0.025);
|
||||
}
|
||||
lines.push((shown, lift));
|
||||
|
||||
@@ -903,6 +903,9 @@ impl HudBatch {
|
||||
color: u32,
|
||||
shadow: bool,
|
||||
) -> f32 {
|
||||
// StringDecomposer.iterateFormatted inside Font.drawInternal:
|
||||
// `§` codes inside a span restyle what follows and are not drawn.
|
||||
let text = text.resolved();
|
||||
let font = &atlas.font;
|
||||
let alpha = (color >> 24) as u8;
|
||||
let mut glyphs = Vec::new();
|
||||
|
||||
@@ -428,6 +428,15 @@ impl Text {
|
||||
}
|
||||
out
|
||||
}
|
||||
/// `resolve_legacy` borrowing `self` when no span holds a `§` code (the
|
||||
/// common case).
|
||||
pub fn resolved(&self) -> std::borrow::Cow<'_, Text> {
|
||||
if self.spans.iter().any(|s| s.text.contains('§')) {
|
||||
std::borrow::Cow::Owned(self.resolve_legacy())
|
||||
} else {
|
||||
std::borrow::Cow::Borrowed(self)
|
||||
}
|
||||
}
|
||||
/// The text without its legacy `§` codes (ChatFormatting.stripFormatting:
|
||||
/// a `§` and a code character, `0`-`9`, `a`-`f`, `k`-`o` or `r`).
|
||||
pub fn strip_legacy(&self) -> Text {
|
||||
@@ -695,4 +704,20 @@ mod tests {
|
||||
assert_eq!(t.spans[1].style.color, Some(GOLD));
|
||||
assert_eq!(t.slice(1, 2).plain(), "y");
|
||||
}
|
||||
#[test]
|
||||
fn resolved_borrows_clean_text_and_restyles_codes() {
|
||||
let clean = Text::colored("Steve", GOLD);
|
||||
assert!(matches!(clean.resolved(), std::borrow::Cow::Borrowed(_)));
|
||||
// A colour code inside a span clears its decorations (a bold
|
||||
// "§cSteve" draws "Steve" red, not bold).
|
||||
let styled = Style {
|
||||
bold: Some(true),
|
||||
..Style::default()
|
||||
};
|
||||
let styled = Text::styled("§cSteve", styled);
|
||||
let resolved = styled.resolved();
|
||||
assert_eq!(resolved.plain(), "Steve");
|
||||
assert_eq!(resolved.spans[0].style.color, Some(RED));
|
||||
assert_eq!(resolved.spans[0].style.bold, Some(false));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,9 +279,17 @@ pub fn centered_text(
|
||||
w: f32,
|
||||
color: u32,
|
||||
) {
|
||||
let width = atlas.font.width_text(text);
|
||||
let text = text.resolved();
|
||||
let width = atlas.font.width_text(&text);
|
||||
if width <= w {
|
||||
batch.text_styled(atlas, text, x + ((w - width) / 2.0).floor(), y, color, true);
|
||||
batch.text_styled(
|
||||
atlas,
|
||||
&text,
|
||||
x + ((w - width) / 2.0).floor(),
|
||||
y,
|
||||
color,
|
||||
true,
|
||||
);
|
||||
} else {
|
||||
// Clip by characters (vanilla scrolls; we show the part that fits).
|
||||
let mut shown = Text::empty();
|
||||
|
||||
Reference in New Issue
Block a user