Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions drawBot/context/baseContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,7 @@ def __init__(self):

# overwrite by a subclass

def _newPage(self, width, height):
def _newPage(self, width, height, pageOptions):
pass

def _save(self):
Expand Down Expand Up @@ -2166,13 +2166,13 @@ def size(self, width=None, height=None):
if height is not None:
self.height = height

def newPage(self, width=None, height=None):
def newPage(self, width=None, height=None, options=None):
if self.width is None and width is None:
raise DrawBotError("A page must have a width")
if self.height is None and height is None:
raise DrawBotError("A page must have a height")
self.hasPage = True
self._newPage(width, height)
self._newPage(width, height, options)

def saveImage(self, path, options):
if not self.hasPage:
Expand Down
4 changes: 2 additions & 2 deletions drawBot/context/gifContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def _frameDuration(self, seconds):
# gifsicle -h: Set frame delay to TIME (in 1/100sec).
self._delayData[-1] = int(seconds * 100)

def _newPage(self, width, height):
super(GIFContext, self)._newPage(width, height)
def _newPage(self, width, height, pageOptions):
super(GIFContext, self)._newPage(width, height, pageOptions)
self._delayData.append(self._delay)

def _writeDataToFile(self, data, path, options):
Expand Down
2 changes: 1 addition & 1 deletion drawBot/context/mp4Context.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self):
def _frameDuration(self, frameDuration):
self._frameDurations[-1] = frameDuration

def _newPage(self, width, height):
def _newPage(self, width, height, pageOptions):
super(MP4Context, self)._newPage(width, height)
self._frameDurations.append(self._defaultFrameDuration)
# https://github.com/typemytype/drawbot/issues/391
Expand Down
27 changes: 21 additions & 6 deletions drawBot/context/pdfContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,41 @@ def __init__(self):
self._hasContext = False
self._cachedImages = {}

def _newPage(self, width, height):
def _newPage(self, width, height, options):
self.size(width, height)
mediaBox = Quartz.CGRectMake(0, 0, self.width, self.height)

auxiliaryInfo = {
Quartz.kCGPDFContextAuthor: "DrawBot"
}
# auxiliaryInfo[Quartz.kCGPDFContextMediaBox] = AppKit.NSValue.valueWithRect_(mediaBox)
if "bleed" in options:
leftBleed, topBleed, rightBleed, bottomBleed = options["bleed"]

mediaBox = Quartz.CGRectMake(0, 0, self.width + leftBleed + rightBleed, self.height + topBleed + bottomBleed)
bleedBox = Quartz.CGRectMake(leftBleed, topBleed, self.width, self.height)

auxiliaryInfo[Quartz.kCGPDFContextBleedBox] = AppKit.NSValue.valueWithRect_(bleedBox)
auxiliaryInfo[Quartz.kCGPDFContextTrimBox] = AppKit.NSValue.valueWithRect_(bleedBox)

auxiliaryInfo[Quartz.kCGPDFContextMediaBox] = AppKit.NSValue.valueWithRect_(mediaBox)

# reset the context
self.reset()
if self._hasContext:
# add a new page
Quartz.CGContextEndPage(self._pdfContext)
Quartz.CGContextBeginPage(self._pdfContext, mediaBox)
Quartz.CGPDFContextEndPage(self._pdfContext)
Quartz.CGPDFContextBeginPage(self._pdfContext, auxiliaryInfo)
else:
# create a new pdf document
self._pdfData = Quartz.CFDataCreateMutable(None, 0)
dataConsumer = Quartz.CGDataConsumerCreateWithCFData(self._pdfData)
self._pdfContext = Quartz.CGPDFContextCreate(dataConsumer, mediaBox, None)
Quartz.CGContextBeginPage(self._pdfContext, mediaBox)
self._pdfContext = Quartz.CGPDFContextCreate(dataConsumer, mediaBox, auxiliaryInfo)
Quartz.CGPDFContextBeginPage(self._pdfContext, auxiliaryInfo)
self._hasContext = True

def _closeContext(self):
Quartz.CGContextEndPage(self._pdfContext)
Quartz.CGPDFContextEndPage(self._pdfContext)
Quartz.CGPDFContextClose(self._pdfContext)
self._hasContext = False

Expand Down
4 changes: 2 additions & 2 deletions drawBot/context/printContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class PrintContext(BaseContext):
fileExtensions = ["*"]
validateSaveImageOptions = False

def _newPage(self, width, height):
print("newPage %s %s" % (width, height))
def _newPage(self, width, height, pageOptions):
print("newPage %s %s %s" % (width, height, pageOptions))

def _save(self):
print("save")
Expand Down
2 changes: 1 addition & 1 deletion drawBot/context/svgContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def _reset(self, other=None):
self._embeddedFonts = set()
self._embeddedImages = dict()

def _newPage(self, width, height):
def _newPage(self, width, height, pageOptions):
if hasattr(self, "_svgContext"):
self._svgContext.endtag("svg")
self.reset()
Expand Down
13 changes: 10 additions & 3 deletions drawBot/drawBotDrawingTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def _addInstruction(self, callback, *args, **kwargs):
self._instructionsStack.append([])
if self._requiresNewFirstPage and not self._hasPage:
self._hasPage = True
self._instructionsStack[-1].insert(0, ("newPage", [self.width(), self.height()], {}))
self._instructionsStack[-1].insert(0, ("newPage", [self.width(), self.height(), {}], {}))
self._instructionsStack[-1].append((callback, args, kwargs))

def _drawInContext(self, context):
Expand Down Expand Up @@ -260,7 +260,7 @@ def size(self, width, height=None):
else:
raise DrawBotError("Can't use 'size()' after drawing has begun. Try to move it to the top of your script.")

def newPage(self, width=None, height=None):
def newPage(self, width=None, height=None, bleed=None):
"""
Create a new canvas to draw in.
This will act like a page in a pdf or a frame in a mov.
Expand Down Expand Up @@ -293,11 +293,18 @@ def newPage(self, width=None, height=None):
if width is None and height is None:
width = self.width()
height = self.height()
pageOptions = {}
if bleed is not None:
if isinstance(bleed, (int, float)):
bleed = (bleed, bleed, bleed, bleed)
if len(bleed) != 4:
raise DrawBotError("Bleed must be a tuple of 4 values.")
pageOptions["bleed"] = bleed
self._width = width
self._height = height
self._hasPage = True
self._dummyContext = DummyContext()
self._addInstruction("newPage", width, height)
self._addInstruction("newPage", width, height, pageOptions)

def pages(self):
"""
Expand Down