]> git.donarmstrong.com Git - lilypond.git/blob - scm/paper-system.scm
Run grand-replace (issue 3765)
[lilypond.git] / scm / paper-system.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2006--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
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 (define-module (scm paper-system))
19
20 (use-modules (lily)
21              (srfi srfi-1)
22              (ice-9 optargs))
23
24 (define-public (paper-system-title? system)
25   (equal? #t (ly:prob-property system 'is-title)
26           ))
27
28 (define (system-stencil system-grob main-stencil)
29   (let* ((padding (ly:grob-property system-grob 'in-note-padding #f))
30          (in-notes (if padding (ly:grob-property system-grob 'in-note-stencil) empty-stencil))
31          (in-notes (if in-notes in-notes empty-stencil))
32          (direction (if padding (ly:grob-property system-grob 'in-note-direction) UP)))
33     (if padding
34         (ly:stencil-combine-at-edge main-stencil Y direction in-notes padding)
35         main-stencil)))
36
37 (define-public (paper-system-stencil system)
38   (let ((main-stencil (ly:prob-property system 'stencil))
39         (system-grob (ly:prob-property system 'system-grob)))
40     (if (ly:grob? system-grob)
41         (system-stencil system-grob main-stencil)
42         main-stencil)))
43
44 (define-public (paper-system-layout system)
45   (let*
46       ((g (paper-system-system-grob system)))
47
48     (if (ly:grob? g)
49         (ly:grob-layout  g)
50         #f)))
51
52 (define-public (paper-system-system-grob paper-system)
53   (ly:prob-property paper-system 'system-grob))
54
55 (define-public (paper-system-extent system axis)
56   (ly:stencil-extent (paper-system-stencil system) axis))
57
58 (define-public (paper-system-staff-extents ps)
59   (ly:prob-property ps 'staff-refpoint-extent '(0 . 0)))
60
61 (define-public (paper-system-annotate-last system layout)
62   (let*
63       ((bottomspace (ly:prob-property system 'bottom-space))
64        (y-extent (paper-system-extent system Y))
65        (x-extent (paper-system-extent system X))
66        (stencil (ly:prob-property system 'stencil))
67
68        (arrow (if (number? bottomspace)
69                   (annotate-y-interval layout
70                                        "bottom-space"
71                                        (cons (- (car y-extent) bottomspace)
72                                              (car y-extent))
73                                        #t)
74                   #f)))
75
76     (if arrow
77         (set! stencil
78               (ly:stencil-add stencil arrow)))
79
80     (set! (ly:prob-property system 'stencil)
81           stencil)
82     ))
83
84
85 ;; Y-ext and next-Y-ext are either skyline-pairs or extents
86 (define*-public (annotate-padding system-Y system-X Y-ext X-ext
87                                   next-system-Y next-system-X next-Y-ext next-X-ext
88                                   layout horizon-padding padding #:key (base-color blue))
89   (let* ((eps 0.001)
90          (skyline (and (ly:skyline-pair? Y-ext)
91                        (ly:skyline-pair::skyline Y-ext DOWN)))
92          (next-skyline (and (ly:skyline-pair? next-Y-ext)
93                             (ly:skyline-pair::skyline next-Y-ext UP)))
94          (annotation-X (cond
95                         ((and skyline next-skyline)
96                          (-
97                           (ly:skyline::get-touching-point skyline next-skyline horizon-padding)
98                           horizon-padding))
99                         (skyline
100                          (ly:skyline::get-max-height-position skyline))
101                         (next-skyline
102                          (ly:skyline::get-max-height-position next-skyline))
103                         (else
104                          (max (cdr X-ext)
105                               (cdr next-X-ext)))))
106          (annotation-Y (if skyline
107                            (ly:skyline::get-height skyline annotation-X)
108                            (car Y-ext)))
109          (next-annotation-Y (if next-skyline
110                                 (- (+ (ly:skyline::get-height next-skyline
111                                                               (- (+ annotation-X system-X)
112                                                                  next-system-X))
113                                       next-system-Y)
114                                    system-Y)
115                                 (cdr next-Y-ext)))
116          (padding-blocks (>= next-annotation-Y (- annotation-Y padding eps)))
117          (contrast-color (append (cdr base-color) (list (car base-color))))
118          (color (if padding-blocks contrast-color base-color))
119          (annotation (ly:stencil-translate-axis
120                       (annotate-y-interval
121                        layout
122                        "padding"
123                        `(,(- annotation-Y padding). ,annotation-Y)
124                        #t
125                        #:color color)
126                       annotation-X X)))
127     (if (> padding 0.0)
128         annotation
129         empty-stencil)))
130
131
132 (define-public (paper-system-annotate system next-system layout)
133   "Add arrows and texts to indicate which lengths are set."
134
135   (let* ((grob (ly:prob-property system 'system-grob))
136          (paper-height (ly:output-def-lookup layout 'paper-height))
137          (bottom-margin (ly:output-def-lookup layout 'bottom-margin))
138          (top-margin (ly:output-def-lookup layout 'top-margin))
139          (spaceable-staves (if (ly:grob? grob) (ly:system::get-spaceable-staves grob) '()))
140          (all-staves (if (ly:grob? grob) (ly:system::get-staves grob) '()))
141          (spaceable-staff-annotate
142           (lambda (before-staff after-staff)
143             (let ((before-Y (ly:grob-relative-coordinate before-staff grob Y))
144                   (after-Y (ly:grob-relative-coordinate after-staff grob Y)))
145               (annotate-spacing-spec
146                layout
147                (ly:get-spacing-spec before-staff after-staff)
148                before-Y
149                after-Y))))
150
151          (staff-padding-annotate
152           (lambda (before-staff after-staff)
153             (let ((before-Y (ly:grob-relative-coordinate before-staff grob Y))
154                   (before-X (ly:grob-relative-coordinate before-staff grob X))
155                   (before-X-ext (ly:grob-extent before-staff before-staff X))
156                   (after-Y (ly:grob-relative-coordinate after-staff grob Y))
157                   (after-X (ly:grob-relative-coordinate after-staff grob X))
158                   (after-X-ext (ly:grob-extent after-staff after-staff X))
159                   (skylines (ly:grob-property before-staff 'vertical-skylines))
160                   (after-skylines (ly:grob-property after-staff 'vertical-skylines))
161                   (padding (assoc-get 'padding
162                                       (ly:get-spacing-spec before-staff after-staff)
163                                       0.0))
164                   (horizon-padding (ly:grob-property before-staff
165                                                      'skyline-horizontal-padding
166                                                      0.0)))
167               (ly:stencil-translate
168                (annotate-padding
169                 before-Y before-X skylines before-X-ext
170                 after-Y after-X after-skylines after-X-ext
171                 layout horizon-padding padding)
172                (cons before-X before-Y)))))
173
174          (staff-annotations (if (< 1 (length spaceable-staves))
175                                 (map spaceable-staff-annotate
176                                      (drop-right spaceable-staves 1)
177                                      (drop spaceable-staves 1))
178                                 '()))
179          (staff-padding-annotations (if (< 1 (length all-staves))
180                                         (map staff-padding-annotate
181                                              (drop-right all-staves 1)
182                                              (drop all-staves 1))
183                                         '()))
184          (estimate-extent (if (ly:grob? grob)
185                               (annotate-y-interval layout
186                                                    "extent-estimate"
187                                                    (ly:grob-property grob 'pure-Y-extent)
188                                                    #f)
189                               #f))
190
191          (spacing-spec (cond ((and next-system
192                                    (paper-system-title? system)
193                                    (paper-system-title? next-system))
194                               (ly:output-def-lookup layout 'markup-markup-spacing))
195                              ((paper-system-title? system)
196                               (ly:output-def-lookup layout 'markup-system-spacing))
197                              ((and next-system
198                                    (paper-system-title? next-system))
199                               (ly:output-def-lookup layout 'score-markup-spacing))
200                              ((not next-system)
201                               (ly:output-def-lookup layout 'last-bottom-spacing))
202                              ((ly:prob-property system 'last-in-score #f)
203                               (ly:output-def-lookup layout 'score-system-spacing))
204                              (else
205                               (ly:output-def-lookup layout 'system-system-spacing))))
206          (last-staff-Y (car (paper-system-staff-extents system)))
207          (system-Y (ly:prob-property system 'Y-offset 0.0))
208          (system-X (ly:prob-property system 'X-offset 0.0))
209          (next-system-Y (and next-system
210                              (ly:prob-property next-system 'Y-offset 0.0)))
211          (next-system-X (and next-system
212                              (ly:prob-property next-system 'X-offset 0.0)))
213          (first-staff-next-system-Y (if next-system
214                                         (- (+ (cdr (paper-system-staff-extents next-system))
215                                               system-Y)
216                                            next-system-Y)
217                                         (+ system-Y top-margin bottom-margin (- paper-height))))
218
219          (skyline (or
220                    (ly:prob-property system 'vertical-skylines #f)
221                    (paper-system-extent system Y)))
222          (next-skyline (and next-system
223                             (or
224                              (ly:prob-property next-system 'vertical-skylines #f)
225                              (paper-system-extent next-system Y))))
226          (horizon-padding (and
227                            (ly:grob? grob)
228                            (ly:grob-property grob 'skyline-horizontal-padding 0)))
229          (padding-annotation (if (skyline-pair-and-non-empty? next-system)
230                                  (annotate-padding
231                                   (- system-Y) system-X skyline (paper-system-extent system X)
232                                   (- next-system-Y) next-system-X next-skyline (paper-system-extent next-system X)
233                                   layout
234                                   horizon-padding
235                                   (assoc-get 'padding spacing-spec 0.0)
236                                   #:base-color blue)
237                                  empty-stencil))
238
239          (system-annotation (annotate-spacing-spec
240                              layout spacing-spec
241                              last-staff-Y
242                              first-staff-next-system-Y))
243          (annotations (ly:stencil-add
244                        padding-annotation
245                        (stack-stencils Y DOWN 0.0 staff-padding-annotations)
246                        (stack-stencils Y DOWN 0.0 (append staff-annotations (list system-annotation))))))
247
248     (if estimate-extent
249         (set! annotations
250               (stack-stencils X RIGHT 5.5
251                               (list annotations
252                                     estimate-extent))))
253
254     (if (not (null? annotations))
255         (set! (ly:prob-property system 'stencil)
256               (ly:stencil-add
257                (ly:prob-property system 'stencil)
258                (ly:make-stencil
259                 (ly:stencil-expr annotations)
260                 (ly:stencil-extent empty-stencil X)
261                 (ly:stencil-extent empty-stencil Y)))))
262     (ly:prob-property system 'stencil)))