working on test12
parent
7189dae52e
commit
976d257930
@ -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);
|
@ -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);
|
@ -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)))
|
Binary file not shown.
@ -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)
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue