Skip to content

Commit 1eae110

Browse files
author
Steve Canny
committed
oxml: convert CT_Point2D to xmlchemy
1 parent 9b3e615 commit 1eae110

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

docx/oxml/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
6868

6969
from docx.oxml.shape import (
7070
CT_Blip, CT_BlipFillProperties, CT_GraphicalObject,
71-
CT_GraphicalObjectData, CT_Inline, CT_Picture, CT_PositiveSize2D
71+
CT_GraphicalObjectData, CT_Inline, CT_Picture, CT_Point2D,
72+
CT_PositiveSize2D
7273
)
7374
register_element_cls('a:blip', CT_Blip)
7475
register_element_cls('a:ext', CT_PositiveSize2D)
7576
register_element_cls('a:graphic', CT_GraphicalObject)
7677
register_element_cls('a:graphicData', CT_GraphicalObjectData)
78+
register_element_cls('a:off', CT_Point2D)
7779
register_element_cls('pic:blipFill', CT_BlipFillProperties)
7880
register_element_cls('pic:pic', CT_Picture)
7981
register_element_cls('wp:extent', CT_PositiveSize2D)

docx/oxml/shape.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
from . import OxmlElement
88
from .ns import nsmap, nspfxmap, qn
9-
from .simpletypes import ST_PositiveCoordinate, ST_RelationshipId, XsdToken
9+
from .simpletypes import (
10+
ST_Coordinate, ST_PositiveCoordinate, ST_RelationshipId, XsdToken
11+
)
1012
from .xmlchemy import (
1113
BaseOxmlElement, OneAndOnlyOne, OptionalAttribute, RequiredAttribute,
1214
ZeroOrOne
@@ -159,11 +161,14 @@ class CT_Point2D(BaseOxmlElement):
159161
Used for ``<a:off>`` element, and perhaps others. Specifies an x, y
160162
coordinate (point).
161163
"""
164+
x = RequiredAttribute('x', ST_Coordinate)
165+
y = RequiredAttribute('y', ST_Coordinate)
166+
162167
@classmethod
163168
def new(cls, nsptagname_str, x, y):
164169
elm = OxmlElement(nsptagname_str)
165-
elm.set('x', str(x))
166-
elm.set('y', str(y))
170+
elm.x = x
171+
elm.y = y
167172
return elm
168173

169174

docx/oxml/simpletypes.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,26 @@ def validate(cls, value):
153153
cls.validate_int_in_range(value, 0, 4294967295)
154154

155155

156+
class ST_Coordinate(BaseIntType):
157+
158+
@classmethod
159+
def convert_from_xml(cls, str_value):
160+
if 'i' in str_value or 'm' in str_value or 'p' in str_value:
161+
return ST_UniversalMeasure.convert_from_xml(str_value)
162+
return int(str_value)
163+
164+
@classmethod
165+
def validate(cls, value):
166+
ST_CoordinateUnqualified.validate(value)
167+
168+
169+
class ST_CoordinateUnqualified(XsdLong):
170+
171+
@classmethod
172+
def validate(cls, value):
173+
cls.validate_int_in_range(value, -27273042329600, 27273042316900)
174+
175+
156176
class ST_DecimalNumber(XsdInt):
157177
pass
158178

@@ -170,3 +190,17 @@ def validate(cls, value):
170190

171191
class ST_RelationshipId(XsdString):
172192
pass
193+
194+
195+
class ST_UniversalMeasure(BaseSimpleType):
196+
197+
@classmethod
198+
def convert_from_xml(cls, str_value):
199+
float_part, units_part = str_value[:-2], str_value[-2:]
200+
quantity = float(float_part)
201+
multiplier = {
202+
'mm': 36000, 'cm': 360000, 'in': 914400, 'pt': 12700,
203+
'pc': 152400, 'pi': 152400
204+
}[units_part]
205+
emu_value = int(round(quantity * multiplier))
206+
return emu_value

0 commit comments

Comments
 (0)