diff --git a/pitfall/pdfkit/lib/font.coffee b/pitfall/pdfkit/lib/font.coffee index c8c58c3a..bf2b6203 100644 --- a/pitfall/pdfkit/lib/font.coffee +++ b/pitfall/pdfkit/lib/font.coffee @@ -6,6 +6,7 @@ class PDFFont if StandardFont.isStandardFont src return new StandardFont document, src, id + console.log("openSync on " + src) font = fontkit.openSync src, family else if Buffer.isBuffer(src) diff --git a/pitfall/pdfkit/lib/font.js b/pitfall/pdfkit/lib/font.js deleted file mode 100644 index ac48b465..00000000 --- a/pitfall/pdfkit/lib/font.js +++ /dev/null @@ -1,75 +0,0 @@ -// Generated by CoffeeScript 1.12.5 -(function() { - var EmbeddedFont, PDFFont, StandardFont, fontkit; - - fontkit = require('fontkit'); - - PDFFont = (function() { - PDFFont.open = function(document, src, family, id) { - var font; - if (typeof src === 'string') { - if (StandardFont.isStandardFont(src)) { - return new StandardFont(document, src, id); - } - font = fontkit.openSync(src, family); - } else if (Buffer.isBuffer(src)) { - font = fontkit.create(src, family); - } else if (src instanceof Uint8Array) { - font = fontkit.create(new Buffer(src), family); - } else if (src instanceof ArrayBuffer) { - font = fontkit.create(new Buffer(new Uint8Array(src)), family); - } - if (font == null) { - throw new Error('Not a supported font format or standard PDF font.'); - } - return new EmbeddedFont(document, font, id); - }; - - function PDFFont() { - throw new Error('Cannot construct a PDFFont directly.'); - } - - PDFFont.prototype.encode = function(text) { - throw new Error('Must be implemented by subclasses'); - }; - - PDFFont.prototype.widthOfString = function(text) { - throw new Error('Must be implemented by subclasses'); - }; - - PDFFont.prototype.ref = function() { - return this.dictionary != null ? this.dictionary : this.dictionary = this.document.ref(); - }; - - PDFFont.prototype.finalize = function() { - if (this.embedded || (this.dictionary == null)) { - return; - } - this.embed(); - return this.embedded = true; - }; - - PDFFont.prototype.embed = function() { - throw new Error('Must be implemented by subclasses'); - }; - - PDFFont.prototype.lineHeight = function(size, includeGap) { - var gap; - if (includeGap == null) { - includeGap = false; - } - gap = includeGap ? this.lineGap : 0; - return (this.ascender + gap - this.descender) / 1000 * size; - }; - - return PDFFont; - - })(); - - module.exports = PDFFont; - - StandardFont = require('./font/standard'); - - EmbeddedFont = require('./font/embedded'); - -}).call(this); diff --git a/pitfall/pdfkit/lib/mixins/fonts.coffee b/pitfall/pdfkit/lib/mixins/fonts.coffee index 6d86d821..4c1a4a0c 100644 --- a/pitfall/pdfkit/lib/mixins/fonts.coffee +++ b/pitfall/pdfkit/lib/mixins/fonts.coffee @@ -37,6 +37,8 @@ module.exports = # load the font id = 'F' + (++@_fontCount) + #console.log("src=" + src) + #console.log("family=" + family) @_font = PDFFont.open(this, src, family, id) # check for existing font familes with the same name already in the PDF @@ -59,6 +61,7 @@ module.exports = @_font.lineHeight @_fontSize, includeGap registerFont: (name, src, family) -> + #console.log(" " + name + " " + src + " " + family) @_registeredFonts[name] = src: src family: family diff --git a/pitfall/pdfkit/lib/mixins/fonts.js b/pitfall/pdfkit/lib/mixins/fonts.js deleted file mode 100644 index 6086e834..00000000 --- a/pitfall/pdfkit/lib/mixins/fonts.js +++ /dev/null @@ -1,69 +0,0 @@ -// Generated by CoffeeScript 1.12.5 -(function() { - var PDFFont; - - PDFFont = require('../font'); - - module.exports = { - initFonts: function() { - this._fontFamilies = {}; - this._fontCount = 0; - this._fontSize = 12; - this._font = null; - this._registeredFonts = {}; - return this.font('Helvetica'); - }, - font: function(src, family, size) { - var cacheKey, font, id, ref; - if (typeof family === 'number') { - size = family; - family = null; - } - if (typeof src === 'string' && this._registeredFonts[src]) { - cacheKey = src; - ref = this._registeredFonts[src], src = ref.src, family = ref.family; - } else { - cacheKey = family || src; - if (typeof cacheKey !== 'string') { - cacheKey = null; - } - } - if (size != null) { - this.fontSize(size); - } - if (font = this._fontFamilies[cacheKey]) { - this._font = font; - return this; - } - id = 'F' + (++this._fontCount); - this._font = PDFFont.open(this, src, family, id); - if (font = this._fontFamilies[this._font.name]) { - this._font = font; - return this; - } - if (cacheKey) { - this._fontFamilies[cacheKey] = this._font; - } - this._fontFamilies[this._font.name] = this._font; - return this; - }, - fontSize: function(_fontSize) { - this._fontSize = _fontSize; - return this; - }, - currentLineHeight: function(includeGap) { - if (includeGap == null) { - includeGap = false; - } - return this._font.lineHeight(this._fontSize, includeGap); - }, - registerFont: function(name, src, family) { - this._registeredFonts[name] = { - src: src, - family: family - }; - return this; - } - }; - -}).call(this); diff --git a/pitfall/pdfkit/node_modules/fontkit/index.js b/pitfall/pdfkit/node_modules/fontkit/index.js index bfacec00..e0ba656a 100644 --- a/pitfall/pdfkit/node_modules/fontkit/index.js +++ b/pitfall/pdfkit/node_modules/fontkit/index.js @@ -41,6 +41,7 @@ fontkit.registerFormat = function (format) { fontkit.openSync = function (filename, postscriptName) { var buffer = fs.readFileSync(filename); + console.log(filename); return fontkit.create(buffer, postscriptName); }; diff --git a/pitfall/pdfkit/node_modules/fontkit/src/base.js b/pitfall/pdfkit/node_modules/fontkit/src/base.js index e224180d..3403ddb3 100644 --- a/pitfall/pdfkit/node_modules/fontkit/src/base.js +++ b/pitfall/pdfkit/node_modules/fontkit/src/base.js @@ -13,6 +13,7 @@ fontkit.registerFormat = function(format) { fontkit.openSync = function(filename, postscriptName) { let buffer = fs.readFileSync(filename); + console.log("wham"); return fontkit.create(buffer, postscriptName); }; diff --git a/pitfall/pitfall/font.rkt b/pitfall/pitfall/font.rkt index a2e89e56..12b13393 100644 --- a/pitfall/pitfall/font.rkt +++ b/pitfall/pitfall/font.rkt @@ -1,5 +1,5 @@ #lang pitfall/racket -(require "standard-fonts.rkt" "afm.rkt" "reference.rkt") +(require "standard-fonts.rkt" "afm.rkt" "reference.rkt" "fontkit.rkt") (provide PDFFont PDFFont-open) (define PDFFont @@ -14,13 +14,22 @@ lineHeight) )) +(define-subclass PDFFont (EmbeddedFont document name id) + (super-new) + 'boing) + (define/contract (PDFFont-open document src family id) (object? any/c any/c any/c . -> . (is-a?/c PDFFont)) (cond - [(and (string? src) (isStandardFont src) - (make-object StandardFont document src id))] - ;; todo: other font-loading cases - [else (raise-argument-error 'PDFFont-open "loadable font name" src)])) + [(and (string? src) (isStandardFont src)) + (make-object StandardFont document src id)] + [else + (define font + (cond + [(string? src) (openSync src family)] + ;; todo: other font-loading cases + [else (raise-argument-error 'PDFFont-open "loadable font thingy" src)])) + (make-object EmbeddedFont document font id)])) (define/contract (ref this) @@ -41,20 +50,18 @@ (* (/ (+ (· this ascender) gap (- (· this descender))) 1000.0) size)) -(define StandardFont - (class PDFFont - (super-new) - (init-field document name id) - (field [font (make-object AFMFont ((hash-ref standard-fonts name - (λ () (raise-argument-error 'PDFFont "valid font name" name)))))] - [ascender (· font ascender)] - [descender (· font descender)] - [bbox (· font bbox)] - [lineGap (· font lineGap)]) - (as-methods - embed - encode - widthOfString))) +(define-subclass PDFFont (StandardFont document name id) + (super-new) + (field [font (make-object AFMFont ((hash-ref standard-fonts name + (λ () (raise-argument-error 'PDFFont "valid font name" name)))))] + [ascender (· font ascender)] + [descender (· font descender)] + [bbox (· font bbox)] + [lineGap (· font lineGap)]) + (as-methods + embed + encode + widthOfString)) (define/contract (embed this) diff --git a/pitfall/pitfall/fontkit.rkt b/pitfall/pitfall/fontkit.rkt new file mode 100644 index 00000000..db6e3243 --- /dev/null +++ b/pitfall/pitfall/fontkit.rkt @@ -0,0 +1,47 @@ +#lang pitfall/racket +(provide (all-defined-out)) + +(define-subclass object% (TTFFont buffer) + (super-new) + + (define (buffer->font buffer) + 'made-ttf-font) + + (define (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))) + + +;; Register font formats +(define formats (list TTFFont)) +;;fontkit.registerFormat(WOFFFont); ;; todo +;;fontkit.registerFormat(WOFF2Font); ;; todo +;;fontkit.registerFormat(TrueTypeCollection); ;; todo +;;fontkit.registerFormat(DFont); ;; todo + +(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 buffer))] + #:when font) + (if postscriptName + (send font getFont postscriptName) + font)) + (error 'create "unknown font format"))) + + +(define/contract (openSync filename [postscriptName #f]) + ((string?) ((or/c string? #f)) . ->* . any/c) + (define buffer (file->bytes filename)) + (create buffer postscriptName)) + + +(module+ test + (require racket/runtime-path) + (define-runtime-path charter-path "test/assets/charter.ttf") + (openSync (path->string charter-path))) \ No newline at end of file diff --git a/pitfall/pitfall/fonts.rkt b/pitfall/pitfall/fonts.rkt index d4d93724..cf257b30 100644 --- a/pitfall/pitfall/fonts.rkt +++ b/pitfall/pitfall/fonts.rkt @@ -18,7 +18,8 @@ initFonts font fontSize - currentLineHeight))) + currentLineHeight + registerFont))) (define/contract (initFonts this) @@ -46,10 +47,10 @@ ;; check registered fonts if src is a string (define cacheKey (let ([this-rfs (· this _registeredFonts)]) (cond - [(and (string? src) (hash-ref this-rfs src #f)) + [(and (string? src) (hash-has-key? this-rfs src)) (define ck src) - (set! src (hash-ref (hash-ref this-rfs src) src #f)) - (set! family (hash-ref (hash-ref this-rfs src) family #f)) + (set! src (· (hash-ref this-rfs ck) src)) + (set! family (· (hash-ref this-rfs ck) family)) ck] [else (let ([ck (or family src)]) (and (string? ck) ck))]))) @@ -87,7 +88,14 @@ (define/contract (currentLineHeight this [includeGap #f]) (() (boolean?) . ->*m . number?) - (send (· this _font) lineHeight (· this _fontSize) includeGap)) + (send (· this _font) lineHeight (· this _fontSize) includeGap)) + + +(define/contract (registerFont this name src [family #f]) + ((string? path-string?) ((or/c string? #f)) . ->*m . object?) + (hash-set! (· this _registeredFonts) name + (mhash 'src src 'family family)) + this) (module+ test (define fo (new (fonts-mixin)))) diff --git a/pitfall/pitfall/test/test12.coffee b/pitfall/pitfall/test/test12.coffee index e246ed8f..3d7a58d0 100644 --- a/pitfall/pitfall/test/test12.coffee +++ b/pitfall/pitfall/test/test12.coffee @@ -18,6 +18,6 @@ doc = new PDFDocument({compress: no}) doc.pipe(fs.createWriteStream('test12.pdf')) make doc -doc = new PDFDocument({compress: yes}) -doc.pipe(fs.createWriteStream('test12c.pdf')) -make doc +#doc = new PDFDocument({compress: yes}) +#doc.pipe(fs.createWriteStream('test12c.pdf')) +#make doc diff --git a/pitfall/pitfall/test/test12.pdf b/pitfall/pitfall/test/test12.pdf index e024bfc9..4a6a5187 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 c4e65852..efc26228 100644 --- a/pitfall/pitfall/test/test12.rkt +++ b/pitfall/pitfall/test/test12.rkt @@ -1,15 +1,20 @@ #lang pitfall/pdftest +(define-runtime-path charter-path "assets/charter.ttf") + (define (proc doc) + ;; Register a font name for use later + (send doc registerFont "Charter" (path->string charter-path)) + + ;; Set the font, draw some text (send* doc - [fillColor "blue"] - [font "Helvetica" 30] - [text "Here is a link!" 100 100 (hash - 'link "http://google.com/" - 'underline #t)])) + [font "Charter"] + [fontSize 25] + [text "Some text with an embedded font" 100 100 (hash + 'width #f)])) -(define-runtime-path this "test11rkt.pdf") -(make-doc this #f proc #:pdfkit #f) +(define-runtime-path this "test12rkt.pdf") +(make-doc this #f proc #:test #f) -(define-runtime-path that "test11crkt.pdf") -(make-doc that #t proc) +(define-runtime-path that "test12crkt.pdf") +(make-doc that #t proc #:test #f) diff --git a/pitfall/pitfall/test/test12c.pdf b/pitfall/pitfall/test/test12c.pdf index cb88d61b..be4b4e39 100644 Binary files a/pitfall/pitfall/test/test12c.pdf and b/pitfall/pitfall/test/test12c.pdf differ