diff --git a/pitfall/pdfkit/lib/mixins/text.coffee b/pitfall/pdfkit/lib/mixins/text.coffee index 70ca5616..48c7fc21 100644 --- a/pitfall/pdfkit/lib/mixins/text.coffee +++ b/pitfall/pdfkit/lib/mixins/text.coffee @@ -296,7 +296,7 @@ module.exports = for pos, i in positions # 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. - console.log("pos.xOffset or pos.yOffset=" + (pos.xOffset or pos.yOffset)) + #console.log("pos.xOffset or pos.yOffset=" + (pos.xOffset or pos.yOffset)) if pos.xOffset or pos.yOffset # Flush the current buffer flush i diff --git a/pitfall/pdfkit/node_modules/fontkit/index.js b/pitfall/pdfkit/node_modules/fontkit/index.js index e0ba656a..8d7e9fc1 100644 --- a/pitfall/pdfkit/node_modules/fontkit/index.js +++ b/pitfall/pdfkit/node_modules/fontkit/index.js @@ -41,7 +41,7 @@ fontkit.registerFormat = function (format) { fontkit.openSync = function (filename, postscriptName) { var buffer = fs.readFileSync(filename); - console.log(filename); + //console.log(filename); return fontkit.create(buffer, postscriptName); }; @@ -12285,6 +12285,7 @@ var TTFFont = (_class = function () { }; TTFFont.prototype._decodeDirectory = function _decodeDirectory() { + console.log("Directory.decode="+Directory.decode) return this.directory = Directory.decode(this.stream, { _startOffset: 0 }); }; diff --git a/pitfall/pitfall/directory.rkt b/pitfall/pitfall/directory.rkt new file mode 100644 index 00000000..d1a84583 --- /dev/null +++ b/pitfall/pitfall/directory.rkt @@ -0,0 +1,5 @@ +#lang pitfall/racket +(provide (all-defined-out)) + +(define (directory-decode . xs) + 'boom) \ No newline at end of file diff --git a/pitfall/pitfall/embedded.rkt b/pitfall/pitfall/embedded.rkt index df85d4f6..f2c9c265 100644 --- a/pitfall/pitfall/embedded.rkt +++ b/pitfall/pitfall/embedded.rkt @@ -59,6 +59,8 @@ For now, we'll just measure width of the characters. (values subset-idx posn))) (list subset-idxs new-positions)) +(require racket/runtime-path) +(define-runtime-path charter-path "test/assets/charter.ttf") (define/contract (embed this) (->m void?) @@ -70,7 +72,7 @@ For now, we'll just measure width of the characters. ;; todo ;; (send (send (· this subset) encodeStream) pipe fontFile) - (send fontFile end) ;; temp + (send fontFile end (send (· this subset) encode)) ;; temp ;; todo ;; (define familyClass (send (· this font) has-table? #"OS/2")) @@ -107,7 +109,7 @@ For now, we'll just measure width of the characters. 'FontFile2) fontFile) (· descriptor end) - (report (· descriptor toString) 'descriptor-id) + #;(report (· descriptor toString) 'descriptor-id) (define descendantFont (send (· this document) ref (mhash @@ -124,7 +126,7 @@ For now, we'll just measure width of the characters. (hash-ref (· this widths) idx (λ () (error 'embed (format "hash key ~a not found" idx))))))))) (· descendantFont end) - (report (· descendantFont toString) 'descendantFont) + #;(report (· descendantFont toString) 'descendantFont) (hash-set*! (· this dictionary payload) 'Type "Font" 'Subtype "Type0" @@ -170,7 +172,7 @@ For now, we'll just measure width of the characters. end end }) - (report (· cmap toString) 'cmap-id) + #;(report (· cmap toString) 'cmap-id) cmap) (define/contract (toHex . codePoints) diff --git a/pitfall/pitfall/fontkit.rkt b/pitfall/pitfall/fontkit.rkt index 5946b885..eca555f7 100644 --- a/pitfall/pitfall/fontkit.rkt +++ b/pitfall/pitfall/fontkit.rkt @@ -1,5 +1,5 @@ #lang pitfall/racket -(require "freetype-ffi.rkt" ffi/unsafe racket/runtime-path "subset.rkt" "glyph.rkt" "layout-engine.rkt" "bbox.rkt" "glyphrun.rkt" "cmap-processor.rkt") +(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") (provide (all-defined-out)) (define-runtime-path charter-path "test/assets/charter.ttf") @@ -7,25 +7,31 @@ ;; approximates ;; https://github.com/devongovett/fontkit/blob/master/src/TTFFont.js -(define-subclass object% (TTFFont filename) +;; 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 (open-input-bytes #"")]) (super-new) - (field [_tables (mhash)] + (when stream (unless (input-port? stream) + (raise-argument-error 'TTFFont "input port" stream))) + (field [_directoryPos (let-values ([(l c p) (port-next-location stream)]) + p)] + [_tables (mhash)] [_glyphs (mhash)] [_layoutEngine #f]) - - (field [buffer (file->bytes filename)]) - (define (buffer->font buffer) - 'made-ttf-font) + (field [directory #f]) + (send this _decodeDirectory) + + (define/public (_decodeDirectory) + (set! directory (directory-decode stream (mhash '_startOffset 0))) + directory) - (define (probe buffer) + (define/public (probe buffer) (and (member (bytes->string/latin-1 (subbytes buffer 0 4)) (list "true" "OTTO" "\u0\u1\u0\u0")) 'TTF-format)) - (and (probe buffer) (buffer->font buffer)) - (field [ft-library (FT_Init_FreeType)]) (field [ft-face (FT_New_Face ft-library charter-path 0)]) @@ -224,21 +230,24 @@ ;;fontkit.registerFormat(DFont); ;; todo -(define/contract (create filename [postscriptName #f]) + +(define/contract (openSync filename [postscriptName #f]) ((string?) ((or/c string? #f)) . ->* . any/c) + (define buffer (file->bytes filename)) + (create buffer postscriptName)) + + + +(define/contract (create buffer [postscriptName #f]) + ((bytes?) ((or/c string? #f)) . ->* . any/c) (or (for*/first ([format (in-list formats)] - [font (in-value (make-object format filename))] - #:when font) + #:when (send (make-object format) probe buffer)) + (define font (make-object format (open-input-bytes buffer))) (if postscriptName (send font getFont postscriptName) ; used to select from collection files like TTC font)) - (error 'create "unknown font format"))) - - -(define/contract (openSync filename [postscriptName #f]) - ((string?) ((or/c string? #f)) . ->* . any/c) - (create filename postscriptName)) + (error 'fontkit:create "unknown font format"))) (module+ test diff --git a/pitfall/pitfall/reference.rkt b/pitfall/pitfall/reference.rkt index 0f43161d..d2dd6a6f 100644 --- a/pitfall/pitfall/reference.rkt +++ b/pitfall/pitfall/reference.rkt @@ -14,17 +14,19 @@ (define/contract (write this x) - ((or/c string? isBuffer?) . ->m . void?) - (push-field! byte-strings this (if (isBuffer? x) - x - (bytes-append (newBuffer x) #"\n")))) + ((or/c string? isBuffer? input-port?) . ->m . void?) + (push-field! byte-strings this + (let loop ([x x]) + (cond + [(isBuffer? x) x] + [(input-port? x) (loop (port->bytes x))] + [else (bytes-append (newBuffer x) #"\n")])))) (define got-byte-strings? pair?) (define/contract (end this [chunk #f]) - (() ((or/c string? isBuffer?)) . ->*m . void?) - (when chunk - (send this write chunk)) + (() ((or/c string? isBuffer? input-port?)) . ->*m . void?) + (when chunk (send this write chunk)) #;(report* 'end! (· this id)) (define bstrs-to-write diff --git a/pitfall/pitfall/subset.rkt b/pitfall/pitfall/subset.rkt index eb375f92..da129930 100644 --- a/pitfall/pitfall/subset.rkt +++ b/pitfall/pitfall/subset.rkt @@ -36,4 +36,11 @@ (define-subclass Subset (TTFSubset) (super-new) - ) \ No newline at end of file + + (as-methods + encode) + ) + +(define/contract (encode this) + (->m input-port?) + (· this font stream)) \ No newline at end of file diff --git a/pitfall/pitfall/test/test12.pdf b/pitfall/pitfall/test/test12.pdf index 0abd17f6..74a772a1 100644 Binary files a/pitfall/pitfall/test/test12.pdf and b/pitfall/pitfall/test/test12.pdf differ diff --git a/pitfall/pitfall/test/test12.rkt b/pitfall/pitfall/test/test12.rkt index 27819d30..f3b98066 100644 --- a/pitfall/pitfall/test/test12.rkt +++ b/pitfall/pitfall/test/test12.rkt @@ -13,11 +13,12 @@ [text "Some text with an embedded font" 100 100 (hash 'width #f)])) +;; test against non-subsetted font version (define-runtime-path this "test12rkt.pdf") -(make-doc this #f proc #:test #f) +(make-doc this #f proc #:pdfkit #f) -#;(define-runtime-path that "test12crkt.pdf") -#;(make-doc that #t proc #:test #f) +(define-runtime-path that "test12crkt.pdf") +(make-doc that #t proc #:pdfkit #f) #;(module+ test (define doc (make-object PDFDocument)) diff --git a/pitfall/pitfall/test/test12crkt copy.pdf b/pitfall/pitfall/test/test12crkt copy.pdf new file mode 100644 index 00000000..e5b8cac0 Binary files /dev/null and b/pitfall/pitfall/test/test12crkt copy.pdf differ diff --git a/pitfall/pitfall/test/test12crkt.pdf b/pitfall/pitfall/test/test12crkt.pdf index f26c03b2..e5b8cac0 100644 Binary files a/pitfall/pitfall/test/test12crkt.pdf and b/pitfall/pitfall/test/test12crkt.pdf differ diff --git a/pitfall/pitfall/test/test12rkt copy.pdf b/pitfall/pitfall/test/test12rkt copy.pdf new file mode 100644 index 00000000..d43571b4 Binary files /dev/null and b/pitfall/pitfall/test/test12rkt copy.pdf differ diff --git a/pitfall/pitfall/test/test12rkt.pdf b/pitfall/pitfall/test/test12rkt.pdf index 6eaa4c82..d43571b4 100644 Binary files a/pitfall/pitfall/test/test12rkt.pdf and b/pitfall/pitfall/test/test12rkt.pdf differ