Skip to content

Add support for ruby (CJK, e.g., furigana for Japanese) #1489

Open

Description

@Andrew15-5

Description

I use ruby package to add furigana on top of the Japanese words in lualatex:

\documentclass[a4paper]{article}
\usepackage[margin=1cm]{geometry}
\pagestyle{empty}
\usepackage{fontspec}
\setmainfont{Noto Sans CJK JP}
\usepackage{ruby}
\usepackage{fontsize}
\changefontsize[21]{14}
\let\fu\ruby
\begin{document}

\begin{center}
  \textbf{柊マグネタイト「マーシャル・マキシマイザー」歌詞}
\end{center}

\begin{center}
  \fu{唖}{あ}\fu{々}{あ}、\fu{臨界実験}{りんかいじっけん}に\fu{付}{つ}き\fu{合}{あ}う\fu{朝}{あさ}
\end{center}

\end{document}

image

I just tried a few things with Typst and I can already tell with 99% of confidence that I can do everything I do with lualatex except adding furigana, which is a crucial part of some Japanese documents (depends on type of document and other things). I was thinking about making some kind of table/math-like vertically nested text, but I'm not a package creator, so I don't know how it is (properly) implemented in ruby package, and therefore I don't know how to implement this in Typst. By this, I mean some easy and concise way to add a Typst alternative of \ruby{}{} (without making a separate feature or template or something).

Use Case

It is not the feature everyone will want, but it should probably be included in the compiler (as a Japanese language "exclusive" feature). As furigana is a way to tell the reader how to pronounce words, this feature would be useful for creating content for Japanese beginners or children such as text books, comics (a.k.a. manga) and much more (or lyrics as per my example).

P.S. It could imply that the "vertical" "right to left" layout must be implemented in the Typst as well (for traditional Japanese style text flow).

