amendments

main
Matthew Butterick 7 years ago
parent fa79310464
commit 5e4f771610

@ -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")

@ -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))

@ -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)))

@ -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)
)

@ -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)))

@ -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)

Binary file not shown.

@ -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

Binary file not shown.

@ -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))

@ -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)))

Loading…
Cancel
Save