You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fake text weight and style (synthesized bold and italic) #394
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?
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.
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.
Italic font has different id with regular font. Dont know how it works.
@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.)
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.
Reacted by Yiklek, J, Cesar, fristzzz, ᡥᠠᡳᡤᡳᠶᠠ ᡥᠠᠯᠠ·ᠨᡝᡴᠣ 猫, qujihan and Yangyang Xu
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.
Reacted by Wenzhuo Liu, OrangeX4, 梦飞翔 and Bernhard Döbler
@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.
Here, we use show regex("\p{script=Han}") for two reasons:
#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.
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.
Reacted by Wenzhuo Liu, Andrew Voynov, OrangeX4, Csimide and CUI CHUAN
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:
#showtext.where(weight: "bold").or(strong): it=> {
showregex("\p{script=Han}"): settext(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.
However, when using KaiTi as the Chinese font and Linux Libertine for English text, this inconsistency in skew looks very unappealing:
P.S. MS Office also exhibits this inconsistency in skew (the example shown is Libertinus Serif + KaiTi):
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.
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
I'm not very familiar with fonts, so I'm not sure what the technical term for this would be. ↩
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.
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.
Activity
yujqiao commentedon Apr 27, 2023
xelatex has the
AutoFakeBoldoption built-in. But I didn't find where I can set options for fonts in typst. Maybe adding a new argument for text, namedauto-fake-bold. @laurmaedje What do you think?laurmaedje commentedon Apr 27, 2023
I can imagine having support for synthesized bold & italic, but it's non-trivial to implement.
yujqiao commentedon Apr 27, 2023
I think an alternative solution is to expose stroke for text to users, then user-defined functions can be used to make fakebold.
yujqiao commentedon Apr 27, 2023
Fakebold Demo
Left: fake bold(Stroke::default() in this case)
Right: original
Just played with the source and it seems working.
Proposed Solution
Add
strokeparameter totext, just as the one inoverline.When
strokeis notNone, callContent::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.
laurmaedje commentedon Apr 27, 2023
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.
yujqiao commentedon Apr 28, 2023
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?
Enter-tainer commentedon Apr 28, 2023
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.Indeed, that's why it is called "fake bold". MS Word employs similar methods when a font does not have a bold variant available.
laurmaedje commentedon Apr 28, 2023
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
flaribbit commentedon Jun 6, 2023
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.
Italic font has different id with regular font. Dont know how it works.
yujqiao commentedon Jun 6, 2023
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.
[-]Fake text weight, text style request[/-][+]Fake text weight and style (synthesized bold and italic)[/+]Atreyu-94 commentedon Nov 15, 2023
I think this is very important in mathematical environments. Bold and italics are widely used to define elements: vectors, matrices, etc.
3 remaining items
flaribbit commentedon Nov 16, 2023
Yes, the document in these two screenshots is created by Microsoft Word. @Enter-tainer
peng1999 commentedon Dec 14, 2023
@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.)
Andrew15-5 commentedon Dec 14, 2023
#2474
Enter-tainer commentedon Dec 14, 2023
So actually what is need to do is #2749 and #2474. With that fake bold/italic can be implemented using packages
OrangeX4 commentedon Dec 14, 2023
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.
tianze0926 commentedon Dec 14, 2023
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.laurmaedje commentedon Dec 14, 2023
@peng1999 I think it would be acceptable for now. We can improve it in the future.
cesaryuan commentedon Mar 10, 2024
@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.Enter-tainer commentedon Mar 10, 2024
how about directly set stroke without using show rules?
PgBiel commentedon Mar 10, 2024
For future reference, after a conversation on Discord between me and @cesaryuan, we ended up settling on
To have it apply to
strong(a.k.a.*stuff*) as well, use.or(strong):Here, we use
show regex("\p{script=Han}")for two reasons:#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.fontproperty 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.csimide commentedon Mar 22, 2024
The fake bold effect in MS Office is achieved by adding a stroke of
0.02857emwidth to characters (in fact, the precise value might be1/35em). Since PR #2970 has introduced a stroke parameter totext, 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:
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.However, when using KaiTi as the Chinese font and Linux Libertine for English text, this inconsistency in skew looks very unappealing:
P.S. MS Office also exhibits this inconsistency in skew (the example shown is Libertinus Serif + KaiTi):
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.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
I'm not very familiar with fonts, so I'm not sure what the technical term for this would be. ↩
Tetragramm commentedon Aug 25, 2024
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.
csimide commentedon Aug 26, 2024
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_skewtransformation to specific characters, and as @Tetragramm mentioned, this part does not yield satisfactory results.When applying the
_skewtransformation to a block of text, it becomes a single entity. This can lead to unexpected outputs, especially when_skewis applied to multi-line text. Alternatively, applying the_skeweffect character by character disrupts line formatting and spacing control. Currently, the best rendering solution might involve using_skewwhile manually managing line breaks, but this approach can be quite cumbersome.jassielof commentedon Dec 2, 2025
For weights or bold fonts, specially serif, over-layering can give a better result than stroke (stroke feels better for sans-serif).
For example