replace some struct-copy

main
Matthew Butterick 5 years ago
parent cc7a64c5ee
commit 2bd5e79704

@ -91,17 +91,17 @@
(define this-origin (pt- ref-pt (to-point q)))
(define shifted-origin (pt+ this-origin (quad-shift q)))
;; need to position before recurring, so subquads have accurate reference point
(define positioned-q (struct-copy quad q
[origin shifted-origin]
;; set shift to zero because it's baked into new origin value
[shift (pt 0 0)]))
(define positioned-q (quad-copy q
[origin shifted-origin]
;; set shift to zero because it's baked into new origin value
[shift (pt 0 0)]))
(define positioned-elems
;; for purposes of positioning the elements, we want to also bake in the `shift-elements` value
;; but we don't want this origin to be permanent on the parent.
;; akin to `push` a graphics state and then `pop` afterwards.
(let ([parent-q (struct-copy quad positioned-q
[origin (pt+ (quad-origin positioned-q) (quad-shift-elems positioned-q))]
[shift-elems (pt 0 0)])])
(let ([parent-q (quad-copy positioned-q
[origin (pt+ (quad-origin positioned-q) (quad-shift-elems positioned-q))]
[shift-elems (pt 0 0)])])
;; can't use for/list here because previous quads provide context for later ones
(let loop ([prev-elems null] [elems (quad-elems parent-q)])
(match elems
@ -112,7 +112,7 @@
(car prev-elems)))
(loop (cons (position-one this-q ref-q) prev-elems) rest)]
[(cons x rest) (loop (cons x prev-elems) rest)]))))
(struct-copy quad positioned-q [elems positioned-elems]))
(quad-copy positioned-q [elems positioned-elems]))
(define (distance q)
(match (pt- (from-point q) (to-point q))
@ -120,7 +120,7 @@
[(list ∆x ∆y) (sqrt (+ (expt ∆x 2) (expt ∆y 2)))]))
(define (flatten-quad q)
(cons (struct-copy quad q [elems null])
(cons (quad-copy q [elems null])
(apply append (map flatten-quad (quad-elems q)))))
(define (bounding-box . qs-in)
@ -133,11 +133,11 @@
(append min-origin max-outer-pt))
(define (attach-to from-q from-pt to-q to-pt)
(struct-copy quad from-q
[elems (cons (struct-copy quad to-q
[from-parent from-pt]
[to to-pt])
(quad-elems from-q))]))
(quad-copy from-q
[elems (cons (quad-copy to-q
[from-parent from-pt]
[to to-pt])
(quad-elems from-q))]))
(module+ test
(require rackunit)

@ -276,28 +276,28 @@
(module+ test
(define q-zero (q #:size (pt 0 0)))
(define q-one (q #:size (pt 1 1) #:printable #t))
(define x (struct-copy quad q-one [elems '(#\x)]))
(define zwx (struct-copy quad q-zero
(define x (quad-copy q-one [elems '(#\x)]))
(define zwx (quad-copy q-zero
[printable (λ _ #t)]
[elems '(#\z)]))
(define hyph (struct-copy quad q-one [elems '(#\-)]))
(define shy (struct-copy quad q-one
(define hyph (quad-copy q-one [elems '(#\-)]))
(define shy (quad-copy q-one
[printable (λ (q [sig #f])
(case sig
[(end) #t]
[else #f]))]
[elems '(#\-)]))
(define a (struct-copy quad q-one [elems '(#\a)]))
(define b (struct-copy quad q-one [elems '(#\b)]))
(define c (struct-copy quad q-one [elems '(#\c)]))
(define d (struct-copy quad q-one [elems '(#\d)]))
(define sp (struct-copy quad q-one
(define a (quad-copy q-one [elems '(#\a)]))
(define b (quad-copy q-one [elems '(#\b)]))
(define c (quad-copy q-one [elems '(#\c)]))
(define d (quad-copy q-one [elems '(#\d)]))
(define sp (quad-copy q-one
[printable (λ (q [sig #f])
(case sig
[(start end) #f]
[else #t]))]
[elems '(#\space)]))
(define lbr (struct-copy quad q-one
(define lbr (quad-copy q-one
[printable (λ _ #f)]
[elems '(#\newline)]))
@ -316,8 +316,8 @@
(for/list ([x (in-list (linewrap (for/list ([c (in-string str)])
(define atom (q c))
(if (equal? (quad-elems atom) '(#\space))
(struct-copy quad sp)
(struct-copy quad q-one
(quad-copy sp)
(quad-copy q-one
[attrs (quad-attrs atom)]
[elems (quad-elems atom)]))) int debug
#:nicely nicely?))]

@ -360,8 +360,7 @@
(filter-map (λ (q) (or (quad-ref q :line-height) (pt-y (size q)))) pcs))
(pt line-width (if (empty? line-heights) line-height (apply max line-heights)))))
(list
(struct-copy
quad line-q
(quad-copy line-q
;; move block attrs up, so they are visible in col wrap
[attrs (copy-block-attrs (quad-attrs elem)
(hash-copy (quad-attrs line-q)))]
@ -380,8 +379,7 @@
(match (and (eq? idx 1) (quad-ref elem :list-index))
[#false null]
[bullet
(define bq (struct-copy
quad q:string ;; copy q:string to get draw routine
(define bq (quad-copy q:string ;; copy q:string to get draw routine
;; borrow attrs from elem
[attrs (quad-attrs elem)]
;; use bullet as elems

@ -65,7 +65,7 @@
#:min-left-length 3
#:min-right-length 3))]
[substr (in-list (regexp-match* (regexp (string hyphen-char)) hstr #:gap-select? #t))])
(struct-copy quad q [elems (list substr)]))]
(quad-copy q [elems (list substr)]))]
[_ (list q)]))))
@ -226,14 +226,14 @@
(define line-wrap-size (/ (- printable-width (* (sub1 column-count) column-gap)) column-count))
(define line-qs (time-log line-wrap (apply-keeps (line-wrap qs line-wrap-size))))
(define col-quad-prototype (struct-copy quad q:column
(define col-quad-prototype (quad-copy q:column
[size (pt line-wrap-size printable-height)]))
(define column-qs (time-log column-wrap (column-wrap line-qs printable-height column-gap col-quad-prototype)))
(define page-quad-prototype
(λ (page-count)
(define left-shift (+ left-margin (if (odd? page-count) gutter-margin 0)))
(struct-copy quad q:page
(quad-copy q:page
[shift (pt left-shift top-margin)]
[size (pt line-wrap-size printable-height)])))
@ -260,28 +260,28 @@
['left
;; blank page goes at beginning of current section
(define page-from-current-section (car section-pages))
(define blank-page (struct-copy quad page-from-current-section [elems null]))
(define new-section (struct-copy quad q:section [elems (cons blank-page section-pages)]))
(define blank-page (quad-copy page-from-current-section [elems null]))
(define new-section (quad-copy q:section [elems (cons blank-page section-pages)]))
(cons new-section sections-acc)]
[_ ;; must be 'right
;; blank page goes at end of previous section (if it exists)
(define new-section (struct-copy quad q:section [elems section-pages]))
(define new-section (quad-copy q:section [elems section-pages]))
(match sections-acc
[(cons previous-section other-sections)
(define previous-section-pages (quad-elems previous-section))
;; we know previous section has pages because we ignore empty sections
(define page-from-previous-section (car previous-section-pages))
(define blank-page (struct-copy quad page-from-previous-section [elems null]))
(define blank-page (quad-copy page-from-previous-section [elems null]))
(define revised-previous-section
(struct-copy quad previous-section
(quad-copy previous-section
[elems (append previous-section-pages (list blank-page))]))
(list* new-section revised-previous-section other-sections)]
[_ (list new-section)])])]
[else (define new-section (struct-copy quad q:section [elems section-pages]) )
[else (define new-section (quad-copy q:section [elems section-pages]) )
(cons new-section sections-acc)])
(section-pages-used (+ (section-pages-used) (length section-pages))))))
(define doc (struct-copy quad q:doc [elems sections]))
(define doc (quad-copy q:doc [elems sections]))
;; correct lines with inner / outer alignment
(for* ([(page page-idx) (in-indexed (for*/list ([section (in-list (quad-elems doc))]

Loading…
Cancel
Save