Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made newPage functions to work without arguments too #14

Merged
merged 4 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 14 additions & 2 deletions src/drawbot_skia/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from .gstate import GraphicsState, GraphicsStateMixin


DEFAULT_CANVAS_DIMENSIONS = (1000, 1000)


class Drawing:

def __init__(self, document=None, flipCanvas=True):
Expand All @@ -24,7 +27,7 @@ def _reset(self, document=None):
@property
def _canvas(self):
if self._skia_canvas is None:
self.size(1000, 1000) # This will create the canvas
self.size(*DEFAULT_CANVAS_DIMENSIONS) # This will create the canvas
return self._skia_canvas

@_canvas.setter
Expand All @@ -42,7 +45,16 @@ def size(self, width, height):
raise DrawbotError("size() can't be called if there's already a canvas active")
self.newPage(width, height)

def newPage(self, width, height):
def newPage(self, width=None, height=None):
if width and not height or height and not width:
justvanrossum marked this conversation as resolved.
Show resolved Hide resolved
raise TypeError("newPage() takes either no argument or two - width and height")
if not width and not height:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not width and not height:
if width is None and height is None:

width = getattr(self._document, 'pageWidth', None)
height = getattr(self._document, 'pageHeight', None)
if not width or not height:
justvanrossum marked this conversation as resolved.
Show resolved Hide resolved
# this is because dimensions are set only after drawing starts,
# if you use this function before, it breaks
width, height = DEFAULT_CANVAS_DIMENSIONS
if self._document.isDrawing:
self._document.endPage()
self._gstate = GraphicsState()
Expand Down
19 changes: 19 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,25 @@ def test_newPage_newGState():
assert (255, 255, 255, 255) == db._gstate.fillPaint.color


def test_newPage_dimensions_arguments():
justvanrossum marked this conversation as resolved.
Show resolved Hide resolved
from drawbot_skia.drawing import DEFAULT_CANVAS_DIMENSIONS
db = Drawing()
db.rect(0, 0, 300, 300) # the dimensions are set only after drawing starts
assert (db.width(), db.height()) == DEFAULT_CANVAS_DIMENSIONS
db.newPage(600, 450)
db.rect(0, 0, 300, 300)
assert (db.width(), db.height()) == (600, 450)
db.newPage()
db.rect(0, 0, 300, 300)
assert (db.width(), db.height()) == (600, 450)
with pytest.raises(TypeError):
db.newPage(10)
with pytest.raises(TypeError):
db.newPage(width=10)
with pytest.raises(TypeError):
db.newPage(height=10)


def test_multipleDocuments(tmpdir):
tmpdir = pathlib.Path(tmpdir)
db = Drawing()
Expand Down