X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=scm%2Foutput-svg.scm;h=9b1b250f77857a631b525bd90fa282b796b6e178;hb=b60f26d395a56e7fbfa272c35cd2e77dccb98ee9;hp=57c5e733a0b63cd9e8e6b0dd0459cd925280b26b;hpb=17463ba49a755dca5682ba18af450c198d94e050;p=lilypond.git diff --git a/scm/output-svg.scm b/scm/output-svg.scm index 57c5e733a0..9b1b250f77 100644 --- a/scm/output-svg.scm +++ b/scm/output-svg.scm @@ -19,10 +19,17 @@ (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)) @@ -114,10 +121,10 @@ (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 '()) @@ -285,11 +292,32 @@ (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 @@ -349,10 +377,64 @@ `(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 (music-string-to-path font size (car glyphs))) @@ -368,6 +450,44 @@ (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) "") @@ -397,7 +517,7 @@ 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* @@ -423,13 +543,27 @@ (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) @@ -481,6 +615,9 @@ (define (resetrotation ang x y) "\n") +(define (resetscale) + "\n") + (define (round-filled-box breapth width depth height blot-diameter) (entity 'rect "" @@ -508,6 +645,10 @@ (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))))) @@ -525,4 +666,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)))))