]> git.donarmstrong.com Git - lilypond.git/blob - scm/flag-styles.scm
Add '-dcrop' option to ps and svg backends
[lilypond.git] / scm / flag-styles.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2008--2015 Reinhold Kainhofer <reinhold@kainhofer.com>
4 ;;;;
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
9 ;;;;
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
17
18 ;;;;  This file implements different flag styles in Scheme / GUILE, most
19 ;;;;  notably the old-straight-flag, the modern-straight-flag and the flat-flag
20 ;;;;  styles.
21
22
23 (define-public (no-flag grob)
24   "No flag: Simply return empty stencil."
25   empty-stencil)
26
27
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;;;;  Straight flags
30 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31
32
33 (define-public (add-stroke-straight stencil grob dir log stroke-style
34                                     offset length thickness stroke-thickness)
35   "Add the stroke for acciaccatura to the given flag stencil.
36 The stroke starts for up-flags at `upper-end-of-flag + (0,length/2)'
37 and ends at `(0, vertical-center-of-flag-end) - (flag-x-width/2,
38 flag-x-width + flag-thickness)'.  Here `length' is the
39 whole length, while `flag-x-width' is just the x-extent and thus depends on
40 the angle!  Other combinations don't look as good.
41
42 For down-stems the y-coordinates are simply mirrored."
43   (let* ((stem-grob (ly:grob-parent grob X))
44          (start (offset-add offset (cons 0  (* (/ length 2) dir))))
45          (end (offset-add (cons 0 (cdr offset))
46                           (cons (- (/ (car offset) 2)) (* (- (+ thickness (car offset))) dir))))
47          (stroke (make-line-stencil stroke-thickness (car start) (cdr start) (car end) (cdr end))))
48     (ly:stencil-add stencil stroke)))
49
50 (define (buildflag flag-stencil remain curr-stencil spacing)
51   "Internal function to recursively create a stencil with @code{remain} flags
52    from the single-flag stencil curr-stencil, which is already translated to
53    the position of the previous flag position."
54   (if (> remain 0)
55       (let* ((translated-stencil (ly:stencil-translate-axis curr-stencil spacing Y))
56              (new-stencil (ly:stencil-add flag-stencil translated-stencil)))
57         (buildflag new-stencil (- remain 1) translated-stencil spacing))
58       flag-stencil))
59
60 (define-public (straight-flag flag-thickness flag-spacing
61                               upflag-angle upflag-length
62                               downflag-angle downflag-length)
63   "Create a stencil for a straight flag.  @var{flag-thickness} and
64 @var{flag-spacing} are given in staff spaces, @var{upflag-angle} and
65 @var{downflag-angle} are given in degrees, and @var{upflag-length} and
66 @var{downflag-length} are given in staff spaces.
67
68 All lengths are scaled according to the font size of the note."
69
70   (lambda (grob)
71     (let* ((stem-grob (ly:grob-parent grob X))
72            (log (ly:grob-property stem-grob 'duration-log))
73            (dir (ly:grob-property stem-grob 'direction))
74            (stem-up (eqv? dir UP))
75            (layout (ly:grob-layout grob))
76            ;; scale with the note size (e.g. for grace notes)
77            (factor (magstep (ly:grob-property grob 'font-size 0)))
78            (grob-stem-thickness (ly:grob-property stem-grob 'thickness))
79            (line-thickness (ly:output-def-lookup layout 'line-thickness))
80            (half-stem-thickness (/ (* grob-stem-thickness line-thickness) 2))
81            (raw-length (if stem-up upflag-length downflag-length))
82            (angle (if stem-up upflag-angle downflag-angle))
83            (flag-length (+ (* raw-length factor) half-stem-thickness))
84            (flag-end (polar->rectangular flag-length angle))
85            (thickness (* flag-thickness factor))
86            (thickness-offset (cons 0 (* -1 thickness dir)))
87            (spacing (* -1 flag-spacing factor dir ))
88            (start (cons (- half-stem-thickness) (* half-stem-thickness dir)))
89            (raw-points
90              (list
91                '(0 . 0)
92                flag-end
93                (offset-add flag-end thickness-offset)
94                thickness-offset))
95            (points (map (lambda (coord) (offset-add coord start)) raw-points))
96            (stencil (ly:round-filled-polygon points half-stem-thickness))
97            ;; Log for 1/8 is 3, so we need to subtract 3
98            (flag-stencil (buildflag stencil (- log 3) stencil spacing))
99            (stroke-style (ly:grob-property grob 'stroke-style)))
100       (cond ((eq? (ly:grob-property grob 'style) 'no-flag)
101              empty-stencil)
102             ((equal? stroke-style "grace")
103              (add-stroke-straight flag-stencil grob
104                                   dir log
105                                   stroke-style
106                                   flag-end flag-length
107                                   thickness
108                                   (* half-stem-thickness 2)))
109             (else flag-stencil)))))
110
111 (define-public (modern-straight-flag grob)
112   "Modern straight flag style (for composers like Stockhausen, Boulez, etc.).
113 The angles are 18 and 22 degrees and thus smaller than for the ancient style
114 of Bach, etc."
115   ((straight-flag 0.55 1 -18 1.1 22 1.2) grob))
116
117 (define-public (old-straight-flag grob)
118   "Old straight flag style (for composers like Bach).  The angles of the
119 flags are both 45 degrees."
120   ((straight-flag 0.55 1 -45 1.2 45 1.4) grob))
121
122 (define-public (flat-flag grob)
123   "Flat flag style.  The angles of the flags are both 0 degrees"
124   ((straight-flag 0.55 1.0 0 1.0 0 1.0) grob))
125
126
127 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
128 ;;;;  Flags created from feta glyphs (normal and mensural flags)
129 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
130
131
132 ;; NOTE: By default, lilypond uses the C++ method Flag::stencil
133 ;; (ly:flag::stencil is the corresponding Scheme interface) to generate the
134 ;; flag stencil. The following functions are simply a reimplementation in
135 ;; Scheme, so that one has that functionality available in Scheme, if one
136 ;; wants to write a flag style, which modifies one of the standard flags
137 ;; by some stencil operations.
138
139
140 (define-public (add-stroke-glyph stencil grob dir stroke-style flag-style)
141   "Load and add a stroke (represented by a glyph in the font) to the given
142 flag stencil."
143   (if (not (string? stroke-style))
144       stencil
145       ;; Otherwise: look up the stroke glyph and combine it with the flag
146       (let* ((stem-grob (ly:grob-parent grob X))
147              (font-char (string-append "flags." flag-style dir stroke-style))
148              (alt-font-char (string-append "flags." dir stroke-style))
149              (font (ly:grob-default-font grob))
150              (tmpstencil (ly:font-get-glyph font font-char))
151              (stroke-stencil (if (ly:stencil-empty? tmpstencil)
152                                  (ly:font-get-glyph font alt-font-char)
153                                  tmpstencil)))
154         (if (ly:stencil-empty? stroke-stencil)
155             (begin
156               (ly:warning (_ "flag stroke `~a' or `~a' not found") font-char alt-font-char)
157               stencil)
158             (ly:stencil-add stencil stroke-stencil)))))
159
160
161 (define-public (retrieve-glyph-flag flag-style dir dir-modifier grob)
162   "Load the correct flag glyph from the font."
163   (let* ((stem-grob (ly:grob-parent grob X))
164          (log (ly:grob-property stem-grob 'duration-log))
165          (font (ly:grob-default-font grob))
166          (font-char (string-append "flags." flag-style dir dir-modifier (number->string log)))
167          (flag (ly:font-get-glyph font font-char)))
168     (if (ly:stencil-empty? flag)
169         (ly:warning "flag ~a not found" font-char))
170     flag))
171
172
173 (define-public (create-glyph-flag flag-style dir-modifier grob)
174   "Create a flag stencil by looking up the glyph from the font."
175   (let* ((stem-grob (ly:grob-parent grob X))
176          (dir (if (eqv? (ly:grob-property stem-grob 'direction) UP) "u" "d"))
177          (flag (retrieve-glyph-flag flag-style dir dir-modifier grob))
178          (stroke-style (ly:grob-property grob 'stroke-style)))
179     (cond ((eq? (ly:grob-property grob 'style) 'no-flag)
180            empty-stencil)
181           ((null? stroke-style)
182            flag)
183           (else
184            (add-stroke-glyph flag grob dir stroke-style flag-style)))))
185
186
187 (define-public (mensural-flag grob)
188   "Mensural flags: Create the flag stencil by loading the glyph from the font.
189 Flags are always aligned with staff lines, so we need to check the end point
190 of the stem: For stems ending on staff lines, use different flags than for
191 notes between staff lines.  The idea is that flags are always vertically
192 aligned with the staff lines, regardless of whether the note head is on a
193 staff line or between two staff lines.  In other words, the inner end of
194 a flag always touches a staff line."
195
196   (let* ((stem-grob (ly:grob-parent grob X))
197          (adjust #t)
198          (d (ly:grob-property stem-grob 'direction))
199          (ss (ly:staff-symbol-staff-space stem-grob))
200          (stem-end (inexact->exact (round (* (index-cell
201                                               (ly:grob-extent stem-grob
202                                                               stem-grob
203                                                               Y)
204                                               d)
205                                              (/ 2 ss)))))
206          ;; For some reason the stem-end is a real instead of an integer...
207          (dir-modifier (if (ly:position-on-line? stem-grob stem-end) "1" "0"))
208          (modifier (if adjust dir-modifier "2")))
209     (create-glyph-flag "mensural" modifier grob)))
210
211
212 (define ((glyph-flag flag-style) grob)
213   "Simulatesthe default way of generating flags: Look up glyphs
214 @code{flags.style[ud][1234]} from the feta font and use it for the flag
215 stencil."
216   (create-glyph-flag flag-style "" grob))
217 (export glyph-flag)
218
219
220 (define-public (normal-flag grob)
221   "Create a default flag."
222   (create-glyph-flag "" "" grob))
223
224
225 (define-public (default-flag grob)
226   "Create a flag stencil for the stem.  Its style will be derived from the
227 @code{'style} Flag property.  By default, @code{lilypond} uses a
228 C++ Function (which is slightly faster) to do exactly the same as this
229 function.  However, if one wants to modify the default flags, this function
230 can be used to obtain the default flag stencil, which can then be modified
231 at will.  The correct way to do this is:
232
233 @example
234 \\override Flag #'stencil = #default-flag
235 \\override Flag #'style = #'mensural
236 @end example
237 "
238   (let* ((stem-grob (ly:grob-parent grob X))
239          (flag-style-symbol (ly:grob-property grob 'style))
240          (flag-style (if (symbol? flag-style-symbol)
241                          (symbol->string flag-style-symbol)
242                          "")))
243     (cond
244      ((equal? flag-style "") (normal-flag grob))
245      ((equal? flag-style "mensural") (mensural-flag grob))
246      ((equal? flag-style "no-flag") empty-stencil)
247      (else ((glyph-flag flag-style) grob)))))
248