Skip to content

Font not united for CJK in raw blocks #5748

Closed

Description

@earmer

Description

Image

As mentioned above, there are errors in the font and text detection in the original block.

List of errors:

  • The font is not uniform, and the Japanese-style font is mixed with the Chinese mainland font.

  • Mistakenly added a space between CJK and Latin characters (automatic space function issue).

  • The language automatic detection algorithm failed.

Reason (maybe because I haven't read the source code of Typst):

  • Use the serval keyword to judge the language environment.

    • Add #set text(lang: "zh") doesn't work.
  • Lang env detects an error in the trigger. Adding "f" can change the font.

  • Style Issues:

    • Should the next character use the style of the previous character? -- This problem was solved incorrectly.

    • The judgment of adding spaces between CJK and Latin characters is not strictly written.

    • Automatically return to the default font of CJK.

Reproduction URL

https://typst.app/project/rab9abXLvijPP5uiBT8NcO

Operating system

Web app, macOS

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Jan 24, 2025
  2. laurmaedje commented on Jan 24, 2025

    @laurmaedje
    Member

    There isn't really automatic language detection. For proper typesetting, you need to set both the language and a good font.

    We're aware that the out-of-the-box experience is bad, but it's more due to missing features than any of the existing systems working incorrectly. Related: #5040

    Note that raw blocks also set the language to English by default because code is often in English. To override this, you need to write

    #show raw: set text(lang: "zh")
  3. hongjr03 commented on Jan 24, 2025

    @hongjr03
    Contributor

    There is a solution in FAQ of typst-doc-cn's guide, have a check on it! 🎉

  4. earmer commented on Jan 24, 2025

    @earmer
    Author

    Thank you. But, I'm still eager to know why it does the font style like this.
    By the way, the search function of the official document is...not good.

  5. earmer commented on Jan 24, 2025

    @earmer
    Author

    @laurmaedje Thanks for the reply, but I have to disagree on a few points you missed.

    First, calling this an "out-of-box experience issue" doesn't really hold up. The font style behavior is clearly undefined and needs proper definition. Plus, we could label any bug or feature request as an "out-of-box experience issue" - if an explanation works for everything, it's probably not explaining anything.

    You also missed a key point: cjk-latin-spacing shouldn't be getting into Raw blocks at all. Your own example with #show raw: set text(lang: "zh") shows that Raw blocks are supposed to ignore normal font/language settings. This just proves my point - the whole question of what styles affect Raw blocks is completely undefined and messy right now.

    The spacing issue is critical because CJK characters are not only used in comments in the code.

    while (true) // 典型的Textbook式死循环

    In this situation, the spacing around Textbook is okey.

    printf("这是%d段代码", 1)

    In this case, if we add a space between %d and 段代码 in the compiled result, it will be confusing, because we can't judge whether it is a simple white space.

  6. added
    text
    on Jan 25, 2025
  7. laurmaedje commented on Jan 25, 2025

    @laurmaedje
    Member

    The font style behavior is clearly undefined and needs proper definition.

    Well, yes, but only because no suitable font is defined, so it's triggering fallback all over the place, which is fundamentally a bit unpredictable. I'm not saying the fallback behaviour can't be improved, but the main issue is that language and font aren't set -- and that's something that should work better out of the box.

    You also missed a key point: cjk-latin-spacing shouldn't be getting into Raw blocks at all.

    Yeah, that's an actual bug. I didn't comment on that specifically, but I did notice it. I think that deserves its own issue though (I'd be happy for you to open it). A very broad issue like this one here is not super actionable.

    The fallback stuff, in my opinion, falls into #5040. (I have reworded the title of the issue since it is not web-app specific).

    That covers the first two points in your list of errors. The third point I'm not sure about since there isn't an automatic language detection algorithm in the first place.

  8. earmer commented on Jan 26, 2025

    @earmer
    Author

    Yes, and I think I can close this issue and open a new one. Thank you again. @laurmaedje

  9. earmer commented on Jan 26, 2025

    @earmer
    Author

    Well, and the most proper solution is like:

    #show raw: set text(font: ("Menlo", "PingFang SC"))
    
    #raw(lang: "cpp", "while (true) // 典型的Textbook式死循环")
    
    #raw(lang: "cpp", "printf(\"这是%d段代码\", 1)")

    Use "Menlo" for mono font, and use "PingFang SC" for a fallback font for CJK.

    @hongjr03

  10. hongjr03 commented on Jan 26, 2025

    @hongjr03
    Contributor

    Well, and the most proper solution is like:

    #show raw: set text(font: ("Menlo", "PingFang SC"))

    #raw(lang: "cpp", "while (true) // 典型的Textbook式死循环")

    #raw(lang: "cpp", "printf("这是%d段代码", 1)")
    Use "Menlo" for mono font, and use "PingFang SC" for a fallback font for CJK.

    @hongjr03

    i agree 🎉

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

    bugtext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions