Skip to content

Fake text weight and style (synthesized bold and italic) #394

Open

Description

@1216892614

Classic Chinese fonts often do not include weight and style. Fake text weight, text style can really help.

Activity

  1. added
    feature request
    text
    on Mar 31, 2023
  2. yujqiao commented on Apr 27, 2023

    @yujqiao

    xelatex has the AutoFakeBold option built-in. But I didn't find where I can set options for fonts in typst. Maybe adding a new argument for text, named auto-fake-bold. @laurmaedje What do you think?

  3. laurmaedje commented on Apr 27, 2023

    @laurmaedje
    Member

    I can imagine having support for synthesized bold & italic, but it's non-trivial to implement.

  4. yujqiao commented on Apr 27, 2023

    @yujqiao

    I think an alternative solution is to expose stroke for text to users, then user-defined functions can be used to make fakebold.

  5. yujqiao commented on Apr 27, 2023

    @yujqiao

    I think an alternative solution is to expose stroke for text to users, then user-defined functions can be used to make fakebold.

    Fakebold Demo

    image

    Left: fake bold(Stroke::default() in this case)
    Right: original

    Just played with the source and it seems working.

    Proposed Solution

    Add stroke parameter to text, just as the one in overline.

    When stroke is not None, call Content::set_text_rendering_mode(pdf_writer::types::TextRenderingMode::FillStroke); and set stroke.

    @laurmaedje Does this idea look fine to you? If so, I can continue to work on this and open a PR.

  6. laurmaedje commented on Apr 27, 2023

    @laurmaedje
    Member

    I'm not sure this works as you want it to since glyph forms are designed for being outlined, not stroked. Moreover, we also need to be able to render to an image, not only as PDF.

  7. yujqiao commented on Apr 28, 2023

    @yujqiao

    https://ctan.org/pkg/xfakebold uses stroke to make fakebold. It doesn't work for all fonts. But in this way users get what they expect for latex.

    Export for image may be an issue. There could be some inconsistency. Do you think it's a must to keep the rendered result exactly the same?

  8. Enter-tainer commented on Apr 28, 2023

    @Enter-tainer
    Contributor

    For export to image, tiny_skia has stroke_path, https://docs.rs/tiny-skia/latest/tiny_skia/struct.PixmapMut.html#method.stroke_path. Maybe it is possible to make fakebold path by using it.

    glyph forms are designed for being outlined, not stroked

    Indeed, that's why it is called "fake bold". MS Word employs similar methods when a font does not have a bold variant available.

  9. laurmaedje commented on Apr 28, 2023

    @laurmaedje
    Member

    As far as I know, the typical fakebold algorithm isn't fill + stroke, but rather a specific outline expansion algorithm. See e.g. here: https://freetype.org/freetype2/docs/reference/ft2-outline_processing.html#ft_outline_embolden

  10. flaribbit commented on Jun 6, 2023

    @flaribbit

    Looking forward to this feature too.

    I tried to create a pdf document with word to see how it works.

    Bold font has same id with regular font. It just have additional stroke. For 100pt font, the stroke width is 2.9pt. Add stroke to regular text gives the same result as word's bold text.

    image

    Italic font has different id with regular font. Dont know how it works.

    image

  11. yujqiao commented on Jun 6, 2023

    @yujqiao

    FWIW, I created a branch that enables stroke on text elem. It's unlikely to be merged but may be useful for those who want to play around.

  12. changed the title [-]Fake text weight, text style request[/-] [+]Fake text weight and style (synthesized bold and italic)[/+] on Nov 6, 2023
  13. Atreyu-94 commented on Nov 15, 2023

    @Atreyu-94

    I think this is very important in mathematical environments. Bold and italics are widely used to define elements: vectors, matrices, etc.

  14. 3 remaining items

  15. flaribbit commented on Nov 16, 2023

    @flaribbit

    How do you create this pdf doc? Is it from Microsoft Word? If both word and LaTeX are adding stokes, I think we can do the same thing. @flaribbit

    Yes, the document in these two screenshots is created by Microsoft Word. @Enter-tainer

  16. peng1999 commented on Dec 14, 2023

    @peng1999
    Contributor

    @laurmaedje According to the recent comment, MS Word and xfakebold used in LaTeX both use stroke to implement fake bold. So will you accept a PR to implement stroke for text in Typst?
    (Not that I'm committing to implementing it, but it's good to hear your perspective nonetheless.)

  17. Andrew15-5 commented on Dec 14, 2023

    @Andrew15-5
    Contributor
  18. Enter-tainer commented on Dec 14, 2023

    @Enter-tainer
    Contributor

    So actually what is need to do is #2749 and #2474. With that fake bold/italic can be implemented using packages

  19. OrangeX4 commented on Dec 14, 2023

    @OrangeX4

    In my opinion, the support for fake bold in Typst is indispensable for Chinese users. Chinese users need to use the "楷体_GB2312" (Kaiti_GB2312) and "仿宋_GB2312" (Fangsong_GB2312) fonts to write many documents, such as academic theses and government papers. Especially for the latter, there are potentially hundreds of thousands or even millions of users. Both of these fonts lack a bold variant, and without support for fake bold, I cannot even create a truly usable nju-thesis-typst, which uses these two fonts. This is the most serious issue I encountered while attempting to implement our university's thesis template using Typst.

    Therefore, I sincerely hope that Typst can implement fake bold at an early date, regardless of the approach adopted.

  20. tianze0926 commented on Dec 14, 2023

    @tianze0926

    For those who can't wait, here is a dirty workaround using fontforge to process the entire font and generate a bold version ttf. It can be done via python script or GUI. By varying the argument passed to changeWeight(), you can control the resulting weight.

  21. laurmaedje commented on Dec 14, 2023

    @laurmaedje
    Member

    @peng1999 I think it would be acceptable for now. We can improve it in the future.

  22. cesaryuan commented on Mar 10, 2024

    @cesaryuan

    @laurmaedje @Enter-tainer Sorry to bother you guys. I just saw that the #2970 has been merged. So if this issue has been solved?

    If it is, how can I bold the text with the font "SimSun"? I just tried the #show text.where(weight: "bold", font: "SimSun"): set text(stroke: 0.5pt), but it does not work.

  23. Enter-tainer commented on Mar 10, 2024

    @Enter-tainer
    Contributor

    how about directly set stroke without using show rules?

  24. PgBiel commented on Mar 10, 2024

    @PgBiel
    Contributor

    For future reference, after a conversation on Discord between me and @cesaryuan, we ended up settling on

    #show text.where(weight: "bold"): it => {
      show regex("\p{script=Han}"): set text(stroke: 0.5pt)
      it
    }
    
    #text(weight: "bold")[测试]

    To have it apply to strong (a.k.a. *stuff*) as well, use .or(strong):

    #show text.where(weight: "bold").or(strong): it => {
      show regex("\p{script=Han}"): set text(stroke: 0.5pt)
      it
    }
    
    #text(weight: "bold")[测试] *测试*

    Here, we use show regex("\p{script=Han}") for two reasons:

    1. #show text.where(font: "SimSun") doesn't work because, apparently, fonts are converted to lowercase, so you'd have to write #show text.where(font: "simsun") for it to work.
    2. Even then, oftentimes the font property is just a list with fallbacks, and you can't tell which fallback font is currently selected. Therefore, we assume that the SimSun font is only used for Chinese characters, and thus only apply the stroke on those.
  25. csimide commented on Mar 22, 2024

    @csimide

    The fake bold effect in MS Office is achieved by adding a stroke of 0.02857em width to characters (in fact, the precise value might be 1/35em). Since PR #2970 has introduced a stroke parameter to text, using text(stroke: 0.02857em) in Typst can achieve a similar effect as in MS Office.

    The code provided by @PgBiel above can be modified as follows:

    #show text.where(weight: "bold").or(strong): it => {
      show regex("\p{script=Han}"): set text(stroke: 0.02857em)
      it
    }

    The cuti package can also be used to set Chinese fake bold. The preset match range of cuti also includes commonly used Chinese symbols and more.


    The fake italic effect in MS Office is obtained by applying a skew of arctan(1/3) to characters. @Enivex provided codes for Skew transform in issue #2749, which can achieve fake italic effect.

    In mixed Chinese and English typesetting tests, I've found that the italic variants of many English fonts often have a skew angle less than arctan(1/3). When using SimSun for Chinese characters and Times New Roman for English characters (which is a common combination in Simplified Chinese), this skew angle is somewhat acceptable.

    image

    However, when using KaiTi as the Chinese font and Linux Libertine for English text, this inconsistency in skew looks very unappealing:

    image

    P.S. MS Office also exhibits this inconsistency in skew (the example shown is Libertinus Serif + KaiTi):

    image

    Furthermore, in the cuti package, I noticed that applying skew transform to individual characters can cause the text box1 to change, especially when acting on punctuation (for example, the symbol ,). The spacing between the punctuation and other characters might pose a problem.

    image


    The solution provided by @PgBiel above, and the one used in cuti, both rely on regular expressions to match a specific range of characters. Yet, it might be more accurate to say that the goal is to apply a fake bold effect specifically to fonts missing a bold weight, rather than to a specific range of characters. If we use text.where(font: "font name"), we might need a very long list of font names lacking bold weight. I'm looking forward to future versions of Typst incorporating automatic fake bold features for fonts lacking bold weight.

    Footnotes

    1. I'm not very familiar with fonts, so I'm not sure what the technical term for this would be. ↩

  26. Tetragramm commented on Aug 25, 2024

    @Tetragramm
    Contributor

    I would like to affirm that fake bold and italic fonts would be appreciated. Sometimes there just isn't another version of the font, or another font that looks sufficiently similar to pass.

    cuti, unfortunately, does not work very well at least for italics. Perhaps the built-in skew function will help, but for now, it makes a mess of especially multi-line text.

  27. csimide commented on Aug 26, 2024

    @csimide

    Actually, cuti's faux italic feature consists of two parts. The first part is _skew, which transforms text into a faux italic format based on code provided by Enivex. This aspect performs quite well. The second part applies the _skew transformation to specific characters, and as @Tetragramm mentioned, this part does not yield satisfactory results.

    When applying the _skew transformation to a block of text, it becomes a single entity. This can lead to unexpected outputs, especially when _skew is applied to multi-line text. Alternatively, applying the _skew effect character by character disrupts line formatting and spacing control. Currently, the best rendering solution might involve using _skew while manually managing line breaks, but this approach can be quite cumbersome.

  28. jassielof commented on Dec 2, 2025

    @jassielof
    Contributor

    For weights or bold fonts, specially serif, over-layering can give a better result than stroke (stroke feels better for sans-serif).

    For example

    #set page(width: 10cm)
    #let over-place(body) = {
      // Catte's solution: iterating over all characters, which causes a 
     show regex("."): it => {
        for dx in ((0.3pt, 0.35pt, -0.35pt, -0.3pt)) {
          box(place(dy: -.7em, dx: dx, it))
        }
        it
      }
      body
    }
    
    #set text(font: "IBM Plex Serif")
    Hasta la vista: #over-place(lorem(20))
    
    #let over-place-no-it(body) = {
      set text(tracking: 0.25pt)
      // set place(clearance: 0em)
      // box(place(top, float: true, scope: "parent", body))
      place(dx: 0.4pt, body)
      place(dx: 0.3pt, body)
      place(dx: -0.3pt, body)
      place(dx: -0.4pt, body)
      hide(body)
    }
    
    // Without iterating content doesn't reflow, and one can hide content but won't reflow
    Hasta la vista: #over-place-no-it(lorem(20))
    
    
    #let over-place-no-it(body) = {
      // set text(tracking: 0.25pt)
      // set place(clearance: 0em)
      // box(place(top, float: true, scope: "parent", body))
    
      place(dx: 0.25pt, body)
      place(dx: 0.2pt, body)
      place(dx: 0.1pt, body)
      place(dx: -0.1pt, body)
      place(dx: -0.2pt, body)
      place(dx: 0.25pt, body)
      hide(body)
    }
    
    Hasta la vista: #strong(lorem(20))
    
    #set text(font: "Noto Serif CJK JP")
    Regular: \
    ロレム・イプサムは、デザインや出版業界で一般的に使用されているプレースホルダー \
    Fake bold: \
    #over-place-no-it[ロレム・イプサムは、デザインや出版業界で一般的に使用されているプレースホルダー] \
    True bold: \
    #strong[ロレム・イプサムは、デザインや出版業界で一般的に使用されているプレースホルダー] \
    Stroke only: \
    #text(stroke: 0.5pt)[ロレム・イプサムは、デザインや出版業界で一般的に使用されているプレースホルダー]
    Image

    There's also Pink's solution which works https://discord.com/channels/1054443721975922748/1088371919725793360/1445503066534051851, but as Pink said: "but maybe dont"

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

    feature requesttext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions