X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=scm%2Foutput-svg.scm;h=9f10629eacf7a6f2a3f4fc913aaa5ff3ee9a65a3;hb=3772b2c7d022122eb9c791bd4a32178e76b97014;hp=6076bdaa72ac0b981d986664c6671d4951295a40;hpb=7ef4ccd3629c2068c8ad70ccd0ea2b2bcd4a6cca;p=lilypond.git diff --git a/scm/output-svg.scm b/scm/output-svg.scm index 6076bdaa72..9f10629eac 100644 --- a/scm/output-svg.scm +++ b/scm/output-svg.scm @@ -1,29 +1,35 @@ -;;;; output-svg.scm -- implement Scheme output routines for SVG1 +;;;; This file is part of LilyPond, the GNU music typesetter. ;;;; -;;;; source file of the GNU LilyPond music typesetter +;;;; Copyright (C) 2002--2012 Jan Nieuwenhuizen +;;;; Patrick McCarty ;;;; -;;;; (c) 2002--2009 Jan Nieuwenhuizen - -;;;; http://www.w3.org/TR/SVG11 -;;;; http://www.w3.org/TR/SVG12/ -- page, pageSet in draft - -;;;; TODO: -;;;; * .cff MUST NOT be in fc's fontpath. -;;;; - workaround: remove mf/out from ~/.fonts.conf, -;;;; instead add ~/.fonts and symlink all /mf/out/*otf there. -;;;; - bug in fontconfig/freetype/pango? - -;;;; * inkscape page/pageSet support -;;;; * inkscape SVG-font support -;;;; - use fontconfig/fc-cache for now, see output-gnome.scm +;;;; LilyPond is free software: you can redistribute it and/or modify +;;;; it under the terms of the GNU General Public License as published by +;;;; the Free Software Foundation, either version 3 of the License, or +;;;; (at your option) any later version. +;;;; +;;;; LilyPond is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;;; GNU General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU General Public License +;;;; along with LilyPond. If not, see . (define-module (scm output-svg)) (define this-module (current-module)) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; globals + +;;; set by framework-gnome.scm +(define paper #f) + (use-modules (guile) (ice-9 regex) (ice-9 format) + (ice-9 optargs) (lily) (srfi srfi-1) (srfi srfi-13)) @@ -35,15 +41,11 @@ (define (dispatch expr) (let ((keyword (car expr))) - (cond - ((eq? keyword 'some-func) "") - ;;((eq? keyword 'placebox) (dispatch (cadddr expr))) - (else - (if (module-defined? this-module keyword) - (apply (eval keyword this-module) (cdr expr)) - (begin - (ly:warning (_ "undefined: ~S") keyword) - "")))))) + (cond ((eq? keyword 'some-func) "") + (else (if (module-defined? this-module keyword) + (apply (eval keyword this-module) (cdr expr)) + (begin (ly:warning (_ "undefined: ~S") keyword) + "")))))) ;; Helper functions (define-public (attributes attributes-alist) @@ -53,20 +55,26 @@ (value (cdr x))) (if (number? value) (set! value (ly:format "~4f" value))) - (format " ~s=\"~a\"" attr value))) + (format #f " ~s=\"~a\"" attr value))) attributes-alist))) (define-public (eo entity . attributes-alist) "o = open" - (format "<~S~a>\n" entity (attributes attributes-alist))) + (format #f "<~S~a>\n" entity (attributes attributes-alist))) (define-public (eoc entity . attributes-alist) - " oc = open/close" - (format "<~S~a/>\n" entity (attributes attributes-alist))) + "oc = open/close" + (format #f "<~S~a/>\n" entity (attributes attributes-alist))) (define-public (ec entity) "c = close" - (format "\n" entity)) + (format #f "\n" entity)) + +(define (start-enclosing-id-node s) + (string-append "\n")) + +(define (end-enclosing-id-node) + "\n") (define-public (comment s) (string-append "\n")) @@ -75,7 +83,7 @@ (if (equal? string "") (apply eoc entity attributes-alist) (string-append - (apply eo (cons entity attributes-alist)) string (ec entity)))) + (apply eo (cons entity attributes-alist)) string (ec entity)))) (define (offset->point o) (ly:format "~4f ~4f" (car o) (- (cdr o)))) @@ -84,7 +92,7 @@ (define (helper lst) (if (null? lst) '() - (cons (format "~S ~S" (car lst) (- (cadr lst))) + (cons (format #f "~S ~S" (car lst) (- (cadr lst))) (helper (cddr lst))))) (string-join (helper lst) " ")) @@ -119,21 +127,18 @@ (make-regexp "^(<[a-z]+ transform=\")(scale.[-0-9. ]+,[-0-9. ]+.\" .*>)")) (define pango-description-regexp-comma - (make-regexp ",( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$")) + (make-regexp ",( Bold)?( Italic)?( Small-Caps)?[ -]([0-9.]+)$")) (define pango-description-regexp-nocomma - (make-regexp "( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$")) + (make-regexp "( Bold)?( Italic)?( Small-Caps)?[ -]([0-9.]+)$")) (define (pango-description-to-text str expr) (define alist '()) (define (set-attribute attr val) (set! alist (assoc-set! alist attr val))) - (let* - ((match-1 (regexp-exec pango-description-regexp-comma str)) - (match-2 (regexp-exec pango-description-regexp-nocomma str)) - (match (if match-1 - match-1 - match-2))) + (let* ((match-1 (regexp-exec pango-description-regexp-comma str)) + (match-2 (regexp-exec pango-description-regexp-nocomma str)) + (match (if match-1 match-1 match-2))) (if (regexp-match? match) (begin @@ -205,12 +210,12 @@ ;; (define (glyph-element-regexp name) (make-regexp (string-append ""))) (define (extract-glyph all-glyphs name size . rest) @@ -274,7 +279,7 @@ (extract-glyph all-glyphs glyph size)))) -(define (feta-alphabet-to-path font size glyph) +(define (music-string-to-path font size glyph) (let* ((name-style (font-name-style font)) (scaled-size (/ size lily-unit-length)) (font-file (ly:find-file (string-append name-style ".svg")))) @@ -293,89 +298,190 @@ (cache-font font-file scaled-size glyph) (ly:warning (_ "cannot find SVG font ~S") font-file)))) +(define (woff-font-smob-to-text font expr) + (let* ((name-style (font-name-style font)) + (scaled-size (modified-font-metric-font-scaling font)) + (font-file (ly:find-file (string-append name-style ".woff"))) + (charcode (ly:font-glyph-name-to-charcode font expr)) + (char-lookup (format #f "&#~S;" charcode)) + (glyph-by-name (eoc 'altglyph `(glyphname . ,expr))) + (apparently-broken + (comment "FIXME: how to select glyph by name, altglyph is broken?")) + (text (string-regexp-substitute "\n" "" + (string-append glyph-by-name apparently-broken char-lookup)))) + (define alist '()) + (define (set-attribute attr val) + (set! alist (assoc-set! alist attr val))) + (set-attribute 'font-family name-style) + (set-attribute 'font-size scaled-size) + (apply entity 'text text (reverse! alist)))) + +(define font-smob-to-text + (if (not (ly:get-option 'svg-woff)) + font-smob-to-path woff-font-smob-to-text)) (define (fontify font expr) (if (string? font) (pango-description-to-text font expr) - (font-smob-to-path font expr))) + (font-smob-to-text font expr))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; stencil outputters ;;; -(define (bezier-sandwich lst thick) - (let* ((first (list-tail lst 4)) - (second (list-head lst 4))) - (entity 'path "" - '(stroke-linejoin . "round") - '(stroke-linecap . "round") - '(stroke . "currentColor") - '(fill . "currentColor") - `(stroke-width . ,thick) - `(d . ,(string-append (svg-bezier first #f) - (svg-bezier second #t))) - ))) - (define (char font i) (dispatch `(fontify ,font ,(entity 'tspan (char->entity (integer->char i)))))) (define (circle radius thick is-filled) (entity - 'circle "" - '(stroke-linejoin . "round") - '(stroke-linecap . "round") - `(fill . ,(if is-filled "currentColor" "none")) - `(stroke . "currentColor") - `(stroke-width . ,thick) - `(r . ,radius))) + 'circle "" + '(stroke-linejoin . "round") + '(stroke-linecap . "round") + `(fill . ,(if is-filled "currentColor" "none")) + `(stroke . "currentColor") + `(stroke-width . ,thick) + `(r . ,radius))) (define (dashed-line thick on off dx dy phase) - (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off)))) + (draw-line thick 0 0 dx dy + `(stroke-dasharray . ,(format #f "~a,~a" on off)))) (define (draw-line thick x1 y1 x2 y2 . alist) (apply entity 'line "" (append - `((stroke-linejoin . "round") - (stroke-linecap . "round") - (stroke-width . ,thick) - (stroke . "currentColor") - (x1 . ,x1) - (y1 . ,(- y1)) - (x2 . ,x2) - (y2 . ,(- y2))) - alist))) + `((stroke-linejoin . "round") + (stroke-linecap . "round") + (stroke-width . ,thick) + (stroke . "currentColor") + (x1 . ,x1) + (y1 . ,(- y1)) + (x2 . ,x2) + (y2 . ,(- y2))) + alist))) (define (ellipse x-radius y-radius thick is-filled) (entity - 'ellipse "" - '(stroke-linejoin . "round") - '(stroke-linecap . "round") - `(fill . ,(if is-filled "currentColor" "none")) - `(stroke . "currentColor") - `(stroke-width . ,thick) - `(rx . ,x-radius) - `(ry . ,y-radius))) + 'ellipse "" + '(stroke-linejoin . "round") + '(stroke-linecap . "round") + `(fill . ,(if is-filled "currentColor" "none")) + `(stroke . "currentColor") + `(stroke-width . ,thick) + `(rx . ,x-radius) + `(ry . ,y-radius))) + +(define (partial-ellipse x-radius y-radius start-angle end-angle thick connect fill) + (define (make-ellipse-radius x-radius y-radius angle) + (/ (* x-radius y-radius) + (sqrt (+ (* (* y-radius y-radius) + (* (cos angle) (cos angle))) + (* (* x-radius x-radius) + (* (sin angle) (sin angle))))))) + (let* + ((new-start-angle (* PI-OVER-180 (angle-0-360 start-angle))) + (start-radius (make-ellipse-radius x-radius y-radius new-start-angle)) + (new-end-angle (* PI-OVER-180 (angle-0-360 end-angle))) + (end-radius (make-ellipse-radius x-radius y-radius new-end-angle)) + (epsilon 1.5e-3) + (x-end (- (* end-radius (cos new-end-angle)) + (* start-radius (cos new-start-angle)))) + (y-end (- (* end-radius (sin new-end-angle)) + (* start-radius (sin new-start-angle))))) + (if (and (< (abs x-end) epsilon) (< (abs y-end) epsilon)) + (entity + 'ellipse "" + `(fill . ,(if fill "currentColor" "none")) + `(stroke . "currentColor") + `(stroke-width . ,thick) + '(stroke-linejoin . "round") + '(stroke-linecap . "round") + '(cx . 0) + '(cy . 0) + `(rx . ,x-radius) + `(ry . ,y-radius)) + (entity + 'path "" + `(fill . ,(if fill "currentColor" "none")) + `(stroke . "currentColor") + `(stroke-width . ,thick) + '(stroke-linejoin . "round") + '(stroke-linecap . "round") + (cons + 'd + (string-append + (ly:format + "M~4f ~4fA~4f ~4f 0 ~4f 0 ~4f ~4f" + (* start-radius (cos new-start-angle)) + (- (* start-radius (sin new-start-angle))) + x-radius + y-radius + (if (> 0 (- new-start-angle new-end-angle)) 0 1) + (* end-radius (cos new-end-angle)) + (- (* end-radius (sin new-end-angle)))) + (if connect + (ly:format "L~4f,~4f" + (* start-radius (cos new-start-angle)) + (- (* start-radius (sin new-start-angle)))) + ""))))))) (define (embedded-svg string) string) -(define (glyph-string font size cid glyphs) +(define (embedded-glyph-string font size cid glyphs) (define path "") (if (= 1 (length glyphs)) - (set! path (feta-alphabet-to-path font size (car glyphs))) + (set! path (music-string-to-path font size (car glyphs))) (begin (set! path (string-append (eo 'g) (string-join (map (lambda (x) - (feta-alphabet-to-path font size x)) + (music-string-to-path font size x)) glyphs) "\n") (ec 'g))))) (set! next-horiz-adv 0.0) path) +(define (woff-glyph-string font-name size cid? w-x-y-named-glyphs) + (let* ((name-style (font-name-style font-name)) + (family-designsize (regexp-exec (make-regexp "(.*)-([0-9]*)") + font-name)) + (family (if (regexp-match? family-designsize) + (match:substring family-designsize 1) + font-name)) + (design-size (if (regexp-match? family-designsize) + (match:substring family-designsize 2) + #f)) + (scaled-size (/ size lily-unit-length)) + (font (ly:paper-get-font paper `(((font-family . ,family) + ,(if design-size + `(design-size . design-size))))))) + (define (glyph-spec w x y g) + (let* ((charcode (ly:font-glyph-name-to-charcode font g)) + (char-lookup (format #f "&#~S;" charcode)) + (glyph-by-name (eoc 'altglyph `(glyphname . ,g))) + (apparently-broken + (comment "XFIXME: how to select glyph by name, altglyph is broken?"))) + ;; what is W? + (ly:format + "~a" + (if (or (> (abs x) 0.00001) + (> (abs y) 0.00001)) + (ly:format " transform=\"translate(~4f,~4f)\"" x y) + " ") + name-style scaled-size + (string-regexp-substitute + "\n" "" + (string-append glyph-by-name apparently-broken char-lookup))))) + + (string-join (map (lambda (x) (apply glyph-spec x)) + (reverse w-x-y-named-glyphs)) "\n"))) + +(define glyph-string + (if (not (ly:get-option 'svg-woff)) embedded-glyph-string woff-glyph-string)) + (define (grob-cause offset grob) "") @@ -385,27 +491,7 @@ (define (no-origin) "") -(define (oval x-radius y-radius thick is-filled) - (let ((x-max x-radius) - (x-min (- x-radius)) - (y-max y-radius) - (y-min (- y-radius))) - (entity - 'path "" - '(stroke-linejoin . "round") - '(stroke-linecap . "round") - `(fill . ,(if is-filled "currentColor" "none")) - `(stroke . "currentColor") - `(stroke-width . ,thick) - `(d . ,(ly:format "M~4f ~4fC~4f ~4f ~4f ~4f ~4f ~4fS~4f ~4f ~4f ~4fz" - x-max 0 - x-max y-max - x-min y-max - x-min 0 - x-max y-min - x-max 0))))) - -(define (path thick commands) +(define* (path thick commands #:optional (cap 'round) (join 'round) (fill? #f)) (define (convert-path-exps exps) (if (pair? exps) (let* @@ -414,6 +500,7 @@ (arity (cond ((memq head '(rmoveto rlineto lineto moveto)) 2) ((memq head '(rcurveto curveto)) 6) + ((eq? head 'closepath) 0) (else 1))) (args (take rest arity)) (svg-head (assoc-get head @@ -422,21 +509,35 @@ (curveto . C) (moveto . M) (lineto . L) - (rlineto . l)) + (rlineto . l) + (closepath . z)) ""))) - (cons (format "~a~a" - svg-head (number-list->point args)) + (cons (format #f "~a~a" svg-head (number-list->point args)) (convert-path-exps (drop rest arity)))) '())) - (entity 'path "" - `(stroke-width . ,thick) - '(stroke-linejoin . "round") - '(stroke-linecap . "round") - '(stroke . "currentColor") - '(fill . "none") - `(d . ,(apply string-append (convert-path-exps commands))))) + (let* ((line-cap-styles '(butt round square)) + (line-join-styles '(miter round bevel)) + (cap-style (if (not (memv cap line-cap-styles)) + (begin + (ly:warning (_ "unknown line-cap-style: ~S") + (symbol->string cap)) + 'round) + cap)) + (join-style (if (not (memv join line-join-styles)) + (begin + (ly:warning (_ "unknown line-join-style: ~S") + (symbol->string join)) + 'round) + join))) + (entity 'path "" + `(stroke-width . ,thick) + `(stroke-linejoin . ,(symbol->string join-style)) + `(stroke-linecap . ,(symbol->string cap-style)) + '(stroke . "currentColor") + `(fill . ,(if fill? "currentColor" "none")) + `(d . ,(apply string-append (convert-path-exps commands)))))) (define (placebox x y expr) (if (string-null? expr) @@ -462,25 +563,14 @@ (define (polygon coords blot-diameter is-filled) (entity - 'polygon "" - '(stroke-linejoin . "round") - '(stroke-linecap . "round") - `(stroke-width . ,blot-diameter) - `(fill . ,(if is-filled "currentColor" "none")) - '(stroke . "currentColor") - `(points . ,(string-join - (map offset->point (ly:list->offsets '() coords)))))) - -(define (repeat-slash width slope thickness) - (define (euclidean-length x y) - (sqrt (+ (* x x) (* y y)))) - (let* ((x-width (euclidean-length thickness (/ thickness slope))) - (height (* width slope))) - (entity - 'path "" - '(fill . "currentColor") - `(d . ,(ly:format "M0 0l~4f 0 ~4f ~4f ~4f 0z" - x-width width (- height) (- x-width)))))) + 'polygon "" + '(stroke-linejoin . "round") + '(stroke-linecap . "round") + `(stroke-width . ,blot-diameter) + `(fill . ,(if is-filled "currentColor" "none")) + '(stroke . "currentColor") + `(points . ,(string-join + (map offset->point (ly:list->offsets '() coords)))))) (define (resetcolor) "\n") @@ -488,35 +578,39 @@ (define (resetrotation ang x y) "\n") +(define (resetscale) + "\n") + (define (round-filled-box breapth width depth height blot-diameter) - (entity 'rect "" - ;; The stroke will stick out. To use stroke, - ;; the stroke-width must be subtracted from all other dimensions. - ;;'(stroke-linejoin . "round") - ;;'(stroke-linecap . "round") - ;;`(stroke-width . ,blot) - ;;'(stroke . "red") - ;;'(fill . "orange") - - `(x . ,(- breapth)) - `(y . ,(- height)) - `(width . ,(+ breapth width)) - `(height . ,(+ depth height)) - `(ry . ,(/ blot-diameter 2)) - '(fill . "currentColor") - )) + (entity + 'rect "" + ;; The stroke will stick out. To use stroke, + ;; the stroke-width must be subtracted from all other dimensions. + ;;'(stroke-linejoin . "round") + ;;'(stroke-linecap . "round") + ;;`(stroke-width . ,blot) + ;;'(stroke . "red") + ;;'(fill . "orange") + + `(x . ,(- breapth)) + `(y . ,(- height)) + `(width . ,(+ breapth width)) + `(height . ,(+ depth height)) + `(ry . ,(/ blot-diameter 2)) + '(fill . "currentColor"))) (define (setcolor r g b) - (format "\n" - (* 100 r) (* 100 g) (* 100 b) - )) + (format #f "\n" + (* 100 r) (* 100 g) (* 100 b))) ;; rotate around given point (define (setrotation ang x y) - (format "\n" - (number->string (* -1 ang)) - (number->string x) - (number->string (* -1 y)))) + (ly:format "\n" + (- ang) x (- y))) + +(define (setscale x y) + (ly:format "\n" + x y)) (define (text font string) (dispatch `(fontify ,font ,(entity 'tspan (string->entities string))))) @@ -535,4 +629,8 @@ (ec 'a))) (define (utf-8-string pango-font-description string) - (dispatch `(fontify ,pango-font-description ,(entity 'tspan string)))) + (let ((escaped-string (string-regexp-substitute + "<" "<" + (string-regexp-substitute "&" "&" string)))) + (dispatch `(fontify ,pango-font-description + ,(entity 'tspan escaped-string)))))