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
Support fontconfig and/or specifying multiple custom font dirs #100
Some usecases (e.g. packaging on NixOS) require specifying multiple custom font locations, which is currently impossible. The best way to do it would be IMO support fontconfig. The second best way is to support $XDG_DATA_DIRS environment variable instead of only supporting $XDG_DATA_HOME.
Reacted by nick, Hans Christian Schmitz, 百地 希留耶, 7sDream, Thiago Franco de Moraes, Tadeo Kondrak, Bauke, Juhan Oskar Hennoste, Caleb Maclennan and Absolutex
I just made a PR for this, it adds custom font search path support.
And, btw, I think fontdb is a good crate to replace the font searcher in cli, it's from the same author of ttf-parser, and has basic support for fontconfig. It doesn't use fontconfig c lib, but parse it's configure file instead, which makes it super easy to compile.
Had a look at fontdb. It leverages the crate fontconfig_parser to parsefonts.conf.
We can also use this parser crate directly.
fontdb + fontconfig_parser
In fontdb, location of fonts.conf is given by FONTCONFIG_FILE, or by merging these files:
$XDG_CONFIG_HOME/fontconfig/fonts.conf
/etc/fonts/local.conf
/etc/fonts/fonts.conf.
I'm not sure why local.conf is searched, according to fonts-conf(5) this is included by fonts.conf already.
fontconfig-rs + libfontconfig
Another option is using FFI to call the real fontconfig library. While the best option is using fontconfig to match fonts, it's easier to just get font directories.
There are binding crates like fontconfig-rs, but this crate doesn't expose FcConfigGetFontDirs so we must do it our own.
It seems that their results are almost same, although
the order may differ, and
libfontconfig also considers fonts under $XDG_DATA_DIRS/share/fonts. This is a feature. However, none of them exists on my OS.
New: After I enabled fonts.fontconfig.enable = true; in home-manager, there is a path which found by FFI but not the parser: /nix/store/kjz70chwjvdcsmmhls4pfx1j6s3rccxb-home-manager-path/share/fonts. I'm figuring why that happens.
Discussion
If you ask me, I prefer to use fontdb/fontconfig_parser, as FFI makes the project complex. On the other hand, I think full support to fontconfig could be made as a plugin.
Nice work! I prefer to parse config file too, as Typst is pure Rust for now and I just don't want to break it. 😆
BTW, I originally planned to start integrating fontdb today, but because you have already written some code, and I am a Windows user, it is not very convenient for me to test the result, so maybe it is more appropriate for you to take over the follow-up intergation work? So I can have a rest on sunday haha 🍹
As RazrFalcon/fontdb#50 has been merged, I think I can open a new PR after fontdb bumps a new version.
And then we can get a simplest work version, i.e., you have a fontconfig file in the default location ~/.config/fontconfig/fonts.conf and your custom font dir is directlly in that file.
I was having a problem using my fonts. They, as most downloadable fonts, are in ~/.fonts/. But from reading the comments, it is not the directory that is read by Typst (although LuaLaTeX does a great job of finding all my fonts). I tried adding a relative symlink to ~/.local/share/fonts/ (the dir didn't even exist) and now it found all my fonts.
@AlexD97 This is a very old fork, but would still love to hear your feedback.
Recently fontdb has released some new versions and my PR is landed in version 0.13.1.
However Riey/fontconfig-parser#3 by @KiruyaMomochi has been merged but not released yet. We need this to solve a bug: "include" in fontconfig configuration files doesn't work if target path contains home tilde ~.
Edit: I just opened an Issue and suggested Riey to release a new version, see Riey/fontconfig-parser#6, after that we can continue our work of integration with fontdb.
As I've already commented, more fonts are recognized with your fork. More specifically, the fonts that are installed “directly” through nix are recognized. The fonts that are managed by home-manager are not detected (I have the fonts.fontconfig.enable option set to true).
@AlexD97 Although I often hear about NixOS, I haven't used it in depth, and I don't know much about some of its concepts. I think we can make the fontconfig configuration works in "normal" cases. If this is not enough for NixOS, we can add support for it on top of this.
BTW, fontconfig-parser has bumped a new version 0.5.3, so I think the fontdb integration work can be started. I can do this but my day job will be busy until september, so anyone can take over it if interested :)
Activity
7sDream commentedon Mar 24, 2023
I just made a PR for this, it adds custom font search path support.
And, btw, I think fontdb is a good crate to replace the font searcher in cli, it's from the same author of ttf-parser, and has basic support for fontconfig. It doesn't use fontconfig c lib, but parse it's configure file instead, which makes it super easy to compile.
drupol commentedon Mar 24, 2023
On my side, I have the variable
FONTCONFIG_FILEset with custom fonts, buttypst --fontsdoesn't return anything.7sDream commentedon Mar 24, 2023
@drupol fontdb will work with this env var.
But I think I should discuss with the author(and other developers) whether this solution is acceptable, before working on a new PR to land it.
laurmaedje commentedon Mar 24, 2023
Interesting that fontdb supports this now. Last time I checked it did pretty much what Typst does now.
laurmaedje commentedon Mar 24, 2023
Using fontdb seems reasonable to me, in principle. Haven't looked at the details though.
7sDream commentedon Mar 24, 2023
looks worth trying, maybe we can merge #270 first, and then I can continue work on fontdb intergation from here?
KiruyaMomochi commentedon Mar 26, 2023
Had a look at fontdb. It leverages the crate fontconfig_parser to parse
fonts.conf.We can also use this parser crate directly.
fontdb + fontconfig_parser
In fontdb, location of
fonts.confis given byFONTCONFIG_FILE, or by merging these files:$XDG_CONFIG_HOME/fontconfig/fonts.conf/etc/fonts/local.conf/etc/fonts/fonts.conf.I'm not sure why
local.confis searched, according to fonts-conf(5) this is included byfonts.confalready.fontconfig-rs + libfontconfig
Another option is using FFI to call the real fontconfig library. While the best option is using fontconfig to match fonts, it's easier to just get font directories.
There are binding crates like fontconfig-rs, but this crate doesn't expose
FcConfigGetFontDirsso we must do it our own.Experimentation
I made an experiment to show the difference between them, you can find code and result at https://github.com/KiruyaMomochi/fontconfig-lab
It seems that their results are almost same, although
libfontconfigalso considers fonts under$XDG_DATA_DIRS/share/fonts. This is a feature. However, none of them exists on my OS.fonts.fontconfig.enable = true;in home-manager, there is a path which found by FFI but not the parser:/nix/store/kjz70chwjvdcsmmhls4pfx1j6s3rccxb-home-manager-path/share/fonts. I'm figuring why that happens.Discussion
If you ask me, I prefer to use fontdb/fontconfig_parser, as FFI makes the project complex. On the other hand, I think full support to fontconfig could be made as a plugin.
7sDream commentedon Mar 26, 2023
@KiruyaMomochi
Nice work! I prefer to parse config file too, as Typst is pure Rust for now and I just don't want to break it. 😆
BTW, I originally planned to start integrating fontdb today, but because you have already written some code, and I am a Windows user, it is not very convenient for me to test the result, so maybe it is more appropriate for you to take over the follow-up intergation work? So I can have a rest on sunday haha 🍹
What do you think?
KiruyaMomochi commentedon Mar 26, 2023
Yes, although I'm not good at typesetting, I can give it a try.
But there is a problem. And decision needs to be made.
fontdb directly loads font into
fontdb::FaceInfo. Comparing to ourtypst::font::book::FontInfo, theirFaceInfodoes not havecoveragefield.That means if we want to use fontdb, and at the same time use FontInfo, we may need to read each font twice.
Some workarounds can be
FaceInfowith ourFontInfo.7sDream commentedon Mar 26, 2023
Yes, but the doubled call is just
ttf-parse::RawFace::parse, which should be pretty cheap, IIRC.7sDream commentedon Mar 26, 2023
@KiruyaMomochi @drupol @GoldsteinE
jsut did some quick coding: https://github.com/7sDream/typst/compare/main..cli/fontdb
It works just like before in my computer, but can you test it to see if the fontconfig thing works for you?
cargo install typst-cli --git https://github.com/7sDream/typst --branch cli/fontdbKiruyaMomochi commentedon Mar 26, 2023
It works for me too.
Screenshot
7sDream commentedon Mar 26, 2023
@KiruyaMomochi And custom font path in your config file automaticlly works too?
7sDream commentedon Mar 26, 2023
As RazrFalcon/fontdb#50 has been merged, I think I can open a new PR after fontdb bumps a new version.
And then we can get a simplest work version, i.e., you have a fontconfig file in the default location
~/.config/fontconfig/fonts.confand your custom font dir is directlly in that file.Andrew15-5 commentedon Jun 14, 2023
I was having a problem using my fonts. They, as most downloadable fonts, are in
~/.fonts/. But from reading the comments, it is not the directory that is read by Typst (although LuaLaTeX does a great job of finding all my fonts). I tried adding a relative symlink to~/.local/share/fonts/(the dir didn't even exist) and now it found all my fonts.Work around Typst not supporting font config paths
AlexD97 commentedon Jul 28, 2023
Does font detection work now on NixOS? For me, it does not work.
Edit: With @7sDream's fork, at least the fonts that are not managed by home-manager are detected.
7sDream commentedon Jul 31, 2023
@AlexD97 This is a very old fork, but would still love to hear your feedback.
Recently fontdb has released some new versions and my PR is landed in version 0.13.1.
However Riey/fontconfig-parser#3 by @KiruyaMomochi has been merged but not released yet. We need this to solve a bug: "include" in fontconfig configuration files doesn't work if target path contains home tilde
~.Edit: I just opened an Issue and suggested Riey to release a new version, see Riey/fontconfig-parser#6, after that we can continue our work of integration with fontdb.
AlexD97 commentedon Jul 31, 2023
As I've already commented, more fonts are recognized with your fork. More specifically, the fonts that are installed “directly” through nix are recognized. The fonts that are managed by home-manager are not detected (I have the
fonts.fontconfig.enableoption set totrue).7sDream commentedon Aug 10, 2023
@AlexD97 Although I often hear about NixOS, I haven't used it in depth, and I don't know much about some of its concepts. I think we can make the fontconfig configuration works in "normal" cases. If this is not enough for NixOS, we can add support for it on top of this.
BTW, fontconfig-parser has bumped a new version 0.5.3, so I think the fontdb integration work can be started. I can do this but my day job will be busy until september, so anyone can take over it if interested :)
7sDream commentedon Oct 23, 2023
Finally found some time to finish this, just updated my code, and there is PR #2472.
Works fine on my macOS machine*, If anyone interedted in fontconfig support, you can install and test it by:
cargo install typst-cli --git https://github.com/7sDream/typst --branch cli/fontdbCC @AlexD97 @KiruyaMomochi @laurmaedje
*: actually it finds more font then using current main branch, this is the diff
laurmaedje commentedon Oct 23, 2023
Nice, that's a surprisingly simple change. And the existing code was adapted from
fontdbanyway.AlexD97 commentedon Oct 23, 2023
@7sDream Thanks! The fork works nicely on my machine (now, even fonts that are installed through home-manager are recognized).
drupol commentedon Oct 23, 2023
Can't wait for this to be comitted!
laurmaedje commentedon Oct 25, 2023
Fontconfig support has landed: #2472