Skip to content

Commit

Permalink
By default, text now wraps to the page margins which are by default, …
Browse files Browse the repository at this point in the history
…1 inch. This will not happen if you pass X and Y coordinates for the text to appear at.
  • Loading branch information
devongovett committed Jul 22, 2011
1 parent a9bb4e0 commit aab337d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/document.coffee
Expand Up @@ -68,8 +68,8 @@ class PDFDocument
@pages.push @page

# reset x and y coordinates
@x = 0
@y = 0
@x = @page.margins.left
@y = @page.margins.top

return this

Expand Down
13 changes: 10 additions & 3 deletions lib/mixins/text.coffee
Expand Up @@ -24,10 +24,17 @@ module.exports =
if typeof x is 'object'
options = x
x = null

# Update the current position
if x? and y?
@x = x
@y = y

# Update the current position
@x = x or @x
@y = y or @y
# wrap to margins if no x or y position passed
else
margins = @page.margins
options.width = @page.width - margins.left - margins.right
options.height = @page.height - margins.top - margins.bottom

# add current font to page if necessary
@page.fonts[@_font.id] ?= @_font.ref
Expand Down
18 changes: 18 additions & 0 deletions lib/page.coffee
Expand Up @@ -8,6 +8,18 @@ class PDFPage
@size = options.size or "letter"
@layout = options.layout or "portrait"

# if margin was passed as a single number
if typeof options.margin is 'number'
@margins =
top: options.margin
left: options.margin
bottom: options.margin
right: options.margin

# default to 1 inch margins
else
@margins = options.margins or DEFAULT_MARGINS

dimensions = if Array.isArray(@size) then @size else SIZES[@size.toUpperCase()]
@width = dimensions[if @layout is 'portrait' then 0 else 1]
@height = dimensions[if @layout is 'portrait' then 1 else 0]
Expand Down Expand Up @@ -43,6 +55,12 @@ class PDFPage
finalize: ->
@content.finalize(@document.compress)

DEFAULT_MARGINS =
top: 72
left: 72
bottom: 72
right: 72

SIZES =
'4A0': [4767.87, 6740.79]
'2A0': [3370.39, 4767.87]
Expand Down

0 comments on commit aab337d

Please sign in to comment.