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/text.rkt

168 lines
6.9 KiB
Racket

#lang racket/base
(require
6 years ago
"core.rkt"
racket/class
racket/match
racket/string
racket/list
sugar/unstable/class
sugar/unstable/dict
sugar/list
racket/promise
6 years ago
fontland/glyph-position)
(provide text-mixin)
#|
approximates
https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
|#
(define (text-mixin [% mixin-tester%])
(class %
(super-new)
6 years ago
(field [@line-gap 0]
[@text-options #f]
[(@x x) 0]
[(@y y) 0])
6 years ago
(inherit-field [@current-font current-font]
[@current-font-size current-font-size]
6 years ago
[@current-fill-color current-fill-color]
@pages)
6 years ago
(inherit [@current-line-height current-line-height])
6 years ago
(inherit save line-width move-to line-to stroke stroke-color transform restore) ; from vector
6 years ago
(inherit add-content) ; from base
6 years ago
6 years ago
(define/public (move-down [lines 1] #:factor [factor 1])
6 years ago
(set! @y (+ @y (* factor (@current-line-height #t) (+ lines @line-gap))))
6 years ago
this)
6 years ago
(define/public (move-up [lines 1])
(move-down this #:factor -1))
6 years ago
(define/public (text str [x #f] [y #f] [options (mhash)])
6 years ago
(when x (set! @x x))
(when y (set! @y y))
6 years ago
(line str options)
6 years ago
this)
(define/public (string-width str [options (mhash)])
6 years ago
(+ (send @current-font string-width str @current-font-size (hash-ref options 'features #f))
6 years ago
(* (hash-ref options 'characterSpacing 0) (sub1 (string-length str)))))
6 years ago
(define/public (line str [options (mhasheq)])
(fragment str @x @y options)
(define line-gap (or (hash-ref options 'line-gap #f) @line-gap 0))
6 years ago
;; 181224 unsuppress size tracking in test mode to preserve test 04
;; otherwise we'll be doing our own line measurement
(when (test-mode) (set! @x (+ @x (send this string-width str))))
6 years ago
(void))
6 years ago
(define/public (fragment text x y-in options)
(define character-spacing (hash-ref options 'characterSpacing 0))
6 years ago
;; calculate the actual rendered width of the string after word and character spacing
6 years ago
(define rendered-width
6 years ago
;; wrap this in delay so it's only calculated if needed
(delay
6 years ago
(+ (string-width text options)
6 years ago
(* character-spacing (sub1 (string-length text))))))
6 years ago
;; create link annotations if the link option is given
6 years ago
(when (hash-ref options 'link #f)
6 years ago
(send this link x y-in (force rendered-width) (@current-line-height) (hash-ref options 'link)))
7 years ago
6 years ago
;; create underline or strikethrough line
6 years ago
(when (or (hash-ref options 'underline #f) (hash-ref options 'strike #f))
6 years ago
(save)
6 years ago
(unless (hash-ref options 'stroke #f)
6 years ago
(define fill-color-args @current-fill-color)
6 years ago
(send this stroke-color . fill-color-args))
6 years ago
(define new-line-width (if (< @current-font-size 10) 0.5 (floor (/ @current-font-size 10))))
(line-width new-line-width)
6 years ago
(define d (if (hash-ref options 'underline) 1 2))
6 years ago
(define line-y (+ y-in (/ (@current-line-height) d)))
6 years ago
(when (hash-ref options 'underline)
6 years ago
(set! line-y (+ line-y (- new-line-width))))
6 years ago
(move-to x line-y)
(line-to (+ x (force rendered-width)) line-y)
(stroke)
(restore))
6 years ago
;; flip coordinate system
6 years ago
(save)
(define page-height (get-field height (first @pages)))
(transform 1 0 0 -1 0 page-height)
(define y (- page-height
6 years ago
y-in
6 years ago
(* (/ (get-field ascender @current-font) 1000)
@current-font-size)))
6 years ago
;; add current font to page if necessary
6 years ago
(define current-font-id (get-field id @current-font))
6 years ago
(hash-ref! (send (first @pages) fonts) current-font-id (λ () (send @current-font make-font-ref)))
6 years ago
(add-content "BT") ; begin the text object
(add-content (format "1 0 0 1 ~a ~a Tm" (numberizer x) (numberizer y))) ; text position
(add-content (format "/~a ~a Tf" current-font-id
6 years ago
(numberizer @current-font-size))) ; font and font size
6 years ago
(let ([mode (+ (if (hash-ref options 'fill #f) 1 0) (if (hash-ref options 'stroke #f) 1 0))])
6 years ago
(when (and mode (not (zero? mode)))
6 years ago
(add-content (format "~a Tr" mode))))
6 years ago
(when (not (zero? character-spacing))
6 years ago
(add-content (format "~a Tc" character-spacing)))
6 years ago
;; Add the actual text
;; 180321: the first call to this operation is very slow from Quad
;; 181126: because `encode` calls `layout`
(match-define (list encoded-char-strs positions)
6 years ago
(map list->vector (send @current-font encode text (hash-ref options 'features #f))))
7 years ago
6 years ago
(define scale (/ @current-font-size 1000.0))
6 years ago
(define commands empty)
7 years ago
6 years ago
;; Adds a segment of text to the TJ command buffer
6 years ago
(define last-segment 0)
(define (add-segment cur)
(when (< last-segment cur)
6 years ago
(define hex (string-append* (for/list ([str (in-vector encoded-char-strs last-segment cur)]) str)))
(define posn (vector-ref positions (sub1 cur)))
6 years ago
(define advance (- (glyph-position-x-advance posn) (glyph-position-advance-width posn)))
(set! commands (cons (format "<~a> ~a" hex (numberizer (- advance))) commands)))
(set! last-segment cur))
6 years ago
;; Flushes the current TJ commands to the output stream
6 years ago
(define (flush idx)
(add-segment idx)
6 years ago
(when (positive? (length commands))
6 years ago
(add-content (format "[~a] TJ" (string-join (reverse commands) " ")))
6 years ago
(set! commands empty)))
7 years ago
6 years ago
(for/fold ([had-offset #f] [x x])
([(posn idx) (in-indexed positions)])
(define having-offset
6 years ago
(cond
;; If we have an x or y offset, we have to break out of the current TJ command
;; so we can move the text position.
[(or (not (zero? (glyph-position-x-offset posn))) (not (zero? (glyph-position-y-offset posn))))
6 years ago
(flush idx)
6 years ago
(add-content ; Move the text position and flush just the current character
6 years ago
(format "1 0 0 1 ~a ~a Tm"
(numberizer (+ x (* (glyph-position-x-offset posn) scale)))
(numberizer (+ y (* (glyph-position-y-offset posn) scale)))))
6 years ago
(flush (add1 idx))
#true]
6 years ago
[else
;; If the last character had an offset, reset the text position
6 years ago
(when had-offset
6 years ago
(add-content (format "1 0 0 1 ~a ~a Tm" (numberizer x) (numberizer y))))
6 years ago
;; Group segments that don't have any advance adjustments
(unless (zero? (- (glyph-position-x-advance posn) (glyph-position-advance-width posn)))
6 years ago
(add-segment (add1 idx)))
#false]))
(values having-offset (+ x (* (glyph-position-x-advance posn) scale))))
6 years ago
6 years ago
(flush (vector-length positions))
6 years ago
(add-content "ET") ; end the text object
6 years ago
(restore)))) ; restore flipped coordinate system