Skip to content

Commit

Permalink
various py3 changes; should all still work on py2.7, too
Browse files Browse the repository at this point in the history
  • Loading branch information
justvanrossum committed Nov 8, 2017
1 parent 3ed52a3 commit cdf38fb
Show file tree
Hide file tree
Showing 21 changed files with 188 additions and 108 deletions.
4 changes: 2 additions & 2 deletions DrawBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class DrawBotAppDelegate(AppKit.NSObject):

def init(self):
self = super(DrawBotAppDelegate, self).init()
code = stringToInt("GURL")
code = stringToInt(b"GURL")
AppKit.NSAppleEventManager.sharedAppleEventManager().setEventHandler_andSelector_forEventClass_andEventID_(self, "getUrl:withReplyEvent:", code, code)
return self

Expand Down Expand Up @@ -146,7 +146,7 @@ def showDebug_(self, sender):
def getUrl_withReplyEvent_(self, event, reply):
import urlparse
import urllib2
code = stringToInt("----")
code = stringToInt(b"----")
url = event.paramDescriptorForKeyword_(code)
urlString = url.stringValue()
documentController = AppKit.NSDocumentController.sharedDocumentController()
Expand Down
4 changes: 3 additions & 1 deletion drawBot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from drawBotDrawingTools import _drawBotDrawingTool
from __future__ import absolute_import

from .drawBotDrawingTools import _drawBotDrawingTool

_drawBotDrawingTool._addToNamespace(globals())
14 changes: 8 additions & 6 deletions drawBot/context/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from pdfContext import PDFContext
from imageContext import ImageContext
from gifContext import GifContext
from svgContext import SVGContext
from movContext import MOVContext
from printContext import PrintContext
from __future__ import absolute_import

from .pdfContext import PDFContext
from .imageContext import ImageContext
from .gifContext import GifContext
from .svgContext import SVGContext
from .movContext import MOVContext
from .printContext import PrintContext


allContexts = [PDFContext, ImageContext, SVGContext, MOVContext, PrintContext, GifContext]
Expand Down
26 changes: 16 additions & 10 deletions drawBot/context/baseContext.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import

import AppKit
import CoreText
import Quartz
Expand All @@ -8,8 +10,8 @@

from drawBot.misc import DrawBotError, cmyk2rgb, warnings

from tools import openType
from tools import variation
from .tools import openType
from .tools import variation


