diff --git a/pitfall/pitfall/charter.otf b/pitfall/pitfall/charter.otf new file mode 100644 index 00000000..d2314d35 Binary files /dev/null and b/pitfall/pitfall/charter.otf differ diff --git a/pitfall/pitfall/render.rkt b/pitfall/pitfall/render.rkt index 256483a5..d7b675de 100644 --- a/pitfall/pitfall/render.rkt +++ b/pitfall/pitfall/render.rkt @@ -17,12 +17,15 @@ @(let ([sep " 00000 n\n"]) (string-join (for/list ([loc (in-list (cdr (sort locs < #:key car)))]) - (~r #:min-width 10 #:pad-string "0" (cdr loc))) sep #:after-last sep)) + (~r #:min-width 10 #:pad-string "0" (cdr loc))) sep #:after-last sep)) })) (define (render-args . args) (render-list args)) +(define (render thing) + (render-list (list thing))) + (define (render-list args) (define-values (bstrs offset locs) (for/fold ([cobstrs null] @@ -40,7 +43,7 @@ (define trailer-str (cosexpr->bytes (co-trailer (co-dict (hasheq 'Size (length bstrs) 'Root (co-io-ref 1 0)))))) (define last-offset (for/sum ([bstr (in-list bstrs)]) - (bytes-length bstr))) + (bytes-length bstr))) (define result (apply bytes-append `(,header-str ,@(reverse bstrs) ,(make-xref-table locs) @@ -76,7 +79,7 @@ << @(string-join (for/list ([(k v) (in-hash (co-dict-dict x))]) - @string-append{@(loop k) @(loop v)}) "\n") + @string-append{@(loop k) @(loop v)}) "\n") >>}] [(co-io-ref? x) @string-append{@(loop (co-io-ref-idx x)) @(loop (co-io-ref-rev x)) R}] @@ -93,6 +96,11 @@ trailer @(loop (co-trailer-dict x)) }] + [(co-hash? x) + (string-join + (map loop (for/list ([(k v) (in-hash (co-hash-hash x))]) + (co-io k 0 v))) "")] + [(co-encoding-datum? x) (symbol->string (co-encoding-datum-datum x))] [(symbol? x) @string-append{/@(symbol->string x)}] [(number? x) @number->string{@x}] [(string? x) x] @@ -119,6 +127,12 @@ 'Contents contents 'Annots annots))) +(define (make-co-hash) + (co-hash (make-hash))) + +(define (+co-hash cosh idx thing) + (hash-set! (co-hash-hash cosh) idx thing)) + (define (make-co-dict . xs) (co-dict (apply hasheq xs))) @@ -128,4 +142,11 @@ (define (make-font-co-stream font-path) (make-co-stream (file->bytes font-path) 'Subtype 'OpenType)) +(define (make-co-io-ref [idx 0] [rev 0]) + (co-io-ref idx rev)) + +(define (make-encoding-list xs) + (co-array (for/list ([x (in-list xs)]) + (co-encoding-datum x)))) + #;(cosexpr->bytes (make-co-dict 'Hello (co-string "World"))) \ No newline at end of file diff --git a/pitfall/pitfall/struct.rkt b/pitfall/pitfall/struct.rkt index d0a1b449..c870abd6 100644 --- a/pitfall/pitfall/struct.rkt +++ b/pitfall/pitfall/struct.rkt @@ -11,3 +11,6 @@ (struct co-io-ref (idx rev) #:transparent) (struct co-comment (text) #:transparent) (struct co-trailer (dict) #:transparent) + +(struct co-hash (hash) #:transparent) +(struct co-encoding-datum (datum) #:transparent) \ No newline at end of file diff --git a/pitfall/pitfall/triple-measured-font.rkt b/pitfall/pitfall/triple-measured-font.rkt new file mode 100644 index 00000000..b88b6466 --- /dev/null +++ b/pitfall/pitfall/triple-measured-font.rkt @@ -0,0 +1,134 @@ +#lang at-exp br +(require pitfall/render) + +(define cosh (make-co-hash)) + +;; catalog object +(+co-hash cosh 1 (co-catalog #:pages (make-co-io-ref 2))) + +;; pages +(+co-hash cosh 2 (co-pages #:kids (list (make-co-io-ref 3)) + #:count 1)) +;; page +(+co-hash cosh 3 (co-page #:parent (make-co-io-ref 2) + #:mediabox '(0 0 400 400) + #:resources (make-co-io-ref 4) + #:contents (make-co-io-ref 5) + + ; the value of annots must be an array + #:annots (co-array (list (make-co-io-ref 7))) + )) +#;#:annots #;(make-co-io-ref 7) + +;; resources +(+co-hash cosh 4 + (make-co-dict + 'ProcSet (co-array '(PDF Text)) + 'Font (make-co-dict 'F1 (make-co-io-ref 6) + 'F2 (make-co-io-ref 9) + 'F3 (make-co-io-ref 12)))) + + + + +(define (typeset-text str size font-path) + (report + (string->bytes/latin-1 + (string-append* + (for/list ([c (in-string str)]) + (format "(~a) Tj ~a 0 Td" c (/ (* size (measure-char font-path c)) 1000.0))))))) + + + +;; Charter font +(define charter-font-path "charter.otf") +(+co-hash cosh 6 + (make-co-dict + 'Type 'Font + 'Subtype 'Type1 + 'Name 'F1 + 'FontDescriptor (make-co-dict + 'Type 'FontDescriptor + 'FontFile3 (make-co-io-ref 8) + 'Flags 4 + 'FontBBox (co-array '(-177 -269 1123 866)) + 'MissingWidth 255 + 'StemV 105 + 'StemH 45 + 'CapHeight 660 + 'XHeight 394 + 'Ascent 720 + 'Descent -270 + 'Leading 83 + 'MaxWidth 1212 + 'AvgWidth 478 + 'ItalicAngle 0))) + +(+co-hash cosh 8 (make-font-co-stream charter-font-path)) + +;; Miso font +(define miso-font-path "miso.otf") +(+co-hash cosh 9 + (make-co-dict + 'Type 'Font + 'Subtype 'Type1 + 'Name 'F2 + 'FontDescriptor (make-co-dict + 'Type 'FontDescriptor + 'FontFile3 (make-co-io-ref 10) + 'Flags 4 + 'FontBBox (co-array '(-177 -269 1123 866)) + 'MissingWidth 255 + 'StemV 105 + 'StemH 45 + 'CapHeight 660 + 'XHeight 394 + 'Ascent 720 + 'Descent -270 + 'Leading 83 + 'MaxWidth 1212 + 'AvgWidth 478 + 'ItalicAngle 0))) + +(+co-hash cosh 10 (make-font-co-stream miso-font-path)) + +;; fira font +(define fira-font-path "fira.otf") +(+co-hash cosh 12 + (make-co-dict + 'Type 'Font + 'Subtype 'Type1 + 'Name 'F3 + 'FontDescriptor (make-co-dict + 'Type 'FontDescriptor + 'FontFile3 (make-co-io-ref 13) + 'Flags 4 + 'FontBBox (co-array '(-177 -269 1123 866)) + 'MissingWidth 255 + 'StemV 105 + 'StemH 45 + 'CapHeight 660 + 'XHeight 394 + 'Ascent 720 + 'Descent -270 + 'Leading 83 + 'MaxWidth 1212 + 'AvgWidth 478 + 'ItalicAngle 0))) + +(+co-hash cosh 13 (make-font-co-stream fira-font-path)) + + +;; contents +(+co-hash cosh 5 + (make-co-stream + (bytes-append + #"BT /F1 36 Tf 50 50 Td" + (typeset-text "Hello World 4-1" 36 charter-font-path) + #"/F2 36 Tf -250 50 Td" + (typeset-text "Enchilada 10" 36 miso-font-path) + #"/F3 36 Tf -150 50 Td" + (typeset-text "Boing Me 5" 36 fira-font-path) + #"ET"))) + +(render cosh)