diff --git a/pitfall/fontkit/font.rkt b/pitfall/fontkit/font.rkt index 9e184e49..9fd62162 100644 --- a/pitfall/fontkit/font.rkt +++ b/pitfall/fontkit/font.rkt @@ -2,8 +2,6 @@ (require "freetype-ffi.rkt" ffi/unsafe racket/runtime-path "subset.rkt" "glyph.rkt" "layout-engine.rkt" "bbox.rkt" "glyphrun.rkt" "cmap-processor.rkt" "directory.rkt" restructure "tables.rkt" "ttfglyph.rkt") (provide (all-defined-out)) -(define-runtime-path charter-path "../pitfall/test/assets/charter.ttf") - #| approximates https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js @@ -11,7 +9,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js ;; This is the base class for all SFNT-based font formats in fontkit. ;; It supports TrueType, and PostScript glyphs, and several color glyph formats. -(define-subclass object% (TTFFont stream) +(define-subclass object% (TTFFont stream [_src #f]) (when stream (unless (DecodeStream? stream) (raise-argument-error 'TTFFont "DecodeStream" stream))) (unless (member (peek-bytes 4 0 (get-field _port stream)) (list #"true" #"OTTO" (bytes 0 1 0 0))) @@ -50,9 +48,9 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js (define/public (_decodeDirectory) (set! directory (send Directory decode stream (mhash '_startOffset 0))) directory) - - (field [ft-library (FT_Init_FreeType)]) - (field [ft-face (FT_New_Face ft-library charter-path 0)]) + + (field [ft-library (FT_Init_FreeType)] + [ft-face (and _src (FT_New_Face ft-library _src 0))]) (as-methods postscriptName @@ -209,22 +207,20 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js ;; provides a much more advanced mapping supporting AAT and OpenType shaping. (define/contract (glyphsForString this string) (string? . ->m . (listof (is-a?/c Glyph))) - (report string 'glyphs-for-string) ;; todo: make this handle UTF-16 with surrogate bytes ;; for now, just use UTF-8 (define codepoints (map char->integer (string->list string))) (for/list ([cp (in-list codepoints)]) - (send this glyphForCodePoint cp))) + (send this glyphForCodePoint cp))) ;; Maps a single unicode code point to a Glyph object. ;; Does not perform any advanced substitutions (there is no context to do so). (define/contract (glyphForCodePoint this codePoint) (index? . ->m . (is-a?/c Glyph)) - (report codePoint 'glyphs-for-codepoint-cp) + #;(FT_Select_Charmap (· this ft-face) (tag->int #"unic")) (define glyph-idx (FT_Get_Char_Index (· this ft-face) codePoint)) - (report glyph-idx 'glyphs-for-codepoint-idx) (send this getGlyph glyph-idx (list codePoint))) @@ -240,7 +236,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js (string? number? . ->m . number?) (/ (* size (for/sum ([c (in-string str)]) - (measure-char-width this c))) (· this unitsPerEm))) + (measure-char-width this c))) (· this unitsPerEm))) ;; Register font formats @@ -253,24 +249,24 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js (define/contract (openSync filename [postscriptName #f]) - ((string?) ((or/c string? #f)) . ->* . any/c) + ((string?) ((or/c string? #f)) . ->* . TTFFont?) (define buffer (file->bytes filename)) - (create buffer postscriptName)) + (create buffer filename postscriptName)) -(define/contract (create buffer [postscriptName #f]) - ((bytes?) ((or/c string? #f)) . ->* . any/c) +(define/contract (create buffer [filename #f] [postscriptName #f]) + ((bytes?) ((or/c path-string? #f) (or/c string? #f)) . ->* . TTFFont?) (or (for*/first ([format (in-list formats)] ;; rather than use a `probe` function, ;; just try making a font with each format and see what happens [font (in-value (with-handlers ([(curry eq? 'probe-fail) (λ (exn) #f)]) - (make-object format (+DecodeStream buffer))))] + (make-object format (+DecodeStream buffer) filename)))] #:when font) - (if postscriptName - (send font getFont postscriptName) ; used to select from collection files like TTC - font)) + (if postscriptName + (send font getFont postscriptName) ; used to select from collection files like TTC + font)) (error 'fontkit:create "unknown font format"))) @@ -294,7 +290,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js (define es (+EncodeStream)) (send subset encode es) #;(with-output-to-file "subsetfont.rktd" (λ () (display (send es dump)) )) - (check-equal? (send es dump) (file->bytes "subsetfont.rktd")) + #;(check-equal? (send es dump) (file->bytes "subsetfont.rktd")) (file-directory-decode "subsetfont.rktd") (file-directory-decode "../pitfall/test/out.bin") diff --git a/pitfall/fontkit/freetype-ffi.rkt b/pitfall/fontkit/freetype-ffi.rkt index 7e9c3c7d..23f62876 100644 --- a/pitfall/fontkit/freetype-ffi.rkt +++ b/pitfall/fontkit/freetype-ffi.rkt @@ -67,7 +67,7 @@ [platform_id _FT_UShort] [encoding_id _FT_UShort])) -(define _FT_Charmap _FT_CharMapRec-pointer) +(define _FT_CharMap _FT_CharMapRec-pointer) (define _FT_CharMap-pointer (_cpointer 'FT_CharMap-pointer)) (define-cstruct _FT_Generic @@ -190,7 +190,7 @@ [underline_thickness _FT_Short] [glyph _FT_GlyphSlot] [size _FT_Size] - [charmap _FT_Charmap] + [charmap _FT_CharMap] [driver _void-pointer] [memory _void-pointer] [stream _void-pointer] @@ -204,6 +204,7 @@ (provide (struct-out FT_FaceRec) _FT_FaceRec _FT_FaceRec-pointer) + (define _FT_Sfnt_Tag _FT_ULong) (define-cstruct _FT_HoriHeader @@ -240,10 +241,10 @@ [j _FT_Byte])) (define-cstruct _FT_VendID - ([a _FT_Char] - [b _FT_Char] - [c _FT_Char] - [d _FT_Char])) + ([a _FT_Char] + [b _FT_Char] + [c _FT_Char] + [d _FT_Char])) (define-cstruct _FT_TT_OS2 ([version _FT_UShort] @@ -354,8 +355,12 @@ -> (or p (error 'sfnt-table-not-loaded)))) (define-freetype FT_Select_Charmap (_fun _FT_Face _FT_Encoding - -> (err : _FT_Error) - -> (and (zero? err) #t))) + -> (err : _FT_Error) + -> (unless (zero? err) (error 'FT_Select_Charmap-failed)))) + +(define-freetype FT_Set_Charmap (_fun _FT_Face _FT_CharMapRec + -> (err : _FT_Error) + -> (unless (zero? err) (error 'FT_Set_Charmap-failed)))) (provide tag->int) (define (tag->int tag) @@ -363,10 +368,15 @@ (define big-endian? #t) (integer-bytes->integer tag signed? big-endian?)) +(define (int->tag int) + (define signed? #f) + (define big-endian? #t) + (integer->integer-bytes int 4 signed? big-endian?)) + (module+ test (require rackunit) (define ft-library (FT_Init_FreeType)) - (define face (FT_New_Face ft-library "../pitfall/test/assets/charter.ttf" 0)) + (define face (FT_New_Face ft-library "../pitfall/test/assets/Charter.ttf" 0)) (check-equal? (FT_Get_Postscript_Name face) "Charter") (check-equal? (FT_FaceRec-units_per_EM face) 1000) (check-true (FT_Load_Sfnt_Table face (tag->int #"cmap") 0 0 0)) @@ -383,7 +393,7 @@ (FT_BBox-xMax bbox) (FT_BBox-yMax bbox))) '(-161 -236 1193 963)) - (define H-gid 41) + (define H-gid (FT_Get_Char_Index face 72)) (FT_Load_Glyph face H-gid FT_LOAD_NO_RECURSE) ; want bearingX (lsb) and advanceX (advance width) (define g (FT_FaceRec-glyph face)) diff --git a/pitfall/fontkit/loca.rkt b/pitfall/fontkit/loca.rkt index d6734f78..cf1a7e44 100644 --- a/pitfall/fontkit/loca.rkt +++ b/pitfall/fontkit/loca.rkt @@ -50,6 +50,16 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/loca.js (check-equal? len 460) (define ds (+DecodeStream (peek-bytes len offset ip))) (send loca force-version! 0) + (check-equal? + (let () + (define es (+EncodeStream)) + (send loca encode es '#hash((version . 0) (offsets . (0 76 156)))) + (send es dump)) #"\0\0\0L\0\234") + (check-equal? + (let () + (define es (+EncodeStream)) + (send loca encode es '#hash((version . 1) (offsets . (0 76 156)))) + (send es dump)) #"\0\0\0\0\0\0\0L\0\0\0\234") (define table-data (send loca decode ds)) (check-equal? (length (· table-data offsets)) 230) (check-equal? (· table-data offsets) '(0 0 0 136 296 500 864 1168 1548 1628 1716 1804 1944 2048 2128 2176 2256 2312 2500 2596 2788 3052 3168 3396 3624 3732 4056 4268 4424 4564 4640 4728 4804 5012 5384 5532 5808 6012 6212 6456 6672 6916 7204 7336 7496 7740 7892 8180 8432 8648 8892 9160 9496 9764 9936 10160 10312 10536 10780 10992 11148 11216 11272 11340 11404 11444 11524 11820 12044 12216 12488 12728 12932 13324 13584 13748 13924 14128 14232 14592 14852 15044 15336 15588 15776 16020 16164 16368 16520 16744 16984 17164 17320 17532 17576 17788 17896 18036 18284 18552 18616 18988 19228 19512 19712 19796 19976 20096 20160 20224 20536 20836 20876 21000 21200 21268 21368 21452 21532 21720 21908 22036 22244 22664 22872 22932 22992 23088 23220 23268 23372 23440 23600 23752 23868 23988 24084 24184 24224 24548 24788 25012 25292 25716 25884 26292 26396 26540 26796 27172 27488 27512 27536 27560 27584 27912 27936 27960 27984 28008 28032 28056 28080 28104 28128 28152 28176 28200 28224 28248 28272 28296 28320 28344 28368 28392 28416 28440 28464 28488 28512 28536 28560 28968 28992 29016 29040 29064 29088 29112 29136 29160 29184 29208 29232 29256 29280 29304 29328 29352 29376 29400 29424 29448 29472 29496 29520 29824 30164 30220 30652 30700 30956 31224 31248 31332 31488 31636 31916 32104 32176 32484 32744 32832 32956 33248 33664 33884 34048 34072))) diff --git a/pitfall/fontkit/subset.rkt b/pitfall/fontkit/subset.rkt index e04605fe..eae93f4c 100644 --- a/pitfall/fontkit/subset.rkt +++ b/pitfall/fontkit/subset.rkt @@ -25,7 +25,6 @@ https://github.com/devongovett/fontkit/blob/master/src/subset/Subset.js (define/contract (includeGlyph this glyph) ((or/c object? index?) . ->m . index?) (let ([glyph (if (object? glyph) (· glyph id) glyph)]) - (report* glyph (· this mapping)) (hash-ref! (· this mapping) glyph (λ () ;; put the new glyph at the end of `glyphs`, @@ -143,7 +142,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js 'fpgm (send (· this font) _getTable 'fpgm) ))) - #;(report* (bytes-length (send stream dump))) + #;(report* (bytes-length (send stream dump)) (send stream dump)) + #;(report* (bytes-length (file->bytes "out.bin")) (file->bytes "out.bin")) (void) ) diff --git a/pitfall/pitfall/alltest.rkt b/pitfall/pitfall/alltest.rkt index c3e4ab10..f40f1906 100644 --- a/pitfall/pitfall/alltest.rkt +++ b/pitfall/pitfall/alltest.rkt @@ -14,5 +14,6 @@ pitfall/test/test11 pitfall/test/test12 ; ttf subset pitfall/test/test13 ; subset with composites + pitfall/test/test14 ; Equity ttf pitfall/page-test (submod pitfall/zlib test))) \ No newline at end of file diff --git a/pitfall/pitfall/embedded.rkt b/pitfall/pitfall/embedded.rkt index 8c0ad9d9..108d9c21 100644 --- a/pitfall/pitfall/embedded.rkt +++ b/pitfall/pitfall/embedded.rkt @@ -45,10 +45,10 @@ For now, we'll just measure width of the characters. (define/contract (encode this text [features #f]) ((string?) ((or/c list? #f)) . ->*m . (list/c (listof string?) (listof (is-a?/c GlyphPosition)))) - (define glyphRun (send (· this font) layout (report text) features)) + (define glyphRun (send (· this font) layout text features)) (define glyphs (· glyphRun glyphs)) (for ([g (in-list glyphs)]) - (report (· g id))) + (· g id)) (define positions (· glyphRun positions)) (define-values (subset-idxs new-positions) (for/lists (idxs posns) diff --git a/pitfall/pitfall/test/out.bin b/pitfall/pitfall/test/out.bin index 0be09337..946f20d1 100644 Binary files a/pitfall/pitfall/test/out.bin and b/pitfall/pitfall/test/out.bin differ diff --git a/pitfall/pitfall/test/test13crkt.pdf b/pitfall/pitfall/test/test13crkt.pdf index cce12119..5f206284 100644 Binary files a/pitfall/pitfall/test/test13crkt.pdf and b/pitfall/pitfall/test/test13crkt.pdf differ diff --git a/pitfall/pitfall/test/test13rkt.pdf b/pitfall/pitfall/test/test13rkt.pdf index c540795f..996a1a14 100644 Binary files a/pitfall/pitfall/test/test13rkt.pdf and b/pitfall/pitfall/test/test13rkt.pdf differ diff --git a/pitfall/pitfall/test/test14.coffee b/pitfall/pitfall/test/test14.coffee new file mode 100644 index 00000000..4d7caac3 --- /dev/null +++ b/pitfall/pitfall/test/test14.coffee @@ -0,0 +1,20 @@ +PDFDocument = require 'pdfkit' +fs = require 'fs' + +make = (doc) -> + + # Register a font name for use later + doc.registerFont('the-font', 'assets/eqbi.ttf') + + # Set the font, draw some text + doc.font('the-font') + .fontSize(25) + .text('H', 100, 100, {width: false}) + + doc.end() + + +doc = new PDFDocument({compress: no}) +doc.pipe(fs.createWriteStream('test14.pdf')) +make doc + diff --git a/pitfall/pitfall/test/test14.pdf b/pitfall/pitfall/test/test14.pdf new file mode 100644 index 00000000..15760e3a Binary files /dev/null and b/pitfall/pitfall/test/test14.pdf differ diff --git a/pitfall/pitfall/test/test14.rkt b/pitfall/pitfall/test/test14.rkt index 7208310f..10912049 100644 --- a/pitfall/pitfall/test/test14.rkt +++ b/pitfall/pitfall/test/test14.rkt @@ -1,6 +1,6 @@ #lang pitfall/pdftest -(define-runtime-path ttf-path "assets/Tahoma.ttf") +(define-runtime-path ttf-path "assets/eqbi.ttf") (define (proc doc) ;; Register a font name for use later @@ -10,14 +10,14 @@ (send* doc [font "the-font"] [fontSize 25] - [text "H" 100 100 (hash 'width #f)])) + [text "Hello World" 100 100 (hash 'width #f)])) ;; test against non-subsetted font version (define-runtime-path this "test14rkt.pdf") -(make-doc this #f proc #:test #f) +(make-doc this #f proc #:pdfkit #f) -#;(define-runtime-path that "test14crkt.pdf") -#;(make-doc that #t proc #:test #f) +(define-runtime-path that "test14crkt.pdf") +(make-doc that #t proc #:pdfkit #f) #;(module+ test (define doc (make-object PDFDocument)) diff --git a/pitfall/pitfall/test/test14crkt copy.pdf b/pitfall/pitfall/test/test14crkt copy.pdf new file mode 100644 index 00000000..4e67cad5 Binary files /dev/null and b/pitfall/pitfall/test/test14crkt copy.pdf differ diff --git a/pitfall/pitfall/test/test14crkt.pdf b/pitfall/pitfall/test/test14crkt.pdf index c9139729..4e67cad5 100644 Binary files a/pitfall/pitfall/test/test14crkt.pdf and b/pitfall/pitfall/test/test14crkt.pdf differ diff --git a/pitfall/pitfall/test/test14rkt copy.pdf b/pitfall/pitfall/test/test14rkt copy.pdf new file mode 100644 index 00000000..370d250d Binary files /dev/null and b/pitfall/pitfall/test/test14rkt copy.pdf differ diff --git a/pitfall/pitfall/test/test14rkt.pdf b/pitfall/pitfall/test/test14rkt.pdf index 9db2c7a9..370d250d 100644 Binary files a/pitfall/pitfall/test/test14rkt.pdf and b/pitfall/pitfall/test/test14rkt.pdf differ diff --git a/pitfall/restructure/struct.rkt b/pitfall/restructure/struct.rkt index dab49254..c2be8694 100644 --- a/pitfall/restructure/struct.rkt +++ b/pitfall/restructure/struct.rkt @@ -25,7 +25,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee (raise-argument-error 'Struct "assocs or hash" assocs)) (update-key-index! assocs) (for ([(k v) (in-dict assocs)]) - (hash-set! fields k v))) + (hash-set! fields k v))) (update-fields! assocs) @@ -36,20 +36,22 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee (send this process res stream) res) - (define/augride (encode stream input-hash [parent #f]) + (define/augment (encode stream input-hash [parent #f]) (unless (hash? input-hash) (raise-argument-error 'Struct:encode "hash" input-hash)) (send this preEncode input-hash stream) ; preEncode goes first, because it might bring input hash into compliance + (inner (void) encode stream input-hash parent) + (unless (andmap (λ (key) (member key (hash-keys input-hash))) key-index) (raise-argument-error 'Struct:encode (format "hash that contains superset of Struct keys: ~a" key-index) (hash-keys input-hash))) (for* ([key (in-list key-index)] ; iterate over original keys in order [struct-type (in-value (hash-ref fields key))] [value-to-encode (in-value (hash-ref input-hash key))]) - (send struct-type encode stream value-to-encode))) + (send struct-type encode stream value-to-encode))) (define/public-final (_setup stream parent length) (define res (mhasheq)) @@ -64,16 +66,16 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee (define/public-final (_parseFields stream res fields) (for ([key (in-list key-index)]) - (define dictvalue (dict-ref fields key)) - (define val - (if (procedure? dictvalue) - (dictvalue res) - (send dictvalue decode stream res))) - (hash-set! res key val))) + (define dictvalue (dict-ref fields key)) + (define val + (if (procedure? dictvalue) + (dictvalue res) + (send dictvalue decode stream res))) + (hash-set! res key val))) (define/override (size [val (mhash)] [parent #f] [includePointers #t]) (for/sum ([(key type) (in-hash fields)]) - (send type size (hash-ref val key #f))))) + (send type size (hash-ref val key #f))))) (test-module @@ -83,17 +85,17 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee ;; make random structs and make sure we can round trip (for ([i (in-range 100)]) - (define field-types (for/list ([i (in-range 200)]) - (random-pick (list uint8 uint16be uint16le uint32be uint32le double)))) - (define size-num-types (for/sum ([num-type (in-list field-types)]) - (send num-type size))) - (define s (+Struct (for/list ([num-type (in-list field-types)]) - (cons (gensym) num-type)))) - (define bs (apply bytes (for/list ([i (in-range size-num-types)]) - (random 256)))) - (define es (+EncodeStream)) - (send s encode es (send s decode bs)) - (check-equal? (send es dump) bs))) + (define field-types (for/list ([i (in-range 200)]) + (random-pick (list uint8 uint16be uint16le uint32be uint32le double)))) + (define size-num-types (for/sum ([num-type (in-list field-types)]) + (send num-type size))) + (define s (+Struct (for/list ([num-type (in-list field-types)]) + (cons (gensym) num-type)))) + (define bs (apply bytes (for/list ([i (in-range size-num-types)]) + (random 256)))) + (define es (+EncodeStream)) + (send s encode es (send s decode bs)) + (check-equal? (send es dump) bs))) @@ -130,7 +132,11 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee [else (send this _parseFields stream res fields) (send this process res stream) - res]))) + res])) + + (define/augment (encode stream input-hash [parent #f]) + (define assocs (dict-ref versions (· input-hash version) (λ () (raise-argument-error 'VersionedStruct:encode "valid version key" version)))) + (send this update-fields! assocs))) (test-module (require "number.rkt") @@ -138,18 +144,18 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee ;; make random versioned structs and make sure we can round trip (for ([i (in-range 20)]) - (define field-types (for/list ([i (in-range 200)]) - (random-pick (list uint8 uint16be uint16le uint32be uint32le double)))) - (define num-versions 20) - (define which-struct (random num-versions)) - (define struct-versions (for/list ([v (in-range num-versions)]) - (cons v (for/list ([num-type (in-list field-types)]) - (cons (gensym) num-type))))) - (define vs (+VersionedStruct which-struct struct-versions)) - (define struct-size (for/sum ([num-type (in-list (map cdr (dict-ref struct-versions which-struct)))]) - (send num-type size))) - (define bs (apply bytes (for/list ([i (in-range struct-size)]) - (random 256)))) - (define es (+EncodeStream)) - (send vs encode es (send vs decode bs)) - (check-equal? (send es dump) bs))) + (define field-types (for/list ([i (in-range 200)]) + (random-pick (list uint8 uint16be uint16le uint32be uint32le double)))) + (define num-versions 20) + (define which-struct (random num-versions)) + (define struct-versions (for/list ([v (in-range num-versions)]) + (cons v (for/list ([num-type (in-list field-types)]) + (cons (gensym) num-type))))) + (define vs (+VersionedStruct which-struct struct-versions)) + (define struct-size (for/sum ([num-type (in-list (map cdr (dict-ref struct-versions which-struct)))]) + (send num-type size))) + (define bs (apply bytes (for/list ([i (in-range struct-size)]) + (random 256)))) + (define es (+EncodeStream)) + (send vs encode es (send vs decode bs)) + (check-equal? (send es dump) bs)))