From 7549e1fca38e16367c66c32a692a5adae283c250 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 6 Dec 2018 16:38:20 -0800 Subject: [PATCH] renames in bbox --- fontland/fontland/bbox.rkt | 71 ++++++++++++++++---------------------- fontland/fontland/font.rkt | 8 ++--- 2 files changed, 33 insertions(+), 46 deletions(-) diff --git a/fontland/fontland/bbox.rkt b/fontland/fontland/bbox.rkt index fab85eab..b5742b8f 100644 --- a/fontland/fontland/bbox.rkt +++ b/fontland/fontland/bbox.rkt @@ -1,44 +1,33 @@ #lang racket/base -(require racket/contract racket/struct) - -(provide (struct-out BBox) make-BBox bbox->list) - -(struct BBox (minX minY maxX maxY) #:transparent #:mutable) - -(define (make-BBox - ; The minimum X position in the bounding box - [minX +inf.0] - ; The minimum Y position in the bounding box - [minY +inf.0] - ; The maxmimum X position in the bounding box - [maxX -inf.0] - ; The maxmimum Y position in the bounding box - [maxY -inf.0]) - (BBox minX minY maxX maxY)) - -;; The width of the bounding box -(define/contract (width bb) - (BBox? . -> . number?) - (- (BBox-maxX bb) (BBox-minX bb))) - - -;; The height of the bounding box -(define/contract (height bb) - (BBox? . -> . number?) - (- (BBox-maxY bb) (BBox-minY bb))) - - -(define/contract (addPoint bb x y) - (BBox? number? number? . -> . void?) - (set-BBox-minX! bb (min x (BBox-minX bb))) - (set-BBox-minY! bb (min y (BBox-minY bb))) - (set-BBox-maxX! bb (max x (BBox-maxX bb))) - (set-BBox-maxY! bb (max y (BBox-maxY bb)))) - - -(define/contract (copy bb) - (BBox? . -> . BBox?) - (apply make-BBox (bbox->list bb))) - +(require racket/struct) +(provide (all-defined-out)) + +(struct bbox (min-x min-y max-x max-y) #:transparent #:mutable) + +(define (+bbox [min-x +inf.0] [min-y +inf.0] [max-x -inf.0] [max-y -inf.0]) + (bbox min-x min-y max-x max-y)) + +(define (bbox-width bb) + (unless (bbox? bb) + (raise-argument-error 'bbox-width "bbox" bb)) + (- (bbox-max-x bb) (bbox-min-x bb))) + +(define (bbox-height bb) + (unless (bbox? bb) + (raise-argument-error 'bbox-height "bbox" bb)) + (- (bbox-max-y bb) (bbox-min-y bb))) + +(define (bbox-add-point bb x y) + (unless (bbox? bb) + (raise-argument-error 'bbox-add-point "bbox" bb)) + (set-bbox-min-x! bb (min x (bbox-min-x bb))) + (set-bbox-min-y! bb (min y (bbox-min-y bb))) + (set-bbox-max-x! bb (max x (bbox-max-x bb))) + (set-bbox-max-y! bb (max y (bbox-max-y bb)))) + +(define (bbox-copy bb) + (unless (bbox? bb) + (raise-argument-error 'bbox-copy "bbox" bb)) + (apply +bbox (bbox->list bb))) (define bbox->list struct->list) diff --git a/fontland/fontland/font.rkt b/fontland/fontland/font.rkt index 82405bda..ca2d5cf2 100644 --- a/fontland/fontland/font.rkt +++ b/fontland/fontland/font.rkt @@ -154,14 +154,12 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js ;; The font’s bounding box, i.e. the box that encloses all glyphs in the font. -(define (bbox this) +(define (font-bbox this) (define head-table (get-head-table this)) - (make-BBox (· head-table xMin) (· head-table yMin) (· head-table xMax) (· head-table yMax))) - -(define font-bbox bbox) ; todo: avoid name collision in pitfall/embedded + (+bbox (· head-table xMin) (· head-table yMin) (· head-table xMax) (· head-table yMax))) (test-module - (check-equal? (bbox->list (bbox f)) '(-161 -236 1193 963))) + (check-equal? (bbox->list (font-bbox f)) '(-161 -236 1193 963))) (define current-layout-caching (make-parameter #false))