svg.scm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2015 Andy Wingo <wingo@igalia.com>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu build svg)
  20. #:use-module (srfi srfi-11)
  21. #:export (svg->png))
  22. ;; We need Guile-RSVG and Guile-Cairo. Load them lazily, at run time, to
  23. ;; allow compilation to proceed. See also <http://bugs.gnu.org/12202>.
  24. (module-autoload! (current-module)
  25. '(rsvg) '(rsvg-handle-new-from-file))
  26. (module-autoload! (current-module)
  27. '(cairo) '(cairo-image-surface-create))
  28. (define* (downscaled-surface surface
  29. #:key
  30. source-width source-height
  31. width height)
  32. "Return a new rendering context where SURFACE is scaled to WIDTH x HEIGHT."
  33. (let ((cr (cairo-create (cairo-image-surface-create 'argb32
  34. width height))))
  35. (cairo-scale cr (/ width source-width) (/ height source-height))
  36. (cairo-set-source-surface cr surface 0 0)
  37. (cairo-pattern-set-filter (cairo-get-source cr) 'best)
  38. (cairo-rectangle cr 0 0 source-width source-height)
  39. (cairo-fill cr)
  40. cr))
  41. (define* (svg->png in-svg out-png
  42. #:key width height)
  43. "Render the file at IN-SVG as a PNG file in OUT-PNG. When WIDTH and HEIGHT
  44. are provided, use them as the dimensions of OUT-PNG; otherwise preserve the
  45. dimensions of IN-SVG."
  46. (define svg
  47. (rsvg-handle-new-from-file in-svg))
  48. (let-values (((origin-width origin-height em ex)
  49. (rsvg-handle-get-dimensions svg)))
  50. (let* ((surf (cairo-image-surface-create 'argb32
  51. origin-width origin-height))
  52. (cr (cairo-create surf)))
  53. (rsvg-handle-render-cairo svg cr)
  54. (cairo-surface-flush surf)
  55. (let ((cr (if (and width height
  56. (not (= width origin-width))
  57. (not (= height origin-height)))
  58. (downscaled-surface surf
  59. #:source-width origin-width
  60. #:source-height origin-height
  61. #:width width
  62. #:height height)
  63. cr)))
  64. (cairo-surface-write-to-png (cairo-get-target cr) out-png)))))
  65. ;;; svg.scm ends here