_FALLBACKFONT = "LucidaGrande"
Expand Down Expand Up @@ -333,10 +335,11 @@ def setNSBezierPath(self, path):
"""
self._path = path

def pointInside(self, (x, y)):
def pointInside(self, xy):
"""
Check if a point `x`, `y` is inside a path.
"""
x, y = xy
return self._path.containsPoint_((x, y))

def bounds(self):
Expand Down Expand Up @@ -930,7 +933,7 @@ def append(self, txt, **kwargs):
existingOpenTypeFeatures = openType.getFeatureTagsForFontName(self._font)
# sort features by their on/off state
# set all disabled features first
orderedOpenTypeFeatures = sorted(self._openTypeFeatures.items(), key=lambda (k, v): v)
orderedOpenTypeFeatures = sorted(self._openTypeFeatures.items(), key=lambda kv: kv[1])
for featureTag, value in orderedOpenTypeFeatures:
coreTextFeatureTag = featureTag
if not value:
Expand Down Expand Up @@ -1755,7 +1758,7 @@ def _transform(self, matrix):
def _textBox(self, txt, box, align):
pass

def _image(self, path, (x, y), alpha, pageNumber):
def _image(self, path, xy, alpha, pageNumber):
pass

def _frameDuration(self, seconds):
Expand All @@ -1770,10 +1773,10 @@ def _saveImage(self, path, multipage):
def _printImage(self, pdf=None):
pass

def _linkDestination(self, name, (x, y)):
def _linkDestination(self, name, xy):
pass

def _linkRect(self, name, (x, y, w, h)):
def _linkRect(self, name, xywh):
pass

#
Expand Down Expand Up @@ -2185,7 +2188,8 @@ def textBox(self, txt, box, align="left"):
self._state.path = None
self._textBox(txt, box, align)

def image(self, path, (x, y), alpha, pageNumber):
def image(self, path, xy, alpha, pageNumber):
x, y = xy
self._image(path, (x, y), alpha, pageNumber)

def installFont(self, path):
Expand Down Expand Up @@ -2218,8 +2222,10 @@ def _fontNameForPath(self, path):
psName = psName.toUnicode()
return psName

def linkDestination(self, name, (x, y)):
def linkDestination(self, name, xy):
x, y = xy
self._linkDestination(name, (x, y))

def linkRect(self, name, (x, y, w, h)):
def linkRect(self, name, xywh):
x, y, w, h = xywh
self._linkRect(name, (x, y, w, h))
4 changes: 3 additions & 1 deletion drawBot/context/drawBotContext.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import absolute_import

import Quartz

from pdfContext import PDFContext
from .pdfContext import PDFContext


class DrawBotContext(PDFContext):
Expand Down
4 changes: 3 additions & 1 deletion drawBot/context/dummyContext.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from baseContext import BaseContext
from __future__ import absolute_import, print_function

from .baseContext import BaseContext


class DummyContext(BaseContext):
Expand Down
6 changes: 4 additions & 2 deletions drawBot/context/gifContext.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import absolute_import

import AppKit
import Quartz

import tempfile

from imageContext import ImageContext
from .imageContext import ImageContext

from tools.gifTools import generateGif
from .tools.gifTools import generateGif


class GifContext(ImageContext):
Expand Down
4 changes: 3 additions & 1 deletion drawBot/context/imageContext.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import absolute_import

import AppKit
import Quartz

import os

from pdfContext import PDFContext
from .pdfContext import PDFContext


class ImageContext(PDFContext):
Expand Down
4 changes: 3 additions & 1 deletion drawBot/context/movContext.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import absolute_import

import AppKit
import QTKit
import Quartz

import os

from drawBot.misc import DrawBotError
from pdfContext import PDFContext
from .pdfContext import PDFContext


class MOVContext(PDFContext):
Expand Down
15 changes: 10 additions & 5 deletions drawBot/context/pdfContext.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import

import platform
from distutils.version import StrictVersion
import AppKit
Expand All @@ -6,9 +8,9 @@

import math

from tools import gifTools
from .tools import gifTools

from baseContext import BaseContext, FormattedString
from .baseContext import BaseContext, FormattedString
from drawBot.misc import DrawBotError, isPDF, isGIF


Expand Down Expand Up @@ -263,7 +265,8 @@ def _getImageSource(self, key, pageNumber):
raise DrawBotError("No image found at %s" % key)
return self._cachedImages[key]

def _image(self, path, (x, y), alpha, pageNumber):
def _image(self, path, xy, alpha, pageNumber):
x, y = xy
self._save()
_isPDF, image = self._getImageSource(path, pageNumber)
if image is not None:
Expand Down Expand Up @@ -382,14 +385,16 @@ def _rgbNSColorToCGColor(self, c):
return Quartz.CGColorCreateGenericGray(c.whiteComponent(), c.alphaComponent())
return Quartz.CGColorCreateGenericRGB(c.redComponent(), c.greenComponent(), c.blueComponent(), c.alphaComponent())

def _linkDestination(self, name, (x, y)):
def _linkDestination(self, name, xy):
x, y = xy
if (x, y) == (None, None):
x, y = self.width * 0.5, self.height * 0.5
x = max(0, min(x, self.width))
y = max(0, min(y, self.height))
centerPoint = Quartz.CGPoint(x, y)
Quartz.CGPDFContextAddDestinationAtPoint(self._pdfContext, name, centerPoint)

def _linkRect(self, name, (x, y, w, h)):
def _linkRect(self, name, xywh):
x, y, w, h = xywh
rectBox = Quartz.CGRectMake(x, y, w, h)
Quartz.CGPDFContextSetDestinationForRect(self._pdfContext, name, rectBox)
46 changes: 26 additions & 20 deletions drawBot/context/printContext.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,57 @@
from baseContext import BaseContext
from __future__ import absolute_import, print_function

from .baseContext import BaseContext


class PrintContext(BaseContext):

fileExtensions = ["*"]

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

def _save(self):
print "save"
print("save")

def _restore(self):
print "restore"
print("restore")

def _blendMode(self, operation):
print "blend mode", operation
print("blend mode", operation)

def _drawPath(self):
print "drawPath", self._state.path
print("drawPath", self._state.path)

def _clipPath(self):
print "clipPath", self._state.path
print("clipPath", self._state.path)

def _transform(self, matrix):
print "transform", matrix
print("transform", matrix)

def _textBox(self, txt, (x, y, w, h), align):
print "textBox", txt, (x, y, w, h), align
def _textBox(self, txt, xywh, align):
x, y, w, h = xywh
print("textBox", txt, (x, y, w, h), align)

def _image(self, path, (x, y), alpha, pageNumber):
print "image", path, x, y, alpha, pageNumber
def _image(self, path, xy, alpha, pageNumber):
x, y = xy
print("image", path, x, y, alpha, pageNumber)

def _frameDuration(self, seconds):
print "frameDuration", seconds
print("frameDuration", seconds)

def _reset(self, other=None):
print "reset", other
print("reset", other)

def _saveImage(self, path, multipage):
print "saveImage", path, multipage
print("saveImage", path, multipage)

def _printImage(self, pdf=None):
print "printImage", pdf
print("printImage", pdf)

def _linkDestination(self, name, (x, y)):
print "linkDestination", name, (x, y)
def _linkDestination(self, name, xy):
x, y = xy
print("linkDestination", name, (x, y))

def _linkRect(self, name, (x, y, w, h)):
print "linkRect", name, (x, y, w, h)
def _linkRect(self, name, xywh):
x, y, w, h = xywh
print("linkRect", name, (x, y, w, h))
9 changes: 6 additions & 3 deletions drawBot/context/svgContext.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import

import AppKit
import CoreText

Expand All @@ -10,8 +12,8 @@

from fontTools.misc.transform import Transform

from tools.openType import getFeatureTagsForFontAttributes
from baseContext import BaseContext, GraphicsState, Shadow, Color, FormattedString, Gradient
from .tools.openType import getFeatureTagsForFontAttributes
from .baseContext import BaseContext, GraphicsState, Shadow, Color, FormattedString, Gradient

from drawBot.misc import warnings, formatNumber

Expand Down Expand Up @@ -433,10 +435,11 @@ def _textBox(self, txt, box, align):
self._svgContext.newline()
self._svgEndClipPath()

def _image(self, path, (x, y), alpha, pageNumber):
def _image(self, path, xy, alpha, pageNumber):
# todo:
# support embedding of images when the source is not a path but
# a nsimage or a pdf / gif with a pageNumber
x, y = xy
self._svgBeginClipPath()
if path.startswith("http"):
url = AppKit.NSURL.URLWithString_(path)
Expand Down
Loading

0 comments on commit cdf38fb

Please sign in to comment.