Skip to content

Adjust CJK punctuation spacing correctly with indents - #8570

Merged
laurmaedje merged 1 commit into
typst:mainfrom
TiggeZaki:fix-start-punc
Oct 22, 2026
Merged

laurmaedje merged 1 commit into
typst:mainfrom
TiggeZaki:fix-start-punc

Conversation

@TiggeZaki

Copy link
Copy Markdown
Contributor

related to #2348

When adjusting at line boundaries, I updated the process to ignore whitespaces created by initial indents. As a result, proper spacing adjustments are now applied to CJK punctuation even when an indent is present.

Properly adjusted CJK punctuation spacing in an indented image

@TiggeZaki

Copy link
Copy Markdown
Contributor Author

I considered applying the same logic to the end of lines for consistency, but since indents shouldn't appear at line ends, I'm putting that on hold for now.

@laurmaedje laurmaedje added cjk Chinese, Japanese, Korean typography. waiting-on-review This PR is waiting to be reviewed. labels Jun 30, 2026
@YDX-2147483647

Copy link
Copy Markdown
Contributor

This PR also makes the first-line indent visually more stable.

Consider the following example. As the width of space decreases, the spacing around punctuation marks will be increased to justify the text.
In Typst v0.15.1, the left side of the initial ( moves according to the width of space.
In this PR, its left side is fixed at the specified first-line indent as expected.

#set par(
  first-line-indent: (amount: 2em, all: true),
  justify: true,
)
#let space = box(width: width, height: 0.8em, fill: green)

(1)桑之未落,其叶#space;沃若。#linebreak(justify: true)
v0.15.1 This PR
图片 图片
Full code
#set text(lang: "zh", font: "Noto Serif CJK SC")
#set par(
  first-line-indent: (amount: 2em, all: true),
  justify: true,
)
#set page(height: auto, width: 16em, margin: 1em, background: context grid(
  columns: (page.margin + par.first-line-indent.amount, 1em, 1fr),
  rows: 1fr,
  stroke: (x, y) => if x > 0 { (left: green + 0.5pt) },
))
#for width in (1.25em, 1em, 0.5em, 0.25em, 0em) {
  let space = box(width: width, height: 0.8em, fill: green)
  [(1)桑之未落,其叶#space;沃若。]
  linebreak(justify: true)
  parbreak()
}

Comment thread crates/typst-layout/src/inline/line.rs Outdated
pub fn leading_visible_text_mut(&mut self) -> Option<&mut ShapedText<'a>> {
self.0
.iter_mut()
.find(|(_, item)| matches!(**item, Item::Text(_)))?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will skip over anything, including e.g. a box, which could be surprising. In the context of this PR I think it works because it's only called if text.trim_start().starts_with(BEGIN_PUNCT_PAT) and the box will not trigger that, but it's still problematic.

Is the idea here to skip over spacing? Perhaps that could be done more directly by filtering out Item::Absolute and potentially Fractional (note though that it can also contain a fractional-width box). We would also need to decide how weak and fractional spacing should interact with this adjustment. It is hard for me to judge.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note also that this part of the code has been refactored on main, so it would be worthwhile to rebase before updating things here.

Comment thread crates/typst-layout/src/inline/line.rs Outdated
fn adjust_cj_at_line_start(p: &Preparation, items: &mut Items) {
let Some(shaped) = items.leading_text_mut() else { return };
let Some(shaped) = items.leading_visible_text_mut() else {
return;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you drop the ;, this formats more nicely.

Comment thread crates/typst-layout/src/inline/line.rs Outdated
let text = &p.text[range];

if text.starts_with(BEGIN_PUNCT_PAT)
// Ignore leading whitespace (e.g., indents) to properly detect beginning punctuation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also worth mentioning that a box(width: 1fr, ..) will also be represented as a space in the text.

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Aug 27, 2026
@TiggeZaki

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

In the end, instead of deciding separately whether Absolute or Fractional spacing should be skipped, I introduced a dedicated Indent item and made only paragraph indentation transparent to the CJK punctuation adjustment. This keeps explicit spacing and things such as fractional-width boxes as actual boundaries, as before.

As part of this change, I also avoided using a broad trim_start() on the textual representation. Instead, only the portion corresponding to Indent items is ignored. This preserves the distinction from other kinds of whitespace, including weak spacing whose existing behavior is relied upon in #7247 to suppress CJ boundary adjustment.

I also think that keeping paragraph indentation explicitly distinct from other inline items provides a useful foundation for addressing cases such as #4011 in the future.

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Sep 7, 2026
@laurmaedje laurmaedje added the text Related to the text category, which is all about text handling, shaping, etc. label Oct 19, 2026
@laurmaedje

Copy link
Copy Markdown
Member

Thanks! The Indent approach looks fine.

I opened and merged #8988 first because it allows us to get rid of the string-based checks entirely, significantly simplifying this.

@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Oct 22, 2026
@laurmaedje
laurmaedje enabled auto-merge October 22, 2026 09:42
@laurmaedje
laurmaedje added this pull request to the merge queue Oct 22, 2026
Merged via the queue into typst:main with commit 20cb02e Oct 22, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cjk Chinese, Japanese, Korean typography. text Related to the text category, which is all about text handling, shaping, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants