X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=scm%2Foutput-svg.scm;h=c916067bdfa38986141752fb193873d6af62e54f;hb=794dcbdb52faf4292036cd1b0270a956cf4316a3;hp=5c6299a2794e1fc63e3077b63dd6b118e5c665ad;hpb=931a5e155845fb5b6207424dcf66967756a55540;p=lilypond.git diff --git a/scm/output-svg.scm b/scm/output-svg.scm index 5c6299a279..c916067bdf 100644 --- a/scm/output-svg.scm +++ b/scm/output-svg.scm @@ -1,43 +1,38 @@ -;;;; output-svg.scm -- implement Scheme output routines for SVG1 +;;;; output-svg.scm -- implement Scheme output routines for SVG ;;;; ;;;; source file of the GNU LilyPond music typesetter ;;;; ;;;; (c) 2002--2009 Jan Nieuwenhuizen +;;;; Patrick McCarty -;;;; http://www.w3.org/TR/SVG11 -;;;; http://www.w3.org/TR/SVG12/ -- page, pageSet in draft +;;;; Recommendations: +;;;; http://www.w3.org/TR/SVG11/ +;;;; http://www.w3.org/TR/SVGTiny12/ +;;;; http://www.w3.org/TR/SVGPrint/ -- 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 (define-module (scm output-svg)) (define this-module (current-module)) (use-modules - (guile) - (ice-9 regex) - (ice-9 format) - (lily) - (srfi srfi-1) - (srfi srfi-13)) + (guile) + (ice-9 regex) + (ice-9 format) + (lily) + (srfi srfi-1) + (srfi srfi-13)) (define fancy-format format) (define format ergonomic-simple-format) -(define lily-unit-length 1.75) +(define lily-unit-length 1.7573) (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)) @@ -48,7 +43,12 @@ ;; Helper functions (define-public (attributes attributes-alist) (apply string-append - (map (lambda (x) (format " ~s=\"~a\"" (car x) (cdr x))) + (map (lambda (x) + (let ((attr (car x)) + (value (cdr x))) + (if (number? value) + (set! value (ly:format "~4f" value))) + (format " ~s=\"~a\"" attr value))) attributes-alist))) (define-public (eo entity . attributes-alist) @@ -63,36 +63,36 @@ "c = close" (format "\n" entity)) - +(define-public (comment s) + (string-append "\n")) (define-public (entity entity string . attributes-alist) (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) - (format " ~S,~S" (car o) (- (cdr o)))) + (ly:format "~4f ~4f" (car o) (- (cdr o)))) (define (number-list->point lst) (define (helper lst) (if (null? lst) '() - (cons (format "~S,~S" (car lst) (cadr lst)) + (cons (format "~S ~S" (car lst) (- (cadr lst))) (helper (cddr lst))))) - (string-join (helper lst) " ")) + (string-join (helper lst) " ")) (define (svg-bezier lst close) (let* ((c0 (car (list-tail lst 3))) (c123 (list-head lst 3))) (string-append - (if (not close) "M " "L ") - (offset->point c0) - "C " (apply string-append (map offset->point c123)) - (if (not close) "" (string-append - "L " (offset->point close)))))) + (if (not close) "M" "L") + (offset->point c0) + "C" (string-join (map offset->point c123) " ") + (if (not close) "" "z")))) (define (sqr x) (* x x)) @@ -108,7 +108,10 @@ (map (lambda (x) (char->entity x)) (string->list string)))) (define svg-element-regexp - (make-regexp "^(<[a-z]+) (.*>)")) + (make-regexp "^(<[a-z]+) ?(.*>)")) + +(define scaled-element-regexp + (make-regexp "^(<[a-z]+ transform=\")(scale.[-0-9. ]+,[-0-9. ]+.\" .*>)")) (define pango-description-regexp-comma (make-regexp ",( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$")) @@ -116,7 +119,7 @@ (define pango-description-regexp-nocomma (make-regexp "( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$")) -(define (pango-description-to-svg-font str expr) +(define (pango-description-to-text str expr) (define alist '()) (define (set-attribute attr val) (set! alist (assoc-set! alist attr val))) @@ -145,51 +148,158 @@ (apply entity 'text expr (reverse! alist)))) -(define (font-smob-to-svg-font font expr) - (let ((name-style (font-name-style font)) - (size (modified-font-metric-font-scaling font))) +(define (dump-path path scale . rest) + (define alist '()) + (define (set-attribute attr val) + (set! alist (assoc-set! alist attr val))) + (if (not (null? rest)) + (let* ((dx (car rest)) + (dy (cadr rest)) + (total-x (+ dx next-horiz-adv))) + (if (or (not (zero? total-x)) + (not (zero? dy))) + (let ((x (ly:format "~4f" total-x)) + (y (ly:format "~4f" dy))) + (set-attribute 'transform + (string-append + "translate(" x ", " y ") " + "scale(" scale ", -" scale ")"))) + (set-attribute 'transform + (string-append + "scale(" scale ", -" scale ")")))) + (set-attribute 'transform (string-append + "scale(" scale ", -" scale ")"))) + + (set-attribute 'd path) + (set-attribute 'fill "currentColor") + (apply entity 'path "" (reverse alist))) + + +;; A global variable for keeping track of the *cumulative* +;; horizontal advance for glyph strings, but only if there +;; is more than one glyph. +(define next-horiz-adv 0.0) + +;; Matches the required "unicode" attribute from +(define glyph-unicode-value-regexp + (make-regexp "unicode=\"([^\"]+)\"")) + +;; Matches the optional path data from +(define glyph-path-regexp + (make-regexp "d=\"([-MmZzLlHhVvCcSsQqTt0-9.\n ]*)\"")) + +;; Matches a complete element with the glyph-name +;; attribute value of NAME. For example: +;; +;; +;; +;; TODO: it would be better to use an XML library to extract +;; the glyphs instead, and store them in a hash table. --pmccarty +;; +(define (glyph-element-regexp name) + (make-regexp (string-append ""))) + +(define (extract-glyph all-glyphs name size . rest) + (let* ((new-name (regexp-quote name)) + (regexp (regexp-exec (glyph-element-regexp new-name) all-glyphs)) + (glyph (match:substring regexp)) + (unicode-attr (regexp-exec glyph-unicode-value-regexp glyph)) + (unicode-attr-value (match:substring unicode-attr 1)) + (unicode-attr? (regexp-match? unicode-attr)) + (d-attr (regexp-exec glyph-path-regexp glyph)) + (d-attr-value "") + (d-attr? (regexp-match? d-attr)) + ;; TODO: not urgent, but do not hardcode this value + (units-per-em 1000) + (font-scale (ly:format "~4f" (/ size units-per-em))) + (path "")) + + (if (and unicode-attr? (not unicode-attr-value)) + (ly:warning (_ "Glyph must have a unicode value"))) + + (if d-attr? (set! d-attr-value (match:substring d-attr 1))) + + (cond ( + ;; Glyph-strings with path data + (and d-attr? (not (null? rest))) + (begin + (set! path (apply dump-path d-attr-value + font-scale + (list (cadr rest) (caddr rest)))) + (set! next-horiz-adv (+ next-horiz-adv + (car rest))) + path)) + ;; Glyph-strings without path data ("space") + ((and (not d-attr?) (not (null? rest))) + (begin + (set! next-horiz-adv (+ next-horiz-adv + (car rest))) + "")) + ;; Font smobs with path data + ((and d-attr? (null? rest)) + (set! path (dump-path d-attr-value font-scale)) + path) + ;; Font smobs without path data ("space") + (else + "")))) + +(define (extract-glyph-info all-glyphs glyph size) + (let* ((offsets (list-head glyph 3)) + (glyph-name (car (reverse glyph)))) + (apply extract-glyph all-glyphs glyph-name size offsets))) + +(define (svg-defs svg-font) + (let ((start (string-contains svg-font "")) + (end (string-contains svg-font ""))) + (substring svg-font (+ start 7) (- end 1)))) + +(define (cache-font svg-font size glyph) + (let ((all-glyphs (svg-defs (cached-file-contents svg-font)))) + (if (list? glyph) + (extract-glyph-info all-glyphs glyph size) + (extract-glyph all-glyphs glyph size)))) + + +(define (feta-alphabet-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")))) + + (if font-file + (cache-font font-file scaled-size glyph) + (ly:warning (_ "cannot find SVG font ~S") font-file)))) + + +(define (font-smob-to-path font glyph) + (let* ((name-style (font-name-style font)) + (scaled-size (modified-font-metric-font-scaling font)) + (font-file (ly:find-file (string-append name-style ".svg")))) + + (if font-file + (cache-font font-file scaled-size glyph) + (ly:warning (_ "cannot find SVG font ~S") font-file)))) - (entity 'text expr - ;; FIXME: The cdr of `name-style' cannot select the - ;; correct SVG font, so we ignore this information for now - `(font-family . ,(car name-style)) - `(font-size . ,size) - '(text-anchor . "start")))) (define (fontify font expr) (if (string? font) - (pango-description-to-svg-font font expr) - (font-smob-to-svg-font font expr))) + (pango-description-to-text font expr) + (font-smob-to-path font expr))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; stencil outputters ;;; - -(define (url-link url x y) - (string-append - (eo 'a `(xlink:href . ,url)) - (eoc 'rect - `(x . ,(car x)) - `(y . ,(car y)) - `(width . ,(- (cdr x) (car x))) - `(height . ,(- (cdr y) (car y))) - '(fill . "none") - '(stroke . "none") - '(stroke-width . "0.0")) - (ec 'a))) - -(define (grob-cause offset grob) - "") - -(define (no-origin) - "") - - - (define (bezier-sandwich lst thick) (let* ((first (list-tail lst 4)) - (first-c0 (car (list-tail first 3))) (second (list-head lst 4))) (entity 'path "" '(stroke-linejoin . "round") @@ -198,53 +308,27 @@ '(fill . "currentColor") `(stroke-width . ,thick) `(d . ,(string-append (svg-bezier first #f) - (svg-bezier second first-c0))) - ))) + (svg-bezier second #t)))))) -(define (path thick commands) - (define (convert-path-exps exps) - (if (pair? exps) - (let* - ((head (car exps)) - (rest (cdr exps)) - (arity - (cond - ((memq head '(rmoveto rlineto lineto moveto)) 2) - ((memq head '(rcurveto curveto)) 6) - (else 1))) - (args (take rest arity)) - (svg-head (assoc-get head '((rmoveto . m) - (rcurveto . c) - (curveto . C) - (moveto . M) - (lineto . L) - (rlineto . l)) - "")) - ) - - (cons (format "~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 . ,(string-join (convert-path-exps commands) " ")))) - (define (char font i) (dispatch `(fontify ,font ,(entity 'tspan (char->entity (integer->char i)))))) -(define-public (comment s) - (string-append "\n")) +(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))) + +(define (dashed-line thick on off dx dy phase) + (draw-line thick 0 0 dx dy + `(stroke-dasharray . ,(format "~a,~a" on off)))) (define (draw-line thick x1 y1 x2 y2 . alist) - (apply entity 'line "" (append `((stroke-linejoin . "round") @@ -257,28 +341,121 @@ (y2 . ,(- y2))) alist))) -(define (dashed-line thick on off dx dy phase) - (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off)))) +(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))) + +(define (embedded-svg string) + string) + +(define (glyph-string font size cid glyphs) + (define path "") + (if (= 1 (length glyphs)) + (set! path (feta-alphabet-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)) + glyphs) + "\n") + (ec 'g))))) + (set! next-horiz-adv 0.0) + path) + +(define (grob-cause offset grob) + "") (define (named-glyph font name) - (dispatch - `(fontify ,font ,(entity 'tspan - (integer->entity - (ly:font-glyph-name-to-charcode font name)))))) + (dispatch `(fontify ,font ,name))) + +(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 (convert-path-exps exps) + (if (pair? exps) + (let* + ((head (car exps)) + (rest (cdr exps)) + (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 + '((rmoveto . m) + (rcurveto . c) + (curveto . C) + (moveto . M) + (lineto . L) + (rlineto . l) + (closepath . z)) + ""))) + + (cons (format "~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))))) (define (placebox x y expr) - (let* - ((match (regexp-exec svg-element-regexp expr)) - (tagname (match:substring match 1)) - (attributes (match:substring match 2))) - - (string-append tagname - ;; FIXME: Not using GNU coding standards - ;; [translate ()] here to work around a - ;; bug in Microsoft Internet Explorer 6.0 - (ly:format " transform=\"translate(~f, ~f)\" " x (- y)) - attributes - "\n"))) + (if (string-null? expr) + "" + (let* + ((normal-element (regexp-exec svg-element-regexp expr)) + (scaled-element (regexp-exec scaled-element-regexp expr)) + (scaled? (if scaled-element #t #f)) + (match (if scaled? scaled-element normal-element)) + (string1 (match:substring match 1)) + (string2 (match:substring match 2))) + + (if scaled? + (string-append string1 + (ly:format "translate(~4f, ~4f) " x (- y)) + string2 + "\n") + (string-append string1 + (ly:format " transform=\"translate(~4f, ~4f)\" " + x (- y)) + string2 + "\n"))))) (define (polygon coords blot-diameter is-filled) (entity @@ -289,15 +466,21 @@ `(fill . ,(if is-filled "currentColor" "none")) '(stroke . "currentColor") `(points . ,(string-join - (map offset->point (ly:list->offsets '() coords)))) - )) + (map offset->point (ly:list->offsets '() coords)))))) -;; rotate around given point -(define (setrotation ang x y) - (format "\n" - (number->string (* -1 ang)) - (number->string x) - (number->string (* -1 y)))) +(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)))))) + +(define (resetcolor) + "\n") (define (resetrotation ang x y) "\n") @@ -317,63 +500,32 @@ `(width . ,(+ breapth width)) `(height . ,(+ depth height)) `(ry . ,(/ blot-diameter 2)) - '(fill . "currentColor") - )) - -(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))) + '(fill . "currentColor"))) -(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))) +(define (setcolor r g b) + (format "\n" + (* 100 r) (* 100 g) (* 100 b))) -(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,~4f C~4f,~4f ~4f,~4f ~4f,~4f S~4f,~4f ~4f,~4f" - x-max 0 - x-max y-max - x-min y-max - x-min 0 - x-max y-min - x-max 0))))) +;; rotate around given point +(define (setrotation ang x y) + (ly:format "\n" + (- ang) x (- y))) (define (text font string) (dispatch `(fontify ,font ,(entity 'tspan (string->entities string))))) +(define (url-link url x y) + (string-append + (eo 'a `(xlink:href . ,url)) + (eoc 'rect + `(x . ,(car x)) + `(y . ,(car y)) + `(width . ,(- (cdr x) (car x))) + `(height . ,(- (cdr y) (car y))) + '(fill . "none") + '(stroke . "none") + '(stroke-width . "0.0")) + (ec 'a))) + (define (utf-8-string pango-font-description string) (dispatch `(fontify ,pango-font-description ,(entity 'tspan string)))) - -(define (embedded-svg string) - string) - -(define (setcolor r g b) - (format "\n" - (* 100 r) (* 100 g) (* 100 b) - )) - -(define (resetcolor) - "\n")