Skip to content

Commit 1904119

Browse files
author
Steve Canny
committed
font: add Font.subscript getter
1 parent 91d48ae commit 1904119

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

docx/oxml/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
143143
register_element_cls('w:pStyle', CT_String)
144144

145145
from .text.run import (
146-
CT_Br, CT_Fonts, CT_HpsMeasure, CT_R, CT_RPr, CT_Text, CT_Underline
146+
CT_Br, CT_Fonts, CT_HpsMeasure, CT_R, CT_RPr, CT_Text, CT_Underline,
147+
CT_VerticalAlignRun
147148
)
148149
register_element_cls('w:b', CT_OnOff)
149150
register_element_cls('w:bCs', CT_OnOff)
@@ -172,4 +173,5 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
172173
register_element_cls('w:t', CT_Text)
173174
register_element_cls('w:u', CT_Underline)
174175
register_element_cls('w:vanish', CT_OnOff)
176+
register_element_cls('w:vertAlign', CT_VerticalAlignRun)
175177
register_element_cls('w:webHidden', CT_OnOff)

docx/oxml/simpletypes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,14 @@ def convert_from_xml(cls, str_value):
361361
}[units_part]
362362
emu_value = Emu(int(round(quantity * multiplier)))
363363
return emu_value
364+
365+
366+
class ST_VerticalAlignRun(XsdStringEnumeration):
367+
"""
368+
Valid values for `w:vertAlign/@val`.
369+
"""
370+
BASELINE = 'baseline'
371+
SUPERSCRIPT = 'superscript'
372+
SUBSCRIPT = 'subscript'
373+
374+
_members = (BASELINE, SUPERSCRIPT, SUBSCRIPT)

docx/oxml/text/run.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
from ...enum.text import WD_UNDERLINE
88
from ..ns import qn
9-
from ..simpletypes import ST_BrClear, ST_BrType, ST_HpsMeasure, ST_String
9+
from ..simpletypes import (
10+
ST_BrClear, ST_BrType, ST_HpsMeasure, ST_String, ST_VerticalAlignRun
11+
)
1012
from ..xmlchemy import (
1113
BaseOxmlElement, OptionalAttribute, RequiredAttribute, ZeroOrMore,
1214
ZeroOrOne
@@ -156,6 +158,7 @@ class CT_RPr(BaseOxmlElement):
156158
webHidden = ZeroOrOne('w:webHidden', successors=_tag_seq[18:])
157159
sz = ZeroOrOne('w:sz', successors=_tag_seq[24:])
158160
u = ZeroOrOne('w:u', successors=_tag_seq[27:])
161+
vertAlign = ZeroOrOne('w:vertAlign', successors=_tag_seq[32:])
159162
rtl = ZeroOrOne('w:rtl', successors=_tag_seq[33:])
160163
cs = ZeroOrOne('w:cs', successors=_tag_seq[34:])
161164
specVanish = ZeroOrOne('w:specVanish', successors=_tag_seq[38:])
@@ -225,6 +228,20 @@ def style(self, style):
225228
else:
226229
self.rStyle.val = style
227230

231+
@property
232+
def subscript(self):
233+
"""
234+
|True| if `w:vertAlign/@w:val` is 'subscript'. |False| if
235+
`w:vertAlign/@w:val` contains any other value. |None| if
236+
`w:vertAlign` is not present.
237+
"""
238+
vertAlign = self.vertAlign
239+
if vertAlign is None:
240+
return None
241+
if vertAlign.val == ST_VerticalAlignRun.SUBSCRIPT:
242+
return True
243+
return False
244+
228245
@property
229246
def sz_val(self):
230247
"""
@@ -314,6 +331,13 @@ def val(self, value):
314331
self.set(qn('w:val'), val)
315332

316333

334+
class CT_VerticalAlignRun(BaseOxmlElement):
335+
"""
336+
``<w:vertAlign>`` element, specifying subscript or superscript.
337+
"""
338+
val = RequiredAttribute('w:val', ST_VerticalAlignRun)
339+
340+
317341
class _RunContentAppender(object):
318342
"""
319343
Service object that knows how to translate a Python string into run

docx/text/run.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,18 @@ def strike(self):
480480
def strike(self, value):
481481
self._set_bool_prop('strike', value)
482482

483+
@property
484+
def subscript(self):
485+
"""
486+
Boolean indicating whether the characters in this |Font| appear as
487+
subscript. |None| indicates the subscript/subscript value is
488+
inherited from the style hierarchy.
489+
"""
490+
rPr = self._element.rPr
491+
if rPr is None:
492+
return None
493+
return rPr.subscript
494+
483495
@property
484496
def underline(self):
485497
"""

tests/text/test_run.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ def it_can_change_its_bool_prop_settings(self, bool_prop_set_fixture):
369369
setattr(font, prop_name, value)
370370
assert font._element.xml == expected_xml
371371

372+
def it_knows_whether_it_is_subscript(self, subscript_get_fixture):
373+
font, expected_value = subscript_get_fixture
374+
assert font.subscript == expected_value
375+
372376
def it_knows_its_underline_type(self, underline_get_fixture):
373377
font, expected_value = underline_get_fixture
374378
assert font.underline is expected_value
@@ -515,6 +519,18 @@ def size_set_fixture(self, request):
515519
expected_xml = xml(expected_r_cxml)
516520
return font, value, expected_xml
517521

522+
@pytest.fixture(params=[
523+
('w:r', None),
524+
('w:r/w:rPr', None),
525+
('w:r/w:rPr/w:vertAlign{w:val=baseline}', False),
526+
('w:r/w:rPr/w:vertAlign{w:val=subscript}', True),
527+
('w:r/w:rPr/w:vertAlign{w:val=superscript}', False),
528+
])
529+
def subscript_get_fixture(self, request):
530+
r_cxml, expected_value = request.param
531+
font = Font(element(r_cxml))
532+
return font, expected_value
533+
518534
@pytest.fixture(params=[
519535
('w:r', None),
520536
('w:r/w:rPr/w:u', None),

0 commit comments

Comments
 (0)