fontutils.scm 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Andrew Tropin <andrew@trop.in>
  3. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  4. ;;; Copyright © 2023 Giacomo Leidi <goodoldpaul@autistici.org>
  5. ;;; Copyright © 2023 Andrew Patterson <andrewpatt7@gmail.com>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (gnu home services fontutils)
  22. #:use-module (gnu home services)
  23. #:use-module (gnu packages fontutils)
  24. #:use-module (guix gexp)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (ice-9 match)
  27. #:use-module (sxml simple)
  28. #:export (home-fontconfig-service-type))
  29. ;;; Commentary:
  30. ;;;
  31. ;;; Services related to fonts. home-fontconfig service provides
  32. ;;; fontconfig configuration, which allows fc-* utilities to find
  33. ;;; fonts in Guix Home's profile and regenerates font cache on
  34. ;;; activation.
  35. ;;;
  36. ;;; Code:
  37. (define (write-fontconfig-doctype)
  38. "Prints fontconfig's DOCTYPE to current-output-port."
  39. ;; This is necessary because SXML doesn't seem to have a way to represent a doctype,
  40. ;; but sxml->xml /does/ currently call any thunks in the SXML with the XML output port
  41. ;; as current-output-port, allowing the output to include arbitrary text instead of
  42. ;; just properly quoted XML.
  43. (format #t "<!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>"))
  44. (define (config->sxml config)
  45. "Converts a <home-fontconfig-configuration> record into the SXML representation
  46. of fontconfig's fonts.conf file."
  47. (define (snippets->sxml snippet)
  48. (match snippet
  49. ((or (? string? dir)
  50. (? gexp? dir))
  51. `(dir ,dir))
  52. ((? list?)
  53. snippet)))
  54. `(*TOP* (*PI* xml "version='1.0'")
  55. ,write-fontconfig-doctype
  56. (fontconfig
  57. ,@(map snippets->sxml config))))
  58. (define (add-fontconfig-config-file config)
  59. `(("fontconfig/fonts.conf"
  60. ,(mixed-text-file
  61. "fonts.conf"
  62. (call-with-output-string
  63. (lambda (port)
  64. (sxml->xml (config->sxml config) port)))))))
  65. (define (regenerate-font-cache-gexp _)
  66. `(("profile/share/fonts"
  67. ,#~(system* #$(file-append fontconfig "/bin/fc-cache") "-fv"))))
  68. (define home-fontconfig-service-type
  69. (service-type (name 'home-fontconfig)
  70. (extensions
  71. (list (service-extension
  72. home-xdg-configuration-files-service-type
  73. add-fontconfig-config-file)
  74. (service-extension
  75. home-run-on-change-service-type
  76. regenerate-font-cache-gexp)
  77. (service-extension
  78. home-profile-service-type
  79. (const (list fontconfig)))))
  80. (compose concatenate)
  81. (extend append)
  82. (default-value '("~/.guix-home/profile/share/fonts"))
  83. (description
  84. "Provides configuration file for fontconfig and make
  85. fc-* utilities aware of font packages installed in Guix Home's profile.")))