From e15a2686505820c8f5924c3edd75692aef3ee6e3 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 21 Dec 2018 22:19:27 -0800 Subject: [PATCH] margin struct --- pitfall/pitfall/core.rkt | 3 +++ pitfall/pitfall/document.rkt | 4 ++-- pitfall/pitfall/page-test.rkt | 3 ++- pitfall/pitfall/page.rkt | 7 +++---- pitfall/pitfall/text.rkt | 5 +++-- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pitfall/pitfall/core.rkt b/pitfall/pitfall/core.rkt index 2c891d87..176de390 100644 --- a/pitfall/pitfall/core.rkt +++ b/pitfall/pitfall/core.rkt @@ -9,6 +9,9 @@ ;; for JPEG and PNG (struct image (label width height obj) #:transparent #:mutable) +;; for page +(struct margin (top left bottom right) #:transparent #:mutable) + ;; params (define test-mode (make-parameter #f)) diff --git a/pitfall/pitfall/document.rkt b/pitfall/pitfall/document.rkt index ddd03e69..58c8c813 100644 --- a/pitfall/pitfall/document.rkt +++ b/pitfall/pitfall/document.rkt @@ -67,8 +67,8 @@ (set! @pages (cons (make-object PDFPage this page-parent options-arg) @pages)) ;; reset x and y coordinates - (set! @x (hash-ref (get-field margins (page)) 'left)) - (set! @y (hash-ref (get-field margins (page)) 'top)) + (set! @x (margin-left (get-field margins (page)))) + (set! @y (margin-right (get-field margins (page)))) ;; flip PDF coordinate system so that the origin is in ;; the top left rather than the bottom left (set! _ctm default-ctm-value) diff --git a/pitfall/pitfall/page-test.rkt b/pitfall/pitfall/page-test.rkt index 684319e2..e50f057b 100644 --- a/pitfall/pitfall/page-test.rkt +++ b/pitfall/pitfall/page-test.rkt @@ -4,11 +4,12 @@ "document.rkt" "page.rkt" "reference.rkt" + "core.rkt" sugar/unstable/js) (define p (make-object PDFPage (make-object PDFDocument))) (check-equal? (· p size) "letter") (check-equal? (· p layout) "portrait") -(check-equal? (· p margins) '#hasheq((right . 72) (bottom . 72) (left . 72) (top . 72))) +(check-equal? (· p margins) (margin 72 72 72 72)) (check-equal? (· p height) 792.0) (check-equal? (· p width) 612.0) (check-equal? (· p resources payload ProcSet) '("PDF" "Text" "ImageB" "ImageC" "ImageI")) diff --git a/pitfall/pitfall/page.rkt b/pitfall/pitfall/page.rkt index 2313f0a9..3fff4b9a 100644 --- a/pitfall/pitfall/page.rkt +++ b/pitfall/pitfall/page.rkt @@ -4,11 +4,10 @@ racket/contract sugar/unstable/class sugar/unstable/js - sugar/unstable/dict) + sugar/unstable/dict + "core.rkt") -(provide PDFPage) - -(struct margin (top left bottom right) #:transparent #:mutable) +(provide (all-defined-out)) (define PDFPage (class object% diff --git a/pitfall/pitfall/text.rkt b/pitfall/pitfall/text.rkt index cb8277c6..44b7f18d 100644 --- a/pitfall/pitfall/text.rkt +++ b/pitfall/pitfall/text.rkt @@ -10,7 +10,8 @@ sugar/unstable/dict sugar/list racket/promise - fontland/glyph-position) + fontland/glyph-position + "core.rkt") (provide text-mixin) #| @@ -111,7 +112,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee ;; wrap to margins if no x or y position passed (unless (not (hash-ref options 'lineBreak #t)) (define margins (· this page margins)) - (hash-ref! options 'width (λ () (- (· this page width) (· this x) (· margins right))))) + (hash-ref! options 'width (λ () (- (· this page width) (· this x) (margin-right margins))))) (hash-ref! options 'columns 0) (hash-ref! options 'columnGap 18) ; 1/4 inch in PS points