Add inline framed fbox

This commit is contained in:
gloamxun committed 2026-10-19 13:03:05 +08:00
1 parent 32b0064e5f
commit 2552008d4b
11 files changed
+273 -28

No files matched your search

+6 -6
View File
@@ -205,8 +205,9 @@ is measured as a single unbreakable unit, spaces inside become fixed-width kerns
(no stretching, no line break, and hyphenation is suppressed inside the box),
and nested boxes and inline rules are preserved. Boxes that do not fit on the
current line overflow like an overfull TeX hbox rather than wrapping.
`\makebox[width][l|c|r]{...}` fixes the box width and aligns its content, and
`\raisebox{lift}{...}` lifts inline content (or a nested box) by a dimension.
`\makebox[width][l|c|r]{...}` fixes the box width and aligns its content,
`\raisebox{lift}{...}` lifts inline content by a dimension, and `\fbox{...}`
draws a framed inline box with 3pt padding and a 0.4pt rule.
`\checkmark`, `\textdagger`, `\textdaggerdbl`, `\textperiodcentered`,
`\textsection`, `\textparagraph`, `\textbrokenbar`, and `\textvisiblespace`
@@ -292,9 +293,8 @@ Known gaps for full document classes: base font-size class options
(`10pt`/`11pt`/`12pt`) are parsed but the engine still uses 11pt; footnotes and
`\thanks` are dropped,
custom `\maketitle` implementations are ignored (the engine title layout is
used), `\minipage` is not implemented yet, inline `\fbox` still starts a block
instead of an inline frame, `\parbox` is block-level rather than inline, and the
abstract prints as body text without an "Abstract" heading.
used), `\minipage` is not implemented yet, `\parbox` is block-level rather than
inline, and the abstract prints as body text without an "Abstract" heading.
Basic `\input{file}` imports are read relative to the main `.tex` file. Imported
files participate in source mapping and frontend cache invalidation. Local style
@@ -454,7 +454,7 @@ advisory breaks, starred `\vspace`/`\hspace`, `center`, `\hrule`, `\fbox`,
| Area | Implemented | Remaining |
| --- | --- | --- |
| Boxes | `\rule`, block `\fbox`/`\parbox`, inline `\mbox`/`\makebox` (fixed width and alignment), `\raisebox`, `center`, `\hrule` | inline framed `\fbox`, `\minipage`, VBox |
| Boxes | `\rule`, inline framed `\fbox`, block `\parbox`, inline `\mbox`/`\makebox` (fixed width and alignment), `\raisebox`, `center`, `\hrule` | `\minipage`, VBox |
| Fonts | serif/sans/mono faces, bold/italic/mono/sans declarations, group scoping, true scaled small caps | — |
| Paragraphs | Knuth-Plass breaking, TeX hyphenation, justification, centering, `\parindent` (17pt default), `\hfill`/`\hfil` | `\raggedright`/`\raggedleft` |
| Notes/floats | — | footnote insertion area, figure/table floats |
+2
View File
@@ -206,6 +206,8 @@ pub struct InlineBox {
pub align: InlineBoxAlign,
/// Vertical lift from `\raisebox{lift}{...}`.
pub raise: common::Scaled,
/// Draw a frame around the box (`\fbox`).
pub frame: bool,
}
/// A filled rectangle produced by `\rule`.
@@ -380,6 +380,7 @@ struct InlineBoxSpec {
width: Option<common::Scaled>,
align: InlineBoxAlign,
raise: common::Scaled,
frame: bool,
}
struct Parser {
@@ -693,12 +694,14 @@ impl Parser {
width,
align,
raise,
frame,
} => {
self.inline_boxes.push(InlineBoxSpec {
runs: Vec::new(),
width: *width,
align: *align,
raise: *raise,
frame: *frame,
});
}
expanded::FrontendEventKind::InlineBoxEnd => {
@@ -716,6 +719,7 @@ impl Parser {
width: spec.width,
align: spec.align,
raise: spec.raise,
frame: spec.frame,
}),
unresolved_reference: None,
bold: false,
+1
View File
@@ -102,6 +102,7 @@ pub enum FrontendEventKind {
width: Option<common::Scaled>,
align: crate::document::InlineBoxAlign,
raise: common::Scaled,
frame: bool,
},
InlineBoxEnd,
MetadataStart(MetadataField),
+4 -8
View File
@@ -6,15 +6,11 @@ are still missing. Each item names the files/functions to touch.
Completed: paragraph indentation (`\parindent`) and `\hfill`/`\hfil`; sans
family and true scaled small caps (see `docs/STATUS.md`).
## 1. Inline framed `\fbox`
## 1. `\minipage` and VBox
- Add `y_offset: Mm` to `PositionedSpan` and `PositionedRule`; PDF/SVG painters
add it to the baseline.
- Represent inline boxes as a nested `LineItem::Box { items, width, ascent,
depth, inset, raise, frame }`; the line breaker measures the content once and
advances by its width.
- Paint child items at `x + inset`, baseline `- raise`; draw frame rules at the
box rectangle when `frame` is set.
- Reuse the block `FramedBox` measurement with an explicit width and vertical
stacking; add `\begin{minipage}{width}` environment parsing.
- Add VBox material for `\vbox`/`\vtop` and nested vertical lists.
## 2. Footnotes
+5 -6
View File
@@ -12,9 +12,9 @@ the source changes; the SVG preview is refreshed per page.
- Headings: numbered `\section`/`\subsection`/`\subsubsection` and starred
`\section*` variants (starred headings do not advance the counter).
- Box material: `\rule` (raise/width/height), block `\fbox`, `\parbox`,
inline `\mbox`, `\makebox[width][align]`, `\raisebox`, `center`, `\hrule`
with `width`/`height`.
- Box material: `\rule` (raise/width/height), inline framed `\fbox`, block
`\parbox`, inline `\mbox`, `\makebox[width][align]`, `\raisebox`, `center`,
`\hrule` with `width`/`height`.
- Fonts: Liberation Serif/Sans/Mono faces, bold/italic/monospace/sans
declarations with group scoping, grouped text-style commands, true small caps
(lowercase uppercase glyphs at 80% size), and sans/serif/mono small caps.
@@ -37,8 +37,7 @@ See `docs/ROADMAP.md` for concrete implementation plans.
## Known approximations / gaps
- Boxes: `\fbox`/`\parbox` are block-level (an inline `\fbox` starts a block);
`\minipage` and VBox material are missing.
- Boxes: `\parbox` is block-level; `\minipage` and VBox material are missing.
- Pages: no footnote insertion area, no floats, no headers/footers (fancyhdr
commands are accepted as no-ops), planner and renderer are separate.
- Lengths: `\textwidth`/`\textheight`/`\linewidth`/`\columnwidth` are fixed
@@ -51,7 +50,7 @@ See `docs/ROADMAP.md` for concrete implementation plans.
- `examples/hello.tex`, `examples/boxes.tex`, `examples/compat.tex` compile to
one A4 page each; the arXiv template project compiles to 3 Letter pages.
- `cargo fmt --check` passes; `cargo test --workspace` (19 tests) covers the three
- `cargo fmt --check` passes; `cargo test --workspace` (20 tests) covers the three
examples, the arXiv fixture, SVG/PDF output, project API, static frontend
assets, path traversal, the legacy `/texrra` endpoint, and page-hash cache
stability.
+96 -6
View File
@@ -1002,6 +1002,24 @@ fn box_font_id_variant(
}
}
fn flat_runs_vertical(runs: &[Run], size: u8) -> (common::Scaled, common::Scaled) {
let em = i32::from(size) * 65_536;
let mut ascent = common::Scaled(em * 3 / 4);
let mut depth = common::Scaled(em / 4);
for run in runs {
if let Some(rule) = &run.rule {
let above = rule.raise.0.saturating_add(rule.height.0);
if above > ascent.0 {
ascent = common::Scaled(above);
}
if rule.raise.0 < 0 && -rule.raise.0 > depth.0 {
depth = common::Scaled(-rule.raise.0);
}
}
}
(ascent, depth)
}
fn flat_runs_width(
runs: &[Run],
font_repo: &box_font::Repo<BoxFont>,
@@ -1084,6 +1102,22 @@ fn paragraph_font_id(
}
#[allow(clippy::too_many_arguments)]
#[derive(Clone, Copy)]
struct InlineFrameMetrics {
width: common::Scaled,
ascent: common::Scaled,
depth: common::Scaled,
raise: common::Scaled,
padding: common::Scaled,
}
#[derive(Clone, Copy)]
enum InlineMarker {
RaiseStart(common::Scaled),
RaiseEnd,
Frame(InlineFrameMetrics),
}
#[allow(clippy::too_many_arguments)]
fn flatten_inline_boxes(
runs: &[Run],
@@ -1096,7 +1130,7 @@ fn flatten_inline_boxes(
small_sans_font_ids: Option<&FontFamily<box_font::Id>>,
cjk_font_id: Option<box_font::Id>,
size: u8,
markers: &mut HashMap<char, Option<common::Scaled>>,
markers: &mut HashMap<char, InlineMarker>,
next_marker: &mut u32,
inside: bool,
) -> Vec<Run> {
@@ -1129,6 +1163,27 @@ fn flatten_inline_boxes(
small_sans_font_ids,
cjk_font_id,
);
let padding = if inline.frame {
common::Scaled((3.4_f64 * 65_536.0).round() as i32)
} else {
common::Scaled::ZERO
};
let (frame_width, frame_ascent, frame_depth) = if inline.frame {
let (ascent, depth) = flat_runs_vertical(&content, size);
(
common::Scaled(
inline
.width
.unwrap_or(content_width)
.0
.saturating_add(padding.0.saturating_mul(2)),
),
ascent,
depth,
)
} else {
(content_width, common::Scaled::ZERO, common::Scaled::ZERO)
};
let (left_pad, right_pad) = match inline.width {
Some(width) if width > content_width => {
let extra = width.0 - content_width.0;
@@ -1141,15 +1196,36 @@ fn flatten_inline_boxes(
}
_ => (common::Scaled::ZERO, common::Scaled::ZERO),
};
if inline.frame {
let frame_char = char::from_u32(*next_marker)
.expect("inline box markers stay inside a Unicode private use plane");
*next_marker += 1;
markers.insert(
frame_char,
InlineMarker::Frame(InlineFrameMetrics {
width: frame_width,
ascent: frame_ascent,
depth: frame_depth,
raise: inline.raise,
padding,
}),
);
flattened.push(Run::plain(frame_char.to_string()));
}
let start_char = char::from_u32(*next_marker)
.expect("inline box markers stay inside a Unicode private use plane");
*next_marker += 1;
let end_char = char::from_u32(*next_marker)
.expect("inline box markers stay inside a Unicode private use plane");
*next_marker += 1;
markers.insert(start_char, Some(inline.raise));
markers.insert(end_char, None);
markers.insert(start_char, InlineMarker::RaiseStart(inline.raise));
markers.insert(end_char, InlineMarker::RaiseEnd);
flattened.push(Run::plain(start_char.to_string()));
if padding.0 > 0 {
let mut pad = Run::plain(String::new());
pad.kern = Some(padding);
flattened.push(pad);
}
if left_pad.0 > 0 {
let mut pad = Run::plain(String::new());
pad.kern = Some(left_pad);
@@ -1161,6 +1237,11 @@ fn flatten_inline_boxes(
pad.kern = Some(right_pad);
flattened.push(pad);
}
if padding.0 > 0 {
let mut pad = Run::plain(String::new());
pad.kern = Some(padding);
flattened.push(pad);
}
flattened.push(Run::plain(end_char.to_string()));
continue;
}
@@ -1289,7 +1370,7 @@ fn boxworks_line_break(
if let Some(sans) = &small_sans_font_ids {
register_style_ids(&mut style_by_font, sans, true, false, small_size);
}
let mut inline_markers: HashMap<char, Option<common::Scaled>> = HashMap::new();
let mut inline_markers: HashMap<char, InlineMarker> = HashMap::new();
let mut next_inline_marker = 0xF2000_u32;
let flattened = flatten_inline_boxes(
runs,
@@ -1562,13 +1643,22 @@ fn boxworks_line_break(
ds::Horizontal::Char(character) => {
if let Some(marker) = inline_markers.get(&character.char) {
match marker {
Some(raise) => {
InlineMarker::RaiseStart(raise) => {
let current = raise_stack.last().copied().unwrap_or_default();
raise_stack.push(common::Scaled(current.0.saturating_add(raise.0)));
}
None => {
InlineMarker::RaiseEnd => {
raise_stack.pop();
}
InlineMarker::Frame(frame) => {
line_items.push(text_layout::LineItem::Frame {
width: frame.width,
ascent: frame.ascent,
depth: frame.depth,
raise: frame.raise,
padding: frame.padding,
});
}
}
continue;
}
+20
View File
@@ -2698,6 +2698,26 @@ pub(super) fn text_line_metrics_scaled(
let mut ascent_mm = Mm::from(0.0);
let mut descent_mm = Mm::from(0.0);
for item in &line.items {
if let text_layout::LineItem::Frame {
ascent,
depth,
raise,
padding,
..
} = item
{
ascent_mm = ascent_mm.max(
Mm::from(scaled_units_to_mm(i64::from(raise.0)))
+ Mm::from(scaled_units_to_mm(i64::from(ascent.0)))
+ Mm::from(scaled_units_to_mm(i64::from(padding.0))),
);
descent_mm = descent_mm.max(
Mm::from(scaled_units_to_mm(i64::from(depth.0)))
+ Mm::from(scaled_units_to_mm(i64::from(padding.0)))
- Mm::from(scaled_units_to_mm(i64::from(raise.0))),
);
continue;
}
if let text_layout::LineItem::Rule {
raise,
height,
+13 -2
View File
@@ -2973,6 +2973,7 @@ fn makebox_primitive(
width,
align,
raise: common::Scaled::ZERO,
frame: false,
},
);
let trace_key = token.trace_key();
@@ -3004,6 +3005,7 @@ fn raisebox_primitive(
width: None,
align: texrra_core::document::InlineBoxAlign::Left,
raise: lift,
frame: false,
},
);
let trace_key = token.trace_key();
@@ -3204,12 +3206,21 @@ fn fbox_primitive(
) -> texlang::prelude::Result<()> {
let content = read_unexpanded_argument(input, token, "expects box content")?;
let inner = strip_braced_tokens(&content).to_vec();
write_event(input, token, FrontendEventKind::FramedBoxStart);
write_event(
input,
token,
FrontendEventKind::InlineBoxStart {
width: None,
align: texrra_core::document::InlineBoxAlign::Left,
raise: common::Scaled::ZERO,
frame: true,
},
);
let trace_key = token.trace_key();
let mut expansion = inner;
expansion.push(control_sequence_token(
input,
"texrraframedboxend",
"texrrainlineboxend",
trace_key,
));
input
+100
View File
@@ -33,6 +33,13 @@ pub(super) enum LineItem {
height: Scaled,
y_offset: Scaled,
},
Frame {
width: Scaled,
ascent: Scaled,
depth: Scaled,
raise: Scaled,
padding: Scaled,
},
Kern(Scaled),
Glue(Scaled),
}
@@ -53,11 +60,21 @@ pub(super) struct PositionedSpan {
pub(super) struct TextLineLayout {
pub(super) spans: Vec<PositionedSpan>,
pub(super) rules: Vec<PositionedRule>,
pub(super) frames: Vec<PositionedFrame>,
pub(super) width: Mm,
pub(super) ascent: Mm,
pub(super) descent: Mm,
}
pub(super) struct PositionedFrame {
pub(super) x: Mm,
pub(super) width: Mm,
pub(super) ascent: Mm,
pub(super) depth: Mm,
pub(super) raise: Mm,
pub(super) padding: Mm,
}
pub(super) fn svg_font_size(font_size: u8) -> f64 {
f64::from(font_size) * 25.4 / PDF_POINTS_PER_INCH
}
@@ -77,6 +94,13 @@ enum TextLineItem {
height: Mm,
y_offset: Mm,
},
Frame {
width: Mm,
ascent: Mm,
depth: Mm,
raise: Mm,
padding: Mm,
},
Kern(Mm),
Glue(Mm),
}
@@ -161,6 +185,30 @@ impl TextLine {
}
text.push(*character);
}
LineItem::Frame {
width,
ascent,
depth,
raise,
padding,
} => {
flush_text(
&mut items,
&mut text,
&mut text_style,
cjk_family,
monospace_family,
sans_family,
font_size,
);
items.push(TextLineItem::Frame {
width: scaled_to_mm(*width),
ascent: scaled_to_mm(*ascent),
depth: scaled_to_mm(*depth),
raise: scaled_to_mm(*raise),
padding: scaled_to_mm(*padding),
});
}
LineItem::Rule {
width,
raise,
@@ -239,6 +287,7 @@ impl TextLine {
pub(super) fn layout_spans(&self, fonts: &fonts::FontCache) -> TextLineLayout {
let mut spans = Vec::with_capacity(self.items.len());
let mut rules = Vec::new();
let mut frames = Vec::new();
let mut x = Mm::from(0.0);
let mut ascent = Mm::from(0.0);
let mut descent = Mm::from(0.0);
@@ -250,6 +299,24 @@ impl TextLine {
.str_width(fonts, " ");
for item in &self.items {
match item {
TextLineItem::Frame {
width,
ascent: frame_ascent,
depth: frame_depth,
raise,
padding,
} => {
frames.push(PositionedFrame {
x,
width: *width,
ascent: *frame_ascent,
depth: *frame_depth,
raise: *raise,
padding: *padding,
});
ascent = ascent.max(*raise + *frame_ascent + *padding);
descent = descent.max(*frame_depth + *padding - *raise);
}
TextLineItem::Rule {
width,
raise,
@@ -296,6 +363,7 @@ impl TextLine {
TextLineLayout {
spans,
rules,
frames,
width: x,
ascent,
descent,
@@ -320,6 +388,16 @@ impl TextLine {
Alignment::Right => (area_width - layout.width).max(Mm::from(0.0)),
};
let baseline = y + layout.ascent;
for frame in &layout.frames {
let _ = write!(
output,
"<rect x=\"{:.4}\" y=\"{:.4}\" width=\"{:.4}\" height=\"{:.4}\" fill=\"none\" stroke=\"#000000\" stroke-width=\"0.1411\"/>",
f64::from(x + horizontal_offset + frame.x),
f64::from(baseline - frame.raise - frame.ascent - frame.padding),
f64::from(frame.width),
f64::from(frame.ascent + frame.depth + frame.padding + frame.padding),
);
}
for rule in &layout.rules {
let _ = write!(
output,
@@ -448,6 +526,28 @@ impl Element for TextLine {
});
}
for frame in &layout.frames {
let left = frame.x + horizontal_offset;
let top = layout.ascent - frame.raise - frame.ascent - frame.padding;
let right = left + frame.width;
let bottom = top + frame.ascent + frame.depth + frame.padding + frame.padding;
area.draw_line(
vec![Position::new(left, top), Position::new(right, top)],
Style::new(),
);
area.draw_line(
vec![Position::new(left, bottom), Position::new(right, bottom)],
Style::new(),
);
area.draw_line(
vec![Position::new(left, top), Position::new(left, bottom)],
Style::new(),
);
area.draw_line(
vec![Position::new(right, top), Position::new(right, bottom)],
Style::new(),
);
}
if !layout.rules.is_empty() {
let mut black = image::RgbImage::new(1, 1);
black.put_pixel(0, 0, image::Rgb([0_u8, 0, 0]));
+22
View File
@@ -404,3 +404,25 @@ X\makebox[20mm][c]{mid}Y
"right-aligned box did not fill width: {right}"
);
}
#[test]
fn inline_fbox_draws_a_frame_around_content() {
let source = r#"\documentclass{article}
\begin{document}
\setlength{\parindent}{0pt}
A\fbox{B}C
\end{document}
"#;
let value = fresh_preview(source, "inline-fbox.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
let svg = page_svg(&value, 0);
assert!(
svg.contains("fill=\"none\" stroke=\"#000000\""),
"inline fbox frame is missing"
);
assert!(svg.contains(">B</text>"), "inline fbox content is missing");
assert!(
svg.contains(">A</text>") && svg.contains(">C</text>"),
"text around fbox missing"
);
}