Skip to content

Commit d490871

Browse files
author
Steve Canny
committed
shp: add InlineShape.width, height setters
1 parent c5b921e commit d490871

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed

docs/dev/analysis/features/shapes-inline-size.rst

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,9 @@ Overview
99
The position of an inline shape is completely determined by the text it is
1010
inline with, however its dimensions can be specified. For some shape types,
1111
both the contained shape and the shape container specify a width and height.
12-
13-
14-
Unit tests
15-
----------
16-
17-
DescribeInlineShape
18-
19-
* it_knows_its_display_dimensions
20-
* it_can_change_its_display_dimensions
21-
22-
23-
Code sketches
24-
-------------
25-
26-
::
27-
28-
@property
29-
def InlineShape.width(self):
30-
assert isinstance(self._inline.extent.cx, _BaseLength)
31-
return self._inline.extent.cx
32-
33-
@width.setter
34-
def width(self, emu):
35-
self._inline.extent = emu
12+
In the case of a picture, the dimensions of the inline shape (container)
13+
determine the display size while the dimension of the pic element determine the
14+
"original size" of the image.
3615

3716

3817
Candidate protocol -- inline shape access

docx/oxml/shape.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,22 @@ def cx(self):
194194
cx = int(cx_str)
195195
return Emu(cx)
196196

197+
@cx.setter
198+
def cx(self, cx):
199+
cx_str = str(cx)
200+
self.set('cx', cx_str)
201+
197202
@property
198203
def cy(self):
199204
cy_str = self.get('cy')
200205
cy = int(cy_str)
201206
return Emu(cy)
202207

208+
@cy.setter
209+
def cy(self, cy):
210+
cy_str = str(cy)
211+
self.set('cy', cy_str)
212+
203213
@classmethod
204214
def new(cls, nsptagname_str, cx, cy):
205215
elm = OxmlElement(nsptagname_str)

docx/shape.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def height(self):
3737
"""
3838
return self._inline.extent.cy
3939

40+
@height.setter
41+
def height(self, cy):
42+
assert isinstance(cy, int)
43+
assert 0 < cy
44+
self._inline.extent.cy = cy
45+
4046
@classmethod
4147
def new_picture(cls, r, image_part, rId, shape_id):
4248
"""
@@ -78,3 +84,9 @@ def width(self):
7884
Return the display width of this inline shape as an |Emu| instance.
7985
"""
8086
return self._inline.extent.cx
87+
88+
@width.setter
89+
def width(self, cx):
90+
assert isinstance(cx, int)
91+
assert 0 < cx
92+
self._inline.extent.cx = cx

features/shp-inline-shape-size.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Feature: Query and change dimensions of inline shape
77
Given an inline shape of known dimensions
88
Then the dimensions of the inline shape match the known values
99

10-
@wip
1110
Scenario: Change inline shape dimensions
1211
Given an inline shape of known dimensions
1312
When I change the dimensions of the inline shape

tests/test_shape.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,32 @@ def it_knows_its_display_dimensions(self, dimensions_get_fixture):
4747
assert isinstance(height, _BaseLength)
4848
assert height == cy
4949

50+
def it_can_change_its_display_dimensions(self, dimensions_set_fixture):
51+
inline_shape, cx, cy, expected_xml = dimensions_set_fixture
52+
inline_shape.width = cx
53+
inline_shape.height = cy
54+
assert inline_shape._inline.xml == expected_xml
55+
5056
# fixtures -------------------------------------------------------
5157

5258
@pytest.fixture
5359
def dimensions_get_fixture(self):
5460
cx, cy = 333, 666
55-
inline = (
56-
an_inline().with_nsdecls().with_child(
57-
an_extent().with_cx(cx).with_cy(cy))
58-
).element
61+
inline = self._inline_bldr_with_dimensions(cx, cy).element
5962
inline_shape = InlineShape(inline)
6063
return inline_shape, cx, cy
6164

65+
@pytest.fixture
66+
def dimensions_set_fixture(self):
67+
# inline_shape -----------------
68+
cx, cy = 333, 666
69+
inline = self._inline_bldr_with_dimensions(cx, cy).element
70+
inline_shape = InlineShape(inline)
71+
# expected_xml -----------------
72+
cx, cy = cx + 111, cy + 222
73+
expected_xml = self._inline_bldr_with_dimensions(cx, cy).xml()
74+
return inline_shape, cx, cy, expected_xml
75+
6276
@pytest.fixture
6377
def image_params(self):
6478
filename = 'foobar.garf'
@@ -127,6 +141,12 @@ def shape_type_fixture(self, request):
127141

128142
return InlineShape(inline), shape_type
129143

144+
def _inline_bldr_with_dimensions(self, cx, cy):
145+
return (
146+
an_inline().with_nsdecls().with_child(
147+
an_extent().with_cx(cx).with_cy(cy))
148+
)
149+
130150
def _inline_with_picture(self, embed=False, link=False):
131151
picture_ns = nsmap['pic']
132152

0 commit comments

Comments
 (0)