From ca32b72cc79c0557a36fb49ae821927461180f19 Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Mon, 30 Sep 2019 13:42:28 +0200 Subject: [PATCH 1/4] initial bleed support --- drawBot/context/baseContext.py | 6 +++--- drawBot/context/pdfContext.py | 8 +++++--- drawBot/drawBotDrawingTools.py | 9 +++++++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/drawBot/context/baseContext.py b/drawBot/context/baseContext.py index 7b708115..8b7f964c 100644 --- a/drawBot/context/baseContext.py +++ b/drawBot/context/baseContext.py @@ -1944,7 +1944,7 @@ def __init__(self): # overwrite by a subclass - def _newPage(self, width, height): + def _newPage(self, width, height, bleed): pass def _save(self): @@ -2003,13 +2003,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, bleed=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, bleed) def saveImage(self, path, options): if not self.hasPage: diff --git a/drawBot/context/pdfContext.py b/drawBot/context/pdfContext.py index cd4d5bd1..14d43818 100644 --- a/drawBot/context/pdfContext.py +++ b/drawBot/context/pdfContext.py @@ -35,10 +35,12 @@ def __init__(self): self._hasContext = False self._cachedImages = {} - def _newPage(self, width, height): + def _newPage(self, width, height, bleed): self.size(width, height) mediaBox = Quartz.CGRectMake(0, 0, self.width, self.height) - + auxiliaryInfo = dict() + if bleed: + auxiliaryInfo[Quartz.kCGPDFContextBleedBox] = Quartz.CGRectMake(-bleed[0], -bleed[1], width + bleed[2], height + bleed[3]) if self._hasContext: # reset the context self.reset() @@ -49,7 +51,7 @@ def _newPage(self, width, height): # create a new pdf document self._pdfData = Quartz.CFDataCreateMutable(None, 0) dataConsumer = Quartz.CGDataConsumerCreateWithCFData(self._pdfData) - self._pdfContext = Quartz.CGPDFContextCreate(dataConsumer, mediaBox, None) + self._pdfContext = Quartz.CGPDFContextCreate(dataConsumer, mediaBox, auxiliaryInfo) Quartz.CGContextBeginPage(self._pdfContext, mediaBox) self._hasContext = True diff --git a/drawBot/drawBotDrawingTools.py b/drawBot/drawBotDrawingTools.py index c59aafea..0086c932 100644 --- a/drawBot/drawBotDrawingTools.py +++ b/drawBot/drawBotDrawingTools.py @@ -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. @@ -293,11 +293,16 @@ def newPage(self, width=None, height=None): if width is None and height is None: width = self.width() height = self.height() + 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.") self._width = width self._height = height self._hasPage = True self._dummyContext = DummyContext() - self._addInstruction("newPage", width, height) + self._addInstruction("newPage", width, height, bleed) def pages(self): """ From cea2956f50e25fa51f8eedafb932f3cb77f85a7f Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Mon, 30 Sep 2019 16:19:22 +0200 Subject: [PATCH 2/4] set info for each page --- drawBot/context/pdfContext.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drawBot/context/pdfContext.py b/drawBot/context/pdfContext.py index 14d43818..849fe9fe 100644 --- a/drawBot/context/pdfContext.py +++ b/drawBot/context/pdfContext.py @@ -39,20 +39,21 @@ def _newPage(self, width, height, bleed): self.size(width, height) mediaBox = Quartz.CGRectMake(0, 0, self.width, self.height) auxiliaryInfo = dict() + auxiliaryInfo[Quartz.kCGPDFContextMediaBox] = mediaBox if bleed: auxiliaryInfo[Quartz.kCGPDFContextBleedBox] = Quartz.CGRectMake(-bleed[0], -bleed[1], width + bleed[2], height + bleed[3]) if self._hasContext: # reset the context self.reset() # 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, auxiliaryInfo) - Quartz.CGContextBeginPage(self._pdfContext, mediaBox) + self._pdfContext = Quartz.CGPDFContextCreate(dataConsumer, mediaBox, None) + Quartz.CGPDFContextBeginPage(self._pdfContext, auxiliaryInfo) self._hasContext = True def _closeContext(self): From 988347e779b3e22bf60c6c2bda4c80e567fb6a36 Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Sat, 25 Dec 2021 16:36:46 +0100 Subject: [PATCH 3/4] trying bleed again still not working --- drawBot/context/baseContext.py | 6 +++--- drawBot/context/gifContext.py | 4 ++-- drawBot/context/mp4Context.py | 2 +- drawBot/context/pdfContext.py | 29 +++++++++++++++++++++++------ drawBot/context/printContext.py | 4 ++-- drawBot/context/svgContext.py | 2 +- drawBot/drawBotDrawingTools.py | 6 ++++-- 7 files changed, 36 insertions(+), 17 deletions(-) diff --git a/drawBot/context/baseContext.py b/drawBot/context/baseContext.py index 047d8590..13d05a41 100644 --- a/drawBot/context/baseContext.py +++ b/drawBot/context/baseContext.py @@ -2104,7 +2104,7 @@ def __init__(self): # overwrite by a subclass - def _newPage(self, width, height, bleed): + def _newPage(self, width, height, pageOptions): pass def _save(self): @@ -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, bleed=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, bleed) + self._newPage(width, height, options) def saveImage(self, path, options): if not self.hasPage: diff --git a/drawBot/context/gifContext.py b/drawBot/context/gifContext.py index c69f1c0f..f2be658d 100644 --- a/drawBot/context/gifContext.py +++ b/drawBot/context/gifContext.py @@ -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): diff --git a/drawBot/context/mp4Context.py b/drawBot/context/mp4Context.py index 2543e465..81f970b4 100644 --- a/drawBot/context/mp4Context.py +++ b/drawBot/context/mp4Context.py @@ -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 diff --git a/drawBot/context/pdfContext.py b/drawBot/context/pdfContext.py index d4b979aa..97e3a6d5 100644 --- a/drawBot/context/pdfContext.py +++ b/drawBot/context/pdfContext.py @@ -29,26 +29,43 @@ 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" + } + print(options) + # 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() + print("auxiliaryInfo:", auxiliaryInfo) 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 diff --git a/drawBot/context/printContext.py b/drawBot/context/printContext.py index e4e846af..4a1c4b60 100644 --- a/drawBot/context/printContext.py +++ b/drawBot/context/printContext.py @@ -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") diff --git a/drawBot/context/svgContext.py b/drawBot/context/svgContext.py index b79a6d6a..a39f560b 100644 --- a/drawBot/context/svgContext.py +++ b/drawBot/context/svgContext.py @@ -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() diff --git a/drawBot/drawBotDrawingTools.py b/drawBot/drawBotDrawingTools.py index b41aca26..7ced8d45 100644 --- a/drawBot/drawBotDrawingTools.py +++ b/drawBot/drawBotDrawingTools.py @@ -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): @@ -293,16 +293,18 @@ def newPage(self, width=None, height=None, bleed=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, bleed) + self._addInstruction("newPage", width, height, pageOptions) def pages(self): """ From 7d791d527ef82bdb044416eac62e0c8d8110d33c Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Sat, 25 Dec 2021 17:05:05 +0100 Subject: [PATCH 4/4] remove print statements --- drawBot/context/pdfContext.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/drawBot/context/pdfContext.py b/drawBot/context/pdfContext.py index 97e3a6d5..a3d4db35 100644 --- a/drawBot/context/pdfContext.py +++ b/drawBot/context/pdfContext.py @@ -36,7 +36,6 @@ def _newPage(self, width, height, options): auxiliaryInfo = { Quartz.kCGPDFContextAuthor: "DrawBot" } - print(options) # auxiliaryInfo[Quartz.kCGPDFContextMediaBox] = AppKit.NSValue.valueWithRect_(mediaBox) if "bleed" in options: leftBleed, topBleed, rightBleed, bottomBleed = options["bleed"] @@ -51,7 +50,6 @@ def _newPage(self, width, height, options): # reset the context self.reset() - print("auxiliaryInfo:", auxiliaryInfo) if self._hasContext: # add a new page Quartz.CGPDFContextEndPage(self._pdfContext)