Skip to content

Support fontconfig and/or specifying multiple custom font dirs #100

Closed

Description

@GoldsteinE

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.

Probably related to #97.

Activity

  1. 7sDream commented on Mar 24, 2023

    @7sDream
    Contributor

    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.

  2. drupol commented on Mar 24, 2023

    @drupol
    Contributor

    On my side, I have the variable FONTCONFIG_FILE set with custom fonts, but typst --fonts doesn't return anything.

  3. 7sDream commented on Mar 24, 2023

    @7sDream
    Contributor

    @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.

  4. laurmaedje commented on Mar 24, 2023

    @laurmaedje
    Member

    Interesting that fontdb supports this now. Last time I checked it did pretty much what Typst does now.

  5. laurmaedje commented on Mar 24, 2023

    @laurmaedje
    Member

    Using fontdb seems reasonable to me, in principle. Haven't looked at the details though.

  6. 7sDream commented on Mar 24, 2023

    @7sDream
    Contributor

    looks worth trying, maybe we can merge #270 first, and then I can continue work on fontdb intergation from here?

  7. KiruyaMomochi commented on Mar 26, 2023

    @KiruyaMomochi
    Contributor

    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.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.

    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

    • 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.

  8. 7sDream commented on Mar 26, 2023

    @7sDream
    Contributor

    @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?

  9. KiruyaMomochi commented on Mar 26, 2023

    @KiruyaMomochi
    Contributor

    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 our typst::font::book::FontInfo, their FaceInfo does not have coverage field.

    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

    1. fork their fontdb, then a) add a coverage to them or, b) replace their FaceInfo with our FontInfo.
    2. submit a PR to them.
    3. or use fontconfig_parser crate directly.
  10. 7sDream commented on Mar 26, 2023

    @7sDream
    Contributor

    Yes, but the doubled call is just ttf-parse::RawFace::parse, which should be pretty cheap, IIRC.

  11. 7sDream commented on Mar 26, 2023

    @7sDream
    Contributor

    @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/fontdb

  12. KiruyaMomochi commented on Mar 26, 2023

    @KiruyaMomochi
    Contributor

    It works for me too.

    Screenshot

    Screenshot_20230326_174421

  13. 7sDream commented on Mar 26, 2023

    @7sDream
    Contributor

    @KiruyaMomochi And custom font path in your config file automaticlly works too?

  14. 7sDream commented on Mar 26, 2023

    @7sDream
    Contributor

    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.

  15. added
    feature request
    ide
    on Apr 5, 2023
  16. Andrew15-5 commented on Jun 14, 2023

    @Andrew15-5
    Contributor

    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.

  17. AlexD97 commented on Jul 28, 2023

    @AlexD97

    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.

  18. 7sDream commented on Jul 31, 2023

    @7sDream
    Contributor

    @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.

  19. AlexD97 commented on Jul 31, 2023

    @AlexD97

    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).

  20. 7sDream commented on Aug 10, 2023

    @7sDream
    Contributor

    @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 :)

  21. 7sDream commented on Oct 23, 2023

    @7sDream
    Contributor

    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/fontdb

    CC @AlexD97 @KiruyaMomochi @laurmaedje

    *: actually it finds more font then using current main branch, this is the diff
    cargo run -- fonts --font-path ~/Cloud/Data/Fonts > fontdb.txt
    --- main.txt	2023-10-23 16:25:18
    +++ fontdb.txt	2023-10-23 16:24:22
    @@ -12,6 +12,8 @@
     Apple Chancery
     Apple Color Emoji
     Apple Color Emoji UI
    +Apple LiGothic
    +Apple LiSung
     Apple SD Gothic Neo
     Apple SD Gothic NeoI
     Apple Symbols
    @@ -34,11 +36,24 @@
     Baloo
     Bangla MN
     Bangla Sangam MN
    +Baoli SC
    +Baoli TC
     Baskerville
     Bebas Neue
     Beirut
     Beirut PUA
    +BiauKaiHK
    +BiauKaiTC
     Big Caslon
    +BIZ UDGothic
    +BIZ UDMincho
    +BM DoHyeon OTF
    +BM HANNA 11yrs old OTF
    +BM HANNA Air OTF
    +BM HANNA Pro OTF
    +BM JUA OTF
    +BM KIRANGHAERANG OTF
    +BM YEONSUNG OTF
     Bodoni 72
     Bodoni 72 Oldstyle
     Bodoni 72 Smallcaps
    @@ -113,11 +128,18 @@
     Grantha Sangam MN
     Gujarati MT
     Gujarati Sangam MN
    +GungSeo
     Gurmukhi MN
     Gurmukhi MT
     Gurmukhi Sangam MN
     HanaMinA
     HanaMinB
    +Hannotate SC
    +Hannotate TC
    +HanziPen SC
    +HanziPen TC
    +HeadLineA
    +Hei
     Heiti SC
     Heiti TC
     Helvetica
    @@ -133,6 +155,7 @@
     Hiragino Mincho Pro
     Hiragino Mincho ProN
     Hiragino Sans
    +Hiragino Sans CNS
     Hiragino Sans GB
     Hiragino Sans GB Interface
     Hoefler Text
    @@ -258,13 +281,17 @@
     JetBrainsMono Nerd Font Mono
     JetBrainsMonoExtraBold Nerd Font Mono
     JetBrainsMonoMedium Nerd Font Mono
    +Kai
     Kailasa
    +Kaiti SC
    +Kaiti TC
     Kannada MN
     Kannada Sangam MN
     Kefa
     Keyboard
     Khmer MN
     Khmer Sangam MN
    +Klee
     Knewave
     Kohinoor Bangla
     Kohinoor Devanagari
    @@ -275,13 +302,21 @@
     KufiStandardGK
     KufiStandardGK PUA
     Kumar One
    +Lantinghei SC
    +Lantinghei TC
     Lao MN
     Lao Sangam MN
     LastResort
     Leckerli One
     LG Smart_0521_HS98
     LG Smart_H
    +Libian SC
    +Libian TC
    +LiHei Pro
    +LingWai SC
    +LingWai TC
     Linux Libertine
    +LiSong Pro
     Lucida Grande
     Lucida Grande UI
     Luminari
    @@ -307,6 +342,10 @@
     Myanmar Sangam MN
     Nadeem
     Nadeem PUA
    +Nanum Brush Script
    +Nanum Pen Script
    +NanumGothic
    +NanumMyeongjo
     New Computer Modern
     New Computer Modern Math
     New Peninim MT
    @@ -443,18 +482,22 @@
     Orbitron
     Oriya MN
     Oriya Sangam MN
    +Osaka
     Pacifico
     Palatino
     Papyrus
     Party LET
     Patua One
    +PCMyungjo
     Petit Formal Script
     Phosphate
    +PilGi
     PingFang HK
     PingFang SC
     PingFang TC
     Plantagenet Cherokee
     Press Start 2P
    +PSL Ornanong Pro
     PT Mono
     PT Sans
     PT Sans Caption
    @@ -539,6 +582,8 @@
     Snell Roundhand
     Songti SC
     Songti TC
    +STFangsong
    +STHeiti
     STIX Two Math
     STIX Two Text
     STIXGeneral
    @@ -554,6 +599,7 @@
     STIXSizeThreeSym
     STIXSizeTwoSym
     STIXVariants
    +STKaiti
     STSong
     Sukhumvit Set
     Superclarendon
    @@ -568,8 +614,14 @@
     ThonburiUIWatch
     Times
     Times New Roman
    +Toppan Bunkyu Gothic
    +Toppan Bunkyu Midashi Gothic
    +Toppan Bunkyu Midashi Mincho
    +Toppan Bunkyu Mincho
     Trattatello
     Trebuchet MS
    +Tsukushi A Round Gothic
    +Tsukushi B Round Gothic
     TTTGB
     TW-Kai
     TW-Kai-Ext-B
    @@ -582,11 +634,26 @@
     Vast Shadow
     Verdana
     Waseem
    +Wawati SC
    +Wawati TC
     Webdings
    +Weibei SC
    +Weibei TC
     Wingdings
     Wingdings 2
     Wingdings 3
    +Xingkai SC
    +Xingkai TC
     Yellowtail
    +Yuanti SC
    +Yuanti TC
    +YuGothic
    +YuKyokasho
    +YuKyokasho Yoko
    +YuMincho
    +YuMincho +36p Kana
    +Yuppy SC
    +Yuppy TC
     Zapf Dingbats
     Zapfino
     更紗黑體 CL
    @@ -597,3 +664,4 @@
     等距更紗黑體 Slab CL
     等距更紗黑體 Slab TC
     等距更紗黑體 TC
    +簡宋
  22. laurmaedje commented on Oct 23, 2023

    @laurmaedje
    Member

    Nice, that's a surprisingly simple change. And the existing code was adapted from fontdb anyway.

  23. AlexD97 commented on Oct 23, 2023

    @AlexD97

    @7sDream Thanks! The fork works nicely on my machine (now, even fonts that are installed through home-manager are recognized).

  24. drupol commented on Oct 23, 2023

    @drupol
    Contributor

    Can't wait for this to be comitted!

  25. laurmaedje commented on Oct 25, 2023

    @laurmaedje
    Member

    Fontconfig support has landed: #2472

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 requestide

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions