def _reset(self, other=None): if other is not None: self._instructionsStack = list(other._instructionsStack) self._dummyContext = other._dummyContext self._width = other._width self._height = other._height self._tempInstalledFonts = dict(other._tempInstalledFonts) self._requiresNewFirstPage = other._requiresNewFirstPage self._hasPage = other._hasPage else: self._instructionsStack = [] self._dummyContext = DummyContext() self._width = None self._height = None self._requiresNewFirstPage = False self._hasPage = False if not hasattr(self, "_tempInstalledFonts"): self._tempInstalledFonts = dict()
def _reset(self, other=None): if other is not None: self._instructionsStack = list(other._instructionsStack) self._dummyContext = other._dummyContext self._width = other._width self._height = other._height self._installedFontPaths = set(other._installedFontPaths) else: self._instructionsStack = [] self._dummyContext = DummyContext() self._width = None self._height = None self._installedFontPaths = set()
def _reset(self, other=None): if other is not None: self._instructionsStack = list(other._instructionsStack) self._dummyContext = other._dummyContext self._width = other._width self._height = other._height self._tempInstalledFonts = dict(other._tempInstalledFonts) else: self._instructionsStack = [] self._dummyContext = DummyContext() self._width = None self._height = None if not hasattr(self, "_tempInstalledFonts"): self._tempInstalledFonts = dict()
class DrawBotDrawingTool(object): def __init__(self): self._reset() def _get__all__(self): return [i for i in dir(self) if not i.startswith("_")] + ["__version__"] __all__ = property(_get__all__) def _get_version(self): try: import drawBotSettings return drawBotSettings.__version__ except: # DrawBot is installed as a module import pkg_resources return pkg_resources.require("drawBot")[0].version return "" __version__ = property(_get_version) def _addToNamespace(self, namespace): namespace.update(_getmodulecontents(self, self.__all__)) namespace.update( _getmodulecontents(random, ["random", "randint", "choice"])) namespace.update(_getmodulecontents(math)) def _addInstruction(self, callback, *args, **kwargs): if self._requiresNewFirstPage and not self._hasPage: self._hasPage = True self._instructionsStack.append( ("newPage", [self.width(), self.height()], {})) self._instructionsStack.append((callback, args, kwargs)) def _drawInContext(self, context): if not self._instructionsStack: return for callback, args, kwargs in self._instructionsStack: attr = getattr(context, callback) attr(*args, **kwargs) def _reset(self, other=None): if other is not None: self._instructionsStack = list(other._instructionsStack) self._dummyContext = other._dummyContext self._width = other._width self._height = other._height self._tempInstalledFonts = dict(other._tempInstalledFonts) self._requiresNewFirstPage = other._requiresNewFirstPage self._hasPage = other._hasPage else: self._instructionsStack = [] self._dummyContext = DummyContext() self._width = None self._height = None self._requiresNewFirstPage = False self._hasPage = False if not hasattr(self, "_tempInstalledFonts"): self._tempInstalledFonts = dict() def _copy(self): new = self.__class__() new._instructionsStack = list(self._instructionsStack) new._dummyContext = self._dummyContext new._width = self._width new._height = self._height new._tempInstalledFonts = dict(self._tempInstalledFonts) return new def newDrawing(self): """ Reset the drawing stack to the clean and empty stack. .. showcode:: /../examples/newDrawing.py """ self._reset() self.installedFonts() def endDrawing(self): """ Explicitly tell drawBot the drawing is done. This is advised when using drawBot as a standalone module. """ self._uninstallAllFonts() gifTools.clearExplodedGifCache() # magic variables def width(self): """ Returns the width of the current page. """ if self._width is None: return 1000 return self._width def _get_width(self): warnings.warn("Magic variables are deprecated.'") return self.width() WIDTH = property(_get_width) def height(self): """ Returns the height of the current page. """ if self._height is None: return 1000 return self._height def _get_height(self): warnings.warn("Magic variables are deprecated.'") return self.height() HEIGHT = property(_get_height) def sizes(self, paperSize=None): """ Returns the width and height of a specified canvas size. If no canvas size is given it will return the dictionary containing all possible page sizes. """ _paperSizes["screen"] = tuple( AppKit.NSScreen.mainScreen().frame().size) if paperSize: return _paperSizes[paperSize] return _paperSizes def pageCount(self): """ Returns the current page count. """ pageCount = 0 for i in self._instructionsStack: if i[0] == "newPage": pageCount += 1 return pageCount def _get_pageCount(self): warnings.warn("Magic variables are deprecated.'") return self.pageCount() PAGECOUNT = property(_get_pageCount) _magicVariables = ["WIDTH", "HEIGHT", "PAGECOUNT"] # ==================== # = public callbacks = # ==================== # size and pages def size(self, width, height=None): """ Set the width and height of the canvas. Without calling `size()` the default drawing board is 1000 by 1000 points. Alternatively `size('A4')` with a supported papersizes or `size('screen')` setting the current screen size as size, can be used. Afterwards the functions `width()` and `height()` can be used for calculations. It is advised to use `size()` always at the top of the script and not use `size()` in a multiple page document use `newPage(w, h)` to set the correct dimentions directly. .. showcode:: /../examples/size.py All supported papersizes: 10x14, 10x14Landscape, A0, A0Landscape, A1, A1Landscape, A2, A2Landscape, A3, A3Landscape, A4, A4Landscape, A4Small, A4SmallLandscape, A5, A5Landscape, B4, B4Landscape, B5, B5Landscape, Executive, ExecutiveLandscape, Folio, FolioLandscape, Ledger, LedgerLandscape, Legal, LegalLandscape, Letter, LetterLandscape, LetterSmall, LetterSmallLandscape, Quarto, QuartoLandscape, Statement, StatementLandscape, Tabloid, TabloidLandscape. """ if width in _paperSizes: width, height = _paperSizes[width] if width == "screen": width, height = AppKit.NSScreen.mainScreen().frame().size if height is None: width, height = width self._width = width self._height = height if not self._instructionsStack: self.newPage(width, height) else: raise DrawBotError( "It is advised to use 'size()' at the top of a script.") def newPage(self, width=None, height=None): """ Create a new canvas to draw in. This will act like a page in a pdf or a frame in a mov. Optionally a `width` and `height` argument can be provided to set the size. If not provided the default size will be used. Alternatively `size('A4')` with a supported papersizes or `size('screen')` setting the current screen size as size, can be used. .. showcode:: /../examples/newPage.py All supported papersizes: 10x14, 10x14Landscape, A0, A0Landscape, A1, A1Landscape, A2, A2Landscape, A3, A3Landscape, A4, A4Landscape, A4Small, A4SmallLandscape, A5, A5Landscape, B4, B4Landscape, B5, B5Landscape, Executive, ExecutiveLandscape, Folio, FolioLandscape, Ledger, LedgerLandscape, Legal, LegalLandscape, Letter, LetterLandscape, LetterSmall, LetterSmallLandscape, Quarto, QuartoLandscape, Statement, StatementLandscape, Tabloid, TabloidLandscape. """ if width in _paperSizes: width, height = _paperSizes[width] if width == "screen": width, height = AppKit.NSScreen.mainScreen().frame().size self._width = width self._height = height self._hasPage = True self._addInstruction("newPage", width, height) def newpage(self, width=None, height=None): _deprecatedWarningLowercase("newPage(%s, %s)" % (width, height)) self.newPage(width, height) def saveImage(self, paths, multipage=None): """ Save or export the canvas to a specified format. The argument `paths` can either be a single path or a list of paths. The file extension is important because it will determine the format in which the image will be exported. All supported file extensions: `pdf`, `svg`, `png`, `jpg`, `jpeg`, `tiff`, `tif`, `gif`, `bmp` and `mov`. * A `pdf` can be multipage. If `multipage` is `False` only the current page is saved. * A `mov` will use each page as a frame. * A `gif` can be animated when there are multiple pages and it will use each page as a frame. * All images and `svg` formats will only save the current page. If `multipage` is `True` all pages are saved to disk (a page index will be added to the file name). .. showcode:: /../examples/saveImage.py """ if isinstance(paths, (str, unicode)): paths = [paths] for rawPath in paths: path = optimizePath(rawPath) dirName = os.path.dirname(path) if not os.path.exists(dirName): raise DrawBotError("Folder '%s' doesn't exists" % dirName) base, ext = os.path.splitext(path) ext = ext.lower()[1:] if not ext: ext = rawPath context = getContextForFileExt(ext) if context is None: raise DrawBotError( "Did not found a supported context for: '%s'" % ext) self._drawInContext(context) context.saveImage(path, multipage) def saveimage(self, paths): _deprecatedWarningLowercase("saveImage()") self.saveImage(paths) def printImage(self, pdf=None): """ Export the canvas to a printing dialog, ready to print. Optionally a `pdf` object can be provided. .. showcode:: /../examples/printImage.py """ context = getContextForFileExt("pdf") if pdf is None: self._drawInContext(context) context.printImage() else: context.printImage(pdf) def pdfImage(self): """ Return the image as a pdf document object. """ from context.drawBotContext import DrawBotContext context = DrawBotContext() self._drawInContext(context) return context.getNSPDFDocument() # graphic state def save(self): """ Save the current state. This will save the state of the canvas (with all the transformations) but also the state of the colors, strokes... """ self._dummyContext.save() self._requiresNewFirstPage = True self._addInstruction("save") def restore(self): """ Restore from a previously saved state. This will restore the state of the canvas (with all the transformations) but also the state of colors, strokes... """ self._dummyContext.restore() self._requiresNewFirstPage = True self._addInstruction("restore") # basic shapes def rect(self, x, y, w, h): """ Draw a rectangle from position x, y with the given width and height. .. showcode:: /../examples/rect.py """ self._requiresNewFirstPage = True self._addInstruction("rect", x, y, w, h) def oval(self, x, y, w, h): """ Draw an oval from position x, y with the given width and height. .. showcode:: /../examples/oval.py """ self._requiresNewFirstPage = True self._addInstruction("oval", x, y, w, h) # path def newPath(self): """ Create a new path. """ self._requiresNewFirstPage = True self._addInstruction("newPath") def newpath(self): _deprecatedWarningLowercase("newPath()") self.newPath() def moveTo(self, (x, y)): """ Move to a point `x`, `y`. """ self._requiresNewFirstPage = True self._addInstruction("moveTo", (x, y))
def _reset(self): self._instructionsStack = [] self._dummyContext = DummyContext() self._width = None self._height = None
class DrawBotDrawingTool(object): def __init__(self): self._reset() def _get__all__(self): return [i for i in dir(self) if not i.startswith("_")] + ["__version__"] __all__ = property(_get__all__) def _get_version(self): return drawBotSettings.__version__ __version__ = property(_get_version) def _addToNamespace(self, namespace): namespace.update(_getmodulecontents(self, self.__all__)) namespace.update(_getmodulecontents(random, ["random", "randint", "choice"])) namespace.update(_getmodulecontents(math)) def _addInstruction(self, callback, *args, **kwargs): self._instructionsStack.append((callback, args, kwargs)) def _drawInContext(self, context): if not self._instructionsStack: return if self._instructionsStack[0][0] != "newPage": self._instructionsStack.insert(0, ("newPage", [self.width(), self.height()], {})) for callback, args, kwargs in self._instructionsStack: attr = getattr(context, callback) attr(*args, **kwargs) def _reset(self): self._instructionsStack = [] self._dummyContext = DummyContext() self._width = None self._height = None def newDrawing(self): """ Reset the drawing stack to the clean and empty stack. .. showcode:: /../examples/newDrawing.py """ self._reset() self.installedFonts() ## magic variables def width(self): """ Returns the width of the current page. """ if self._width is None: return 1000 return self._width def _get_width(self): warnings.warn("Magic variables are deprecated.'") return self.width() WIDTH = property(_get_width) def height(self): """ Returns the height of the current page. """ if self._height is None: return 1000 return self._height def _get_height(self): warnings.warn("Magic variables are deprecated.'") return self.height() HEIGHT = property(_get_height) def sizes(self, paperSize=None): """ Returns the width and height of a specified canvas size. If no canvas size is given it will return the dictionary containing all possible page sizes. """ if paperSize: return _paperSizes[paperSize] return _paperSizes def pageCount(self): """ Returns the current page count. """ pageCount = 1 if self._instructionsStack and self._instructionsStack[0][0] == "newPage": pageCount = 0 for i in self._instructionsStack: if i[0] == "newPage": pageCount += 1 return pageCount def _get_pageCount(self): warnings.warn("Magic variables are deprecated.'") return self.pageCount() PAGECOUNT = property(_get_pageCount) _magicVariables = ["WIDTH", "HEIGHT", "PAGECOUNT"] ## public callbacks # size and pages def size(self, width, height=None): """ Set the width and height of the canvas. Without calling `size()` the default drawing board is 1000 by 1000 points. Alternatively `size('A4')` can be used. Afterwards the functions `width()` and `height()` can be used for calculations. It is advised to use `size()` always at the top of the script and not use `size()` in a multiple page document as a `newPage(w, h)` set the correct dimentions directly. .. showcode:: /../examples/size.py All supported papersizes: 10x14, 10x14Landscape, A0, A0Landscape, A1, A1Landscape, A2, A2Landscape, A3, A3Landscape, A4, A4Landscape, A4Small, A4SmallLandscape, A5, A5Landscape, B4, B4Landscape, B5, B5Landscape, Executive, ExecutiveLandscape, Folio, FolioLandscape, Ledger, LedgerLandscape, Legal, LegalLandscape, Letter, LetterLandscape, LetterSmall, LetterSmallLandscape, Quarto, QuartoLandscape, Statement, StatementLandscape, Tabloid, TabloidLandscape. """ if width in _paperSizes: width, height = _paperSizes[width] if height is None: width, height = width self._width = width self._height = height if not self._instructionsStack: self.newPage(width, height) else: self._addInstruction("size", width, height) def newPage(self, width=None, height=None): """ Create a new canvas to draw in. This will act like a page in a pdf or a frame in a mov. Optionally a `width` and `height` argument can be provided to set the size. If not provided the default size will be used. Alternatively `size('A4')` can be used. .. showcode:: /../examples/newPage.py All supported papersizes: 10x14, 10x14Landscape, A0, A0Landscape, A1, A1Landscape, A2, A2Landscape, A3, A3Landscape, A4, A4Landscape, A4Small, A4SmallLandscape, A5, A5Landscape, B4, B4Landscape, B5, B5Landscape, Executive, ExecutiveLandscape, Folio, FolioLandscape, Ledger, LedgerLandscape, Legal, LegalLandscape, Letter, LetterLandscape, LetterSmall, LetterSmallLandscape, Quarto, QuartoLandscape, Statement, StatementLandscape, Tabloid, TabloidLandscape. """ if width in _paperSizes: width, height = _paperSizes[width] self._width = width self._height = height self._addInstruction("newPage", width, height) def newpage(self, width=None, height=None): _deprecatedWarningLowercase("newPage(%s, %s)" % (width, height)) self.newPage(width, height) def saveImage(self, paths, multipage=None): """ Save or export the canvas to a specified format. The argument `paths` can either be a single path or a list of paths. The file extension is important because it will determine the format in which the image will be exported. All supported file extensions: `pdf`, `svg`, `png`, `jpg`, `jpeg`, `tiff`, `tif`, `gif`, `bmp` and `mov`. * A `pdf` can be multipage. If `multipage` is `False` only the current page is saved. * A `mov` will use each page as a frame. * A `gif` can be animated when there are multiple pages and it will use each page as a frame. * All images and `svg` formats will only save the current page. If `multipage` is `True` all pages are saved to disk (a page index will be added to the file name). .. showcode:: /../examples/saveImage.py """ if isinstance(paths, (str, unicode)): paths = [paths] for path in paths: path = optimizePath(path) dirName = os.path.dirname(path) if not os.path.exists(dirName): raise DrawBotError, "Folder '%s' doesn't exists" % dirName base, ext = os.path.splitext(path) ext = ext.lower()[1:] context = getContextForFileExt(ext) self._drawInContext(context) context.saveImage(path, multipage) def saveimage(self, paths): _deprecatedWarningLowercase("saveImage()") self.saveImage(paths) def printImage(self, pdf=None): """ Export the canvas to a printing dialog, ready to print. Optionally a `pdf` object can be provided. .. showcode:: /../examples/printImage.py """ context = getContextForFileExt("pdf") if pdf is None: self._drawInContext(context) context.printImage() else: context.printImage(pdf) def pdfImage(self): """ Return the image as a pdf document object. """ from context.drawBotContext import DrawBotContext context = DrawBotContext() self._drawInContext(context) return context.getNSPDFDocument() # graphic state def save(self): """ Save the current state. This will save the state of the canvas (with all the transformations) but also the state of the colors, strokes... """ self._dummyContext.save() self._addInstruction("save") def restore(self): """ Restore from a previously saved state. This will restore the state of the canvas (with all the transformations) but also the state of colors, strokes... """ self._dummyContext.restore() self._addInstruction("restore") # basic shapes def rect(self, x, y, w, h): """ Draw a rectangle from position x, y with the given width and height. .. showcode:: /../examples/rect.py """ self._addInstruction("rect", x, y, w, h) def oval(self, x, y, w, h): """ Draw an oval from position x, y with the given width and height. .. showcode:: /../examples/oval.py """ self._addInstruction("oval", x, y, w, h) # path def newPath(self): """ Create a new path. """ self._addInstruction("newPath") def newpath(self): _deprecatedWarningLowercase("newPath()") self.newPath() def moveTo(self, (x, y)): """ Move to a point `x`, `y`. """ self._addInstruction("moveTo", (x, y))