pdfkit
Advanced tools
Comparing version 0.1.8 to 0.1.9
@@ -201,4 +201,4 @@ (function() { | ||
segCountX2 = segCount * 2; | ||
searchRange = 2 * Math.pow(2, Math.floor(Math.log(segCount) / Math.LN2)); | ||
entrySelector = Math.log(segCount) / Math.LN2; | ||
searchRange = 2 * Math.pow(Math.log(segCount) / Math.LN2, 2); | ||
entrySelector = Math.log(searchRange / 2) / Math.LN2; | ||
rangeShift = 2 * segCount - searchRange; | ||
@@ -212,3 +212,3 @@ deltas = []; | ||
if (startCode === 0xFFFF) { | ||
deltas.push(1); | ||
deltas.push(0); | ||
rangeOffsets.push(0); | ||
@@ -215,0 +215,0 @@ break; |
@@ -39,2 +39,5 @@ (function() { | ||
}, | ||
widthOfString: function(string) { | ||
return this._font.widthOfString(string, this._fontSize); | ||
}, | ||
currentLineHeight: function(includeGap) { | ||
@@ -41,0 +44,0 @@ if (includeGap == null) includeGap = false; |
(function() { | ||
var PDFImage; | ||
var __hasProp = Object.prototype.hasOwnProperty, __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (__hasProp.call(this, i) && this[i] === item) return i; } return -1; }; | ||
@@ -12,3 +13,3 @@ PDFImage = require('../image'); | ||
image: function(src, x, y, options) { | ||
var bh, bp, bw, h, hp, image, ip, label, w, wp, _ref, _ref2, _ref3, _ref4; | ||
var bh, bp, bw, h, hp, image, ip, label, pages, w, wp, _ref, _ref2, _ref3, _ref4, _ref5; | ||
if (options == null) options = {}; | ||
@@ -22,7 +23,10 @@ if (typeof x === 'object') { | ||
if (this._imageRegistry[src]) { | ||
_ref3 = this._imageRegistry[src], image = _ref3[0], this.page = _ref3[1], label = _ref3[2]; | ||
_ref3 = this._imageRegistry[src], image = _ref3[0], label = _ref3[1], pages = _ref3[2]; | ||
if (_ref4 = this.page, __indexOf.call(pages, _ref4) < 0) { | ||
pages.push(this.page); | ||
} | ||
} else { | ||
image = PDFImage.open(src); | ||
label = "I" + (++this._imageCount); | ||
this._imageRegistry[src] = [image, this.page, label]; | ||
this._imageRegistry[src] = [image, label, [this.page]]; | ||
} | ||
@@ -43,3 +47,3 @@ w = options.width || image.width; | ||
} else if (options.fit) { | ||
_ref4 = options.fit, bw = _ref4[0], bh = _ref4[1]; | ||
_ref5 = options.fit, bw = _ref5[0], bh = _ref5[1]; | ||
bp = bw / bh; | ||
@@ -77,9 +81,12 @@ ip = image.width / image.height; | ||
return (proceed = function() { | ||
var image, label, page, _ref; | ||
var image, label, pages, _ref; | ||
if (images.length) { | ||
_ref = images.shift(), image = _ref[0], page = _ref[1], label = _ref[2]; | ||
_ref = images.shift(), image = _ref[0], label = _ref[1], pages = _ref[2]; | ||
return image.object(_this, function(obj) { | ||
var _base, _ref2; | ||
if ((_ref2 = (_base = page.xobjects)[label]) == null) { | ||
_base[label] = obj; | ||
var page, _base, _i, _len, _ref2; | ||
for (_i = 0, _len = pages.length; _i < _len; _i++) { | ||
page = pages[_i]; | ||
if ((_ref2 = (_base = page.xobjects)[label]) == null) { | ||
_base[label] = obj; | ||
} | ||
} | ||
@@ -86,0 +93,0 @@ return proceed(); |
@@ -78,2 +78,3 @@ (function() { | ||
} | ||
this.lastLine = true; | ||
this.emit('lastLine', options, this); | ||
@@ -80,0 +81,0 @@ options.textWidth = width(buffer.trim(), options) + wordSpacing * (wc - 1); |
(function() { | ||
var LineWrapper, WORD_RE; | ||
var WORD_RE; | ||
WORD_RE = /([^ ,\/!.?:;\-\n]+[ ,\/!.?:;\-]*)|\n/g; | ||
LineWrapper = require('./line_wrapper'); | ||
module.exports = { | ||
@@ -13,3 +11,3 @@ initText: function() { | ||
this._lineGap = 0; | ||
return this._textState = { | ||
this._textState = { | ||
mode: 0, | ||
@@ -19,2 +17,3 @@ wordSpacing: 0, | ||
}; | ||
return this._wrapState = {}; | ||
}, | ||
@@ -25,90 +24,4 @@ lineGap: function(_lineGap) { | ||
}, | ||
moveDown: function(lines) { | ||
if (lines == null) lines = 1; | ||
this.y += this.currentLineHeight(true) * lines + this._lineGap; | ||
return this; | ||
}, | ||
moveUp: function(lines) { | ||
if (lines == null) lines = 1; | ||
this.y -= this.currentLineHeight(true) * lines + this._lineGap; | ||
return this; | ||
}, | ||
text: function(text, x, y, options) { | ||
var line, paragraphs, wrapper, _i, _len; | ||
options = this._initOptions(x, y, options); | ||
text = '' + text; | ||
if (options.wordSpacing) text = text.replace(/\s{2,}/g, ' '); | ||
paragraphs = text.split('\n'); | ||
if (options.width) { | ||
wrapper = new LineWrapper(this); | ||
wrapper.on('line', this._line.bind(this)); | ||
wrapper.wrap(paragraphs, options); | ||
} else { | ||
for (_i = 0, _len = paragraphs.length; _i < _len; _i++) { | ||
line = paragraphs[_i]; | ||
this._line(line, options); | ||
} | ||
} | ||
return this; | ||
}, | ||
list: function(list, x, y, options, wrapper) { | ||
var flatten, i, indent, itemIndent, items, level, levels, r; | ||
var _this = this; | ||
options = this._initOptions(x, y, options); | ||
r = Math.round((this._font.ascender / 1000 * this._fontSize) / 3); | ||
indent = options.textIndent || r * 5; | ||
itemIndent = options.bulletIndent || r * 8; | ||
level = 1; | ||
items = []; | ||
levels = []; | ||
flatten = function(list) { | ||
var i, item, _len, _results; | ||
_results = []; | ||
for (i = 0, _len = list.length; i < _len; i++) { | ||
item = list[i]; | ||
if (Array.isArray(item)) { | ||
level++; | ||
flatten(item); | ||
_results.push(level--); | ||
} else { | ||
items.push(item); | ||
_results.push(levels.push(level)); | ||
} | ||
} | ||
return _results; | ||
}; | ||
flatten(list); | ||
wrapper = new LineWrapper(this); | ||
wrapper.on('line', this._line.bind(this)); | ||
level = 1; | ||
i = 0; | ||
wrapper.on('firstLine', function() { | ||
var diff, l; | ||
if ((l = levels[i++]) !== level) { | ||
diff = itemIndent * (l - level); | ||
_this.x += diff; | ||
wrapper.lineWidth -= diff; | ||
level = l; | ||
} | ||
_this.circle(_this.x - indent + r, _this.y + r + (r / 2), r); | ||
return _this.fill(); | ||
}); | ||
wrapper.on('sectionStart', function() { | ||
var pos; | ||
pos = indent + itemIndent * (level - 1); | ||
_this.x += pos; | ||
return wrapper.lineWidth -= pos; | ||
}); | ||
wrapper.on('sectionEnd', function() { | ||
var pos; | ||
pos = indent + itemIndent * (level - 1); | ||
_this.x -= pos; | ||
return wrapper.lineWidth += pos; | ||
}); | ||
wrapper.wrap(items, options); | ||
this.x -= indent; | ||
return this; | ||
}, | ||
_initOptions: function(x, y, options) { | ||
var margins, _ref, _ref2, _ref3; | ||
var margins, match, matches, _i, _len, _ref, _ref2, _ref3; | ||
if (x == null) x = {}; | ||
@@ -120,11 +33,3 @@ if (options == null) options = {}; | ||
} | ||
options = (function() { | ||
var k, opts, v; | ||
opts = {}; | ||
for (k in options) { | ||
v = options[k]; | ||
opts[k] = v; | ||
} | ||
return opts; | ||
})(); | ||
text = '' + text; | ||
if ((x != null) || (y != null)) { | ||
@@ -142,40 +47,78 @@ this.x = x || this.x; | ||
} | ||
options.columns || (options.columns = 0); | ||
options.columns || (options.columns = 1); | ||
if ((_ref3 = options.columnGap) == null) options.columnGap = 18; | ||
return options; | ||
if (options.wordSpacing) text = text.replace(/\s+/g, ' '); | ||
if (options.width) { | ||
this._wrap(text, options); | ||
} else if ((matches = text.split('\n')).length > 1) { | ||
for (_i = 0, _len = matches.length; _i < _len; _i++) { | ||
match = matches[_i]; | ||
this._line(match, options); | ||
} | ||
} else { | ||
this._line(text, options); | ||
} | ||
return this; | ||
}, | ||
widthOfString: function(string, options) { | ||
if (options == null) options = {}; | ||
return this._font.widthOfString(string, this._fontSize) + (options.characterSpacing || 0) * (string.length - 1); | ||
moveDown: function(lines) { | ||
if (lines == null) lines = 1; | ||
this.y += this.currentLineHeight(true) * lines + this._lineGap; | ||
return this; | ||
}, | ||
moveUp: function(lines) { | ||
if (lines == null) lines = 1; | ||
this.y -= this.currentLineHeight(true) * lines + this._lineGap; | ||
return this; | ||
}, | ||
list: function(array, ox, oy) { | ||
var gap, item, x, y, _i, _len; | ||
gap = Math.round((this._font.ascender / 1000 * this._fontSize) / 2); | ||
this.x = x = ox || this.x; | ||
this.y = y = oy || this.y; | ||
for (_i = 0, _len = array.length; _i < _len; _i++) { | ||
item = array[_i]; | ||
this.circle(x + 3, this.y + gap + 3, 3); | ||
this.text(item, x + 15); | ||
this.y += 3; | ||
} | ||
this.x = x; | ||
return this.fill(); | ||
}, | ||
_line: function(text, options) { | ||
var lineGap; | ||
this._fragment(text, this.x, this.y, options); | ||
var lineGap, paragraphGap, wrap; | ||
wrap = this._wrapState; | ||
paragraphGap = (wrap.firstLine && this.y !== wrap.startY && options.paragraphGap) || 0; | ||
lineGap = options.lineGap || this._lineGap || 0; | ||
return this.y += this.currentLineHeight(true) + lineGap; | ||
this._fragment(text, this.x, this.y + paragraphGap, options); | ||
return this.y += this.currentLineHeight(true) + lineGap + paragraphGap; | ||
}, | ||
_fragment: function(text, x, y, options) { | ||
var align, characterSpacing, i, mode, spaceWidth, state, textWidth, wordSpacing, words, _base, _name, _ref; | ||
var align, characterSpacing, i, indent, lineWidth, mode, state, textWidth, wordSpacing, words, wrap, _base, _name, _ref; | ||
if (options == null) options = {}; | ||
text = '' + text; | ||
if (text.length === 0) return; | ||
state = this._textState; | ||
wrap = this._wrapState; | ||
align = options.align || 'left'; | ||
indent = (wrap.firstLine && options.indent) || 0; | ||
wordSpacing = options.wordSpacing || 0; | ||
characterSpacing = options.characterSpacing || 0; | ||
if (options.width) { | ||
lineWidth = wrap.lineWidth - indent - wrap.extraSpace; | ||
switch (align) { | ||
case 'right': | ||
x += options.lineWidth - options.textWidth; | ||
x += lineWidth - this.widthOfString(text); | ||
break; | ||
case 'center': | ||
x += options.lineWidth / 2 - options.textWidth / 2; | ||
x += lineWidth / 2 - this.widthOfString(text) / 2; | ||
break; | ||
case 'justify': | ||
words = text.match(WORD_RE) || [text]; | ||
if (wrap.lastLine) break; | ||
words = text.match(WORD_RE); | ||
if (!words) break; | ||
textWidth = this.widthOfString(text.replace(/\s+/g, ''), options); | ||
spaceWidth = this.widthOfString(' ') + characterSpacing; | ||
wordSpacing = (options.lineWidth - textWidth) / (words.length - 1) - spaceWidth; | ||
textWidth = this.widthOfString(text.replace(/\s+/g, '')); | ||
wordSpacing = (lineWidth - textWidth) / (words.length - 1) - this.widthOfString(' '); | ||
} | ||
} | ||
x += indent; | ||
y = this.page.height - y - (this._font.ascender / 1000 * this._fontSize); | ||
@@ -208,2 +151,63 @@ if ((_ref = (_base = this.page.fonts)[_name = this._font.id]) == null) { | ||
return state.wordSpacing = wordSpacing; | ||
}, | ||
_wrap: function(text, options) { | ||
var buffer, i, indent, lastLine, len, lineWidth, nextY, spaceLeft, w, width, word, wordWidths, words, wrap, _len, _ref; | ||
wrap = this._wrapState; | ||
width = this.widthOfString.bind(this); | ||
indent = options.indent || 0; | ||
lineWidth = (options.width - (options.columnGap * (options.columns - 1))) / options.columns; | ||
wrap.column = 1; | ||
wrap.startY = this.y; | ||
wrap.lineWidth = lineWidth; | ||
wrap.firstLine = true; | ||
wrap.lastLine = false; | ||
wrap.maxY = this.y + options.height - this.currentLineHeight(); | ||
words = text.match(WORD_RE); | ||
wrap.extraSpace = (options.wordSpacing || 0) * (words.length - 1) + (options.characterSpacing || 0) * (text.length - 1); | ||
spaceLeft = lineWidth - indent - wrap.extraSpace; | ||
wordWidths = {}; | ||
len = words.length; | ||
buffer = ''; | ||
for (i = 0, _len = words.length; i < _len; i++) { | ||
word = words[i]; | ||
w = (_ref = wordWidths[word]) != null ? _ref : wordWidths[word] = width(word); | ||
if (w > spaceLeft || word === '\n') { | ||
if (wrap.lastLine) { | ||
wrap.firstLine = true; | ||
wrap.lastLine = false; | ||
} | ||
if (word === '\n') { | ||
wrap.lastLine = true; | ||
w += indent; | ||
} | ||
lastLine = buffer.trim(); | ||
this._line(lastLine, options); | ||
wrap.firstLine = false; | ||
nextY = this.y + this.currentLineHeight(true); | ||
if (this.y > wrap.maxY || (wrap.lastLine && nextY > wrap.maxY)) { | ||
this._nextSection(options); | ||
} | ||
spaceLeft = lineWidth - w - wrap.extraSpace; | ||
buffer = word === '\n' ? '' : word; | ||
} else { | ||
spaceLeft -= w; | ||
buffer += word; | ||
} | ||
} | ||
wrap.lastLine = true; | ||
this._line(buffer.trim(), options); | ||
return this._wrapState = {}; | ||
}, | ||
_nextSection: function(options) { | ||
var wrap; | ||
wrap = this._wrapState; | ||
if (++wrap.column > options.columns) { | ||
this.addPage(); | ||
wrap.column = 1; | ||
wrap.startY = this.page.margins.top; | ||
return wrap.maxY = this.page.maxY(); | ||
} else { | ||
this.x += wrap.lineWidth + options.columnGap; | ||
return this.y = wrap.startY; | ||
} | ||
} | ||
@@ -210,0 +214,0 @@ }; |
@@ -5,3 +5,3 @@ (function() { | ||
SVGPath = (function() { | ||
var apply, arcToSegments, cx, cy, parameters, parse, px, py, runners, segmentToBezier, solveArc; | ||
var apply, arcToSegments, cx, cy, parameters, parse, px, py, runners, segmentToBezier, solveArc, sx, sy; | ||
@@ -89,7 +89,7 @@ function SVGPath() {} | ||
cx = cy = px = py = 0; | ||
cx = cy = px = py = sx = sy = 0; | ||
apply = function(commands, doc) { | ||
var c, i, _len, _name; | ||
cx = cy = px = py = 0; | ||
cx = cy = px = py = sx = sy = 0; | ||
for (i = 0, _len = commands.length; i < _len; i++) { | ||
@@ -109,2 +109,4 @@ c = commands[i]; | ||
px = py = null; | ||
sx = cx; | ||
sy = cy; | ||
return doc.moveTo(cx, cy); | ||
@@ -116,2 +118,4 @@ }, | ||
px = py = null; | ||
sx = cx; | ||
sy = cy; | ||
return doc.moveTo(cx, cy); | ||
@@ -236,6 +240,10 @@ }, | ||
Z: function(doc) { | ||
return doc.closePath(); | ||
doc.closePath(); | ||
cx = sx; | ||
return cy = sy; | ||
}, | ||
z: function(doc) { | ||
return doc.closePath(); | ||
doc.closePath(); | ||
cx = sx; | ||
return cy = sy; | ||
} | ||
@@ -251,3 +259,3 @@ }; | ||
seg = segs[_i]; | ||
bez = segmentToBezier.apply(null, segs); | ||
bez = segmentToBezier.apply(null, seg); | ||
_results.push(doc.bezierCurveTo.apply(doc, bez)); | ||
@@ -254,0 +262,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"keywords": ["pdf", "pdf writer", "pdf generator", "graphics", "document", "vector"], | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"homepage": "http://pdfkit.org/", | ||
@@ -8,0 +8,0 @@ "author": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1531525
109
4399