You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/pitfall/pitfall/core.rkt

66 lines
1.7 KiB
Racket

#lang racket/base
(require racket/match racket/port)
(provide (all-defined-out))
;; structs
(struct String (string) #:transparent)
;; 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))
(define current-compress-streams? (make-parameter #f))
(define current-pdf-version (make-parameter 1.3))
(define current-auto-first-page (make-parameter #t))
(define current-auto-helvetica (make-parameter #t))
(define current-default-margins (make-parameter (margin 72 72 72 72)))
(define current-font (make-parameter #f))
(define current-font-size (make-parameter 12))
(define current-ref-listeners (make-parameter null))
;; helpers
(define (numberizer x #:round [round? #true])
(unless (and (number? x) (< -1e21 x 1e21))
(raise-argument-error 'number "valid number" x))
(let ([x (if round? (/ (round (* x 1e6)) 1e6) x)])
(number->string (if (integer? x)
(inexact->exact x)
x))))
(define (to-bytes x)
(match x
[(? bytes?) x]
[(? input-port?) (port->bytes x)]
[_ (string->bytes/latin-1 (string-append x "\n"))]))
(define (write-bytes-out x)
(void (write-bytes (to-bytes x))))
(define (bounded low x high)
(if (high . < . low)
(bounded high x low)
(max low (min high x))))
(module+ test
(require rackunit)
(check-equal? (bounded 0 2 1) 1)
(check-equal? (bounded 1 2 0) 1)
(check-equal? (bounded 0 -2 1) 0)
(check-equal? (bounded 1 -2 0) 0)
(check-equal? (bounded 0 .5 1) 0.5)
(check-equal? (bounded 0 0 1) 0)
(check-equal? (bounded 0 1 1) 1))