Skip to content

Calligraphic Math Unicode literals with variation selectors not recognised as a single character #6328

Closed

Description

@JeppeKlitgaard

Description

I have a strong preference towards inputting Unicode directly in my source files, which makes the source files significantly easier to read and use in my opinion.

I was playing around with the calligraphic math characters, which as of Unicode 14.0 take a variation selector (0xFE00 or 0xFE01 suffix), but these are not recognised as a single character/grapheme, which leads to an Unknown Variable error.

$
"Default (OK)" : &ℱ\
"Calligraphic": &ℱ︀\
"Script": &ℱ︁\
$

Which gives error "Unknown variable ℱ︀", which I suspect is due to the fact that it considers both the base character 0x2131 and variation selector 0xFE01 to be characters, which is not the case.


### Reproduction URL

_No response_

### Operating system

Windows

### Typst version

- [x] I am using the latest version of Typst

Activity

  1. added
    bug
    on May 24, 2025
  2. wrzian commented on May 24, 2025

    @wrzian
    Contributor

    Good find! We should add this as a test case based on the resolution.

    This is a lexing issue. We treat text with a valid start character plus a valid continuing character as an identifier. And it seems that we treat variation selectors as continuing characters for identifiers1, which makes us lex this as an identifier (which errors since it's undefined).

    Here's the relevant source code:

    // Identifiers.
    c if is_math_id_start(c) && self.s.at(is_math_id_continue) => {
    self.s.eat_while(is_math_id_continue);
    let (kind, node) = self.math_ident_or_field(start);
    return (kind, Some(node));
    }
    // Other math atoms.
    _ => self.math_text(start, c),

    /// Whether a character can start an identifier in math.
    #[inline]
    fn is_math_id_start(c: char) -> bool {
    is_xid_start(c)
    }
    /// Whether a character can continue an identifier in math.
    #[inline]
    fn is_math_id_continue(c: char) -> bool {
    is_xid_continue(c) && c != '_'
    }

    A possible fix could be to assert that identifiers in math aren't single grapheme clusters, but I would want a second opinion before deciding on it.

    Footnotes

    1. This assert succeeds: assert!(is_math_id_continue('\u{FE01}')) ↩

  3. added
    syntax
    on May 24, 2025
  4. mkorje commented on May 28, 2025

    @mkorje
    Collaborator

    A possible fix could be to assert that identifiers in math aren't single grapheme clusters, but I would want a second opinion before deciding on it.

    I think this is quite reasonable, and would fit in with the changes to the GlyphFragment struct I made in #6336 (if/when that gets merged).

  5. PatrickMassot commented on Jun 1, 2025

    @PatrickMassot
    SponsorContributor

    A related case: the single letter test also fails when using something like φ̅ (where a φ letter is followed by a combining overline).

  6. PgBiel commented on Jun 1, 2025

    @PgBiel
    Contributor

    A possible fix could be to assert that identifiers in math aren't single grapheme clusters

    Do we do this in other places in the syntax? This feels a bit dangerous not only in terms of comparative performance when lexing identifiers, but also because, in theory, grapheme cluster splitting depends on the Unicode version and may change over time, if my memory serves me well, which could be a bit problematic for the syntax... but it's possible this is a dismissable concern (just wanted to bring it up).

  7. added
    math
    on Jun 3, 2025
  8. mkorje commented on Jun 3, 2025

    @mkorje
    Collaborator

    A related case: the single letter test also fails when using something like φ̅ (where a φ letter is followed by a combining overline).

    This is tracked in #1524.

  9. JeppeKlitgaard commented on Jun 4, 2025

    @JeppeKlitgaard
    ContributorAuthor

    in theory, grapheme cluster splitting depends on the Unicode version and may change over time, if my memory serves me well, which could be a bit problematic for the syntax... but it's possible this is a dismissable concern (just wanted to bring it up).

    I think at least for this specific instance (calligraphic vs roundhand distinction), it is not likely that the clustering will ever change now that it has been adopted, though I think you were referring to the idea of applying this approach more generally, in which case I guess there is a small chance that breakages could occur. A possible solution I think could be to consider any updates to the Unicode version used in Typst to be a potentially breaking change. Consequently, updates to the Unicode version used in Typst could be sign-posted as a potentially breaking change (in the few if any instances where the update does change grapheme clustering)

  10. mkorje commented on Jun 25, 2025

    @mkorje
    Collaborator

    This is blocked on #6489 currently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmathsyntax

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions