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/quad/experimental.rkt

64 lines
3.3 KiB
Racket

10 years ago
#lang racket/base
10 years ago
(require racket/list sugar/define)
10 years ago
(require "samples.rkt" "quads.rkt" "utils.rkt")
(define ti (block '(measure 54 leading 18) "Meg is " (box '(foo 42)) " ally."))
10 years ago
(define tib (block '(measure 240 font "Equity Text B" leading 16 size 13.5 x-align justify x-align-last-line left) (block #f (block '(weight bold font "Equity Caps B") "Hello") (block-break) (box '(width 15)))))
10 years ago
;ti
10 years ago
;; convert quad into tokenized representation:
;; 1) vector of atomic tokens
;; 2) list of (attribute + range of tokens it applies to)
;; this representation is designed to:
;; 1) preserve all information in the original quad
;; 2) be compact / not duplicate information unnecessarily
;; 3) allow sequential access to the tokens
;; 4) allow fast computation of token state (i.e., attrs that apply)
10 years ago
(define+provide (make-tokens-and-attrs quad-in)
10 years ago
(define-values (all-tokens all-attrs _)
10 years ago
(let loop ([current-quad quad-in][attr-acc empty][starting-tidx 0])
(cond
10 years ago
[(empty? (quad-list current-quad)); no subelements, so treat this quad as single token
(let ([current-quad-attrs (quad-attrs current-quad)]
[ending-tidx (add1 starting-tidx)])
(values (quad (quad-name current-quad) #f empty)
(if current-quad-attrs
(cons (vector current-quad-attrs starting-tidx ending-tidx) attr-acc)
attr-acc)
ending-tidx))]
10 years ago
[else ; replace quad with its tokens, exploded
10 years ago
(define-values (tokens-from-fold subattrs-from-fold ending-tidx-from-fold)
10 years ago
(for/fold ([token-acc empty][subattr-acc empty][tidx starting-tidx])
([item (in-list (quad-list current-quad))])
(cond
[(quad? item)
(define-values (sub-tokens sub-attrs sub-last-tidx) (loop item attr-acc tidx))
(values (cons sub-tokens token-acc) (cons sub-attrs subattr-acc) sub-last-tidx)]
10 years ago
[else ; item is a string of length > 0 (quad contract guarantees this)
(define-values (exploded-chars last-idx-of-exploded-chars)
(for/fold ([chars empty][last-idx #f])([(c i) (in-indexed item)])
(values (cons c chars) i))) ; fold manually to get reversed items & length at same time
(values (cons exploded-chars token-acc) subattr-acc (+ tidx (add1 last-idx-of-exploded-chars)))])))
10 years ago
(values tokens-from-fold
10 years ago
(let ([current-quad-attrs (quad-attrs current-quad)])
(if current-quad-attrs
(cons (vector current-quad-attrs starting-tidx ending-tidx-from-fold) subattrs-from-fold)
subattrs-from-fold))
ending-tidx-from-fold)])))
(values (list->vector (reverse (flatten all-tokens))) (flatten all-attrs)))
10 years ago
(define-values (tokens attrs) (make-tokens-and-attrs (ti5)))
(define+provide current-tokens (make-parameter tokens))
(define+provide current-token-attrs (make-parameter attrs))
10 years ago
;(filter (λ(idx) (box? (vector-ref tokens idx))) (range (vector-length tokens)))
(define (attr-ref-hash a) (vector-ref a 0))
(define (attr-ref-start a) (vector-ref a 1))
(define (attr-ref-end a) (vector-ref a 2))
10 years ago
(define (calc-attrs tref)
10 years ago
(map attr-ref-hash (filter (λ(attr) (<= (attr-ref-start attr) tref (sub1 (attr-ref-end attr)))) (current-token-attrs))))