(module (s9fes char-canvas shape vgroup) (;export ; real-shape-vgroup ; shape-vgroup) (import scheme utf8) (import (chicken base)) (import (chicken fixnum)) (import (chicken type)) (import (only (srfi 1) list-copy)) (import record-variants) (import (s9fes char-canvas) (s9fes char-canvas rect) (s9fes char-canvas shape shape)) (include-relative "s9fes.char-canvas.types") (include-relative "s9fes.char-canvas.inlines") (include-relative "s9fes.char-canvas.shape.types") (include-relative "s9fes.char-canvas.shape.inlines") ;group rest arg is (list-of shape-plotter) or (list (list-ofshape-plotter)) ;due to new group not using `apply' (: real-shape-vgroup (#!rest real-shape-plotter -> real-shape-plotter)) (: shape-vgroup (#!rest shape-plotter -> shape-plotter)) ;; Private (define (*info-elements info) (the (list-of shape-plotter) (@info 0 info))) (define (*new-vgroup info elms) (case (@info-coords info) ((real) (real-shape-vgroup elms)) (else (shape-vgroup elms))) ) (define (*canvas-draw-vgroup cv x0 y0 shps) ;FIXME (define (next shp x y) (receive (w h) (shape-size shp) (values x (fx+ y h)))) (let loop ((shps (reverse shps)) (x x0) (y y0)) (unless (null? shps) (let ((shp (car shps))) (shp cv x y) (receive (x y) (next shp x y) (loop (cdr shps) x y)) ) ) ) cv ) (define (*canvas-plot-vgroup cv x0 y0 shps) ;FIXME (define (next shp x y) (receive (w h) (shape-size shp) (values x (+ y h)))) (let loop ((shps (reverse shps)) (x x0) (y y0)) (unless (null? shps) (let ((shp (car shps))) (shp cv x y) (receive (x y) (next shp x y) (loop (cdr shps) x y)) ) ) ) cv ) ;; Public #; ;w/ check- & error- (define (real-shape-vgroup #!rest args) (let ((shps (check-shapes 'real-shape-vgroup (if (%length-1/kind? args list?) (car args) args))) ) (lambda (#!optional cv x0 y0) (if cv (begin (check-real-xy 'real-shape-vgroup x0 y0) (*canvas-draw-vgroup (check-canvas 'real-shape-vgroup cv) x0 y0 shps) ) (^real-info 'vgroup shps)) ) ) ) (define (real-shape-vgroup #!rest args) (let ((shps (if (%length-1/kind? args list?) (car args) args))) (assert (%shapes? shps) 'real-shape-vgroup "bad argument type - not list-of shape" shps) (lambda (#!optional cv x0 y0) (if cv (*canvas-draw-vgroup cv x0 y0 shps) (^real-info 'vgroup shps) ) ) ) ) (define (shape-vgroup #!rest args) (let ((shps (if (%length-1/kind? args list?) (car args) args))) (assert (%shapes? shps) 'shape-vgroup "bad argument type - not list-of shape" shps) (lambda (#!optional cv x0 y0) (if cv (*canvas-plot-vgroup cv 0 y0 shps) (^virtual-info 'vgroup shps) ) ) ) ) ;; (define (class-shape-size shp info) (let* ((shps (*info-elements info)) (sizs (map (lambda (shp) (receive (shape-size shp))) shps)) ) (values (apply max (map car sizs)) (apply + (map cadr sizs))) ) ) (define-inline (what-per shps per) (if (null? (cdr shps)) ;then this is last (round per) ;else per bit (floor per)) ) (define (class-shape-layout shp info bb) (let-values (((rqr-wd rqr-ht) (class-shape-size shp info))) ;short-cut ; v is bottom to top (let* ((shps (*info-elements info)) (nshps (length shps)) (ht-per (/ (%rect-ht bb) nshps)) ) (let ((rem-x (%rect-x bb)) (rem-wd (%rect-wd bb)) (rem-ht (%rect-ht bb)) ) (if (or (< (%rect-wd bb) rqr-wd) (< (%rect-ht bb) rqr-ht)) ;then can't fit (error 'vgroup-layout "overflows bounding-box" rqr-wd rqr-ht bb) ;else try to fit (let loop ((shps (reverse shps)) (new-y (%rect-y bb)) (rem-ht rem-ht) (jsts '())) ;FIXME what if out of room (if (null? shps) (*new-vgroup info jsts) (let ((shp (car shps))) ;FIXME new-ht negotiated how? (let* ((this-ht (what-per shps ht-per)) (this-bb (%rect rem-x new-y rem-wd this-ht)) ) ;rem-ht (loop (cdr shps) (+ new-y this-ht) (- rem-ht this-ht) (cons (shape-layout shp this-bb) jsts) ) ) ) ) ) ) ) ) ) ) (define (class-shape-justified? shp info) (let loop ((shps (*info-elements info))) (or (null? shps) (let ((shp (car shps))) (and (shape-justified? shp) (loop (cdr shps))) ) ) ) ) (define (class-shape-reify-args noshp noinfo args) (let ((elm-infos (car args))) `(,(map shape-reify elm-infos) ,@(cdr args)) ) ) (define (class-shape-reflect shp info) (^info (@info-class info) (@info-coords info) (map (cut shape-reflect <>) (*info-elements info)) ) ) (register-shape 'vgroup shape-vgroup real-shape-vgroup) (register-shape-method 'vgroup 'size class-shape-size) (register-shape-method 'vgroup 'cardinality (lambda (shp info) (length (*info-elements info)))) (register-shape-method 'vgroup 'elements (lambda (shp info) (list-copy (*info-elements info)))) (register-shape-method 'vgroup 'layout class-shape-layout) (register-shape-method 'vgroup 'justified? class-shape-justified?) (register-shape-method 'vgroup 'reify-args class-shape-reify-args) (register-shape-method 'vgroup 'reflect class-shape-reflect) ) ;module (s9fes char-canvas shape vgroup)