]> git.donarmstrong.com Git - lilypond.git/blob - scm/document-backend.scm
Add '-dcrop' option to ps and svg backends
[lilypond.git] / scm / document-backend.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2000--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;; Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;;;
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18
19 (define (sort-grob-properties props)
20   ;; force 'meta to the end of each prop-list
21   (let ((meta (assoc 'meta props)))
22     (append (sort (assoc-remove! props 'meta) ly:alist-ci<?)
23             (list meta))))
24
25 ;; properly sort all properties and interfaces
26 ;; within the all-grob-descriptions alist
27 (set! all-grob-descriptions
28   (map!
29     (lambda (grob-description)
30       (let* ((grob-key      (car grob-description))
31              (props         (assoc-ref all-grob-descriptions grob-key))
32              (meta          (assoc-ref props 'meta))
33              (interfaces    (assoc-ref meta 'interfaces))
34              (sorted-ifaces (sort interfaces ly:symbol-ci<?))
35              (new-meta      (assoc-set! meta 'interfaces sorted-ifaces))
36              (new-props     (assoc-set! props 'meta new-meta))
37              (sorted-props  (sort-grob-properties new-props)))
38         (cons grob-key sorted-props)))
39     all-grob-descriptions))
40
41 ;; sort all grobs in the all-grob-descriptions alist
42 (set! all-grob-descriptions (sort all-grob-descriptions ly:alist-ci<?))
43
44 (define (interface-doc-string interface grob-description)
45   (let* ((name (car interface))
46          (desc (cadr interface))
47          (props (caddr interface))
48          (docfunc (lambda (pr)
49                     (property->texi
50                      'backend pr grob-description)))
51          (iprops (filter (lambda (x) (object-property x 'backend-internal))
52                          props))
53          (uprops (filter
54                   (lambda (x) (not (object-property x 'backend-internal)))
55                   props))
56          (user-propdocs (map docfunc uprops))
57          (internal-propdocs (map docfunc iprops)))
58
59     (string-append
60      desc
61
62      (if (pair? uprops)
63          (string-append
64           "\n\n@subsubheading User settable properties:\n"
65           (description-list->texi user-propdocs #t))
66          "")
67
68      (if (pair? iprops)
69          (string-append
70           "\n\n@subsubheading Internal properties:\n"
71           (description-list->texi internal-propdocs #t))
72          ""))))
73
74 (define iface->grob-table (make-hash-table 61))
75 ;; extract ifaces, and put grob into the hash table.
76 (for-each
77  (lambda (x)
78    (let* ((meta (assoc-get 'meta (cdr x)))
79           (ifaces (assoc-get 'interfaces meta)))
80
81      (for-each (lambda (iface)
82                  (hashq-set!
83                   iface->grob-table iface
84                   (cons (car x)
85                         (hashq-ref iface->grob-table iface '()))))
86                ifaces)))
87  all-grob-descriptions)
88
89 ;; First level Interface description
90 (define (interface-doc interface)
91   (let* ((name (symbol->string (car interface)))
92          (interface-list (human-listify
93                           (map ref-ify
94                                (sort
95                                 (map symbol->string
96                                      (hashq-ref iface->grob-table
97                                                 (car interface)
98                                                 '()))
99                                 ly:string-ci<?)))))
100     (make <texi-node>
101       #:name name
102       #:text (string-append
103               (interface-doc-string (cdr interface) '())
104               "\n\n"
105               "This grob interface "
106               (if (equal? interface-list "none")
107                   "is not used in any graphical object"
108                   (string-append
109                    "is used in the following graphical object(s): "
110                    interface-list))
111               "."))))
112
113 (define (grob-alist->texi alist)
114   (let* ((uprops (filter (lambda (x) (not (object-property x 'backend-internal)))
115                          (map car alist))))
116
117     (description-list->texi
118      (map (lambda (y) (property->texi 'backend y alist))
119           uprops)
120      #t)))
121
122 (define (grob-doc description)
123   "Given a property alist DESCRIPTION, make a documentation
124 node."
125
126   (let* ((meta (assoc-get 'meta description))
127          (name (assoc-get 'name meta))
128          ;;       (bla (display name))
129          (ifaces (map lookup-interface (assoc-get 'interfaces meta)))
130          (ifacedoc (map ref-ify
131                         (sort
132                          (map (lambda (iface)
133                                 (if (pair? iface)
134                                     (symbol->string (car iface))
135                                     (ly:error (_ "pair expected in doc ~s") name)))
136                               ifaces)
137                          ly:string-ci<?)))
138          (engravers (filter
139                      (lambda (x) (engraver-makes-grob? name x))
140                      all-engravers-list))
141          (namestr (symbol->string name))
142          (engraver-names (map symbol->string
143                               (map ly:translator-name engravers)))
144          (engraver-list (human-listify
145                          (map ref-ify
146                               (map engraver-name engraver-names)))))
147
148     (make <texi-node>
149       #:name namestr
150       #:text
151       (string-append
152        namestr " objects "
153        (if (equal? engraver-list "none")
154            "are not created by any engraver"
155            (string-append
156             "are created by: "
157             engraver-list))
158        "."
159
160        "\n\nStandard settings:\n"
161        (grob-alist->texi description)
162        "\n\nThis object supports the following interface(s):\n"
163        (human-listify ifacedoc)
164        "."))))
165
166 (define (all-grobs-doc)
167   (make <texi-node>
168     #:name "All layout objects"
169     #:desc "Description and defaults for all graphical objects (grobs)."
170     #:children
171     (map (lambda (x) (grob-doc (cdr x)))  all-grob-descriptions)))
172
173 (define interface-description-alist
174   (hash-fold
175    (lambda (key val prior)
176      (cons (cons key val)  prior))
177    '() (ly:all-grob-interfaces)))
178
179 ;; sort user-settable and internal props within each grob-interface
180 (set! interface-description-alist
181   (map! (lambda (iface-desc)
182           (let* ((key-name-docstr (list-head iface-desc 3))
183                  (props           (list-tail iface-desc 3))
184                  (sorted-props    (list (sort (car props) ly:symbol-ci<?))))
185             (append key-name-docstr sorted-props)))
186         interface-description-alist))
187
188 ;; sort list of grob interfaces
189 (set! interface-description-alist
190       (sort interface-description-alist ly:alist-ci<?))
191
192 ;;;;;;;;;; check for dangling backend properties.
193 (define (mark-interface-properties entry)
194   (for-each (lambda (x) (set-object-property! x 'iface-marked #t))
195             (caddr (cdr entry))))
196
197 (for-each mark-interface-properties interface-description-alist)
198
199 (define (check-dangling-properties prop)
200   (if (not (object-property prop 'iface-marked))
201       (ly:error (string-append "define-grob-properties.scm: "
202                                (_ "cannot find interface for property: ~S")) prop)))
203
204 (for-each check-dangling-properties all-backend-properties)
205
206 ;;;;;;;;;;;;;;;;
207
208 (define (lookup-interface name)
209   (let* ((entry (hashq-ref (ly:all-grob-interfaces) name #f)))
210     (if entry
211         entry
212         (ly:error (_ "unknown Grob interface: ~S") name))))
213
214 (define (all-interfaces-doc)
215   (make <texi-node>
216     #:name "Graphical Object Interfaces"
217     #:desc "Building blocks of graphical objects."
218     #:children
219     (map interface-doc interface-description-alist)))
220
221 (define (backend-properties-doc-string lst)
222   (let* ((ps (sort (map symbol->string lst) ly:string-ci<?))
223          (descs (map (lambda (prop)
224                        (property->texi 'backend (string->symbol prop) '())) ps))
225          (texi (description-list->texi descs #f)))
226     texi))
227
228 ;;(dump-node (grob-doc (cdadr all-grob-descriptions)) (current-output-port) 0 )
229 (define (backend-doc-node)
230   (make <texi-node>
231     #:name "Backend"
232     #:desc "Reference for the layout engine."
233     #:children
234     (list
235      (all-grobs-doc)
236      (all-interfaces-doc)
237      (make <texi-node>
238        #:name "User backend properties"
239        #:desc "All tunable properties in a big list."
240        #:text (backend-properties-doc-string all-user-grob-properties))
241      (make <texi-node>
242        #:name "Internal backend properties"
243        #:desc "All internal layout properties in a big list."
244        #:text (backend-properties-doc-string all-internal-grob-properties)))))