Skip to content

Commit 7d04ac9

Browse files
author
Steve Canny
committed
font: add Font.superscript getter
1 parent 1904119 commit 7d04ac9

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

docx/oxml/text/run.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,20 @@ def subscript(self):
242242
return True
243243
return False
244244

245+
@property
246+
def superscript(self):
247+
"""
248+
|True| if `w:vertAlign/@w:val` is 'superscript'. |False| if
249+
`w:vertAlign/@w:val` contains any other value. |None| if
250+
`w:vertAlign` is not present.
251+
"""
252+
vertAlign = self.vertAlign
253+
if vertAlign is None:
254+
return None
255+
if vertAlign.val == ST_VerticalAlignRun.SUPERSCRIPT:
256+
return True
257+
return False
258+
245259
@property
246260
def sz_val(self):
247261
"""

docx/text/run.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,18 @@ def subscript(self):
492492
return None
493493
return rPr.subscript
494494

495+
@property
496+
def superscript(self):
497+
"""
498+
Boolean indicating whether the characters in this |Font| appear as
499+
superscript. |None| indicates the subscript/superscript value is
500+
inherited from the style hierarchy.
501+
"""
502+
rPr = self._element.rPr
503+
if rPr is None:
504+
return None
505+
return rPr.superscript
506+
495507
@property
496508
def underline(self):
497509
"""

features/txt-font-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ Feature: Get or set font properties
7979
| single | WD_UNDERLINE.DOUBLE | WD_UNDERLINE.DOUBLE |
8080

8181

82-
@wip
8382
Scenario Outline: Get font sub/superscript value
8483
Given a font having <vertAlign-state> vertical alignment
8584
Then font.subscript is <sub-value>

tests/text/test_run.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ def it_knows_whether_it_is_subscript(self, subscript_get_fixture):
373373
font, expected_value = subscript_get_fixture
374374
assert font.subscript == expected_value
375375

376+
def it_knows_whether_it_is_superscript(self, superscript_get_fixture):
377+
font, expected_value = superscript_get_fixture
378+
assert font.superscript == expected_value
379+
376380
def it_knows_its_underline_type(self, underline_get_fixture):
377381
font, expected_value = underline_get_fixture
378382
assert font.underline is expected_value
@@ -531,6 +535,18 @@ def subscript_get_fixture(self, request):
531535
font = Font(element(r_cxml))
532536
return font, expected_value
533537

538+
@pytest.fixture(params=[
539+
('w:r', None),
540+
('w:r/w:rPr', None),
541+
('w:r/w:rPr/w:vertAlign{w:val=baseline}', False),
542+
('w:r/w:rPr/w:vertAlign{w:val=subscript}', False),
543+
('w:r/w:rPr/w:vertAlign{w:val=superscript}', True),
544+
])
545+
def superscript_get_fixture(self, request):
546+
r_cxml, expected_value = request.param
547+
font = Font(element(r_cxml))
548+
return font, expected_value
549+
534550
@pytest.fixture(params=[
535551
('w:r', None),
536552
('w:r/w:rPr/w:u', None),

0 commit comments

Comments
 (0)