structify bbox

main
Matthew Butterick 6 years ago
parent 7f5e3dae3e
commit 9063f1c45b

@ -1,10 +1,11 @@
#lang racket/base #lang racket/base
(require "racket.rkt") (require racket/contract racket/struct)
(provide BBox bbox->list) (provide (struct-out BBox) make-BBox bbox->list)
(define-subclass object% (BBox (struct BBox (minX minY maxX maxY) #:transparent #:mutable)
; The minimum X position in the bounding box
(define (make-BBox ; The minimum X position in the bounding box
[minX +inf.0] [minX +inf.0]
; The minimum Y position in the bounding box ; The minimum Y position in the bounding box
[minY +inf.0] [minY +inf.0]
@ -12,41 +13,31 @@
[maxX -inf.0] [maxX -inf.0]
; The maxmimum Y position in the bounding box ; The maxmimum Y position in the bounding box
[maxY -inf.0]) [maxY -inf.0])
(BBox minX minY maxX maxY))
(as-methods
width
height
addPoint
copy))
;; The width of the bounding box ;; The width of the bounding box
(define/contract (width this) (define/contract (width bb)
(->m number?) (BBox? . -> . number?)
(- (· this maxX) (· this minX))) (- (BBox-maxX bb) (BBox-minX bb)))
;; The height of the bounding box ;; The height of the bounding box
(define/contract (height this) (define/contract (height bb)
(->m number?) (BBox? . -> . number?)
(- (· this maxY) (· this minY))) (- (BBox-maxY bb) (BBox-minY bb)))
(define/contract (addPoint this x y) (define/contract (addPoint bb x y)
(number? number? . ->m . void?) (BBox? number? number? . -> . void?)
(set-field! minX this (min x (· this minX))) (set-BBox-minX! bb (min x (BBox-minX bb)))
(set-field! minY this (min y (· this minY))) (set-BBox-minY! bb (min y (BBox-minY bb)))
(set-field! maxX this (max x (· this maxX))) (set-BBox-maxX! bb (max x (BBox-maxX bb)))
(set-field! maxY this (max y (· this maxY)))) (set-BBox-maxY! bb (max y (BBox-maxY bb))))
(define/contract (copy this) (define/contract (copy bb)
(->m (is-a?/c BBox)) (BBox? . -> . BBox?)
(make-object BBox (· this minX) (apply make-BBox (bbox->list bb)))
(· this minY)
(· this maxX)
(· this maxY)))
(define/contract (bbox->list this) (define bbox->list struct->list)
((is-a?/c BBox) . -> . (list/c number? number? number? number?))
(list (· this minX) (· this minY) (· this maxX) (· this maxY)))

@ -182,8 +182,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
;; The fonts bounding box, i.e. the box that encloses all glyphs in the font. ;; The fonts bounding box, i.e. the box that encloses all glyphs in the font.
(define/contract (bbox this) (define/contract (bbox this)
(->m (is-a?/c BBox)) (->m BBox?)
(make-object BBox (· this head xMin) (make-BBox (· this head xMin)
(· this head yMin) (· this head yMin)
(· this head xMax) (· this head xMax)
(· this head yMax))) (· this head yMax)))

@ -1,7 +1,7 @@
#lang racket/base #lang racket/base
(require "racket.rkt") (require "racket.rkt")
(require "bbox.rkt" (prefix-in Script- "script.rkt")) (require (prefix-in Script- "script.rkt"))
(provide (all-defined-out)) (provide (all-defined-out))
#| #|

Loading…
Cancel
Save