Activity

  1. added
    feature request
    on Jun 13, 2023
  2. Andrew15-5 commented on Jun 13, 2023

    @Andrew15-5
    ContributorAuthor

    I can think of the most compact and kind of intuitive way of writing it in Typst:

    (日本語)^(にほんご)
    

    This way you can easily change where the furigana will be placed. And in the simple case, as with the 五^ご you don't even need to use parenthesis.

    This looks very clean and makes a lot of sense of you are writing documents with furigana.

  3. bluebear94 commented on Jun 14, 2023

    @bluebear94
    Contributor

    Note that this feature is complicated to implement properly – see “Requirements for Japanese Text Layout” for more info.

  4. Andrew15-5 commented on Jun 14, 2023

    @Andrew15-5
    ContributorAuthor

    If you are referring to grouping methods of the ruby and/or base text, then it is purely manual thing. The author chooses which ruby should go next to which base text (see my examples above). The line spacing, as I checked, is only affected by the presence of ruby between the title and the first line of lyrics. All other lines stay the same (1.5 distance) and the ruby goes right in the empty space between the lines. Which means that ruby in most cases doesn't alter line spacing.

    In conclusion, it is the author who must make sure that there is enough room for ruby to fit in nicely and be readable and not the typesetting program. The program only needs to make sure that selected ruby is centered right next to selected base text (on top or to the right of the base text). Selection is made of {} in LaTeX (or () as in my previous comment).

    P.S. I didn't mention the font size of the ruby. The ruby package has the \rubysize macro that stores font size ratio compared to \normalsize and by default is equal to 0.4 (whereas 1 is equal to \normalsize).

  5. changed the title [-]Add support for furigana (Japanese)[/-] [+]Add support for ruby/furigana (Japanese)[/+] on Jun 14, 2023
  6. Andrew15-5 commented on Jun 14, 2023

    @Andrew15-5
    ContributorAuthor

    I don't know if Typst output must be the exact same as LaTeX (probably not), but I was able to completely rewrite my document in Typst (with a lot of small size offsets as compared to LaTeX) using ruby-typ library:

    #import "ruby.typ": get_ruby
    #let fu = get_ruby(size: 0.4em, dy: 1pt)
    
    #set page(margin: 1cm)
    #set text(font: "Noto Sans CJK JP", size: 14pt)
    
    #let leading = 0.75em // Changed from 0.5em to make 1.5 leading (1em + 0.5em already have to be 1.5em leading)
    #set par(leading: leading)
    #set block(spacing: leading)
    
    #set align(center)
    
    *柊マグネタイト「マーシャル・マキシマイザー」歌詞*
    
    #v(1.4em) // Doesn't matter to me here
    
    #block([
    
    #set align(left)
    
    #fu("あ", "唖")#fu("あ", "々")、#fu("りんかいじっけん", "臨界実験")に#fu("つ", "付")き#fu("あ", "合")う#fu("あさ", "朝")
    
    ])

    image

    The most noticeable things (that are also relative to the issue) are:

    • ruby is not accounted for when starting a paragraph (it could go out of the defined page bounds) or between words (it could overlap with another ruby next to it)
    • Japanese quotes treaded as a thin character instead of full-width (like all others), as opposed to LaTeX

    Here are LaTeX and Typst examples, respectively:

    image

    image

    LaTeX — green, Typst — red:

    image

    Overall, both have some positive and negative points, but ruby-typ is not included anywhere (whereas ruby is included in TeX Live and is on CTAN) and has a major issue of not checking for ruby collisions with page margin boundaries and text itself.

  7. Andrew15-5 commented on Jun 14, 2023

    @Andrew15-5
    ContributorAuthor

    Oh, sorry. It can prevent ruby overlapping by manually putting "delimeter" character between adjacent base text and ruby:

    #fu("じゅうよん|ポンド")[14|听]

    image

    Which resolves only half of the problem (it still goes out of the defined page bounds), but also now I have to manually check if there is any overlapping and change the Typst code accordingly. This type of stuff should be automated by the library (or compiler) to eliminate human error (maybe someone didn't check properly and some overlapping is still present in the document).

  8. Andrew15-5 commented on Jun 28, 2023

    @Andrew15-5
    ContributorAuthor

    I added [][] format for the library: https://gist.github.com/Andrew15-5/725f1b036fdb376363bd96d8f814aaf7.

    Now I can use #ruby[ふりがな][振り仮名] which removes additional "" and changes first () to []. Delimiter also works.

    This format of the function looks very production ready (not the code itself). Either #r[][]/#ruby[][] or something like ()^() looks pretty good (if it would be the official Typst syntax/feature).

    Update: Added auto_spacing: true to get_ruby() which results in automatic addition of space if ruby is wider than base text (by adding missing delimiter around every content/string). This way there won't be any ruby overlapping (when 2 or more rubies are next to each other) and there will be always enough space around base text (if ruby is wider than base text, but there is no adjacent rubies).

  9. Andrew15-5 commented on Jun 30, 2023

    @Andrew15-5
    ContributorAuthor

    I have good news. I've tried adding delimiter around the text (which is something I never thought of even trying), and the problem with ruby going out of page boundaries is resolved. The last problem left is not related to the ruby, so I made a separate discussion.

    P.S. I created a repository with improved syntax, which is also a candidate for the public package.

    Update:

    The package is now merged in Typst @preview namespace and can be used like this:

    #import "@preview/rubby:<version>": get-ruby
  10. added
    text
    on Jul 3, 2023
  11. changed the title [-]Add support for ruby/furigana (Japanese)[/-] [+]Add support for ruby/furigana (CJK, e.g., Japanese)[/+] on Dec 12, 2023
  12. changed the title [-]Add support for ruby/furigana (CJK, e.g., Japanese)[/-] [+]Add support for ruby (CJK, e.g., furigana for Japanese)[/+] on Feb 8, 2024
  13. minamotorin commented on Apr 25, 2024

    @minamotorin

    Referencing ruby package of LaTeX, a part of CJK package, is not a good idea. It does not follow Requirements for Japanese Text Layout and it outputs bad typesetting.

    In LaTeX, I recommend to refer to pxrubrica package or luatexja-ruby package, a part of luatexja package.

    For general information on Japanese typesetting with LaTeX, I reccomend texdoc japanese.

  14. Andrew15-5 commented on Apr 25, 2024

    @Andrew15-5
    ContributorAuthor

    Interesting, I will try reading it when I got time.

    I was using this package only because it looked like the most popular one. I don't remember seeing any other ruby package mentioned before.

  15. added
    cjk
    on Feb 10, 2025
  16. OverflowCat commented on Feb 21, 2025

    @OverflowCat
    Contributor

    Rendering result before Chrome 128 with long ruby text:

    Rendering result from Chrome 128 with long ruby text:

    So, yeah, ruby is involved with linebreaks. The userland implementation can't achieve this effect.

  17. Andrew15-5 commented on Feb 23, 2025

    @Andrew15-5
    ContributorAuthor

    The userland implementation can't achieve this effect.

    What does this mean? You mean packages can't fully implement this and only native implementation can truly work? Since ruby is glued to the base text, I don't think it changes anything. It would just depend on/follow how the base text is typeset.

  18. OverflowCat commented on Feb 23, 2025

    @OverflowCat
    Contributor

    ruby is glued to the base text

    Take this example, #ruby[プルミエール、ダンス][第一位舞踏者] is a single unit, which is different from those that can separated like #ruby[とう|きょう|こう|ぎょう|だい|がく][東|京|工|業|大|学]. We don't know if or where this text will break lines. If it breaks at 第一位舞踏|者, we need to measure the linebreak, that is, calculate the lengths of 第一位舞踏 and 者, and divide プルミエール、ダンス into a 5:1 length ratio. (Both the ruby base and the ruby itself might not be monospace, so counting characters do not work.)

  19. Andrew15-5 commented on Feb 23, 2025

    @Andrew15-5
    ContributorAuthor

    OK, I think I understand. But I still don't know of the text line breaking rules in Japanese. I guess it is indeed would be much easier to do this perfectly in the native code.

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

    cjkfeature requesttext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions