Skip to content

Commit

Permalink
Merge pull request #13 from devongovett/master
Browse files Browse the repository at this point in the history
Update fork
  • Loading branch information
alafr authored Nov 7, 2016
2 parents a944efd + f9ab215 commit 8b4f153
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 30 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ doc.font('fonts/PalatinoBold.ttf')
.fontSize(25)
.text('Some text with an embedded font!', 100, 100)

# Add an image, constrain it to a given size, and center it vertically and horizontally
doc.image('path/to/image.png', {
fit: [250, 300],
align: 'center',
valign: 'center'
});

# Add another page
doc.addPage()
.fontSize(25)
Expand Down
2 changes: 1 addition & 1 deletion docs/getting_started.coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ You can also set some options for the page, such as it's size and orientation.
The `layout` property can be either `portrait` (the default) or `landscape`.
The `size` property can be either an array specifying `[width, height]` in PDF
points (72 per inch), or a string specifying a predefined size. A
list of the predefined paper sizes can be seen [here](http://pdfkit.org/docs/paper_sizes.html). The
list of the predefined paper sizes can be seen [here](https://github.com/devongovett/pdfkit/blob/b13423bf0a391ed1c33a2e277bc06c00cabd6bf9/lib/page.coffee#L72-L122). The
default is `letter`.

Passing a page options object to the `PDFDocument` constructor will
Expand Down
6 changes: 6 additions & 0 deletions docs/images.coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ Here is an example showing some of these options.
# Scale the image
doc.image('images/test.jpeg', 320, 280, scale: 0.25)
.text('Scale', 320, 265)
# Fit the image in the dimensions, and center it both horizontally and vertically
doc.image('images/test.jpeg', 430, 15, fit: [100, 100], align: 'center', valign: 'center')
.rect(430, 15, 100, 100)
.stroke()
.text('Centered', 430, 0)
* * *

Expand Down
12 changes: 5 additions & 7 deletions lib/line_wrapper.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ class LineWrapper extends EventEmitter

buffer = buffer + @ellipsis

if bk.required and w > @spaceLeft
buffer = word
textWidth = w
wc = 1

emitLine()

# if we've reached the edge of the page,
Expand All @@ -170,13 +175,6 @@ class LineWrapper extends EventEmitter

# reset the space left and buffer
if bk.required
# if there's a word but no space left, emit single line
if w > @spaceLeft
buffer = word
textWidth = w
wc = 1
emitLine()

@spaceLeft = @lineWidth
buffer = ''
textWidth = 0
Expand Down
54 changes: 39 additions & 15 deletions lib/mixins/images.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@ module.exports =
if typeof x is 'object'
options = x
x = null

x = x ? options.x ? @x
y = y ? options.y ? @y
unless Buffer.isBuffer(src)

if typeof src is 'string'
image = @_imageRegistry[src]

if not image
image = PDFImage.open src, 'I' + (++@_imageCount)
if src.width and src.height
image = src
else
image = @openImage src

unless image.obj
image.embed this
unless Buffer.isBuffer(src)
@_imageRegistry[src] = image


@page.xobjects[image.label] ?= image.obj

w = options.width or image.width
h = options.height or image.height

if options.width and not options.height
wp = w / image.width
w = image.width * wp
Expand All @@ -51,25 +54,46 @@ module.exports =
else
h = bh
w = bh * ip


else if options.cover
[bw, bh] = options.cover
bp = bw / bh
ip = image.width / image.height
if ip > bp
h = bh
w = bh * ip
else
w = bw
h = bw / ip

if options.fit or options.cover
if options.align is 'center'
x = x + bw / 2 - w / 2

else if options.align is 'right'
x = x + bw - w

if options.valign is 'center'
y = y + bh / 2 - h / 2

else if options.valign is 'bottom'
y = y + bh - h

# Set the current y position to below the image if it is in the document flow
@y += h if @y is y

@save()
@transform w, 0, 0, -h, x, y + h
@addContent "/#{image.label} Do"
@restore()

return this

openImage: (src) ->
if typeof src is 'string'
image = @_imageRegistry[src]

if not image
image = PDFImage.open src, 'I' + (++@_imageCount)
if typeof src is 'string'
@_imageRegistry[src] = image

return image
5 changes: 3 additions & 2 deletions lib/mixins/text.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ module.exports =
list: (list, x, y, options, wrapper) ->
options = @_initOptions(x, y, options)

r = Math.round (@_font.ascender / 1000 * @_fontSize) / 3
midLine = Math.round (@_font.ascender / 1000 * @_fontSize) / 2
r = options.bulletRadius or Math.round (@_font.ascender / 1000 * @_fontSize) / 3
indent = options.textIndent or r * 5
itemIndent = options.bulletIndent or r * 8

Expand Down Expand Up @@ -102,7 +103,7 @@ module.exports =
wrapper.lineWidth -= diff
level = l

@circle @x - indent + r, @y + r + (r / 2), r
@circle @x - indent + r, @y + midLine, r
@fill()

wrapper.on 'sectionStart', =>
Expand Down
13 changes: 8 additions & 5 deletions lib/mixins/vector.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ module.exports =

dash: (length, options = {}) ->
return this unless length?

space = options.space ? length
phase = options.phase or 0

@addContent "[#{length} #{space}] #{phase} d"
if Array.isArray length
length = length.join ' '
phase = options.phase or 0
@addContent "[#{length}] #{phase} d"
else
space = options.space ? length
phase = options.phase or 0
@addContent "[#{length} #{space}] #{phase} d"

undash: ->
@addContent "[] 0 d"
Expand Down

0 comments on commit 8b4f153

Please sign in to comment.