/[mcclim]/mcclim/recording.lisp
ViewVC logotype

Contents of /mcclim/recording.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.142 - (show annotations)
Tue Aug 19 15:56:50 2008 UTC (5 years, 7 months ago) by ahefner
Branch: MAIN
Changes since 1.141: +27 -0 lines
Apparently, when rgb-designs were merged into the core of mcclim, the
output recording definitions got left out.
1 ;;; -*- Mode: Lisp; Package: CLIM-INTERNALS -*-
2
3 ;;; (c) copyright 1998,1999,2000,2001 by Michael McDonald (mikemac@mikemac.com)
4 ;;; (c) copyright 2000 by
5 ;;; Robert Strandh (strandh@labri.u-bordeaux.fr)
6 ;;; (c) copyright 2001 by
7 ;;; Arnaud Rouanet (rouanet@emi.u-bordeaux.fr)
8 ;;; Lionel Salabartan (salabart@emi.u-bordeaux.fr)
9 ;;; (c) copyright 2001, 2002 by Alexey Dejneka (adejneka@comail.ru)
10 ;;; (c) copyright 2003 by Gilbert Baumann <unk6@rz.uni-karlsruhe.de>
11
12 ;;; This library is free software; you can redistribute it and/or
13 ;;; modify it under the terms of the GNU Library General Public
14 ;;; License as published by the Free Software Foundation; either
15 ;;; version 2 of the License, or (at your option) any later version.
16 ;;;
17 ;;; This library is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;;; Library General Public License for more details.
21 ;;;
22 ;;; You should have received a copy of the GNU Library General Public
23 ;;; License along with this library; if not, write to the
24 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;;; Boston, MA 02111-1307 USA.
26
27 ;;; TODO:
28 ;;;
29 ;;; - Scrolling does not work correctly. Region is given in "window"
30 ;;; coordinates, without bounding-rectangle-position transformation.
31 ;;; (Is it still valid?)
32 ;;;
33 ;;; - Redo setf*-output-record-position, extent recomputation for
34 ;;; compound records
35 ;;;
36 ;;; - When DRAWING-P is NIL, should stream cursor move?
37 ;;;
38 ;;; - :{X,Y}-OFFSET.
39 ;;;
40 ;;; - (SETF OUTPUT-RECORD-START-CURSOR-POSITION) does not affect the
41 ;;; bounding rectangle. What does it affect?
42 ;;;
43 ;;; - How should (SETF OUTPUT-RECORD-POSITION) affect the bounding
44 ;;; rectangle of the parent? Now its bounding rectangle is accurately
45 ;;; recomputed, but it is very inefficient for table formatting. It
46 ;;; seems that CLIM is supposed to keep a "large enougn" rectangle and
47 ;;; to shrink it to the correct size only when the layout is complete
48 ;;; by calling TREE-RECOMPUTE-EXTENT.
49 ;;;
50 ;;; - Computation of the bounding rectangle of lines/polygons ignores
51 ;;; LINE-STYLE-CAP-SHAPE.
52 ;;;
53 ;;; - Rounding of coordinates.
54 ;;;
55 ;;; - Document carefully the interface of
56 ;;; STANDARD-OUTPUT-RECORDING-STREAM.
57 ;;;
58 ;;; - COORD-SEQ is a sequence, not a list.
59
60 ;;; Troubles
61
62 ;;; DC
63 ;;;
64 ;;; Some GFs are defined to have "a default method on CLIM's standard
65 ;;; output record class". What does it mean? What is "CLIM's standard
66 ;;; output record class"? Is it OUTPUT-RECORD or BASIC-OUTPUT-RECORD?
67 ;;; Now they are defined on OUTPUT-RECORD.
68
69
70 (in-package :clim-internals)
71
72 ;;; 16.2.1. The Basic Output Record Protocol
73 #+:cmu(declaim (ftype (function (output-record) (values rational rational))
74 output-record-position))
75 ;; XXX What does #+:CMU mean? FTYPE was excluded from ANSI CL? Other
76 ;; compilers try to check type declarations?
77 (defgeneric output-record-position (record)
78 (:documentation
79 "Returns the x and y position of RECORD. The position is the
80 position of the upper-left corner of its bounding rectangle. The
81 position is relative to the stream, where (0,0) is (initially) the
82 upper-left corner of the stream."))
83
84 (defgeneric* (setf output-record-position) (x y record)
85 (:documentation
86 "Changes the x and y position of the RECORD to be X and Y, and
87 updates the bounding rectangle to reflect the new position (and saved
88 cursor positions, if the output record stores it). If RECORD has any
89 children, all of the children (and their descendants as well) will be
90 moved by the same amount as RECORD was moved. The bounding rectangles
91 of all of RECORD's ancestors will also be updated to be large enough
92 to contain RECORD."))
93
94 #+:cmu(declaim (ftype (function (output-record) (values integer integer))
95 output-record-start-cursor-position))
96 (defgeneric output-record-start-cursor-position (record)
97 (:documentation
98 "Returns the x and y starting cursor position of RECORD. The
99 positions are relative to the stream, where (0,0) is (initially) the
100 upper-left corner of the stream."))
101
102 (defgeneric* (setf output-record-start-cursor-position) (x y record))
103
104 #+:cmu(declaim (ftype (function (output-record) (values integer integer))
105 output-record-end-cursor-position))
106 (defgeneric output-record-end-cursor-position (record)
107 (:documentation
108 "Returns the x and y ending cursor position of RECORD. The
109 positions are relative to the stream, where (0,0) is (initially) the
110 upper-left corner of the stream."))
111
112 (defgeneric* (setf output-record-end-cursor-position) (x y record))
113
114 (defgeneric output-record-parent (record)
115 (:documentation
116 "Returns the output record that is the parent of RECORD, or NIL if
117 RECORD has no parent."))
118
119 (defgeneric (setf output-record-parent) (parent record)
120 (:documentation "Non-standard function."))
121
122 (defgeneric replay-output-record (record stream
123 &optional region x-offset y-offset)
124 (:documentation "Displays the output captured by RECORD on the
125 STREAM, exactly as it was originally captured. The current user
126 transformation, line style, text style, ink and clipping region of
127 STREAM are all ignored. Instead, these are gotten from the output
128 record.
129
130 Only those records that overlap REGION are displayed."))
131
132 (defgeneric output-record-hit-detection-rectangle* (record))
133
134 (defgeneric output-record-refined-position-test (record x y))
135
136 (defgeneric highlight-output-record (record stream state))
137
138 (defgeneric displayed-output-record-ink (displayed-output-record))
139
140 ;;; 16.2.2. Output Record "Database" Protocol
141
142 (defgeneric output-record-children (record))
143
144 (defgeneric add-output-record (child record))
145
146 (defgeneric delete-output-record (child record &optional errorp))
147
148 (defgeneric clear-output-record (record))
149
150 (defgeneric output-record-count (record))
151
152 (defgeneric map-over-output-records-containing-position
153 (function record x y &optional x-offset y-offset &rest function-args)
154 (:documentation "Maps over all of the children of RECORD that
155 contain the point at (X,Y), calling FUNCTION on each one. FUNCTION is
156 a function of one or more arguments, the first argument being the
157 record containing the point. FUNCTION is also called with all of
158 FUNCTION-ARGS as APPLY arguments.
159
160 If there are multiple records that contain the point,
161 MAP-OVER-OUTPUT-RECORDS-CONTAINING-POSITION hits the most recently
162 inserted record first and the least recently inserted record
163 last. Otherwise, the order in which the records are traversed is
164 unspecified."))
165
166 (defgeneric map-over-output-records-overlapping-region
167 (function record region &optional x-offset y-offset &rest function-args)
168 (:documentation "Maps over all of the children of the RECORD that
169 overlap the REGION, calling FUNCTION on each one. FUNCTION is a
170 function of one or more arguments, the first argument being the record
171 overlapping the region. FUNCTION is also called with all of
172 FUNCTION-ARGS as APPLY arguments.
173
174 If there are multiple records that overlap the region and that overlap
175 each other, MAP-OVER-OUTPUT-RECORDS-OVERLAPPING-REGION hits the least
176 recently inserted record first and the most recently inserted record
177 last. Otherwise, the order in which the records are traversed is
178 unspecified. "))
179
180 ;;; From the Franz CLIM user's guide but not in the spec... clearly necessary.
181
182 (defgeneric map-over-output-records-1
183 (continuation record continuation-args))
184
185 (defun map-over-output-records
186 (continuation record &optional x-offset y-offset &rest continuation-args)
187 (declare (ignore x-offset y-offset))
188 (map-over-output-records-1 continuation record continuation-args))
189
190 ;;; 16.2.3. Output Record Change Notification Protocol
191
192 (defgeneric recompute-extent-for-new-child (record child))
193
194 (defgeneric recompute-extent-for-changed-child
195 (record child old-min-x old-min-y old-max-x old-max-y))
196
197 (defgeneric tree-recompute-extent (record))
198
199 ;;; 21.3 Incremental Redisplay Protocol. These generic functions need
200 ;;; to be implemented for all the basic displayed-output-records, so they are
201 ;;; defined in this file.
202 ;;;
203 ;;; match-output-records and find-child-output-record, as defined in
204 ;;; the CLIM spec, are pretty silly. How does incremental redisplay know
205 ;;; what keyword arguments to supply to find-child-output-record? Through
206 ;;; a gf specialized on the type of the record it needs to match... why
207 ;;; not define the search function and the predicate on two records then!
208 ;;;
209 ;;; We'll implement match-output-records and find-child-output-record,
210 ;;; but we won't actually use them. Instead, output-record-equal will
211 ;;; match two records, and find-child-record-equal will search for the
212 ;;; equivalent record.
213
214 (defgeneric match-output-records (record &rest args))
215
216 ;;; These gf's use :most-specific-last because one of the least
217 ;;; specific methods will check the bounding boxes of the records, which
218 ;;; should cause an early out most of the time.
219
220 (defgeneric match-output-records-1 (record &key)
221 (:method-combination and :most-specific-last))
222
223 (defgeneric output-record-equal (record1 record2)
224 (:method-combination and :most-specific-last))
225
226 (defmethod output-record-equal :around (record1 record2)
227 (cond ((eq record1 record2)
228 ;; Some unusual record -- like a Goatee screen line -- might
229 ;; exist in two trees at once
230 t)
231 ((eq (class-of record1) (class-of record2))
232 (let ((result (call-next-method)))
233 (if (eq result 'maybe)
234 nil
235 result)))
236 (t nil)))
237
238 ;;; A fallback method so that something's always applicable.
239
240 (defmethod output-record-equal and (record1 record2)
241 (declare (ignore record1 record2))
242 'maybe)
243
244 ;;; The code for match-output-records-1 and output-record-equal
245 ;;; methods are very similar, hence this macro. In order to exploit
246 ;;; the similarities, it's necessary to treat the slots of the second
247 ;;; record like variables, so for convenience the macro will use
248 ;;; slot-value on both records.
249
250 (defmacro defrecord-predicate (record-type slots &body body)
251 "Each element of SLOTS is either a symbol or (:initarg-name slot-name)."
252 (let* ((slot-names (mapcar #'(lambda (slot-spec)
253 (if (consp slot-spec)
254 (cadr slot-spec)
255 slot-spec))
256 slots))
257 (supplied-vars (mapcar #'(lambda (slot)
258 (gensym (symbol-name
259 (symbol-concat slot '#:-p))))
260 slot-names))
261 (key-args (mapcar #'(lambda (slot-spec supplied)
262 `(,slot-spec nil ,supplied))
263 slots supplied-vars))
264 (key-arg-alist (mapcar #'cons slot-names supplied-vars)))
265 `(progn
266 (defmethod output-record-equal and ((record ,record-type)
267 (record2 ,record-type))
268 (macrolet ((if-supplied ((var &optional (type t)) &body supplied-body)
269 (declare (ignore var type))
270 `(progn ,@supplied-body)))
271 (with-slots ,slot-names
272 record2
273 ,@body)))
274 (defmethod match-output-records-1 and ((record ,record-type)
275 &key ,@key-args)
276 (macrolet ((if-supplied ((var &optional (type t)) &body supplied-body)
277 (let ((supplied-var (cdr (assoc var ',key-arg-alist))))
278 (unless supplied-var
279 (error "Unknown slot ~S" var))
280 `(or (null ,supplied-var)
281 ,@(if (eq type t)
282 `((progn ,@supplied-body))
283 `((if (typep ,var ',type)
284 (progn ,@supplied-body)
285 (error 'type-error
286 :datum ,var
287 :expected-type ',type))))))))
288 ,@body)))
289
290 ))
291 ;;; Macros
292 (defmacro with-output-recording-options ((stream
293 &key (record nil record-supplied-p)
294 (draw nil draw-supplied-p))
295 &body body)
296 (setq stream (stream-designator-symbol stream '*standard-output*))
297 (with-gensyms (continuation)
298 `(flet ((,continuation (,stream)
299 ,(declare-ignorable-form* stream)
300 ,@body))
301 (declare (dynamic-extent #',continuation))
302 (invoke-with-output-recording-options
303 ,stream #',continuation
304 ,(if record-supplied-p record `(stream-recording-p ,stream))
305 ,(if draw-supplied-p draw `(stream-drawing-p ,stream))))))
306
307 ;;; Macro masturbation...
308
309 (defmacro define-invoke-with (macro-name func-name record-type doc-string)
310 `(defmacro ,macro-name ((stream
311 &optional
312 (record-type '',record-type)
313 (record (gensym))
314 &rest initargs)
315 &body body)
316 ,doc-string
317 (setq stream (stream-designator-symbol stream '*standard-output*))
318 (with-gensyms (constructor continuation)
319 (multiple-value-bind (bindings m-i-args)
320 (rebind-arguments initargs)
321 `(let ,bindings
322 (flet ((,constructor ()
323 (make-instance ,record-type ,@m-i-args))
324 (,continuation (,stream ,record)
325 ,(declare-ignorable-form* stream record)
326 ,@body))
327 (declare (dynamic-extent #'constructor #'continuation))
328 (,',func-name ,stream #',continuation ,record-type #',constructor
329 ,@m-i-args)))))))
330
331 (define-invoke-with with-new-output-record invoke-with-new-output-record
332 standard-sequence-output-record
333 "Creates a new output record of type RECORD-TYPE and then captures
334 the output of BODY into the new output record, and inserts the new
335 record into the current \"open\" output record assotiated with STREAM.
336 If RECORD is supplied, it is the name of a variable that will be
337 lexically bound to the new output record inside the body. INITARGS are
338 CLOS initargs that are passed to MAKE-INSTANCE when the new output
339 record is created.
340 It returns the created output record.
341 The STREAM argument is a symbol that is bound to an output
342 recording stream. If it is T, *STANDARD-OUTPUT* is used.")
343
344 (define-invoke-with with-output-to-output-record
345 invoke-with-output-to-output-record
346 standard-sequence-output-record
347 "Creates a new output record of type RECORD-TYPE and then captures
348 the output of BODY into the new output record. The cursor position of
349 STREAM is initially bound to (0,0)
350 If RECORD is supplied, it is the name of a variable that will be
351 lexically bound to the new output record inside the body. INITARGS are
352 CLOS initargs that are passed to MAKE-INSTANCE when the new output
353 record is created.
354 It returns the created output record.
355 The STREAM argument is a symbol that is bound to an output
356 recording stream. If it is T, *STANDARD-OUTPUT* is used.")
357
358
359 ;;;; Implementation
360
361 (defclass basic-output-record (standard-bounding-rectangle output-record)
362 ((parent :initarg :parent ; XXX
363 :initform nil
364 :accessor output-record-parent)) ; XXX
365 (:documentation "Implementation class for the Basic Output Record Protocol."))
366
367 (defmethod initialize-instance :after ((record basic-output-record)
368 &key (x-position 0.0d0)
369 (y-position 0.0d0))
370 (setf (rectangle-edges* record)
371 (values x-position y-position x-position y-position)))
372
373 ;;; XXX I'd really like to get rid of the x and y slots. They are surely
374 ;;; redundant with the bounding rectangle coordinates.
375 (defclass compound-output-record (basic-output-record)
376 ((x :initarg :x-position
377 :initform 0.0d0
378 :documentation "X-position of the empty record.")
379 (y :initarg :y-position
380 :initform 0.0d0
381 :documentation "Y-position of the empty record.")
382 (in-moving-p :initform nil
383 :documentation "Is set while changing the position."))
384 (:documentation "Implementation class for output records with children."))
385
386 ;;; 16.2.1. The Basic Output Record Protocol
387 (defmethod output-record-position ((record basic-output-record))
388 (bounding-rectangle-position record))
389
390 (defmethod* (setf output-record-position) (nx ny (record basic-output-record))
391 (with-standard-rectangle (x1 y1 x2 y2)
392 record
393 (let ((dx (- nx x1))
394 (dy (- ny y1)))
395 (setf (rectangle-edges* record)
396 (values nx ny (+ x2 dx) (+ y2 dy)))))
397 (values nx ny))
398
399 (defmethod* (setf output-record-position) :around
400 (nx ny (record basic-output-record))
401 (with-bounding-rectangle* (min-x min-y max-x max-y) record
402 (call-next-method)
403 (let ((parent (output-record-parent record)))
404 (when (and parent (not (and (typep parent 'compound-output-record)
405 (slot-value parent 'in-moving-p)))) ; XXX
406 (recompute-extent-for-changed-child parent record
407 min-x min-y max-x max-y)))
408 (values nx ny)))
409
410 (defmethod* (setf output-record-position)
411 :before (nx ny (record compound-output-record))
412 (with-standard-rectangle* (:x1 x1 :y1 y1)
413 record
414 (letf (((slot-value record 'in-moving-p) t))
415 (let ((dx (- nx x1))
416 (dy (- ny y1)))
417 (map-over-output-records
418 (lambda (child)
419 (multiple-value-bind (x y) (output-record-position child)
420 (setf (output-record-position child)
421 (values (+ x dx) (+ y dy)))))
422 record)))))
423
424 (defmethod output-record-start-cursor-position ((record basic-output-record))
425 (values nil nil))
426
427 (defmethod* (setf output-record-start-cursor-position)
428 (x y (record basic-output-record))
429 (values x y))
430
431 (defmethod output-record-end-cursor-position ((record basic-output-record))
432 (values nil nil))
433
434 (defmethod* (setf output-record-end-cursor-position)
435 (x y (record basic-output-record))
436 (values x y))
437
438 #+cmu
439 (progn
440 ;; Sometimes CMU's PCL fails with forward reference classes, so this
441 ;; is a kludge to keep it happy.
442 ;;
443 ;; This was reported as a bug to cmucl-imp [<E18vKN1-0004DQ-00@saphir.local>]
444 ;;
445 ;; In short it exposes itself when you compile and load into a
446 ;; _virgin_ lisp the following:
447 ;;
448 ;; (defclass foo (bar) ())
449 ;; (defun barz () (make-instance 'foo))
450 ;; (defclass bar () ())
451 ;;
452 ;; --GB 2003-03-18
453 ;;
454 (defclass gs-ink-mixin () ())
455 (defclass gs-clip-mixin () ())
456 (defclass gs-line-style-mixin () ())
457 (defclass gs-text-style-mixin () ()))
458
459 ;;; Humph. It'd be nice to tie this to the actual definition of a
460 ;;; medium. -- moore
461 (defclass complete-medium-state
462 (gs-ink-mixin gs-clip-mixin gs-line-style-mixin gs-text-style-mixin)
463 ())
464
465 (defun replay (record stream &optional (region (or (pane-viewport-region stream)
466 (sheet-region stream))))
467 (if (typep stream 'encapsulating-stream)
468 (replay record (encapsulating-stream-stream stream) region)
469 (progn
470 (stream-close-text-output-record stream)
471 (when (stream-drawing-p stream)
472 (with-cursor-off stream ;;FIXME?
473 (letf (((stream-cursor-position stream) (values 0 0))
474 ((stream-recording-p stream) nil)
475 ;; Is there a better value to bind to baseline?
476 ((slot-value stream 'baseline) (slot-value stream 'baseline)))
477 (with-sheet-medium (medium stream)
478 (let ((transformation (medium-transformation medium)))
479 (unwind-protect
480 (progn
481 (setf (medium-transformation medium)
482 +identity-transformation+)
483 (replay-output-record record stream region))
484 (setf (medium-transformation medium) transformation))))))))))
485
486 (defmethod replay-output-record ((record compound-output-record) stream
487 &optional region (x-offset 0) (y-offset 0))
488 (when (null region)
489 (setq region (or (pane-viewport-region stream) +everywhere+)))
490 (with-drawing-options (stream :clipping-region region)
491 (map-over-output-records-overlapping-region
492 #'replay-output-record record region x-offset y-offset
493 stream region x-offset y-offset)))
494
495 (defmethod output-record-hit-detection-rectangle* ((record output-record))
496 ;; XXX DC
497 (bounding-rectangle* record))
498
499 (defmethod output-record-refined-position-test ((record basic-output-record)
500 x y)
501 (declare (ignore x y))
502 t)
503
504 (defun highlight-output-record-rectangle (record stream state)
505 (with-identity-transformation (stream)
506 (multiple-value-bind (x1 y1 x2 y2)
507 (output-record-hit-detection-rectangle* record)
508 (ecase state
509 (:highlight
510 (draw-rectangle* (sheet-medium stream) x1 y1 (1- x2) (1- y2)
511 :filled nil :ink +foreground-ink+)) ; XXX +FLIPPING-INK+?
512 (:unhighlight
513 ;; FIXME: repaint the hit detection rectangle. It could be bigger than
514 ;; the bounding rectangle.
515 (repaint-sheet stream record)
516
517 ;; Using queue-repaint should be faster in apps (such as clouseau) that
518 ;; highlight/unhighlight many bounding rectangles at once. The event
519 ;; code should merge these into a single larger repaint. Unfortunately,
520 ;; since an enqueued repaint does not occur immediately, and highlight
521 ;; rectangles are not recorded, newer highlighting gets wiped out
522 ;; shortly after being drawn. So, we aren't ready for this yet.
523 ;; ..Actually, it isn't necessarily faster. Depends on the app.
524 #+NIL
525 (queue-repaint stream (make-instance 'window-repaint-event
526 :sheet stream
527 :region (transform-region
528 (sheet-native-transformation stream)
529 record))))))))
530
531 ;;; XXX Should this only be defined on recording streams?
532 (defmethod highlight-output-record ((record output-record) stream state)
533 ;; XXX DC
534 ;; XXX Disable recording?
535 (highlight-output-record-rectangle record stream state))
536
537 ;;; 16.2.2. The Output Record "Database" Protocol
538
539 ;; These two aren't in the spec, but are needed to make indirect adding/deleting
540 ;; of GADGET-OUTPUT-RECORDs work:
541
542 (defgeneric note-output-record-lost-sheet (record sheet))
543 (defgeneric note-output-record-got-sheet (record sheet))
544
545 (defmethod note-output-record-lost-sheet ((record output-record) sheet)
546 (declare (ignore record sheet))
547 (values))
548
549 (defmethod note-output-record-lost-sheet :after ((record compound-output-record) sheet)
550 (map-over-output-records #'note-output-record-lost-sheet record 0 0 sheet))
551
552 (defmethod note-output-record-got-sheet ((record output-record) sheet)
553 (declare (ignore record sheet))
554 (values))
555
556 (defmethod note-output-record-got-sheet :after ((record compound-output-record) sheet)
557 (map-over-output-records #'note-output-record-got-sheet record 0 0 sheet))
558
559 (defun find-output-record-sheet (record)
560 "Walks up the parents of RECORD, searching for an output history from which
561 the associated sheet can be determined."
562 (typecase record
563 (stream-output-history-mixin (output-history-stream record))
564 (basic-output-record (find-output-record-sheet (output-record-parent record)))))
565
566 (defmethod output-record-children ((record basic-output-record))
567 nil)
568
569 (defmethod add-output-record (child (record basic-output-record))
570 (declare (ignore child))
571 (error "Cannot add a child to ~S." record))
572
573 (defmethod add-output-record :before (child (record compound-output-record))
574 (let ((parent (output-record-parent child)))
575 (cond (parent
576 (restart-case
577 (error "~S already has a parent ~S." child parent)
578 (delete ()
579 :report "Delete from the old parent."
580 (delete-output-record child parent))))
581 ((eq record child)
582 (error "~S is being added to itself" record))
583 ((eq (output-record-parent record) child)
584 (error "child ~S is being added to its own child ~S"
585 child record)))))
586
587 (defmethod add-output-record :after (child (record compound-output-record))
588 (recompute-extent-for-new-child record child)
589 (when (eq record (output-record-parent child))
590 (let ((sheet (find-output-record-sheet record)))
591 (when sheet (note-output-record-got-sheet child sheet)))))
592
593 (defmethod delete-output-record :before (child (record basic-output-record)
594 &optional (errorp t))
595 (declare (ignore errorp))
596 (let ((sheet (find-output-record-sheet record)))
597 (when sheet
598 (note-output-record-lost-sheet child sheet))))
599
600 (defmethod delete-output-record (child (record basic-output-record)
601 &optional (errorp t))
602 (declare (ignore child))
603 (when errorp (error "Cannot delete a child from ~S." record)))
604
605 (defmethod delete-output-record :after (child (record compound-output-record)
606 &optional (errorp t))
607 (declare (ignore errorp))
608 (with-bounding-rectangle* (x1 y1 x2 y2) child
609 (recompute-extent-for-changed-child record child x1 y1 x2 y2)))
610
611 (defmethod clear-output-record ((record basic-output-record))
612 (error "Cannot clear ~S." record))
613
614 (defmethod clear-output-record :before ((record compound-output-record))
615 (let ((sheet (find-output-record-sheet record)))
616 (when sheet
617 (map-over-output-records #'note-output-record-lost-sheet record 0 0 sheet))))
618
619 (defmethod clear-output-record :around ((record compound-output-record))
620 (multiple-value-bind (x1 y1 x2 y2) (bounding-rectangle* record)
621 (call-next-method)
622 (assert (null-bounding-rectangle-p record))
623 (when (output-record-parent record)
624 (recompute-extent-for-changed-child
625 (output-record-parent record) record x1 y1 x2 y2))))
626
627 (defmethod clear-output-record :after ((record compound-output-record))
628 ;; XXX banish x and y
629 (with-slots (x y) record
630 (setf (rectangle-edges* record) (values x y x y))))
631
632 (defmethod output-record-count ((record displayed-output-record))
633 0)
634
635 (defmethod map-over-output-records-1
636 (function (record displayed-output-record) function-args)
637 (declare (ignore function function-args))
638 nil)
639
640 ;;; This needs to work in "most recently added last" order. Is this
641 ;;; implementation right? -- APD, 2002-06-13
642 #+nil
643 (defmethod map-over-output-records
644 (function (record compound-output-record)
645 &optional (x-offset 0) (y-offset 0)
646 &rest function-args)
647 (declare (ignore x-offset y-offset))
648 (map nil (lambda (child) (apply function child function-args))
649 (output-record-children record)))
650
651 (defmethod map-over-output-records-containing-position
652 (function (record displayed-output-record) x y
653 &optional (x-offset 0) (y-offset 0)
654 &rest function-args)
655 (declare (ignore function x y x-offset y-offset function-args))
656 nil)
657
658 ;;; This needs to work in "most recently added first" order. Is this
659 ;;; implementation right? -- APD, 2002-06-13
660 #+nil
661 (defmethod map-over-output-records-containing-position
662 (function (record compound-output-record) x y
663 &optional (x-offset 0) (y-offset 0)
664 &rest function-args)
665 (declare (ignore x-offset y-offset))
666 (map nil
667 (lambda (child)
668 (when (and (multiple-value-bind (min-x min-y max-x max-y)
669 (output-record-hit-detection-rectangle* child)
670 (and (<= min-x x max-x) (<= min-y y max-y)))
671 (output-record-refined-position-test child x y))
672 (apply function child function-args)))
673 (output-record-children record)))
674
675 (defmethod map-over-output-records-overlapping-region
676 (function (record displayed-output-record) region
677 &optional (x-offset 0) (y-offset 0)
678 &rest function-args)
679 (declare (ignore function region x-offset y-offset function-args))
680 nil)
681
682 ;;; This needs to work in "most recently added last" order. Is this
683 ;;; implementation right? -- APD, 2002-06-13
684 #+nil
685 (defmethod map-over-output-records-overlapping-region
686 (function (record compound-output-record) region
687 &optional (x-offset 0) (y-offset 0)
688 &rest function-args)
689 (declare (ignore x-offset y-offset))
690 (map nil
691 (lambda (child) (when (region-intersects-region-p region child)
692 (apply function child function-args)))
693 (output-record-children record)))
694
695 ;;; XXX Dunno about this definition... -- moore
696 ;;; Your apprehension is justified, but we lack a better means by which
697 ;;; to distinguish "empty" compound records (roots of trees of compound
698 ;;; records, containing no non-compound records). Such subtrees should
699 ;;; not affect bounding rectangles. -- Hefner
700 (defun null-bounding-rectangle-p (bbox)
701 (with-bounding-rectangle* (x1 y1 x2 y2) bbox
702 (and (= x1 x2)
703 (= y1 y2))))
704
705 ;;; 16.2.3. Output Record Change Notification Protocol
706 (defmethod recompute-extent-for-new-child
707 ((record compound-output-record) child)
708 (unless (null-bounding-rectangle-p child)
709 (with-bounding-rectangle* (old-x1 old-y1 old-x2 old-y2) record
710 (cond
711 ((null-bounding-rectangle-p record)
712 (setf (rectangle-edges* record) (bounding-rectangle* child)))
713 ((not (null-bounding-rectangle-p child))
714 (assert (not (null-bounding-rectangle-p record))) ; important.
715 (with-bounding-rectangle* (x1-child y1-child x2-child y2-child)
716 child
717 (setf (rectangle-edges* record)
718 (values (min old-x1 x1-child) (min old-y1 y1-child)
719 (max old-x2 x2-child) (max old-y2 y2-child))))))
720 (let ((parent (output-record-parent record)))
721 (when parent
722 (recompute-extent-for-changed-child
723 parent record old-x1 old-y1 old-x2 old-y2)))))
724 record)
725
726 (defmethod %tree-recompute-extent* ((record compound-output-record))
727 ;; Internal helper function
728 (let ((new-x1 0)
729 (new-y1 0)
730 (new-x2 0)
731 (new-y2 0)
732 (first-time t))
733 (map-over-output-records
734 (lambda (child)
735 (unless (null-bounding-rectangle-p child)
736 (if first-time
737 (progn
738 (multiple-value-setq (new-x1 new-y1 new-x2 new-y2)
739 (bounding-rectangle* child))
740 (setq first-time nil))
741 (with-bounding-rectangle* (cx1 cy1 cx2 cy2) child
742 (minf new-x1 cx1)
743 (minf new-y1 cy1)
744 (maxf new-x2 cx2)
745 (maxf new-y2 cy2)))))
746 record)
747 (if first-time
748 ;; XXX banish x y
749 (with-slots (x y) record
750 (values x y x y))
751 (values new-x1 new-y1 new-x2 new-y2))))
752
753 (defgeneric tree-recompute-extent-aux (record))
754
755 (defmethod tree-recompute-extent-aux (record)
756 (bounding-rectangle* record))
757
758 (defmethod tree-recompute-extent-aux ((record compound-output-record))
759 (let ((new-x1 0)
760 (new-y1 0)
761 (new-x2 0)
762 (new-y2 0)
763 (first-time t))
764 (map-over-output-records
765 (lambda (child)
766 (if first-time
767 (progn
768 (multiple-value-setq (new-x1 new-y1 new-x2 new-y2)
769 (tree-recompute-extent-aux child))
770 (setq first-time nil))
771 (multiple-value-bind (cx1 cy1 cx2 cy2)
772 (tree-recompute-extent-aux child)
773 (minf new-x1 cx1)
774 (minf new-y1 cy1)
775 (maxf new-x2 cx2)
776 (maxf new-y2 cy2))))
777 record)
778 (with-slots (x y)
779 record
780 (if first-time ;No children
781 (bounding-rectangle* record)
782 (progn
783 ;; XXX banish x,y
784 (setf x new-x1 y new-y1)
785 (setf (rectangle-edges* record)
786 (values new-x1 new-y1 new-x2 new-y2)))))))
787
788 (defmethod recompute-extent-for-changed-child
789 ((record compound-output-record) changed-child
790 old-min-x old-min-y old-max-x old-max-y)
791 (with-bounding-rectangle* (ox1 oy1 ox2 oy2) record
792 (with-bounding-rectangle* (cx1 cy1 cx2 cy2) changed-child
793 ;; If record is currently empty, use the child's bbox directly. Else..
794 ;; Does the new rectangle of the child contain the original rectangle?
795 ;; If so, we can use min/max to grow record's current rectangle.
796 ;; If not, the child has shrunk, and we need to fully recompute.
797 (multiple-value-bind (nx1 ny1 nx2 ny2)
798 (cond
799 ;; The child has been deleted; who knows what the
800 ;; new bounding box might be.
801 ;; This case shouldn't be really necessary.
802 ((not (output-record-parent changed-child))
803 (%tree-recompute-extent* record))
804 ;; Only one child of record, and we already have the bounds.
805 ((eql (output-record-count record) 1)
806 ;; See output-record-children for why this assert breaks:
807 ;; (assert (eq changed-child (elt (output-record-children record) 0)))
808 (values cx1 cy1 cx2 cy2))
809 ;; If our record occupied no space (had no children, or had only
810 ;; children similarly occupying no space, hackishly determined by
811 ;; null-bounding-rectangle-p), recompute the extent now, otherwise
812 ;; the next COND clause would, as an optimization, attempt to extend
813 ;; our current bounding rectangle, which is invalid.
814 ((null-bounding-rectangle-p record)
815 (%tree-recompute-extent* record))
816 ;; In the following cases, we can grow the new bounding rectangle
817 ;; from its previous state:
818 ((or
819 ;; If the child was originally empty, it could not have affected
820 ;; previous computation of our bounding rectangle.
821 ;; This is hackish for reasons similar to the above.
822 (and (= old-min-x old-max-x) (= old-min-y old-max-y))
823 ;; For each edge of the original child bounds, if it was within
824 ;; its respective edge of the old parent bounding rectangle,
825 ;; or if it has not changed:
826 (and (or (> old-min-x ox1) (= old-min-x cx1))
827 (or (> old-min-y oy1) (= old-min-y cy1))
828 (or (< old-max-x ox2) (= old-max-x cx2))
829 (or (< old-max-y oy2) (= old-max-y cy2)))
830 ;; New child bounds contain old child bounds, so use min/max
831 ;; to extend the already-calculated rectangle.
832 (and (<= cx1 old-min-x) (<= cy1 old-min-y)
833 (>= cx2 old-max-x) (>= cy2 old-max-y)))
834 (values (min cx1 ox1) (min cy1 oy1)
835 (max cx2 ox2) (max cy2 oy2)))
836 ;; No shortcuts - we must compute a new bounding box from those of
837 ;; all our children. We want to avoid this - in worst cases, such as
838 ;; a toplevel output history, large graph, or table, there may exist
839 ;; thousands of children. Without the above optimizations,
840 ;; construction becomes O(N^2) due to bounding rectangle calculation.
841 (t (%tree-recompute-extent* record)))
842 ;; XXX banish x, y
843 (with-slots (x y)
844 record
845 (setf x nx1 y ny1)
846 (setf (rectangle-edges* record) (values nx1 ny1 nx2 ny2))
847 (let ((parent (output-record-parent record)))
848 (unless (or (null parent)
849 (and (= nx1 ox1) (= ny1 oy1)
850 (= nx2 ox2) (= nx2 oy2)))
851 (recompute-extent-for-changed-child parent record
852 ox1 oy1 ox2 oy2)))))))
853 record)
854
855 (defmethod tree-recompute-extent ((record compound-output-record))
856 (tree-recompute-extent-aux record)
857 record)
858
859 (defmethod tree-recompute-extent :around ((record compound-output-record))
860 (with-bounding-rectangle* (old-x1 old-y1 old-x2 old-y2)
861 record
862 (call-next-method)
863 (with-bounding-rectangle* (x1 y1 x2 y2)
864 record
865 (let ((parent (output-record-parent record)))
866 (when (and parent
867 (not (and (= old-x1 x1)
868 (= old-y1 y1)
869 (= old-x2 x2)
870 (= old-y2 y2))))
871 (recompute-extent-for-changed-child parent record
872 old-x1 old-y1
873 old-x2 old-y2)))))
874 record)
875
876 ;;; 16.3.1. Standard output record classes
877
878 (defclass standard-sequence-output-record (compound-output-record)
879 ((children :initform (make-array 8 :adjustable t :fill-pointer 0)
880 :reader output-record-children)))
881
882 (defmethod add-output-record (child (record standard-sequence-output-record))
883 (vector-push-extend child (output-record-children record))
884 (setf (output-record-parent child) record))
885
886 (defmethod delete-output-record (child (record standard-sequence-output-record)
887 &optional (errorp t))
888 (with-slots (children) record
889 (let ((pos (position child children :test #'eq)))
890 (if (null pos)
891 (when errorp
892 (error "~S is not a child of ~S" child record))
893 (progn
894 (setq children (replace children children
895 :start1 pos
896 :start2 (1+ pos)))
897 (decf (fill-pointer children))
898 (setf (output-record-parent child) nil))))))
899
900 (defmethod clear-output-record ((record standard-sequence-output-record))
901 (let ((children (output-record-children record)))
902 (map 'nil (lambda (child) (setf (output-record-parent child) nil))
903 children)
904 (fill children nil)
905 (setf (fill-pointer children) 0)))
906
907 (defmethod output-record-count ((record standard-sequence-output-record))
908 (length (output-record-children record)))
909
910 (defmethod map-over-output-records-1
911 (function (record standard-sequence-output-record) function-args)
912 "Applies FUNCTION to all children in the order they were added."
913 (if function-args
914 (loop with children = (output-record-children record)
915 for child across children
916 do (apply function child function-args))
917 (loop with children = (output-record-children record)
918 for child across children
919 do (funcall function child))))
920
921 (defmethod map-over-output-records-containing-position
922 (function (record standard-sequence-output-record) x y
923 &optional (x-offset 0) (y-offset 0)
924 &rest function-args)
925 "Applies FUNCTION to children, containing (X,Y), in the reversed
926 order they were added."
927 (declare (ignore x-offset y-offset))
928 (loop with children = (output-record-children record)
929 for i from (1- (length children)) downto 0
930 for child = (aref children i)
931 when (and (multiple-value-bind (min-x min-y max-x max-y)
932 (output-record-hit-detection-rectangle* child)
933 (and (<= min-x x max-x) (<= min-y y max-y)))
934 (output-record-refined-position-test child x y))
935 do (apply function child function-args)))
936
937 (defmethod map-over-output-records-overlapping-region
938 (function (record standard-sequence-output-record) region
939 &optional (x-offset 0) (y-offset 0)
940 &rest function-args)
941 "Applies FUNCTION to children, overlapping REGION, in the order they
942 were added."
943 (declare (ignore x-offset y-offset))
944 (loop with children = (output-record-children record)
945 for child across children
946 when (region-intersects-region-p region child)
947 do (apply function child function-args)))
948
949
950 ;;; tree output recording
951
952 (defclass tree-output-record-entry ()
953 ((record :initarg :record :reader tree-output-record-entry-record)
954 (cached-rectangle :initform nil :accessor tree-output-record-entry-cached-rectangle)
955 (inserted-nr :initarg :inserted-nr :accessor tree-output-record-entry-inserted-nr)))
956
957 (defun make-tree-output-record-entry (record inserted-nr)
958 (make-instance 'tree-output-record-entry :record record :inserted-nr inserted-nr))
959
960 (defun %record-to-spatial-tree-rectangle (r)
961 (rectangles:make-rectangle
962 :lows `(,(bounding-rectangle-min-x r)
963 ,(bounding-rectangle-min-y r))
964 :highs `(,(bounding-rectangle-max-x r)
965 ,(bounding-rectangle-max-y r))))
966
967 (defun %output-record-entry-to-spatial-tree-rectangle (r)
968 (when (null (tree-output-record-entry-cached-rectangle r))
969 (let* ((record (tree-output-record-entry-record r)))
970 (setf (tree-output-record-entry-cached-rectangle r) (%record-to-spatial-tree-rectangle record))))
971 (tree-output-record-entry-cached-rectangle r))
972
973 (defun %make-tree-output-record-tree ()
974 (spatial-trees:make-spatial-tree :r
975 :rectfun #'%output-record-entry-to-spatial-tree-rectangle))
976
977 (defclass standard-tree-output-record (compound-output-record)
978 ((children :initform (%make-tree-output-record-tree)
979 :accessor %tree-record-children)
980 (children-hash :initform (make-hash-table :test #'eql) :reader %tree-record-children-cache)
981 (child-count :initform 0)
982 (last-insertion-nr :initform 0 :accessor last-insertion-nr)))
983
984 (defun %entry-in-children-cache (record entry)
985 (gethash entry (%tree-record-children-cache record)))
986
987 (defun (setf %entry-in-children-cache) (new-val record entry)
988 (setf (gethash entry (%tree-record-children-cache record)) new-val))
989
990 (defun %remove-entry-from-children-cache (record entry)
991 (remhash entry (%tree-record-children-cache record)))
992
993 (defmethod output-record-children ((record standard-tree-output-record))
994 (with-bounding-rectangle* (min-x min-y max-x max-y) record
995 (map 'list
996 #'tree-output-record-entry-record
997 (spatial-trees:search
998 ;; Originally, (%record-to-spatial-tree-rectangle record).
999 ;; The form below intends to fix output-record-children not
1000 ;; reporting empty children, which may lie outside the reported
1001 ;; bounding rectangle of their parent.
1002 ;; Assumption: null bounding records are always at the origin.
1003 ;; I've never noticed this violated, but it's out of line with
1004 ;; what null-bounding-rectangle-p checks, and setf of
1005 ;; output-record-position may invalidate it. Seems to work, but
1006 ;; fix that and try again later.
1007 ;; Note that max x or y may be less than zero..
1008 (rectangles:make-rectangle
1009 :lows (list (min 0 min-x) (min 0 min-y))
1010 :highs (list (max 0 max-x) (max 0 max-y)))
1011 (%tree-record-children record)))))
1012
1013 (defmethod add-output-record (child (record standard-tree-output-record))
1014 (let ((entry (make-tree-output-record-entry child (incf (last-insertion-nr record)))))
1015 (spatial-trees:insert entry (%tree-record-children record))
1016 (setf (output-record-parent child) record)
1017 (setf (%entry-in-children-cache record child) entry))
1018 (incf (slot-value record 'child-count))
1019 (values))
1020
1021 (defmethod delete-output-record (child (record standard-tree-output-record) &optional (errorp t))
1022 (let ((entry (find child (spatial-trees:search (%entry-in-children-cache record child)
1023 (%tree-record-children record))
1024 :key #'tree-output-record-entry-record)))
1025 (decf (slot-value record 'child-count))
1026 (cond
1027 ((not (null entry))
1028 (spatial-trees:delete entry (%tree-record-children record))
1029 (%remove-entry-from-children-cache record child)
1030 (setf (output-record-parent child) nil))
1031 (errorp (error "~S is not a child of ~S" child record)))))
1032
1033 (defmethod clear-output-record ((record standard-tree-output-record))
1034 (map nil (lambda (child)
1035 (setf (output-record-parent child) nil)
1036 (%remove-entry-from-children-cache record child))
1037 (output-record-children record))
1038 (setf (slot-value record 'child-count) 0)
1039 (setf (%tree-record-children record) (%make-tree-output-record-tree)))
1040
1041 (defmethod output-record-count ((record standard-tree-output-record))
1042 (slot-value record 'child-count))
1043
1044 (defun map-over-tree-output-records (function record rectangle sort-order function-args)
1045 (dolist (child (sort (spatial-trees:search rectangle
1046 (%tree-record-children record))
1047 (ecase sort-order
1048 (:most-recent-first #'>)
1049 (:most-recent-last #'<))
1050 :key #'tree-output-record-entry-inserted-nr))
1051 (apply function (tree-output-record-entry-record child) function-args)))
1052
1053 (defmethod map-over-output-records-1 (function (record standard-tree-output-record) function-args)
1054 (map-over-tree-output-records function record
1055 (%record-to-spatial-tree-rectangle record) :most-recent-last
1056 function-args))
1057
1058 (defmethod map-over-output-records-containing-position
1059 (function (record standard-tree-output-record) x y
1060 &optional x-offset y-offset &rest function-args)
1061 (declare (ignore x-offset y-offset))
1062 (map-over-tree-output-records function record
1063 (rectangles:make-rectangle :lows `(,x ,y) :highs `(,x ,y)) :most-recent-first
1064 function-args))
1065
1066 (defmethod map-over-output-records-overlapping-region
1067 (function (record standard-tree-output-record) region
1068 &optional x-offset y-offset &rest function-args)
1069 (declare (ignore x-offset y-offset))
1070 (typecase region
1071 (everywhere-region (map-over-output-records-1 function record function-args))
1072 (nowhere-region nil)
1073 (otherwise (map-over-tree-output-records
1074 (lambda (child)
1075 (if (region-intersects-region-p (multiple-value-call 'make-rectangle* (bounding-rectangle* child))
1076 region)
1077 (apply function child function-args)))
1078 record (%record-to-spatial-tree-rectangle (bounding-rectangle region)) :most-recent-last
1079 nil))))
1080
1081 (defmethod recompute-extent-for-changed-child :around ((record standard-tree-output-record) child old-min-x old-min-y old-max-x old-max-y)
1082 (when (eql record (output-record-parent child))
1083 (let ((entry (%entry-in-children-cache record child)))
1084 (spatial-trees:delete entry (%tree-record-children record))
1085 (setf (tree-output-record-entry-cached-rectangle entry) nil)
1086 (spatial-trees:insert entry (%tree-record-children record))))
1087 (call-next-method))
1088
1089 ;;;
1090
1091 (defmethod match-output-records ((record t) &rest args)
1092 (apply #'match-output-records-1 record args))
1093
1094 ;;; Factor out the graphics state portions of the output records so
1095 ;;; they can be manipulated seperately e.g., by incremental
1096 ;;; display. The individual slots of a graphics state are factored into mixin
1097 ;;; classes so that each output record can capture only the state that it needs.
1098 ;;; -- moore
1099
1100 ;;; It would be appealing to define a setf method, e.g. (setf
1101 ;;; medium-graphics-state), for setting a medium's state from a graphics state
1102 ;;; object, but that would require us to define a medium-graphics-state reader
1103 ;;; that would cons a state object. I don't want to do that.
1104
1105 (defclass graphics-state ()
1106 ()
1107 (:documentation "Stores those parts of the medium/stream graphics state
1108 that need to be restored when drawing an output record"))
1109
1110 (defclass gs-ink-mixin (graphics-state)
1111 ((ink :initarg :ink :accessor graphics-state-ink)))
1112
1113 (defmethod initialize-instance :after ((obj gs-ink-mixin)
1114 &key (stream nil)
1115 (medium (when stream
1116 (sheet-medium stream))))
1117 (when (and medium (not (slot-boundp obj 'ink)))
1118 (setf (slot-value obj 'ink) (medium-ink medium))))
1119
1120 (defmethod replay-output-record :around
1121 ((record gs-ink-mixin) stream &optional region x-offset y-offset)
1122 (declare (ignore region x-offset y-offset))
1123 (with-drawing-options (stream :ink (graphics-state-ink record))
1124 (call-next-method)))
1125
1126 (defrecord-predicate gs-ink-mixin (ink)
1127 (if-supplied (ink)
1128 (design-equalp (slot-value record 'ink) ink)))
1129
1130 (defclass gs-clip-mixin (graphics-state)
1131 ((clip :initarg :clipping-region :accessor graphics-state-clip
1132 :documentation "Clipping region in stream coordinates.")))
1133
1134 (defmethod initialize-instance :after ((obj gs-clip-mixin)
1135 &key (stream nil)
1136 (medium (when stream
1137 (sheet-medium stream))))
1138 (when medium
1139 (with-slots (clip)
1140 obj
1141 (let ((clip-region (if (slot-boundp obj 'clip)
1142 (region-intersection (medium-clipping-region
1143 medium)
1144 clip)
1145 (medium-clipping-region medium))))
1146 (setq clip (transform-region (medium-transformation medium)
1147 clip-region))))))
1148
1149 (defmethod replay-output-record :around
1150 ((record gs-clip-mixin) stream &optional region x-offset y-offset)
1151 (declare (ignore region x-offset y-offset))
1152 (let ((clipping-region (graphics-state-clip record)))
1153 (if (or (eq clipping-region +everywhere+) ; !!!
1154 (region-contains-region-p clipping-region (medium-clipping-region stream)))
1155 (call-next-method)
1156 (with-drawing-options (stream :clipping-region (graphics-state-clip record))
1157 (call-next-method)))))
1158
1159 (defrecord-predicate gs-clip-mixin ((:clipping-region clip))
1160 (if-supplied (clip)
1161 (region-equal (slot-value record 'clip) clip)))
1162
1163 ;;; 16.3.2. Graphics Displayed Output Records
1164 (defclass standard-displayed-output-record (gs-clip-mixin gs-ink-mixin
1165 basic-output-record
1166 displayed-output-record)
1167 ((ink :reader displayed-output-record-ink)
1168 (stream :initarg :stream))
1169 (:documentation "Implementation class for DISPLAYED-OUTPUT-RECORD.")
1170 (:default-initargs :stream nil))
1171
1172 (defclass gs-line-style-mixin (graphics-state)
1173 ((line-style :initarg :line-style :accessor graphics-state-line-style)))
1174
1175 (defmethod initialize-instance :after ((obj gs-line-style-mixin)
1176 &key (stream nil)
1177 (medium (when stream
1178 (sheet-medium stream))))
1179 (when medium
1180 (unless (slot-boundp obj 'line-style)
1181 (setf (slot-value obj 'line-style) (medium-line-style medium)))))
1182
1183 (defmethod replay-output-record :around
1184 ((record gs-line-style-mixin) stream &optional region x-offset y-offset)
1185 (declare (ignore region x-offset y-offset))
1186 (with-drawing-options (stream :line-style (graphics-state-line-style record))
1187 (call-next-method)))
1188
1189 (defrecord-predicate gs-line-style-mixin (line-style)
1190 (if-supplied (line-style)
1191 (line-style-equalp (slot-value record 'line-style) line-style)))
1192
1193 (defgeneric graphics-state-line-style-border (record medium)
1194 (:method ((record gs-line-style-mixin) medium)
1195 (/ (line-style-effective-thickness (graphics-state-line-style record)
1196 medium)
1197 2)))
1198
1199 (defclass gs-text-style-mixin (graphics-state)
1200 ((text-style :initarg :text-style :accessor graphics-state-text-style)))
1201
1202 (defmethod initialize-instance :after ((obj gs-text-style-mixin)
1203 &key (stream nil)
1204 (medium (when stream
1205 (sheet-medium stream))))
1206 (when medium
1207 (unless (slot-boundp obj 'text-style)
1208 (setf (slot-value obj 'text-style) (medium-text-style medium)))))
1209
1210 (defmethod replay-output-record :around
1211 ((record gs-text-style-mixin) stream &optional region x-offset y-offset)
1212 (declare (ignore region x-offset y-offset))
1213 (with-drawing-options (stream :text-style (graphics-state-text-style record))
1214 (call-next-method)))
1215
1216 (defrecord-predicate gs-text-style-mixin (text-style)
1217 (if-supplied (text-style)
1218 (text-style-equalp (slot-value record 'text-style) text-style)))
1219
1220 (defclass standard-graphics-displayed-output-record
1221 (standard-displayed-output-record
1222 graphics-displayed-output-record)
1223 ())
1224
1225 (defmethod match-output-records-1 and
1226 ((record standard-displayed-output-record)
1227 &key (x1 nil x1-p) (y1 nil y1-p)
1228 (x2 nil x2-p) (y2 nil y2-p)
1229 (bounding-rectangle nil bounding-rectangle-p))
1230 (if bounding-rectangle-p
1231 (region-equal record bounding-rectangle)
1232 (multiple-value-bind (my-x1 my-y1 my-x2 my-y2)
1233 (bounding-rectangle* record)
1234 (macrolet ((coordinate=-or-lose (key mine)
1235 `(if (typep ,key 'coordinate)
1236 (coordinate= ,mine ,key)
1237 (error 'type-error
1238 :datum ,key
1239 :expected-type 'coordinate))))
1240 (and (or (null x1-p)
1241 (coordinate=-or-lose x1 my-x1))
1242 (or (null y1-p)
1243 (coordinate=-or-lose y1 my-y1))
1244 (or (null x2-p)
1245 (coordinate=-or-lose x2 my-x2))
1246 (or (null y2-p)
1247 (coordinate=-or-lose y2 my-y2)))))))
1248
1249 (defmethod output-record-equal and ((record standard-displayed-output-record)
1250 (record2 standard-displayed-output-record))
1251 (region-equal record record2))
1252
1253 (defclass coord-seq-mixin ()
1254 ((coord-seq :accessor coord-seq :initarg :coord-seq))
1255 (:documentation "Mixin class that implements methods for records that contain
1256 sequences of coordinates."))
1257
1258 (defun coord-seq-bounds (coord-seq border)
1259 (setf border (ceiling border))
1260 (let* ((min-x (elt coord-seq 0))
1261 (min-y (elt coord-seq 1))
1262 (max-x min-x)
1263 (max-y min-y))
1264 (do-sequence ((x y) coord-seq)
1265 (minf min-x x)
1266 (minf min-y y)
1267 (maxf max-x x)
1268 (maxf max-y y))
1269 (values (floor (- min-x border))
1270 (floor (- min-y border))
1271 (ceiling (+ max-x border))
1272 (ceiling (+ max-y border)))))
1273
1274 ;;; record must be a standard-rectangle
1275
1276 (defmethod* (setf output-record-position) :around
1277 (nx ny (record coord-seq-mixin))
1278 (with-standard-rectangle* (:x1 x1 :y1 y1)
1279 record
1280 (let ((dx (- nx x1))
1281 (dy (- ny y1))
1282 (coords (slot-value record 'coord-seq)))
1283 (multiple-value-prog1
1284 (call-next-method)
1285 (loop for i from 0 below (length coords) by 2
1286 do (progn
1287 (incf (aref coords i) dx)
1288 (incf (aref coords (1+ i)) dy)))))))
1289
1290 (defmethod match-output-records-1 and ((record coord-seq-mixin)
1291 &key (coord-seq nil coord-seq-p))
1292 (or (null coord-seq-p)
1293 (let* ((my-coord-seq (slot-value record 'coord-seq))
1294 (len (length my-coord-seq)))
1295 (and (eql len (length coord-seq))
1296 (loop for elt1 across my-coord-seq
1297 for elt2 across coord-seq
1298 always (coordinate= elt1 elt2))))))
1299
1300 (defmacro generate-medium-recording-body (class-name method-name args)
1301 (let ((arg-list (loop for arg in args
1302 nconc `(,(intern (symbol-name arg) :keyword) ,arg))))
1303 `(with-sheet-medium (medium stream)
1304 (when (stream-recording-p stream)
1305 (let ((record
1306 ;; Hack: the coord-seq-mixin makes the assumption that, well
1307 ;; coord-seq is a coord-vector. So we morph a possible
1308 ;; coord-seq argument into a vector.
1309 (let (,@(when (member 'coord-seq args)
1310 `((coord-seq
1311 (if (vectorp coord-seq)
1312 coord-seq
1313 (coerce coord-seq 'vector))))))
1314 (make-instance ',class-name
1315 :stream stream
1316 ,@arg-list))))
1317 (stream-add-output-record stream record)))
1318 (when (stream-drawing-p stream)
1319 (,method-name medium ,@args)))))
1320
1321 ;; DEF-GRECORDING: This is the central interface through which recording
1322 ;; is implemented for drawing functions. The body provided is used to
1323 ;; compute the bounding rectangle of the rendered output. DEF-GRECORDING
1324 ;; will define a class for the output record, with slots corresponding to the
1325 ;; drawing function arguments. It also defines an INITIALIZE-INSTANCE method
1326 ;; computing the bounding rectangle of the record. It defines a method for
1327 ;; the medium drawing function specialized on output-recording-stream, which
1328 ;; is responsible for creating the output record and adding it to the stream
1329 ;; history. It also defines a REPLAY-OUTPUT-RECORD method, which calls the
1330 ;; medium drawing function based on the recorded slots.
1331
1332 (defmacro def-grecording (name ((&rest mixins) &rest args)
1333 (&key (class t)
1334 (medium-fn t)
1335 (replay-fn t)) &body body)
1336 (let ((method-name (symbol-concat '#:medium- name '*))
1337 (class-name (symbol-concat name '#:-output-record))
1338 (medium (gensym "MEDIUM"))
1339 (class-vars `((stream :initarg :stream)
1340 ,@(loop for arg in args
1341 collect `(,arg
1342 :initarg ,(intern (symbol-name arg)
1343 :keyword))))))
1344 `(progn
1345 ,@(when class
1346 `((defclass ,class-name (,@mixins standard-graphics-displayed-output-record)
1347 ,class-vars)
1348 (defmethod initialize-instance :after ((graphic ,class-name)
1349 &key)
1350 (with-slots (stream ink clipping-region
1351 line-style text-style ,@args)
1352 graphic
1353 (let* ((medium (sheet-medium stream)))
1354 (setf (rectangle-edges* graphic)
1355 (progn ,@body)))))))
1356 ,(when medium-fn
1357 `(defmethod ,method-name :around ((stream output-recording-stream) ,@args)
1358 ;; XXX STANDARD-OUTPUT-RECORDING-STREAM ^?
1359 (generate-medium-recording-body ,class-name ,method-name ,args)))
1360 ,(when replay-fn
1361 `(defmethod replay-output-record ((record ,class-name) stream
1362 &optional (region +everywhere+)
1363 (x-offset 0) (y-offset 0))
1364 (declare (ignore x-offset y-offset region))
1365 (with-slots (,@args) record
1366 (let ((,medium (sheet-medium stream))
1367 ;; is sheet a sheet-with-medium-mixin? --GB
1368 )
1369 ;; Graphics state is set up in :around method.
1370 (,method-name ,medium ,@args))))))))
1371
1372 (def-grecording draw-point ((gs-line-style-mixin) point-x point-y) ()
1373 (let ((border (graphics-state-line-style-border graphic medium)))
1374 (with-transformed-position ((medium-transformation medium) point-x point-y)
1375 (setf (slot-value graphic 'point-x) point-x
1376 (slot-value graphic 'point-y) point-y)
1377 (values (- point-x border)
1378 (- point-y border)
1379 (+ point-x border)
1380 (+ point-y border)))))
1381
1382 (defmethod* (setf output-record-position) :around
1383 (nx ny (record draw-point-output-record))
1384 (with-standard-rectangle* (:x1 x1 :y1 y1)
1385 record
1386 (with-slots (point-x point-y)
1387 record
1388 (let ((dx (- nx x1))
1389 (dy (- ny y1)))
1390 (multiple-value-prog1
1391 (call-next-method)
1392 (incf point-x dx)
1393 (incf point-y dy))))))
1394
1395 (defrecord-predicate draw-point-output-record (point-x point-y)
1396 (and (if-supplied (point-x coordinate)
1397 (coordinate= (slot-value record 'point-x) point-x))
1398 (if-supplied (point-y coordinate)
1399 (coordinate= (slot-value record 'point-y) point-y))))
1400
1401 (def-grecording draw-points ((coord-seq-mixin gs-line-style-mixin) coord-seq) ()
1402 (let ((transformed-coord-seq (transform-positions (medium-transformation medium) coord-seq))
1403 (border (graphics-state-line-style-border graphic medium)))
1404 (setf (slot-value graphic 'coord-seq) transformed-coord-seq)
1405 (coord-seq-bounds transformed-coord-seq border)))
1406
1407 (def-grecording draw-line ((gs-line-style-mixin)
1408 point-x1 point-y1 point-x2 point-y2) ()
1409 (let ((transform (medium-transformation medium))
1410 (border (graphics-state-line-style-border graphic medium)))
1411 (with-transformed-position (transform point-x1 point-y1)
1412 (with-transformed-position (transform point-x2 point-y2)
1413 (setf (slot-value graphic 'point-x1) point-x1
1414 (slot-value graphic 'point-y1) point-y1
1415 (slot-value graphic 'point-x2) point-x2
1416 (slot-value graphic 'point-y2) point-y2)
1417 (values (- (min point-x1 point-x2) border)
1418 (- (min point-y1 point-y2) border)
1419 (+ (max point-x1 point-x2) border)
1420 (+ (max point-y1 point-y2) border))))))
1421
1422 (defmethod* (setf output-record-position) :around
1423 (nx ny (record draw-line-output-record))
1424 (with-standard-rectangle* (:x1 x1 :y1 y1)
1425 record
1426 (with-slots (point-x1 point-y1 point-x2 point-y2)
1427 record
1428 (let ((dx (- nx x1))
1429 (dy (- ny y1)))
1430 (multiple-value-prog1
1431 (call-next-method)
1432 (incf point-x1 dx)
1433 (incf point-y1 dy)
1434 (incf point-x2 dx)
1435 (incf point-y2 dy))))))
1436
1437 (defrecord-predicate draw-line-output-record (point-x1 point-y1
1438 point-x2 point-y2)
1439 (and (if-supplied (point-x1 coordinate)
1440 (coordinate= (slot-value record 'point-x1) point-x1))
1441 (if-supplied (point-y1 coordinate)
1442 (coordinate= (slot-value record 'point-y1) point-y1))
1443 (if-supplied (point-x2 coordinate)
1444 (coordinate= (slot-value record 'point-x2) point-x2))
1445 (if-supplied (point-y2 coordinate)
1446 (coordinate= (slot-value record 'point-y2) point-y2))))
1447
1448 (def-grecording draw-lines ((coord-seq-mixin gs-line-style-mixin) coord-seq) ()
1449 (let ((transformed-coord-seq (transform-positions (medium-transformation medium) coord-seq))
1450 (border (graphics-state-line-style-border graphic medium)))
1451 (setf coord-seq transformed-coord-seq)
1452 (coord-seq-bounds transformed-coord-seq border)))
1453
1454 ;;; (setf output-record-position) and predicates for draw-lines-output-record
1455 ;;; are taken care of by methods on superclasses.
1456
1457 ;;; Helper function
1458 (defun normalize-coords (dx dy &optional unit)
1459 (let ((norm (sqrt (+ (* dx dx) (* dy dy)))))
1460 (cond ((= norm 0.0d0)
1461 (values 0.0d0 0.0d0))
1462 (unit
1463 (let ((scale (/ unit norm)))
1464 (values (* dx scale) (* dy scale))))
1465 (t (values (/ dx norm) (/ dy norm))))))
1466
1467 (defun polygon-record-bounding-rectangle
1468 (coord-seq closed filled line-style border miter-limit)
1469 (cond (filled
1470 (coord-seq-bounds coord-seq 0))
1471 ((eq (line-style-joint-shape line-style) :round)
1472 (coord-seq-bounds coord-seq border))
1473 (t (let* ((x1 (svref coord-seq 0))
1474 (y1 (svref coord-seq 1))
1475 (min-x x1)
1476 (min-y y1)
1477 (max-x x1)
1478 (max-y y1)
1479 (len (length coord-seq)))
1480 (unless closed
1481 (setq min-x (- x1 border) min-y (- y1 border)
1482 max-x (+ x1 border) max-y (+ y1 border)))
1483 ;; Setup for iterating over the coordinate vector. If the polygon
1484 ;; is closed deal with the extra segment.
1485 (multiple-value-bind (initial-xp initial-yp
1486 final-xn final-yn
1487 initial-index final-index)
1488 (if closed
1489 (values (svref coord-seq (- len 2))
1490 (svref coord-seq (- len 1))
1491 x1 y1
1492 0 (- len 2))
1493 (values x1 y1
1494 (svref coord-seq (- len 2))
1495 (svref coord-seq (- len 1))
1496 2 (- len 4)))
1497 (ecase (line-style-joint-shape line-style)
1498 (:miter
1499 ;;FIXME: Remove successive positively proportional segments
1500 (loop with sin-limit = (sin (* 0.5 miter-limit))
1501 and xn and yn
1502 for i from initial-index to final-index by 2
1503 for xp = initial-xp then x
1504 for yp = initial-yp then y
1505 for x = (svref coord-seq i)
1506 for y = (svref coord-seq (1+ i))
1507 do (setf (values xn yn)
1508 (if (eql i final-index)
1509 (values final-xn final-yn)
1510 (values (svref coord-seq (+ i 2))
1511 (svref coord-seq (+ i
1512 3)))))
1513 (multiple-value-bind (ex1 ey1)
1514 (normalize-coords (- x xp) (- y yp))
1515 (multiple-value-bind (ex2 ey2)
1516 (normalize-coords (- x xn) (- y yn))
1517 (let* ((cos-a (+ (* ex1 ex2) (* ey1 ey2)))
1518 (sin-a/2 (sqrt (* 0.5 (- 1.0 cos-a)))))
1519 (if (< sin-a/2 sin-limit)
1520 (let ((nx (* border
1521 (max (abs ey1) (abs ey2))))
1522 (ny (* border
1523 (max (abs ex1) (abs ex2)))))
1524 (minf min-x (- x nx))
1525 (minf min-y (- y ny))
1526 (maxf max-x (+ x nx))
1527 (maxf max-y (+ y ny)))
1528 (let ((length (/ border sin-a/2)))
1529 (multiple-value-bind (dx dy)
1530 (normalize-coords (+ ex1 ex2)
1531 (+ ey1 ey2)
1532 length)
1533 (minf min-x (+ x dx))
1534 (minf min-y (+ y dy))
1535 (maxf max-x (+ x dx))
1536 (maxf max-y (+ y dy))))))))))
1537 ((:bevel :none)
1538 (loop with xn and yn
1539 for i from initial-index to final-index by 2
1540 for xp = initial-xp then x
1541 for yp = initial-yp then y
1542 for x = (svref coord-seq i)
1543 for y = (svref coord-seq (1+ i))
1544 do (setf (values xn yn)
1545 (if (eql i final-index)
1546 (values final-xn final-yn)
1547 (values (svref coord-seq (+ i 2))
1548 (svref coord-seq (+ i
1549 3)))))
1550 (multiple-value-bind (ex1 ey1)
1551 (normalize-coords (- x xp) (- y yp))
1552 (multiple-value-bind (ex2 ey2)
1553 (normalize-coords (- x xn) (- y yn))
1554 (let ((nx (* border (max (abs ey1) (abs ey2))))
1555 (ny (* border (max (abs ex1) (abs ex2)))))
1556 (minf min-x (- x nx))
1557 (minf min-y (- y ny))
1558 (maxf max-x (+ x nx))
1559 (maxf max-y (+ y ny))))))))
1560 (unless closed
1561 (multiple-value-bind (x y)
1562 (values (svref coord-seq final-index)
1563 (svref coord-seq (1+ final-index)))
1564 (minf min-x (- x border))
1565 (minf min-y (- y border))
1566 (maxf max-x (+ x border))
1567 (maxf max-y (+ y border)))))
1568 (values min-x min-y max-x max-y)))))
1569
1570 (def-grecording draw-polygon ((coord-seq-mixin gs-line-style-mixin)
1571 coord-seq closed filled) ()
1572 (let ((transformed-coord-seq (transform-positions (medium-transformation medium) coord-seq))
1573 (border (graphics-state-line-style-border graphic medium)))
1574 (setf coord-seq transformed-coord-seq)
1575 (polygon-record-bounding-rectangle transformed-coord-seq
1576 closed filled line-style border
1577 (medium-miter-limit medium))))
1578
1579 (defrecord-predicate draw-polygon-output-record (closed filled)
1580 (and (if-supplied (closed)
1581 (eql (slot-value record 'closed) closed))
1582 (if-supplied (filled)
1583 (eql (slot-value record 'filled) filled))))
1584
1585 (def-grecording draw-rectangle ((gs-line-style-mixin)
1586 left top right bottom filled) (:medium-fn nil)
1587 (let* ((transform (medium-transformation medium))
1588 (border (graphics-state-line-style-border graphic medium))
1589 (pre-coords (expand-rectangle-coords left top right bottom))
1590 (coords (transform-positions transform pre-coords)))
1591 (setf (values left top) (transform-position transform left top))
1592 (setf (values right bottom) (transform-position transform right bottom))
1593 (polygon-record-bounding-rectangle coords t filled line-style border
1594 (medium-miter-limit medium))))
1595
1596 (defmethod medium-draw-rectangle* :around ((stream output-recording-stream) left top right bottom filled)
1597 (let ((tr (medium-transformation stream)))
1598 (if (rectilinear-transformation-p tr)
1599 (generate-medium-recording-body draw-rectangle-output-record
1600 medium-draw-rectangle*
1601 (left top right bottom filled))
1602 (medium-draw-polygon* stream
1603 (expand-rectangle-coords left top right bottom)
1604 t
1605 filled))))
1606
1607 (defmethod* (setf output-record-position) :around
1608 (nx ny (record draw-rectangle-output-record))
1609 (with-standard-rectangle* (:x1 x1 :y1 y1)
1610 record
1611 (with-slots (left top right bottom)
1612 record
1613 (let ((dx (- nx x1))
1614 (dy (- ny y1)))
1615 (multiple-value-prog1
1616 (call-next-method)
1617 (incf left dx)
1618 (incf top dy)
1619 (incf right dx)
1620 (incf bottom dy))))))
1621
1622 (defrecord-predicate draw-rectangle-output-record (left top right bottom filled)
1623 (and (if-supplied (left coordinate)
1624 (coordinate= (slot-value record 'left) left))
1625 (if-supplied (top coordinate)
1626 (coordinate= (slot-value record 'top) top))
1627 (if-supplied (right coordinate)
1628 (coordinate= (slot-value record 'right) right))
1629 (if-supplied (bottom coordinate)
1630 (coordinate= (slot-value record 'bottom) bottom))
1631 (if-supplied (filled)
1632 (eql (slot-value record 'filled) filled))))
1633
1634 (def-grecording draw-ellipse ((gs-line-style-mixin)
1635 center-x center-y
1636 radius-1-dx radius-1-dy radius-2-dx radius-2-dy
1637 start-angle end-angle filled) ()
1638 (let ((transform (medium-transformation medium)))
1639 (setf (values center-x center-y)
1640 (transform-position transform center-x center-y))
1641 (setf (values radius-1-dx radius-1-dy)
1642 (transform-distance transform radius-1-dx radius-1-dy))
1643 (setf (values radius-2-dx radius-2-dy)
1644 (transform-distance transform radius-2-dx radius-2-dy))
1645 ;; I think this should be untransform-angle below, as the ellipse angles
1646 ;; go counter-clockwise in screen coordinates, whereas our transformations
1647 ;; rotate clockwise in the default coorinate system.. this is quite possibly
1648 ;; wrong depending on how one reads the spec, but just reversing it here
1649 ;; will break other things. -Hefner
1650 (setf start-angle (untransform-angle transform start-angle))
1651 (setf end-angle (untransform-angle transform end-angle))
1652 (when (reflection-transformation-p transform)
1653 (rotatef start-angle end-angle))
1654 (multiple-value-bind (min-x min-y max-x max-y)
1655 (bounding-rectangle* (make-ellipse* center-x center-y
1656 radius-1-dx radius-1-dy
1657 radius-2-dx radius-2-dy
1658 :start-angle start-angle
1659 :end-angle end-angle))
1660 (if filled
1661 (values min-x min-y max-x max-y)
1662 (let ((border (graphics-state-line-style-border graphic medium)))
1663 (values (- min-x border)
1664 (- min-y border)
1665 (+ max-x border)
1666 (+ max-y border)))))))
1667
1668 (defmethod* (setf output-record-position) :around
1669 (nx ny (record draw-ellipse-output-record))
1670 (with-standard-rectangle* (:x1 x1 :y1 y1)
1671 record
1672 (with-slots (center-x center-y)
1673 record
1674 (let ((dx (- nx x1))
1675 (dy (- ny y1)))
1676 (multiple-value-prog1
1677 (call-next-method)
1678 (incf center-x dx)
1679 (incf center-y dy))))))
1680
1681 (defrecord-predicate draw-ellipse-output-record (center-x center-y)
1682 (and (if-supplied (center-x coordinate)
1683 (coordinate= (slot-value record 'center-x) center-x))
1684 (if-supplied (center-y coordinate)
1685 (coordinate= (slot-value record 'center-y) center-y))))
1686
1687 ;;;; Patterns
1688
1689 ;;; The Spec says that "transformation only affects the position at
1690 ;;; which the pattern is drawn, not the pattern itself"
1691 (def-grecording draw-pattern (() pattern x y) ()
1692 (let ((width (pattern-width pattern))
1693 (height (pattern-height pattern))
1694 (transform (medium-transformation medium)))
1695 (setf (values x y) (transform-position transform x y))
1696 (values x y (+ x width) (+ y height))))
1697
1698 (defmethod* (setf output-record-position) :around
1699 (nx ny (record draw-pattern-output-record))
1700 (with-standard-rectangle* (:x1 x1 :y1 y1)
1701 record
1702 (with-slots (x y)
1703 record
1704 (let ((dx (- nx x1))
1705 (dy (- ny y1)))
1706 (multiple-value-prog1
1707 (call-next-method)
1708 (incf x dx)
1709 (incf y dy))))))
1710
1711 (defrecord-predicate draw-pattern-output-record (x y pattern)
1712 ;; ### I am not so sure about the correct usage of DEFRECORD-PREDICATE
1713 ;; --GB 2003-08-15
1714 (and (if-supplied (x coordinate)
1715 (coordinate= (slot-value record 'x) x))
1716 (if-supplied (y coordinate)
1717 (coordinate= (slot-value record 'y) y))
1718 (if-supplied (pattern pattern)
1719 (eq (slot-value record 'pattern) pattern))))
1720
1721 ;;;; RGB images
1722
1723 (def-grecording draw-image-design (() image-design x y) ()
1724 (let ((width (image-width (image image-design)))
1725 (height (image-height (image image-design)))
1726 (transform (medium-transformation medium)))
1727 (setf (values x y) (transform-position transform x y))
1728 (values x y (+ x width) (+ y height))))
1729
1730 (defmethod* (setf output-record-position) :around
1731 (nx ny (record draw-image-design-output-record))
1732 (with-standard-rectangle* (:x1 x1 :y1 y1) record
1733 (with-slots (x y) record
1734 (let ((dx (- nx x1))
1735 (dy (- ny y1)))
1736 (multiple-value-prog1 (call-next-method)
1737 (incf x dx)
1738 (incf y dy))))))
1739
1740 (defrecord-predicate draw-image-design-output-record (x y image-design)
1741 (and (if-supplied (x coordinate)
1742 (coordinate= (slot-value record 'x) x))
1743 (if-supplied (y coordinate)
1744 (coordinate= (slot-value record 'y) y))
1745 (if-supplied (image-design rgb-image-design)
1746 (eq (slot-value record 'image-design) image-design))))
1747
1748 ;;;; Text
1749
1750 (def-grecording draw-text ((gs-text-style-mixin) string point-x point-y start end
1751 align-x align-y toward-x toward-y transform-glyphs) ()
1752 ;; FIXME!!! Text direction.
1753 ;; FIXME: Multiple lines.
1754 (let* ((text-style (graphics-state-text-style graphic))
1755 (width (if (characterp string)
1756 (stream-character-width stream string :text-style text-style)
1757 (stream-string-width stream string
1758 :start start :end end
1759 :text-style text-style)) )
1760 (ascent (text-style-ascent text-style (sheet-medium stream)))
1761 (descent (text-style-descent text-style (sheet-medium stream)))
1762 (transform (medium-transformation medium)))
1763 (setf (values point-x point-y)
1764 (transform-position transform point-x point-y))
1765 (multiple-value-bind (left top right bottom)
1766 (text-bounding-rectangle* medium string
1767 :start start :end end :text-style text-style)
1768 (ecase align-x
1769 (:left (incf left point-x) (incf right point-x))
1770 (:right (incf left (- point-x width)) (incf right (- point-x width)))
1771 (:center (incf left (- point-x (round width 2)))
1772 (incf right (- point-x (round width 2)))))
1773 (ecase align-y
1774 (:baseline (incf top point-y) (incf bottom point-y))
1775 (:top (incf top (+ point-y ascent))
1776 (incf bottom (+ point-y ascent)))
1777 (:bottom (incf top (- point-y descent))
1778 (incf bottom (- point-y descent)))
1779 (:center (incf top (+ point-y (ceiling (- ascent descent) 2)))
1780 (incf bottom (+ point-y (ceiling (- ascent descent) 2)))))
1781 (values left top right bottom))))
1782
1783 (defmethod* (setf output-record-position) :around
1784 (nx ny (record draw-text-output-record))
1785 (with-standard-rectangle* (:x1 x1 :y1 y1)
1786 record
1787 (with-slots (point-x point-y toward-x toward-y)
1788 record
1789 (let ((dx (- nx x1))
1790 (dy (- ny y1)))
1791 (multiple-value-prog1
1792 (call-next-method)
1793 (incf point-x dx)
1794 (incf point-y dy)
1795 (incf toward-x dx)
1796 (incf toward-y dy))))))
1797
1798 (defrecord-predicate draw-text-output-record
1799 (string start end point-x point-y align-x align-y toward-x toward-y
1800 transform-glyphs)
1801 (and (if-supplied (string)
1802 (string= (slot-value record 'string) string))
1803 (if-supplied (start)
1804 (eql (slot-value record 'start) start))
1805 (if-supplied (end)
1806 (eql (slot-value record 'end) end))
1807 (if-supplied (point-x coordinate)
1808 (coordinate= (slot-value record 'point-x) point-x))
1809 (if-supplied (point-y coordinate)
1810 (coordinate= (slot-value record 'point-y) point-y))
1811 (if-supplied (align-x)
1812 (eq (slot-value record 'align-x) align-x))
1813 (if-supplied (align-y)
1814 (eq (slot-value record 'align-y) align-y))
1815 (if-supplied (toward-x coordinate)
1816 (coordinate= (slot-value record 'toward-x) toward-x))
1817 (if-supplied (toward-y coordinate)
1818 (coordinate= (slot-value record 'toward-y) toward-y))
1819 (if-supplied (transform-glyphs)
1820 (eq (slot-value record 'transform-glyphs) transform-glyphs))))
1821
1822 ;;; 16.3.3. Text Displayed Output Record
1823
1824 (defclass styled-string (gs-text-style-mixin gs-clip-mixin gs-ink-mixin)
1825 ((start-x :initarg :start-x)
1826 (string :initarg :string :reader styled-string-string)))
1827
1828 (defmethod output-record-equal and ((record styled-string)
1829 (record2 styled-string))
1830 (and (coordinate= (slot-value record 'start-x)
1831 (slot-value record2 'start-x))
1832 (string= (slot-value record 'string)
1833 (slot-value record2 'string))))
1834
1835 (defclass standard-text-displayed-output-record
1836 (text-displayed-output-record standard-displayed-output-record)
1837 ((initial-x1 :initarg :start-x)
1838 (initial-y1 :initarg :start-y)
1839 (strings :initform nil)
1840 (baseline :initform 0)
1841 (width :initform 0)
1842 (max-height :initform 0)
1843 ;; FIXME (or rework this comment): CLIM does not separate the
1844 ;; notions of the text width and the bounding box; however, we need
1845 ;; to, because some fonts will render outside the logical
1846 ;; coordinates defined by the start position and the width. LEFT
1847 ;; and RIGHT here (and below) deal with this in a manner completely
1848 ;; hidden from the user. (should we export
1849 ;; TEXT-BOUNDING-RECTANGLE*?)
1850 (left :initarg :start-x)
1851 (right :initarg :start-x)
1852 (start-x :initarg :start-x)
1853 (start-y :initarg :start-y)
1854 (end-x :initarg :start-x)
1855 (end-y :initarg :start-y)
1856 (wrapped :initform nil
1857 :accessor text-record-wrapped)
1858 (medium :initarg :medium :initform nil)))
1859
1860 (defmethod initialize-instance :after
1861 ((obj standard-text-displayed-output-record) &key stream)
1862 (when stream
1863 (setf (slot-value obj 'medium) (sheet-medium stream))))
1864
1865 ;;; Forget match-output-records-1 for standard-text-displayed-output-record; it
1866 ;;; doesn't make much sense because these records have state that is not
1867 ;;; initialized via initargs.
1868
1869 (defmethod output-record-equal and
1870 ((record standard-text-displayed-output-record)
1871 (record2 standard-text-displayed-output-record))
1872 (with-slots
1873 (initial-x1 initial-y1 start-x start-y left right end-x end-y wrapped strings)
1874 record2
1875 (and (coordinate= (slot-value record 'initial-x1) initial-x1)
1876 (coordinate= (slot-value record 'initial-y1) initial-y1)
1877 (coordinate= (slot-value record 'start-x) start-x)
1878 (coordinate= (slot-value record 'start-y) start-y)
1879 (coordinate= (slot-value record 'left) left)
1880 (coordinate= (slot-value record 'right) right)
1881 (coordinate= (slot-value record 'end-x) end-x)
1882 (coordinate= (slot-value record 'end-y) end-y)
1883 (eq (slot-value record 'wrapped) wrapped)
1884 (coordinate= (slot-value record 'baseline)
1885 (slot-value record2 'baseline))
1886 (eql (length (slot-value record 'strings)) (length strings));XXX
1887 (loop for s1 in (slot-value record 'strings)
1888 for s2 in strings
1889 always (output-record-equal s1 s2)))))
1890
1891 (defmethod print-object ((self standard-text-displayed-output-record) stream)
1892 (print-unreadable-object (self stream :type t :identity t)
1893 (with-slots (start-x start-y strings) self
1894 (format stream "~D,~D ~S"
1895 start-x start-y
1896 (mapcar #'styled-string-string strings)))))
1897
1898 (defmethod* (setf output-record-position) :around
1899 (nx ny (record standard-text-displayed-output-record))
1900 (with-standard-rectangle* (:x1 x1 :y1 y1)
1901 record
1902 (with-slots (start-x start-y end-x end-y strings baseline)
1903 record
1904 (let ((dx (- nx x1))
1905 (dy (- ny y1)))
1906 (multiple-value-prog1
1907 (call-next-method)
1908 (incf start-x dx)
1909 (incf start-y dy)
1910 (incf end-x dx)
1911 (incf end-y dy)
1912 ;(incf baseline dy)
1913 (loop for s in strings
1914 do (incf (slot-value s 'start-x) dx)))))))
1915
1916 (defmethod replay-output-record ((record standard-text-displayed-output-record)
1917 stream
1918 &optional region (x-offset 0) (y-offset 0))
1919 (declare (ignore region x-offset y-offset))
1920 (with-slots (strings baseline max-height start-y wrapped)
1921 record
1922 (with-sheet-medium (medium stream) ;is sheet a sheet-with-medium-mixin? --GB
1923 ;; FIXME:
1924 ;; 1. SLOT-VALUE...
1925 ;; 2. It should also save a "current line".
1926 (setf (slot-value stream 'baseline) baseline)
1927 (loop for substring in strings
1928 do (with-slots (start-x string)
1929 substring
1930 (setf (stream-cursor-position stream)
1931 (values start-x start-y))
1932 ;; FIXME: a bit of an abstraction inversion. Should
1933 ;; the styled strings here not simply be output
1934 ;; records? Then we could just replay them and all
1935 ;; would be well. -- CSR, 20060528.
1936 ;; But then we'd have to implement the output record
1937 ;; protocols for them. Are we allowed no internal
1938 ;; structure of our own? -- Hefner, 20080118
1939
1940 ;; Some optimization might be possible here.
1941 (with-drawing-options (stream
1942 :ink (graphics-state-ink substring)
1943 :clipping-region (graphics-state-clip substring)
1944 :text-style (graphics-state-text-style substring))
1945 (stream-write-output stream string nil))))
1946 (when wrapped ; FIXME
1947 (draw-rectangle* medium
1948 (+ wrapped 0) start-y
1949 (+ wrapped 4) (+ start-y max-height)
1950 :ink +foreground-ink+
1951 :filled t)))))
1952
1953 (defmethod output-record-start-cursor-position
1954 ((record standard-text-displayed-output-record))
1955 (with-slots (start-x start-y) record
1956 (values start-x start-y)))
1957
1958 (defmethod output-record-end-cursor-position
1959 ((record standard-text-displayed-output-record))
1960 (with-slots (end-x end-y) record
1961 (values end-x end-y)))
1962
1963 (defmethod tree-recompute-extent
1964 ((text-record standard-text-displayed-output-record))
1965 (with-standard-rectangle* (:y1 y1)
1966 text-record
1967 (with-slots (max-height left right)
1968 text-record
1969 (setf (rectangle-edges* text-record)
1970 (values (coordinate left)
1971 y1
1972 (coordinate right)
1973 (coordinate (+ y1 max-height))))))
1974 text-record)
1975
1976 (defmethod add-character-output-to-text-record ; XXX OAOO with ADD-STRING-...
1977 ((text-record standard-text-displayed-output-record)
1978 character text-style char-width height new-baseline)
1979 (with-slots (strings baseline width max-height left right start-y end-x end-y medium)
1980 text-record
1981 (if (and strings
1982 (let ((string (last1 strings)))
1983 (match-output-records string
1984 :text-style text-style
1985 :ink (medium-ink medium)
1986 :clipping-region (medium-clipping-region
1987 medium))))
1988 (vector-push-extend character (slot-value (last1 strings) 'string))
1989 (nconcf strings
1990 (list (make-instance
1991 'styled-string
1992 :start-x end-x
1993 :text-style text-style
1994 :medium medium ; pick up ink and clipping region
1995 :string (make-array 1 :initial-element character
1996 :element-type 'character
1997 :adjustable t
1998 :fill-pointer t)))))
1999 (multiple-value-bind (minx miny maxx maxy)
2000 (text-bounding-rectangle* medium character :text-style text-style)
2001 (declare (ignore miny maxy))
2002 (setq baseline (max baseline new-baseline)
2003 ;; KLUDGE: note END-X here is really START-X of the new
2004 ;; string
2005 left (min left (+ end-x minx))
2006 end-x (+ end-x char-width)
2007 right (+ end-x (max 0 (- maxx char-width)))
2008 max-height (max max-height height)
2009 end-y (max end-y (+ start-y max-height))
2010 width (+ width char-width))))
2011 (tree-recompute-extent text-record))
2012
2013 (defmethod add-string-output-to-text-record
2014 ((text-record standard-text-displayed-output-record)
2015 string start end text-style string-width height new-baseline)
2016 (setf end (or end (length string)))
2017 (let ((length (max 0 (- end start))))
2018 (cond
2019 ((eql length 1)
2020 (add-character-output-to-text-record text-record
2021 (aref string start)
2022 text-style
2023 string-width height new-baseline))
2024 (t (with-slots (strings baseline width max-height left right start-y end-x end-y
2025 medium)
2026 text-record
2027 (let ((styled-string (make-instance
2028 'styled-string
2029 :start-x end-x
2030 :text-style text-style
2031 :medium medium
2032 :string (make-array length
2033 :element-type 'character
2034 :adjustable t
2035 :fill-pointer t))))
2036 (nconcf strings (list styled-string))
2037 (replace (styled-string-string styled-string) string
2038 :start2 start :end2 end))
2039 (multiple-value-bind (minx miny maxx maxy)
2040 (text-bounding-rectangle* medium string
2041 :text-style text-style
2042 :start start :end end)
2043 (declare (ignore miny maxy))
2044 (setq baseline (max baseline new-baseline)
2045 ;; KLUDGE: note that END-X here really means
2046 ;; START-X of the new string.
2047 left (min left (+ end-x minx))
2048 end-x (+ end-x string-width)
2049 right (+ end-x (max 0 (- maxx string-width)))
2050 max-height (max max-height height)
2051 end-y (max end-y (+ start-y max-height))
2052 width (+ width string-width))))
2053 (tree-recompute-extent text-record)))))
2054
2055 (defmethod text-displayed-output-record-string
2056 ((record standard-text-displayed-output-record))
2057 (with-slots (strings) record
2058 (if (= 1 (length strings))
2059 (styled-string-string (first strings))
2060 (with-output-to-string (result)
2061 (loop for styled-string in strings
2062 do (write-string (styled-string-string styled-string) result))))))
2063
2064 ;;; 16.3.4. Top-Level Output Records
2065 (defclass stream-output-history-mixin ()
2066 ((stream :initarg :stream :reader output-history-stream)))
2067
2068 (defclass standard-sequence-output-history
2069 (standard-sequence-output-record stream-output-history-mixin)
2070 ())
2071
2072 (defclass standard-tree-output-history
2073 (standard-tree-output-record stream-output-history-mixin)
2074 ())
2075
2076 ;;; 16.4. Output Recording Streams
2077 (defclass standard-output-recording-stream (output-recording-stream)
2078 ((recording-p :initform t :reader stream-recording-p)
2079 (drawing-p :initform t :accessor stream-drawing-p)
2080 (output-history :initform (make-instance 'standard-tree-output-history)
2081 :reader stream-output-history)
2082 (current-output-record :accessor stream-current-output-record)
2083 (current-text-output-record :initform nil
2084 :accessor stream-current-text-output-record)
2085 (local-record-p :initform t
2086 :documentation "This flag is used for dealing with streams outputting strings char-by-char.")))
2087
2088 (defmethod initialize-instance :after
2089 ((stream standard-output-recording-stream) &rest args)
2090 (declare (ignore args))
2091 (let ((history (make-instance 'standard-tree-output-history :stream stream)))
2092 (setf (slot-value stream 'output-history) history
2093 (stream-current-output-record stream) history)))
2094
2095 ;;; Used in initializing clim-stream-pane
2096
2097 (defmethod reset-output-history ((stream
2098 standard-output-recording-stream))
2099 (setf (slot-value stream 'output-history)
2100 (make-instance 'standard-tree-output-history :stream stream))
2101 (setf (stream-current-output-record stream) (stream-output-history stream)))
2102
2103 ;;; 16.4.1 The Output Recording Stream Protocol
2104 (defmethod (setf stream-recording-p)
2105 (recording-p (stream standard-output-recording-stream))
2106 (let ((old-val (slot-value stream 'recording-p)))
2107 (setf (slot-value stream 'recording-p) recording-p)
2108 (when (not (eq old-val recording-p))
2109 (stream-close-text-output-record stream))
2110 recording-p))
2111
2112 (defmethod stream-add-output-record
2113 ((stream standard-output-recording-stream) record)
2114 (add-output-record record (stream-current-output-record stream)))
2115
2116 (defmethod stream-replay
2117 ((stream standard-output-recording-stream) &optional region)
2118 (replay (stream-output-history stream) stream region))
2119
2120 (defun output-record-ancestor-p (ancestor child)
2121 (loop for record = child then parent
2122 for parent = (output-record-parent record)
2123 when (eq parent nil) do (return nil)
2124 when (eq parent ancestor) do (return t)))
2125
2126 (defun rounded-bounding-rectangle (region)
2127 ;; return a bounding rectangle whose coordinates have been rounded to
2128 ;; lock into the pixel grid. Includes some extra safety to make
2129 ;; sure antialiasing around the theoretical limits are included, too.
2130 (with-bounding-rectangle* (x1 y1 x2 y2) region
2131 (make-rectangle* (floor (- x1 0.5))
2132 (floor (- y1 0.5))
2133 (ceiling (+ x2 0.5))
2134 (ceiling (+ y2 0.5)))))
2135
2136 (defmethod erase-output-record (record (stream standard-output-recording-stream)
2137 &optional (errorp t))
2138 (letf (((stream-recording-p stream) nil))
2139 (let ((region (rounded-bounding-rectangle record)))
2140 (with-bounding-rectangle* (x1 y1 x2 y2) region
2141 (if (output-record-ancestor-p (stream-output-history stream) record)
2142 (progn
2143 (delete-output-record record (output-record-parent record))
2144 (with-output-recording-options (stream :record nil)
2145 (draw-rectangle* stream x1 y1 x2 y2 :ink +background-ink+))
2146 (stream-replay stream region))
2147 (when errorp
2148 (error "~S is not contained in ~S." record stream)))))))
2149
2150 ;;; 16.4.3. Text Output Recording
2151 (defmethod stream-text-output-record
2152 ((stream standard-output-recording-stream) text-style)
2153 (declare (ignore text-style))
2154 (let ((record (stream-current-text-output-record stream)))
2155 (unless (and record (typep record 'standard-text-displayed-output-record))
2156 (multiple-value-bind (cx cy) (stream-cursor-position stream)
2157 (setf record (make-instance 'standard-text-displayed-output-record
2158 :x-position cx :y-position cy
2159 :start-x cx :start-y cy
2160 :stream stream)
2161 (stream-current-text-output-record stream) record)))
2162 record))
2163
2164 (defmethod stream-close-text-output-record
2165 ((stream standard-output-recording-stream))
2166 (let ((record (stream-current-text-output-record stream)))
2167 (when record
2168 (setf (stream-current-text-output-record stream) nil)
2169 #|record stream-current-cursor-position to (end-x record) - already done|#
2170 (stream-add-output-record stream record))))
2171
2172 (defmethod stream-add-character-output
2173 ((stream standard-output-recording-stream)
2174 character text-style width height baseline)
2175 (add-character-output-to-text-record
2176 (stream-text-output-record stream text-style)
2177 character text-style width height baseline))
2178
2179 (defmethod stream-add-string-output ((stream standard-output-recording-stream)
2180 string start end text-style
2181 width height baseline)
2182 (add-string-output-to-text-record (stream-text-output-record stream
2183 text-style)
2184 string start end text-style
2185 width height baseline))
2186
2187 ;;; Text output catching methods
2188 (defmacro without-local-recording (stream &body body)
2189 `(letf (((slot-value ,stream 'local-record-p) nil))
2190 ,@body))
2191
2192 (defmethod stream-write-output :around
2193 ((stream standard-output-recording-stream)
2194 line
2195 string-width
2196 &optional (start 0) end)
2197
2198 (when (and (stream-recording-p stream)
2199 (slot-value stream 'local-record-p))
2200 (let* ((medium (sheet-medium stream))
2201 (text-style (medium-text-style medium))
2202 (height (text-style-height text-style medium))
2203 (ascent (text-style-ascent text-style medium)))
2204 (if (characterp line)
2205 (stream-add-character-output stream line text-style
2206 (stream-character-width
2207 stream line :text-style text-style)
2208 height
2209 ascent)
2210 (stream-add-string-output stream line start end text-style
2211 (or string-width
2212 (stream-string-width stream line
2213 :start start :end end
2214 :text-style text-style))
2215 height
2216 ascent))))
2217
2218 (when (stream-drawing-p stream)
2219 (without-local-recording stream
2220 (call-next-method))))
2221
2222 #+nil
2223 (defmethod stream-write-char :around ((stream standard-output-recording-stream) char)
2224 (when (and (stream-recording-p stream)
2225 (slot-value stream 'local-record-p))
2226 (if (or (eql char #\return)
2227
2228 (stream-close-text-output-record stream)
2229 (let* ((medium (sheet-medium stream))
2230 (text-style (medium-text-style medium)))
2231 (stream-add-character-output stream char text-style
2232 (stream-character-width stream char :text-style text-style)
2233 (text-style-height text-style medium)
2234 (text-style-ascent text-style medium)))))
2235 (without-local-recording stream
2236 (call-next-method))))
2237
2238 #+nil
2239 (defmethod stream-write-string :around ((stream standard-output-recording-stream) string
2240 &optional (start 0) end)
2241 (when (and (stream-recording-p stream)
2242 (slot-value stream 'local-record-p))
2243 (let* ((medium (sheet-medium stream))
2244 (text-style (medium-text-style medium)))
2245 (stream-add-string-output stream string start end text-style
2246 (stream-string-width stream string
2247 :start start :end end
2248 :text-style text-style)
2249 (text-style-height text-style medium)
2250 (text-style-ascent text-style medium))))
2251 (without-local-recording stream
2252 (call-next-method)))
2253
2254
2255 (defmethod stream-finish-output :after ((stream standard-output-recording-stream))
2256 (stream-close-text-output-record stream))
2257
2258 (defmethod stream-force-output :after ((stream standard-output-recording-stream))
2259 (stream-close-text-output-record stream))
2260
2261 (defmethod stream-terpri :after ((stream standard-output-recording-stream))
2262 (stream-close-text-output-record stream))
2263
2264 (defmethod* (setf stream-cursor-position) :after (x y (stream standard-output-recording-stream))
2265 (declare (ignore x y))
2266 (stream-close-text-output-record stream))
2267
2268 ;(defmethod stream-set-cursor-position :after ((stream standard-output-recording-stream))
2269 ; (stream-close-text-output-record stream))
2270
2271 (defmethod stream-wrap-line :before ((stream standard-output-recording-stream))
2272 (when (stream-recording-p stream)
2273 (setf (text-record-wrapped (stream-text-output-record stream nil)) ; FIXME!
2274 (stream-text-margin stream))))
2275
2276 ;;; 16.4.4. Output Recording Utilities
2277
2278 (defmethod invoke-with-output-recording-options
2279 ((stream output-recording-stream) continuation record draw)
2280 "Calls CONTINUATION on STREAM enabling or disabling recording and drawing
2281 according to the flags RECORD and DRAW."
2282 (letf (((stream-recording-p stream) record)
2283 ((stream-drawing-p stream) draw))
2284 (funcall continuation stream)))
2285
2286 (defmethod invoke-with-new-output-record ((stream output-recording-stream)
2287 continuation record-type
2288 constructor
2289 &key parent)
2290 (declare (ignore record-type))
2291 (stream-close-text-output-record stream)
2292 (let ((new-record (funcall constructor)))
2293 (letf (((stream-current-output-record stream) new-record))
2294 ;; Should we switch on recording? -- APD
2295 (funcall continuation stream new-record)
2296 (finish-output stream))
2297 (if parent
2298 (add-output-record new-record parent)
2299 (stream-add-output-record stream new-record))
2300 new-record))
2301
2302 (defmethod invoke-with-new-output-record ((stream output-recording-stream)
2303 continuation record-type
2304 (constructor null)
2305 &rest initargs
2306 &key parent)
2307 (with-keywords-removed (initargs (:parent))
2308 (stream-close-text-output-record stream)
2309 (let ((new-record (apply #'make-instance record-type initargs)))
2310 (letf (((stream-current-output-record stream) new-record))
2311 ;; Should we switch on recording? -- APD
2312 (funcall continuation stream new-record)
2313 (finish-output stream))
2314 (if parent
2315 (add-output-record new-record parent)
2316 (stream-add-output-record stream new-record))
2317 new-record)))
2318
2319 (defmethod invoke-with-output-to-output-record
2320 ((stream output-recording-stream) continuation record-type constructor
2321 &key)
2322 (declare (ignore record-type))
2323 (stream-close-text-output-record stream)
2324 (let ((new-record (funcall constructor)))
2325 (with-output-recording-options (stream :record t :draw nil)
2326 (letf (((stream-current-output-record stream) new-record)
2327 ((stream-cursor-position stream) (values 0 0)))
2328 (funcall continuation stream new-record)
2329 (finish-output stream)))
2330 new-record))
2331
2332 (defmethod invoke-with-output-to-output-record
2333 ((stream output-recording-stream) continuation record-type (constructor null)
2334 &rest initargs)
2335 (stream-close-text-output-record stream)
2336 (let ((new-record (apply #'make-instance record-type initargs)))
2337 (with-output-recording-options (stream :record t :draw nil)
2338 (letf (((stream-current-output-record stream) new-record)
2339 ((stream-cursor-position stream) (values 0 0)))
2340 (funcall continuation stream new-record)
2341 (finish-output stream)))
2342 new-record))
2343
2344 (defmethod make-design-from-output-record (record)
2345 ;; FIXME
2346 (declare (ignore record))
2347 (error "Not implemented."))
2348
2349
2350 ;;; Additional methods
2351 (defmethod scroll-vertical :around ((stream output-recording-stream) dy)
2352 (declare (ignore dy))
2353 (with-output-recording-options (stream :record nil)
2354 (call-next-method)))
2355
2356 (defmethod scroll-horizontal :around ((stream output-recording-stream) dx)
2357 (declare (ignore dx))
2358 (with-output-recording-options (stream :record nil)
2359 (call-next-method)))
2360
2361 ;;; FIXME: Change things so the rectangle below is only drawn in response
2362 ;;; to explicit repaint requests from the user, not exposes from X
2363 ;;; FIXME: Use DRAW-DESIGN*, that is fix DRAW-DESIGN*.
2364
2365 (defmethod handle-repaint ((stream output-recording-stream) region)
2366 (when (output-recording-stream-p stream)
2367 (unless (region-equal region +nowhere+) ; ignore repaint requests for +nowhere+
2368 (let ((region (if (region-equal region +everywhere+)
2369 (sheet-region stream) ; fallback to the sheet's region for +everwhere+
2370 (bounding-rectangle region))))
2371 (with-bounding-rectangle* (x1 y1 x2 y2) region
2372 (with-output-recording-options (stream :record nil)
2373 (draw-rectangle* stream x1 y1 x2 y2 :filled t :ink +background-ink+)))
2374 (stream-replay stream region)))))
2375
2376
2377 (defmethod scroll-extent :around ((stream output-recording-stream) x y)
2378 (declare (ignore x y))
2379 (when (stream-drawing-p stream)
2380 (call-next-method)))
2381
2382 ;;; ----------------------------------------------------------------------------
2383 ;;; Complicated, underspecified...
2384 ;;;
2385 ;;; From examining old Genera documentation, I believe that
2386 ;;; with-room-for-graphics is supposed to set the medium transformation to
2387 ;;; give the desired coordinate system; i.e., it doesn't preserve any
2388 ;;; rotation, scaling or translation in the current medium transformation.
2389 (defmethod invoke-with-room-for-graphics (cont stream
2390 &key (first-quadrant t)
2391 height
2392 (move-cursor t)
2393 (record-type
2394 'standard-sequence-output-record))
2395 ;; I am not sure what exactly :height should do.
2396 ;; --GB 2003-05-25
2397 ;; The current behavior is consistent with 'classic' CLIM
2398 ;; --Hefner 2004-06-19
2399 ;; Don't know if it still is :)
2400 ;; -- Moore 2005-01-26
2401 (multiple-value-bind (cx cy)
2402 (stream-cursor-position stream)
2403 (with-sheet-medium (medium stream)
2404 (letf (((medium-transformation medium)
2405 (if first-quadrant
2406 (make-scaling-transformation 1 -1)
2407 +identity-transformation+)))
2408 (let ((record (with-output-to-output-record (stream record-type)
2409 (funcall cont stream))))
2410 ;; Bounding rectangle is in sheet coordinates!
2411 (with-bounding-rectangle* (x1 y1 x2 y2)
2412 record
2413 (declare (ignore x2))
2414 (if first-quadrant
2415 (setf (output-record-position record)
2416 (values (max cx (+ cx x1))
2417 (if height
2418 (max cy (+ cy (- height (- y2 y1))))
2419 cy)))
2420 (setf (output-record-position record)
2421 (values (max cx (+ cx x1)) (max cy (+ cy y1)))))
2422 (when (stream-recording-p stream)
2423 (stream-add-output-record stream record))
2424 (when (stream-drawing-p stream)
2425 (replay record stream))
2426 (if move-cursor
2427 (let ((record-height (- y2 y1)))
2428 (setf (stream-cursor-position stream)
2429 (values cx
2430 (if first-quadrant
2431 (+ cy (max (- y1)
2432 (or height 0)
2433 record-height))
2434 (+ cy (max (or height 0)
2435 record-height))))))
2436 (setf (stream-cursor-position stream) (values cx cy)))
2437 record))))))
2438
2439 ;;; ----------------------------------------------------------------------------
2440 ;;; Baseline
2441 ;;;
2442
2443 (defmethod output-record-baseline ((record output-record))
2444 "Fall back method"
2445 (with-bounding-rectangle* (x1 y1 x2 y2)