Skip to content

Redundant CJK-Latin space at manual line breaks #6539

@YDX-2147483647

Description

@YDX-2147483647

Original title: cjk-latin-space + manual linebreak + align(center) ⇒ Unexpected left shift

Description

If the line is broken manually between a CJK and a Latin character, then typst will insert an extra CJK-Latin space. This space becomes noticeable when text is aligned to the right or center.

In the following example, the text 国国 is intended to be horizontally centered, but it appears slightly shifted to the left.

#set text(font: "Noto Serif CJK SC")
#box(width: 2.3em, stroke: (x: green), align(center, [国国\ T T]))

Image

This issue only occurs when

  • cjk-latin-spacing is enabled (which is the default),
  • and the line break is manually created by linebreak() or \ .

This issue is NOT related to the linebreaks algorithm, as it occurs in both #set par(linebreaks: "simple") and #set par(linebreaks: "optimized").

More examples

Image

Full code
#let test(body, width: 2.3em) = box(width: width, stroke: (x: green), align(
  center,
  body,
))

#set page(width: auto, height: auto, margin: 1em)
#set text(
  font: "Noto Serif CJK SC",
  lang: "zh",
  top-edge: "ascender",
  bottom-edge: "descender",
)

#set raw(lang: "typc")
#show raw: set text(font: ("DejaVu Sans Mono", "Noto Serif CJK SC"))

#set table.hline(stroke: 0.5pt)

#table(
  columns: 3,
  stroke: none,
  align: (horizon, center + horizon, horizon),
  ..(
    ([✅], `[国国TT]`),
    table.hline(),
    ([❌], `[国国\ TT]`),
    (
      [❌],
      `[国国] + linebreak() + [TT]
// Equivalent to the above`,
    ),
    (
      [❌],
      `text(cjk-latin-spacing: auto)[国国\ TT]
// Equivalent to the above`,
    ),
    ([✅], `text(cjk-latin-spacing: none)[国国\ TT]`),
    table.hline(),
    ([❌], `[国国\ T国]`),
    ([✅], `[国T\ T国]`),
    ([✅], `[国T\ 国国]`),
    ([✅], `[国国\ 国国]`),
  )
    .map(row => if type(row) == array and row.len() == 2 {
      let (ok, body) = row
      (ok, test(eval(body.text)), body)
    } else { row })
    .flatten(),
  table.hline(),
  [❌],
  test(width: 2.3em)[国国\ TT],
  [`[国国\ TT] + 2.3em`, where `2.3em` is width],
  [❌], test(width: 2.5em)[国国\ TT], `[国国\ TT] + 2.5em`,
  [❌], test(width: 2.8em)[国国\ TT], `[国国\ TT] + 2.8em`,
  [❌], test(width: 3.5em)[国国\ TT], `[国国\ TT] + 3.5em`,
)

Additionally, the cjk-latin space also affects align(end).

Image

Links

Reproduction URL

No response

Operating system

Windows, Web app

Typst version

  • I am using the latest version of Typst

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcjkChinese, Japanese, Korean typography.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions