Add paragraph indentation and hfill

This commit is contained in:
gloamxun committed 2026-10-19 12:43:30 +08:00
1 parent bad477fbee
commit e44d4c8669
11 files changed
+362 -42

No files matched your search

+4 -1
View File
@@ -25,7 +25,9 @@ const PRELUDE: &str = r#"
\def\today{__TEXRRA_TODAY__}
\def\maketitle{\texrramaketitle}
\def\tableofcontents{}
\def\noindent{}
\def\noindent{\texrranoindent}
\def\hfill{\hskip0pt plus 1fil\relax}
\def\hfil{\hskip0pt plus 1fil\relax}
\def\allowbreak{\penalty0}
\def\nobreak{\penalty10000}
\def\centering{}
@@ -484,6 +486,7 @@ const PRELUDE: &str = r#"
\def\hbox#1{#1}
\def\rule{\texrrarule}
\skipdef\parindent=100
\parindent=17pt\relax
\skipdef\parskip=101
\skipdef\baselineskip=102
\skipdef\lineskip=103
+12
View File
@@ -1247,6 +1247,16 @@ fn boxworks_line_break(
let mut params = Params::plain_tex_defaults();
params.emergency_stretch = scaled_points(2.0);
// boxworks always appends a 1fil parfillskip. When the paragraph contains
// explicit infinite glue (\hfill), that trailing glue is excluded from the
// line but still counted in the stretch denominator, which halves every
// \hfill. Drop it so explicit fill glue can claim the full line.
if runs.iter().any(|run| {
run.glue
.is_some_and(|glue| !matches!(glue.stretch_order, texrra_core::TeXGlueOrder::Normal))
}) {
params.par_fill_skip = common::Glue::ZERO;
}
let widths = [scaled_points(width_mm * PDF_POINTS_PER_INCH / 25.4)];
let indents = [Scaled::ZERO];
let hyphenator = TeXHyphenator::get();
@@ -2147,6 +2157,8 @@ pub(super) fn render_pdf(
bold: true,
italic: false,
monospace: false,
paragraph_indent: false,
}];
document.push(paragraph(
&heading,
+70 -4
View File
@@ -54,6 +54,9 @@ pub(super) use texrra_core::source::{SourceLocation, SourceOrigin};
const DEFAULT_JOT: common::Scaled = common::Scaled(3 * 65_536);
/// Index of the `\parindent` skip register defined by the compatibility prelude.
const PARINDENT_REGISTER_INDEX: usize = 100;
static PARAGRAPH_BREAK_TAG: texlang::command::StaticTag = texlang::command::StaticTag::new();
static LINE_BREAK_TAG: texlang::command::StaticTag = texlang::command::StaticTag::new();
static PAGE_BREAK_TAG: texlang::command::StaticTag = texlang::command::StaticTag::new();
@@ -1441,17 +1444,17 @@ impl vm::Handlers<CompilerState> for OutputHandlers {
NativePrimitive::VerticalSkip => {
execute_layout_primitive(LayoutPrimitive::VerticalSkip, token, input)
}
NativePrimitive::Advance => engine_arithmetic::execute(
NativePrimitive::Advance => arithmetic_with_parindent(
engine_arithmetic::ArithmeticOperation::Advance,
token,
input,
),
NativePrimitive::Multiply => engine_arithmetic::execute(
NativePrimitive::Multiply => arithmetic_with_parindent(
engine_arithmetic::ArithmeticOperation::Multiply,
token,
input,
),
NativePrimitive::Divide => engine_arithmetic::execute(
NativePrimitive::Divide => arithmetic_with_parindent(
engine_arithmetic::ArithmeticOperation::Divide,
token,
input,
@@ -1469,7 +1472,13 @@ impl vm::Handlers<CompilerState> for OutputHandlers {
command: &texlang::variable::Command<CompilerState>,
scope: texcraft_stdext::collections::groupingmap::Scope,
) -> Option<texlang::prelude::Result<()>> {
engine_variable::assign_arithmetic(command, token, input, scope)
let parindent =
command.is_glue() && command.static_index() == Some(PARINDENT_REGISTER_INDEX);
let result = engine_variable::assign_arithmetic(command, token, input, scope);
if parindent && matches!(&result, Some(Ok(()))) {
emit_parindent(input, token);
}
result
}
fn begin_group_hook(input: &mut vm::ExecutionInput<CompilerState>, token: token::Token) {
@@ -1481,7 +1490,9 @@ impl vm::Handlers<CompilerState> for OutputHandlers {
fn end_group_hook(input: &mut vm::ExecutionInput<CompilerState>, token: token::Token) {
let state = input.state_mut();
state.end_engine_group();
let parindent = current_parindent(input.state());
write_event(input, token, FrontendEventKind::GroupEnd);
write_event(input, token, FrontendEventKind::Parindent(parindent));
}
fn character_handler(
@@ -1828,6 +1839,57 @@ fn layout_primitive_tag(primitive: LayoutPrimitive) -> texlang::command::Tag {
}
}
fn current_parindent(state: &CompilerState) -> common::Scaled {
state
.engine
.skip_registers
.get_index_ref(PARINDENT_REGISTER_INDEX)
.width
}
fn emit_parindent(input: &mut vm::ExecutionInput<CompilerState>, token: token::Token) {
let value = current_parindent(input.state());
write_event(input, token, FrontendEventKind::Parindent(value));
}
fn next_token_is_parindent(
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<bool> {
let Some(next) = read_next_non_space_unexpanded(input)? else {
return Ok(false);
};
let is_parindent = match next.value() {
token::Value::CommandRef(command_ref) => matches!(
engine_command_name(command_ref, input.vm().cs_name_interner()),
EngineCommandName::ControlSequence(name) if name == "parindent"
),
_ => false,
};
input.unexpanded().back(next);
Ok(is_parindent)
}
fn arithmetic_with_parindent(
operation: engine_arithmetic::ArithmeticOperation,
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
let parindent = next_token_is_parindent(input)?;
let result = engine_arithmetic::execute(operation, token, input);
if parindent && result.is_ok() {
emit_parindent(input, token);
}
result
}
fn no_indent_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
write_event(input, token, FrontendEventKind::NoIndent);
Ok(())
}
fn make_title_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
@@ -6735,6 +6797,10 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
"texrraheadingend",
texlang::command::BuiltIn::new_execution(heading_end_primitive),
);
built_ins.insert(
"texrranoindent",
texlang::command::BuiltIn::new_execution(no_indent_primitive),
);
built_ins.insert(
"texrralabel",
texlang::command::BuiltIn::new_execution(label_primitive),