Skip to content

Commit f2a3d7c

Browse files
author
Steve Canny
committed
font: add Font.color
1 parent 946864f commit f2a3d7c

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
8282
.. |Cm| replace:: :class:`.Cm`
8383
84+
.. |ColorFormat| replace:: :class:`.ColorFormat`
85+
8486
.. |_Column| replace:: :class:`._Column`
8587
8688
.. |_Columns| replace:: :class:`._Columns`

docx/text/font.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11+
from ..dml.color import ColorFormat
1112
from ..shared import ElementProxy
1213

1314

@@ -42,6 +43,14 @@ def bold(self):
4243
def bold(self, value):
4344
self._set_bool_prop('b', value)
4445

46+
@property
47+
def color(self):
48+
"""
49+
A |ColorFormat| object providing a way to get and set the text color
50+
for this font.
51+
"""
52+
return ColorFormat(self._element)
53+
4554
@property
4655
def complex_script(self):
4756
"""

features/txt-font-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ Feature: Get or set font properties
4848
| 18 pt | None |
4949

5050

51-
@wip
5251
Scenario: Get font color object
5352
Given a font
5453
Then font.color is a ColorFormat object

tests/text/test_font.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,25 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11+
from docx.dml.color import ColorFormat
1112
from docx.enum.text import WD_UNDERLINE
1213
from docx.shared import Pt
1314
from docx.text.font import Font
1415

1516
import pytest
1617

1718
from ..unitutil.cxml import element, xml
19+
from ..unitutil.mock import class_mock, instance_mock
1820

1921

2022
class DescribeFont(object):
2123

24+
def it_provides_access_to_its_color_object(self, color_fixture):
25+
font, color_, ColorFormat_ = color_fixture
26+
color = font.color
27+
ColorFormat_.assert_called_once_with(font.element)
28+
assert color is color_
29+
2230
def it_knows_its_typeface_name(self, name_get_fixture):
2331
font, expected_value = name_get_fixture
2432
assert font.name == expected_value
@@ -161,6 +169,11 @@ def bool_prop_set_fixture(self, request):
161169
expected_xml = xml(expected_cxml)
162170
return font, prop_name, value, expected_xml
163171

172+
@pytest.fixture
173+
def color_fixture(self, ColorFormat_, color_):
174+
font = Font(element('w:r'))
175+
return font, color_, ColorFormat_
176+
164177
@pytest.fixture(params=[
165178
('w:r', None),
166179
('w:r/w:rPr', None),
@@ -325,3 +338,15 @@ def underline_set_fixture(self, request):
325338
run = Font(element(initial_r_cxml), None)
326339
expected_xml = xml(expected_cxml)
327340
return run, value, expected_xml
341+
342+
# fixture components ---------------------------------------------
343+
344+
@pytest.fixture
345+
def color_(self, request):
346+
return instance_mock(request, ColorFormat)
347+
348+
@pytest.fixture
349+
def ColorFormat_(self, request, color_):
350+
return class_mock(
351+
request, 'docx.text.font.ColorFormat', return_value=color_
352+
)

0 commit comments

Comments
 (0)