fix(chat): font sizes match the CSS sizes Teams sends
- xx-small renders at 9px and x-large at 24px against the 14px body, as in Teams web; Small was barely smaller than Normal. - Other CSS keywords map to their absolute pixel size. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -7,8 +7,9 @@ pub const CODE_PADDING: &str = "\u{2004}";
7
7
const URL_PREFIXES: [&str; 3] = ["https://", "http://", "www."];
8
8
const TRAILING_PUNCTUATION: &str = ".,;:!?'\"";
9
9
const BASE_FONT_PIXELS: f32 = 14.;
10
const SMALL_SCALE: f32 = 0.75;
11
const LARGE_SCALE: f32 = 1.5;
10
// CSS absolute sizes Teams sends: xx-small = 9px, x-large = 24px, against a 14px body.
11
const SMALL_SCALE: f32 = 9. / 14.;
12
const LARGE_SCALE: f32 = 24. / 14.;
12
13
const SCRIPT_SCALE: f32 = 0.75;
13
14
const SUPERSCRIPT_RAISE: f32 = 0.35;
14
15
const SUBSCRIPT_DROP: f32 = 0.2;
@@ -895,9 +896,9 @@ mod tests {
895
896
assert_eq!(inline.text, "2bigsp");
896
897
assert_eq!(styles[0].script, Some(Script::Super));
897
898
assert_eq!(styles[0].font_scale(), 0.75);
898
assert_eq!(styles[1].font_scale(), 1.5);
899
assert_eq!(styles[1].font_scale(), 24. / 14.);
899
900
assert_eq!(styles[2].script, Some(Script::Sub));
900
assert_eq!(styles[2].font_scale(), 0.5625);
901
assert_eq!(styles[2].font_scale(), 9. / 14. * 0.75);
901
902
assert_eq!(styles[3].font_scale(), 1.5);
902
903
}
903
904
@@ -7,8 +7,9 @@ pub const CODE_PADDING: &str = "\u{2004}";7
const URL_PREFIXES: [&str; 3] = ["https://", "http://", "www."];7
const URL_PREFIXES: [&str; 3] = ["https://", "http://", "www."];8
const TRAILING_PUNCTUATION: &str = ".,;:!?'\"";8
const TRAILING_PUNCTUATION: &str = ".,;:!?'\"";9
const BASE_FONT_PIXELS: f32 = 14.;9
const BASE_FONT_PIXELS: f32 = 14.;10
const SMALL_SCALE: f32 = 0.75;10
// CSS absolute sizes Teams sends: xx-small = 9px, x-large = 24px, against a 14px body.11
const LARGE_SCALE: f32 = 1.5;11
const SMALL_SCALE: f32 = 9. / 14.;12
const LARGE_SCALE: f32 = 24. / 14.;12
const SCRIPT_SCALE: f32 = 0.75;13
const SCRIPT_SCALE: f32 = 0.75;13
const SUPERSCRIPT_RAISE: f32 = 0.35;14
const SUPERSCRIPT_RAISE: f32 = 0.35;14
const SUBSCRIPT_DROP: f32 = 0.2;15
const SUBSCRIPT_DROP: f32 = 0.2;@@ -895,9 +896,9 @@ mod tests {895
assert_eq!(inline.text, "2bigsp");896
assert_eq!(inline.text, "2bigsp");896
assert_eq!(styles[0].script, Some(Script::Super));897
assert_eq!(styles[0].script, Some(Script::Super));897
assert_eq!(styles[0].font_scale(), 0.75);898
assert_eq!(styles[0].font_scale(), 0.75);898
assert_eq!(styles[1].font_scale(), 1.5);899
assert_eq!(styles[1].font_scale(), 24. / 14.);899
assert_eq!(styles[2].script, Some(Script::Sub));900
assert_eq!(styles[2].script, Some(Script::Sub));900
assert_eq!(styles[2].font_scale(), 0.5625);901
assert_eq!(styles[2].font_scale(), 9. / 14. * 0.75);901
assert_eq!(styles[3].font_scale(), 1.5);902
assert_eq!(styles[3].font_scale(), 1.5);902
}903
}903
904
@@ -245,8 +245,12 @@ fn push_colored(element: ElementRef<'_>, spans: &mut Vec<Span>) {
245
245
fn parse_font_size(value: &str) -> Option<FontSize> {
246
246
let value = value.trim().to_ascii_lowercase();
247
247
match value.as_str() {
248
"xx-small" | "x-small" | "small" | "smaller" => Some(FontSize::Small),
249
"large" | "x-large" | "xx-large" | "larger" => Some(FontSize::Large),
248
"xx-small" => Some(FontSize::Small),
249
"x-large" => Some(FontSize::Large),
250
"x-small" => Some(FontSize::Pixels(10)),
251
"small" | "smaller" => Some(FontSize::Pixels(13)),
252
"large" | "larger" => Some(FontSize::Pixels(18)),
253
"xx-large" => Some(FontSize::Pixels(32)),
250
254
"0" => Some(FontSize::Hidden),
251
255
_ => {
252
256
let pixels: f32 = value.strip_suffix("px")?.trim().parse().ok()?;
@@ -245,8 +245,12 @@ fn push_colored(element: ElementRef<'_>, spans: &mut Vec<Span>) {245
fn parse_font_size(value: &str) -> Option<FontSize> {245
fn parse_font_size(value: &str) -> Option<FontSize> {246
let value = value.trim().to_ascii_lowercase();246
let value = value.trim().to_ascii_lowercase();247
match value.as_str() {247
match value.as_str() {248
"xx-small" | "x-small" | "small" | "smaller" => Some(FontSize::Small),248
"xx-small" => Some(FontSize::Small),249
"large" | "x-large" | "xx-large" | "larger" => Some(FontSize::Large),249
"x-large" => Some(FontSize::Large),250
"x-small" => Some(FontSize::Pixels(10)),251
"small" | "smaller" => Some(FontSize::Pixels(13)),252
"large" | "larger" => Some(FontSize::Pixels(18)),253
"xx-large" => Some(FontSize::Pixels(32)),250
"0" => Some(FontSize::Hidden),254
"0" => Some(FontSize::Hidden),251
_ => {255
_ => {252
let pixels: f32 = value.strip_suffix("px")?.trim().parse().ok()?;256
let pixels: f32 = value.strip_suffix("px")?.trim().parse().ok()?;@@ -425,16 +425,23 @@ fn superscript_and_subscript() {
425
425
fn font_sizes_map_to_small_large_and_pixels() {
426
426
let sized =
427
427
|value: &str| html_to_spans(&format!("<span style=\"font-size:{value};\">t</span>"));
428
for value in ["xx-small", "x-small", "small"] {
429
assert_eq!(
430
sized(value),
431
vec![Span::Sized(FontSize::Small, vec![text("t")])]
432
);
433
}
434
for value in ["large", "x-large", "xx-large"] {
428
assert_eq!(
429
sized("xx-small"),
430
vec![Span::Sized(FontSize::Small, vec![text("t")])]
431
);
432
assert_eq!(
433
sized("x-large"),
434
vec![Span::Sized(FontSize::Large, vec![text("t")])]
435
);
436
for (value, pixels) in [
437
("x-small", 10),
438
("small", 13),
439
("large", 18),
440
("xx-large", 32),
441
] {
435
442
assert_eq!(
436
443
sized(value),
437
vec![Span::Sized(FontSize::Large, vec![text("t")])]
444
vec![Span::Sized(FontSize::Pixels(pixels), vec![text("t")])]
438
445
);
439
446
}
440
447
assert_eq!(
@@ -425,16 +425,23 @@ fn superscript_and_subscript() {425
fn font_sizes_map_to_small_large_and_pixels() {425
fn font_sizes_map_to_small_large_and_pixels() {426
let sized =426
let sized =427
|value: &str| html_to_spans(&format!("<span style=\"font-size:{value};\">t</span>"));427
|value: &str| html_to_spans(&format!("<span style=\"font-size:{value};\">t</span>"));428
for value in ["xx-small", "x-small", "small"] {428
assert_eq!(429
assert_eq!(429
sized("xx-small"),430
sized(value),430
vec![Span::Sized(FontSize::Small, vec![text("t")])]431
vec![Span::Sized(FontSize::Small, vec![text("t")])]431
);432
);432
assert_eq!(433
}433
sized("x-large"),434
for value in ["large", "x-large", "xx-large"] {434
vec![Span::Sized(FontSize::Large, vec![text("t")])]435
);436
for (value, pixels) in [437
("x-small", 10),438
("small", 13),439
("large", 18),440
("xx-large", 32),441
] {435
assert_eq!(442
assert_eq!(436
sized(value),443
sized(value),437
vec![Span::Sized(FontSize::Large, vec![text("t")])]444
vec![Span::Sized(FontSize::Pixels(pixels), vec![text("t")])]438
);445
);439
}446
}440
assert_eq!(447
assert_eq!(